0% found this document useful (0 votes)
59 views

Java Labmanual

The document contains code snippets from 18 Java programs demonstrating various Java concepts like classes, objects, inheritance, polymorphism, interfaces, etc. The programs include finding the maximum of three numbers, reversing a number, adding matrices, finding prime numbers, defining student and rectangle classes, using static and final keywords, method overriding, multilevel inheritance, multiple inheritance, toString method, and implementing multiple interfaces.

Uploaded by

DHRUV PATEL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Java Labmanual

The document contains code snippets from 18 Java programs demonstrating various Java concepts like classes, objects, inheritance, polymorphism, interfaces, etc. The programs include finding the maximum of three numbers, reversing a number, adding matrices, finding prime numbers, defining student and rectangle classes, using static and final keywords, method overriding, multilevel inheritance, multiple inheritance, toString method, and implementing multiple interfaces.

Uploaded by

DHRUV PATEL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Qs 1 -

public class MaxOfThreeNumbers


{
public static void main(String[] args)
{
int num1 = 10;
int num2 = 20;
int num3 = 15;
int max = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2
: num3);
System.out.println("Maximum of three numbers is: " + max);
}
}

Qs 2 -
public class ReverseNumber
{
public static void main(String[] args)
{
int num = 12345;
int reversed = 0;
while (num != 0)
{
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed Number: " + reversed);
}
}

Qs 3 -
public class MatrixAddition
{
public static void main(String[] args)
{
int[][] matrix1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] matrix2 = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int[][] result = new int[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
System.out.println("Resultant Matrix: ");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}

Qs 4 -
public class PrimeNumbers
{
public static void main(String[] args)
{
int n = 10;
int count = 0;
int num = 2;
while (count != n)
{
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++)
{
if (num % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
System.out.print(num + " ");
count++;
}
num++;
}
}
}

Qs 5 -
class Student
{
int enrollmentNo;
String name;

Student(int enrollmentNo, String name)


{
this.enrollmentNo = enrollmentNo;
this.name = name;
}
}

public class Main


{
public static void main(String[] args)
{
Student student1 = new Student(1, "John");
Student student2 = new Student(2, "Jane");
Student student3 = new Student(3, "Bob");

System.out.println("Student 1: " + student1.name);


System.out.println("Student 2: " + student2.name);
System.out.println("Student 3: " + student3.name);
}
}

Qs 6 -
class Rectangle
{
int height;
int width;

Rectangle(int height, int width)


{
this.height = height;
this.width = width;
}
}
public class Main
{
public static void main(String[] args)
{
Rectangle rectangle = new Rectangle(10, 20);
System.out.println("Height: " + rectangle.height);
System.out.println("Width: " + rectangle.width);
}
}

Qs 7 -
class Student
{
int id;
String name;
Student(int id, String name)
{
this.id = id;
this.name = name;
}

void display()
{
System.out.println("Student ID: " + this.id);
System.out.println("Student Name: " + this.name);
}
}
class Test
{
public static void main(String[] args)
{
Student s1 = new Student(101, "Alice");
Student s2 = new Student(102, "Bob");
s1.display();
s2.display();
}
}

QS 8 -
class Counter
{
static int count = 0;
Counter()
{
count++;
}
static void display()
{
System.out.println("Number of objects created: " + count);
}
}
class Test
{
public static void main(String[] args)
{
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
Counter.display();
}
}

Qs 9 -
class Circle
{
final double PI = 3.14;
double area(double radius)
{
return PI * radius * radius;
}
final void displayPI()
{
System.out.println("The value of pi is: " + PI);
}
}
final class Sphere extends Circle
{
double volume(double radius)
{
return (4.0 / 3.0) * PI * radius * radius * radius;
}
}
class Test
{
public static void main(String[] args)
{
Circle c = new Circle();
System.out.println("The area of a circle with radius 2 is: " + c.area(2));
c.displayPI();
Sphere s = new Sphere();
System.out.println("The volume of a sphere with radius 2 is: " +
s.volume(2));
s.displayPI();
}
}

Qs 10 -
class Shape
{
void area(float radius)
{
double area = Math.PI * radius * radius;
System.out.println("The area of a circle with radius " + radius + " is: " +
area);
}
void area(float length, float width)
{
double area = length * width;
System.out.println("The area of a rectangle with length " + length + " and
width " + width + " is: " + area);
}
}
class Test
{
public static void main(String[] args)
{
Shape s = new Shape();
s.area(2.0f);
s.area(3.0f, 4.0f);
}
}

Qs 11 -
class Student
{
int id;
String name;
double marks;
Student()
{
id = 0;
name = "Unknown";
marks = 0.0;
}
Student(int id)
{
this.id = id;
name = "Unknown";
marks = 0.0;
}
Student(int id, String name, double marks)
{
this.id = id;
this.name = name;
this.marks = marks;
}
void display()
{
System.out.println("Student ID: " + id);
System.out.println("Student Name: " + name);
System.out.println("Student Marks: " + marks);
}
}
class Test
{
public static void main(String[] args)
{
Student s1 = new Student();
Student s2 = new Student(101);
Student s3 = new Student(102, "Alice", 95.0);
s1.display();
s2.display();
s3.display();
}
}

Qs 12 -
class Test
{
public static void main(String[] args)
{
String s = "Hello World";
System.out.println("The character at index 4 is: " + s.charAt(4));
System.out.println("Does the string contain 'World'? " +
s.contains("World"));
System.out.println("The formatted string is: " + String.format("My name is
%s and I am %d years old.","Alice", 20));
System.out.println("The length of the string is: " + s.length());
String[] words = s.split(" ");
System.out.println("The words in the string are:");
for (String word : words)
{
System.out.println(word);
}
}
}

Qs 13 -
class Parent
{
public void parentMethod()
{
System.out.println("This is a method in the parent class.");
}
}

class Child extends Parent


{
public void childMethod()
{
System.out.println("This is a method in the child class.");
}
}

public class Main


{
public static void main(String[] args)
{
Child c = new Child();
c.parentMethod();
c.childMethod();
}
}

Qs 14 -
class Grandparent
{
public void printGrandparent()
{
System.out.println("This is the grandparent class.");
}
}
class Parent extends Grandparent
{
public void printParent()
{
System.out.println("This is the parent class.");
}
}
class Child extends Parent
{
public void printChild()
{
System.out.println("This is the child class.");
}
}
public class MultilevelInheritance
{
public static void main(String[] args)
{
Child c = new Child();
c.printChild();
c.printParent();
c.printGrandparent();
}
}

Qs 15 -
class Parent
{
public void parentMethod()
{
System.out.println("This is a method in the parent class.");
}
}
class Child1 extends Parent
{
public void child1Method()
{
System.out.println("This is a method in the first child class.");
}
}
class Child2 extends Parent
{
public void child2Method()
{
System.out.println("This is a method in the second child class.");
}
}
public class Main
{
public static void main(String[] args)
{
Child1 c1 = new Child1();
c1.parentMethod();
c1.child1Method();
Child2 c2 = new Child2();
c2.parentMethod();
c2.child2Method();
}
}

Qs 16 -
class Parent
{
public void printMessage()
{
System.out.println("This is a message from the parent class.");
}
}
class Child extends Parent
{
public void printMessage()
{
System.out.println("This is a message from the child class.");
}
}
public class Main
{
public static void main(String[] args)
{
Parent p = new Parent();
p.printMessage();
Child c = new Child();
c.printMessage();
}
}

Qs 17 -
class Car
{
private int topSpeed;
private String name;
public Car(int topSpeed, String name)
{
this.topSpeed = topSpeed;
this.name = name;
}
public String toString()
{
return "Car [topSpeed=" + topSpeed + ", name=" + name + "]";
}
}
public class Main
{
public static void main(String[] args)
{
Car car1 = new Car(200, "Ferrari");
Car car2 = new Car(180, "Lamborghini");
Car car3 = new Car(220, "Bugatti");
Car car4 = new Car(190, "McLaren");
Car car5 = new Car(210, "Koenigsegg");
System.out.println(car1);
System.out.println(car2);
System.out.println(car3);
System.out.println(car4);
System.out.println(car5);
}
}

Qs 18 -
interface Interface1
{
public void method1();
}
interface Interface2
{
public void method2();
}

class Child implements Interface1, Interface2


{
public void method1()
{
System.out.println("This is method1 from Interface1.");
}

public void method2()


{
System.out.println("This is method2 from Interface2.");
}
}

public class Main


{
public static void main(String[] args)
{
Child c = new Child();
c.method1();
c.method2();
}
}

Qs 19 -
abstract class Shape
{
abstract double area();
}
class Triangle extends Shape
{
double base;
double height;
Triangle(double base, double height)
{
this.base = base;
this.height = height;
}
@Override
double area()
{
return 0.5 * base * height;
}
}
class Rectangle extends Shape
{
double length;
double width;
Rectangle(double length, double width)
{
this.length = length;
this.width = width;
}
@Override
double area()
{
return length * width;
}
}
class Circle extends Shape
{
double radius;
Circle(double radius)
{
this.radius = radius;
}
@Override
double area()
{
return Math.PI * radius * radius;
}
}

Qs 20 -
final class FinalClass
{
void display()
{
System.out.println("This is a final class.");
}
}
public class Main
{
public static void main(String[] args)
{
FinalClass fc = new FinalClass();
fc.display();
}
}

You might also like