Professional Documents
Culture Documents
1
10 Write a Java program to implement 20 17
dynamic binding.
11 . Write a Java program to implement 21 171
abstract class.
12 . Demonstrate the use of super keyword. 23 24
13 Implement Dynamic method dispatch 25 24
using Abstract class.
14 Create an abstract class Shape with 27-29 27
methods calc_area and calc_volume.
Derive
three classes Sphere (radius), Cone
(radius, height) and Cylinder (radius,
height), Box (length, breadth, height)
from it. Calculate area and volume of all.
(Use Method overriding).
15 Implement a class having private 29 29
variables and methods. Demonstrate
how to use those variables and methods
by objects.
16 Implement use of static variables and 30
methods in a class
17 Perform method overloading with basic 30
example.
18 Perform constructor overloading with 31 31
basic example.
19 Implement class shape take members 32 32
length breadth height & calculate area ()
perimeter () volume () etc. Create
another supportive class to create
objects. Input will be taken from user.
20 Write a Java program to accepts an 33 33
integer and count the factors of the
number.
2
1.Create a class Room which will store the length, width and height of the room in three private
variables, constructor to initialize the variables, method to calculate the surface area, method to
calculate the volume of the room. Create another Main class which will use the earlier class,
create instances of rooms, set the values of the variables and would calculate the surface area
and volume of the room object.
class Room {
private double L;
private double W;
private double H;
}
}
System.out.print("L: ");
double L1 = scanner.nextDouble();
3
System.out.print("W: ");
double W1 = scanner.nextDouble();
System.out.print("H: ");
double H1 = scanner.nextDouble();
Room room1 = new Room(L1, W1, H1);
System.out.print("L: ");
double L2 = scanner.nextDouble();
System.out.print("W: ");
double W2 = scanner.nextDouble();
System.out.print("H: ");
double H2 = scanner.nextDouble();
Room room2 = new Room(L2, W2, H2);
scanner.close();
}
}
4
2.Create a class Student with the variables to represent the name of the student and marks of
the five subjects store into an array, constructor to initialize the variables. Write method to
compute total marks, average marks, method to calculate letter grade, toString method to return
object as a string. Create Main class to demonstrate the functioning of the above.
Ans
import java.util.Scanner;
class Student {
private String name;
private int[] marks = new int[5];
5
return "D";
} else {
return "F";
}
}
@Override
public String toString() {
return "Student Name: " + name +
"\nTotal Marks: " + computeTotalMarks() +
"\nAverage Marks: " + computeAverageMarks() +
"\nLetter Grade: " + calculateLetterGrade();
}
}
6
Out put of 2.
Output of 3
7
3. Create a class Account with the variables to represent the account holder's name, account
number, account type (S for savings and C for Current), balance amount, use constructor to
initialize the variables. Write method to deposit money, withdrawal of money(balance shouldn't be
below ₹1000), toString method to return object as a string. Use static variable to keep track the
number of transactions. Create Main class to demonstrate the functioning of the above.
Ans import java.util.Scanner;
class Account {
private String accountHolderName;
private String accountNumber;
private char accountType;
private double balance;
private static int numTransactions = 0;
@Override
public String toString() {
return "Account Holder Name: " + accountHolderName +
"\nAccount Number: " + accountNumber +
"\nAccount Type: " + accountType +
"\nBalance: ₹" + balance;
}
}
8
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
switch (choice) {
case 1:
System.out.print("Enter the deposit amount: ₹");
double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);
break;
case 2:
System.out.print("Enter the withdrawal amount: ₹");
double withdrawalAmount = scanner.nextDouble();
account.withdraw(withdrawalAmount);
break;
case 3:
System.out.println("\nAccount Details:");
System.out.println(account);
break;
case 4:
System.out.println("Number of Transactions: " + Account.getNumTransactions());
break;
case 0:
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice. Please enter a valid option.");
}
} while (choice != 0);
scanner.close();
}
}
9
4.WAP in java to demonstrate the use of method overloading:Overloaded Method: area(),
area(int) for square, area(int,int) for rectangle, area(int,int,int) for triangle, area(double) for circle,
area(int,int,int,int) for straight line (i.e. calculate length of straight in this case)
Ans import java.util.Scanner;
public class MethodOverloadingDemo {
public double area() {
return 0; }
public double area(int side) {
return side * side; // Area of a square
}
public double area(int length, int width) {
return length * width; // Area of a rectangle }
public double area(int base, int height, int side) {
return 0.5 * base * height; // Area of a triangle
}
public double area(double radius) {
return Math.PI * radius * radius; // Area of a circle
}
public double area(int x1, int y1, int x2, int y2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); // Length of a straight line
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
MethodOverloadingDemo demo = new MethodOverloadingDemo();
System.out.println("Choose an option:");
System.out.println("1. Area of a Square");
System.out.println("2. Area of a Rectangle");
System.out.println("3. Area of a Triangle");
System.out.println("4. Area of a Circle");
System.out.println("5. Length of a Straight Line");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the side length of the square: ");
int side = scanner.nextInt();
System.out.println("Area of the square: " + demo.area(side));
10
break;
case 2:
System.out.print("Enter the length of the rectangle: ");
int length = scanner.nextInt();
System.out.print("Enter the width of the rectangle: ");
int width = scanner.nextInt();
System.out.println("Area of the rectangle: " + demo.area(length, width));
break;
case 3:
System.out.print("Enter the base of the triangle: ");
int base = scanner.nextInt();
System.out.print("Enter the height of the triangle: ");
int height = scanner.nextInt();
System.out.println("Area of the triangle: " + demo.area(base, height, 0));
break;
case 4:
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
System.out.println("Area of the circle: " + demo.area(radius));
break;
case 5:
System.out.print("Enter the coordinates of the first point (x1, y1): ");
int x1 = scanner.nextInt();
int y1 = scanner.nextInt();
System.out.print("Enter the coordinates of the second point (x2, y2): ");
int x2 = scanner.nextInt();
int y2 = scanner.nextInt();
System.out.println("Length of the straight line: " + demo.area(x1, y1, x2, y2));
break;
default:
System.out.println("Invalid choice.");
Break: }
scanner.close();
}
}
11
12
5.Demonstrate the concept for the following operation.
a. Stopping Overriding with Final
b. Stopping Inheritance with Final
c. Creating Constant with Final
class Parent {
public final void display() {
System.out.println("This method cannot be overridden.");
}
}
13
b. Stopping Inheritance with Final
14
6. Write a Java program to implement single inheritance.
// Define the superclass (parent class)
class Animal {
void eat() {
System.out.println("The animal is eating.");
}
void sleep() {
System.out.println("The animal is sleeping.");
}
}
15
7. Write a Java program to implement multilevel inheritance.
// Define the first (base) class
class Animal {
void eat() {
System.out.println("The animal is eating.");
}
}
16
7. Output Window of multilevel inheritance
17
8.Write a Java program to implement hierarchical inheritance.
// Define the base class
class Animal {
void eat() {
System.out.println("The animal is eating.");
}
}
// Define the first subclass
class Dog extends Animal {
void bark() {
System.out.println("The dog is barking.");
}
}
class Cat extends Animal {
void meow() {
System.out.println("The cat is meowing.");
}
}
public class HierarchicalInheritanceDemo {
public static void main(String[] args) {
// Create an instance of the Dog class
Dog myDog = new Dog();
18
9.Write a Java program to implement static binding.
class Animal {
void sound() {
System.out.println("This is an animal.");
}
}
19
10.Write a Java program to implement dynamic binding.
class Animal {
void sound() {
System.out.println("This is an animal.");
}
}
20
11.Write a Java program to implement abstract class.
import java.util.Scanner;
Circle(double radius) {
this.radius = radius;
}
@Override
void draw() {
System.out.println("Drawing a circle");
}
@Override
double area() {
return Math.PI * radius * radius;
}
}
21
this.width = width;
}
@Override
void draw() {
System.out.println("Drawing a rectangle");
}
@Override
double area() {
return length * width;
}
}
22
12.Demonstrate the use of super keyword.
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
String breed;
Dog(String name, String breed) {
super(name); // Call superclass constructor
this.breed = breed;
}
@Override
void sound() {
super.sound(); // Call superclass method
System.out.println("The dog barks.");
}
void display() {
System.out.println("Name: " + name);
System.out.println("Breed: " + breed);
}
}
public class SuperKeywordDemo {
public static void main(String[] args) {
Dog myDog = new Dog("Buddy", "Golden Retriever");
myDog.display();
myDog.sound();
}
}
23
12..Demonstration the use of super keyword.
24
13.Implement Dynamic method dispatch using Abstract class.
abstract class Animal {
abstract void makeSound(); // Abstract method to be overridden by subclasses
}
25
14.Create an abstract class Shape with methods calc_area and calc_volume. Derive
three classes Sphere (radius), Cone (radius, height) and Cylinder (radius,
height), Box (length, breadth, height) from it. Calculate area and volume of all.
(Use Method overriding).
import java.util.Scanner;
Sphere(double radius) {
this.radius = radius;
}
@Override
double calc_area() {
return 4 * Math.PI * Math.pow(radius, 2);
}
@Override
double calc_volume() {
return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
}
@Override
double calc_area() {
double slantHeight = Math.sqrt(Math.pow(radius, 2) + Math.pow(height, 2));
return Math.PI * radius * (radius + slantHeight);
}
@Override
double calc_volume() {
return (1.0 / 3.0) * Math.PI * Math.pow(radius, 2) * height;
}
}
26
Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
@Override
double calc_area() {
return 2 * Math.PI * radius * (radius + height);
}
@Override
double calc_volume() {
return Math.PI * Math.pow(radius, 2) * height;
}
}
@Override
double calc_area() {
return 2 * (length * breadth + breadth * height + height * length);
}
@Override
double calc_volume() {
return length * breadth * height;
}
}
System.out.println("Select a shape:");
System.out.println("1. Sphere");
System.out.println("2. Cone");
System.out.println("3. Cylinder");
System.out.println("4. Box");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the radius of the sphere: ");
radius = scanner.nextDouble();
27
Sphere sphere = new Sphere(radius);
System.out.println("Sphere Area: " + sphere.calc_area());
System.out.println("Sphere Volume: " + sphere.calc_volume());
break;
case 2:
System.out.print("Enter the radius of the cone: ");
radius = scanner.nextDouble();
System.out.print("Enter the height of the cone: ");
height = scanner.nextDouble();
Cone cone = new Cone(radius, height);
System.out.println("Cone Area: " + cone.calc_area());
System.out.println("Cone Volume: " + cone.calc_volume());
break;
case 3:
System.out.print("Enter the radius of the cylinder: ");
radius = scanner.nextDouble();
System.out.print("Enter the height of the cylinder: ");
height = scanner.nextDouble();
Cylinder cylinder = new Cylinder(radius, height);
System.out.println("Cylinder Area: " + cylinder.calc_area());
System.out.println("Cylinder Volume: " + cylinder.calc_volume());
break;
case 4:
System.out.print("Enter the length of the box: ");
length = scanner.nextDouble();
System.out.print("Enter the breadth of the box: ");
breadth = scanner.nextDouble();
System.out.print("Enter the height of the box: ");
height = scanner.nextDouble();
Box box = new Box(length, breadth, height);
System.out.println("Box Area: " + box.calc_area());
System.out.println("Box Volume: " + box.calc_volume());
break;
default:
System.out.println("Invalid choice.");
}
scanner.close();
}
}
28
15.Implement a class having private variables and methods. Demonstrate how to use those
variables and methods by objects.
class PrivateDemo {
private int privateVar; // Private instance variable
29
16.Implement use of static variables and methods in a class.
class MyClass {
// Static variable shared by all instances of the class
static int staticVar = 0;
int instanceVar;
MyClass(int instanceVar) {
this.instanceVar = instanceVar;
}
// Static method
static void staticMethod() {
System.out.println("This is a static method.");
}
void instanceMethod() {
System.out.println("This is an instance method.");
}
}
30
17. Perform method overloading with basic example.Give output also
31
int result2 = MathOperations.subtract(10, 5);
System.out.println("Subtraction Result: " + result2);
32
20.Write a Java program to accepts an integer and count the factors of the number. import
java.util.Scanner;
scanner.close();
}
33