[Go to site: main page, start]

0% found this document useful (0 votes)
4 views8 pages

Java Lab Programs

The document contains a list of 20 Java lab programs covering various concepts such as data types, variable scopes, string operations, inheritance, exceptions, and threading. Each program includes a brief description and the corresponding Java code demonstrating the concept. The programs range from basic tasks like printing 'Hello World' to more complex operations like implementing interfaces and handling exceptions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Java Lab Programs

The document contains a list of 20 Java lab programs covering various concepts such as data types, variable scopes, string operations, inheritance, exceptions, and threading. Each program includes a brief description and the corresponding Java code demonstrating the concept. The programs range from basic tasks like printing 'Hello World' to more complex operations like implementing interfaces and handling exceptions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Lab Programs (1–20)

1. Hello World + Data Types Size


class DataTypesSize {
public static void main(String[] args) {
[Link]("Hello World");
[Link]("Size of byte: " + [Link] + " bits");
[Link]("Size of short: " + [Link] + " bits");
[Link]("Size of int: " + [Link] + " bits");
[Link]("Size of long: " + [Link] + " bits");
[Link]("Size of float: " + [Link] + " bits");
[Link]("Size of double: " + [Link] + " bits");
[Link]("Size of char: " + [Link] + " bits");
}
}

2. Static, Local and Global Variables


class VariableDemo {
int globalVar = 10;
static int staticVar = 20;

void display() {
int localVar = 5;
[Link]("Local Variable: " + localVar);
[Link]("Global Variable: " + globalVar);
[Link]("Static Variable: " + staticVar);
}

public static void main(String[] args) {


VariableDemo obj = new VariableDemo();
[Link]();
}
}
3. String Operations
class StringOperations {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "World";
[Link]("Length: " + [Link]());
String result = [Link](" " + str2);
[Link]("Concatenation: " + result);
[Link]("Substring: " + [Link](1, 5));
}
}

4. Maximum of Three Numbers


import [Link];

class MaxOfThree {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter three numbers:");
int a = [Link]();
int b = [Link]();
int c = [Link]();

if (a > b && a > c)


[Link]("Maximum is: " + a);
else if (b > c)
[Link]("Maximum is: " + b);
else
[Link]("Maximum is: " + c);
}
}

5. Odd or Even
import [Link];

class OddEven {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number:");
int num = [Link]();
if (num % 2 == 0)
[Link]("Even Number");
else
[Link]("Odd Number");
}
}

6. Constructors
class Student {
String name;
int age;

Student() {
name = "Unknown";
age = 0;
}

Student(String n, int a) {
name = n;
age = a;
}

void display() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}

public static void main(String[] args) {


Student s1 = new Student();
[Link]();
Student s2 = new Student("Rahul", 20);
[Link]();
}
}

7. Array of Objects
class Student {
String name;

Student(String n) {
name = n;
}
void display() {
[Link](name);
}

public static void main(String[] args) {


Student[] s = new Student[3];
s[0] = new Student("A");
s[1] = new Student("B");
s[2] = new Student("C");

for (int i = 0; i < 3; i++) {


s[i].display();
}
}
}

8. Single Inheritance
class Animal {
void eat() {
[Link]("Eating...");
}
}

class Dog extends Animal {


void bark() {
[Link]("Barking...");
}
}

class Test {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
}
}

9. Multiple Inheritance using Interface


interface A {
void show();
}
interface B {
void display();
}

class Demo implements A, B {


public void show() {
[Link]("Interface A method");
}

public void display() {


[Link]("Interface B method");
}

public static void main(String[] args) {


Demo obj = new Demo();
[Link]();
[Link]();
}
}

10. Applet
import [Link];
import [Link];

public class MyApplet extends Applet {


public void paint(Graphics g) {
[Link]("Hello Applet", 50, 50);
}
}

11. Division by Zero Exception


class DivisionException {
public static void main(String[] args) {
try {
int a = 10;
int b = 0;
int result = a / b;
} catch (ArithmeticException e) {
[Link]("Error: Division by zero is not allowed");
}
}
}

12. Method Overloading


class Addition {
int add(int a, int b) { return a + b; }
float add(float a, float b) { return a + b; }

public static void main(String[] args) {


Addition obj = new Addition();
[Link]([Link](10, 20));
[Link]([Link](2.5f, 3.5f));
}
}

13. Runtime Polymorphism


class Animal {
void sound() { [Link]("Animal sound"); }
}

class Dog extends Animal {


void sound() { [Link]("Dog barks"); }
}

class Test {
public static void main(String[] args) {
Animal obj = new Dog();
[Link]();
}
}

14. Negative Array Size Exception


class NegativeArray {
public static void main(String[] args) {
try {
int arr[] = new int[-5];
} catch (NegativeArraySizeException e) {
[Link]("Negative size not allowed");
}
}
}
15. Null Pointer Exception + finally
class NullPointerDemo {
public static void main(String[] args) {
try {
String str = null;
[Link]([Link]());
} catch (NullPointerException e) {
[Link]("Null Pointer Exception");
} finally {
[Link]("Finally executed");
}
}
}

16. User Defined Package


// package file
package mypack;
public class MyClass {
public void show() {
[Link]("Hello from package");
}
}

// main file
import [Link];
class TestPackage {
public static void main(String[] args) {
MyClass obj = new MyClass();
[Link]();
}
}

17. Palindrome
class Palindrome {
public static void main(String[] args) {
int num = 121, rev = 0, temp = num;
while (num > 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
[Link](temp == rev ? "Palindrome" : "Not Palindrome");
}
}

18. Factorial (Command Line)


class FactorialCLI {
public static void main(String[] args) {
for (String s : args) {
int num = [Link](s);
int fact = 1;
for (int i = 1; i <= num; i++) fact *= i;
[Link](fact);
}
}
}

19. Prime Numbers Between Limits


class PrimeNumbers {
public static void main(String[] args) {
int start = 10, end = 20;
for (int i = start; i <= end; i++) {
int count = 0;
for (int j = 1; j <= i; j++)
if (i % j == 0) count++;
if (count == 2) [Link](i + " ");
}
}
}

20. Thread using Runnable


class MyThread implements Runnable {
public void run() {
[Link]("Thread running...");
}

public static void main(String[] args) {


Thread t = new Thread(new MyThread());
[Link]();
}
}

You might also like