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

assignment2

The document contains multiple Java programs demonstrating method overloading and inheritance. It includes programs for calculating areas of geometric shapes (square, rectangle, triangle) and volumes of 3D shapes (cube, cuboid, cylinder), as well as hierarchical and multi-level inheritance examples for students and laborers. Each program is accompanied by sample input and output to illustrate functionality.

Uploaded by

littlepoplovesu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

assignment2

The document contains multiple Java programs demonstrating method overloading and inheritance. It includes programs for calculating areas of geometric shapes (square, rectangle, triangle) and volumes of 3D shapes (cube, cuboid, cylinder), as well as hierarchical and multi-level inheritance examples for students and laborers. Each program is accompanied by sample input and output to illustrate functionality.

Uploaded by

littlepoplovesu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Write a Java program to find the areas of square (side given, x*x) ,rectangle(length and

breadth given , l*b) and triangle (three sides a,b,c given) by ge ng length and breadth of the

rectangle and three sides of the triangle through keyboard using method overloading

concept. (formula for finding area of triangle = squareroot of (s*(s-a) * (s-b) *(s-c) ) where s=

(a+b+c)/2 )

//program

package bhavana;

import java.u l.Scanner;

public class AreaCalculator {

// Method to calculate area of a square

public sta c double calculateArea(double side) {

return side * side;

// Method to calculate area of a rectangle

public sta c double calculateArea(double length, double breadth) {

return length * breadth;

// Method to calculate area of a triangle

public sta c double calculateArea(double a, double b, double c) {

double s = (a + b + c) / 2.0;

return Math.sqrt(s * (s - a) * (s - b) * (s - c));

public sta c void main(String[] args) {

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the side of the square: ");

double side = scanner.nextDouble();

double squareArea = calculateArea(side);

System.out.print("Enter the length of the rectangle: ");

double length = scanner.nextDouble();

System.out.print("Enter the breadth of the rectangle: ");

double breadth = scanner.nextDouble();

double rectangleArea = calculateArea(length, breadth);

System.out.print("Enter the first side of the triangle: ");

double a = scanner.nextDouble();

System.out.print("Enter the second side of the triangle: ");

double b = scanner.nextDouble();

System.out.print("Enter the third side of the triangle: ");

double c = scanner.nextDouble();

double triangleArea = calculateArea(a, b, c);

scanner.close();

System.out.println("Area of Square: " + squareArea);

System.out.println("Area of Rectangle: " + rectangleArea);

System.out.println("Area of Triangle: " + triangleArea);

//output
Enter the side of the square: 2
Enter the length of the rectangle: 5
Enter the breadth of the rectangle: 6
Enter the first side of the triangle: 2
Enter the second side of the triangle: 5
Enter the third side of the triangle: 5
Area of Square: 4.0
Area of Rectangle: 30.0
Area of Triangle: 4.898979485566356
2. Write a Java program for finding volumes of cube(x*x*x), cuboid (l*b*h) and cylinder( pi

r*r*h) using method overloading concept

//program

import java.u l.Scanner;

public class VolumeCalculator {

// Method to calculate volume of a cube

public sta c double calculateVolume(double side) {

return side * side * side;

// Method to calculate volume of a cuboid

public sta c double calculateVolume(double length, double breadth, double height) {

return length * breadth * height;

// Method to calculate volume of a cylinder

public sta c double calculateVolume(double radius, double height) {

return Math.PI * radius * radius * height;

public sta c void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the side of the cube: ");

double side = scanner.nextDouble();

double cubeVolume = calculateVolume(side);

System.out.print("Enter the length of the cuboid: ");

double length = scanner.nextDouble();


System.out.print("Enter the breadth of the cuboid: ");

double breadth = scanner.nextDouble();

System.out.print("Enter the height of the cuboid: ");

double height = scanner.nextDouble();

double cuboidVolume = calculateVolume(length, breadth, height);

System.out.print("Enter the radius of the cylinder: ");

double radius = scanner.nextDouble();

System.out.print("Enter the height of the cylinder: ");

double cylinderHeight = scanner.nextDouble();

double cylinderVolume = calculateVolume(radius, cylinderHeight);

scanner.close();

System.out.println("Volume of Cube: " + cubeVolume);

System.out.println("Volume of Cuboid: " + cuboidVolume);

System.out.println("Volume of Cylinder: " + cylinderVolume);

//output

Enter the side of the cube: 3

Enter the length of the cuboid: 5

Enter the breadth of the cuboid: 4

Enter the height of the cuboid: 6

Enter the radius of the cylinder: 2.5

Enter the height of the cylinder: 8

Volume of Cube: 27.0

Volume of Cuboid: 120.0

Volume of Cylinder: 157.07963267948966

3. 4. Write a heirarchical inheritance program with method overriding using super() in java for

Student (rollno, sname)->Regular(rollno,sname,hours,dura on) with totalhour()


//program

class Student {

int rollno;

String sname;

Student(int rollno, String sname) {

this.rollno = rollno;

this.sname = sname;

class Regular extends Student {

int hours;

int dura on;

Regular(int rollno, String sname, int hours, int dura on) {

super(rollno, sname);

this.hours = hours;

this.dura on = dura on;

int totalHour() {

return hours * dura on;

class Par me extends Student {

int hours;

int dura on;

Par me(int rollno, String sname, int hours, int dura on) {
super(rollno, sname);

this.hours = hours;

this.dura on = dura on;

int totalHour() {

return hours * dura on;

public class StudentTest {

public sta c void main(String[] args) {

Regular regularStudent = new Regular(100, "Raj", 6, 30);

Par me partTimeStudent = new Par me(101, "Ravi", 3, 70);

System.out.println("Details of All Students are:");

System.out.println("~~~~~Regular Students' details~~~~~");

System.out.println("Rollno: " + regularStudent.rollno);

System.out.println("Student name: " + regularStudent.sname);

System.out.println("Hours taken per day: " + regularStudent.hours);

System.out.println("Dura on in days: " + regularStudent.dura on);

System.out.println("Total hours for Regular students: " + regularStudent.totalHour());

System.out.println("~~~~~Part_ me Students' details~~~~~");

System.out.println("Rollno: " + partTimeStudent.rollno);

System.out.println("Student name: " + partTimeStudent.sname);

System.out.println("Hours taken per day: " + partTimeStudent.hours);

System.out.println("Dura on in days: " + partTimeStudent.dura on);

System.out.println("Total hours for Par me students: " + partTimeStudent.totalHour());

}
}

//output

Details of All Students are:

Regular Students' details

Rollno: 100

Student name: Raj

Hours taken per day: 6

Dura on in days: 30

Total hours for Regular students: 180

Part_ me Students' details

Rollno: 101

Student name: Ravi

Hours taken per day: 3

Dura on in days: 70

Total hours for Par me students: 210

4. Write a mul level inheritance program with method overriding using super() in java for

Labourer(empno, ename)->Assistant(empno,ename,wages) - >

- >TechAssist(empno,ename,wages, salary) -

>SkilledAsst(empno,ename,wages, salary,bonus)

Create LabourTest class to incorporate all the aforemen oned classes and create objects for

all the classes and display the informa on of the respec ve classes

//program

class Labourer {

int empno;

String ename;

Labourer(int empno, String ename) {

this.empno = empno;

this.ename = ename;
}

void display() {

System.out.println("~~~~~Labour's details~~~~~");

System.out.println("Employee no: " + empno);

System.out.println("Employee name: " + ename);

class Assistant extends Labourer {

int wages;

Assistant(int empno, String ename, int wages) {

super(empno, ename);

this.wages = wages;

void display() {

super.display();

System.out.println("~~~~~Assistant's details~~~~~");

System.out.println("Wages: " + wages);

class TechAssist extends Assistant {

int salary;

TechAssist(int empno, String ename, int wages, int salary) {

super(empno, ename, wages);

this.salary = salary;

}
void display() {

super.display();

System.out.println("~~~~~TechAsst's details~~~~~");

System.out.println("Salary: " + salary);

class SkilledAsst extends TechAssist {

int bonus;

SkilledAsst(int empno, String ename, int wages, int salary, int bonus) {

super(empno, ename, wages, salary);

this.bonus = bonus;

void display() {

super.display();

System.out.println("~~~~~SkilledAsst's details~~~~~");

System.out.println("Bonus: " + bonus);

public class LabourTest {

public sta c void main(String[] args) {

Labourer labourer = new Labourer(101, "Ram");

Assistant assistant = new Assistant(102, "Ravi", 500);

TechAssist techAssist = new TechAssist(103, "Ragu", 10000, 15000);

SkilledAsst skilledAsst = new SkilledAsst(104, "Vimal", 15000, 20000, 10000);

System.out.println("Details of All Employees are:");


labourer.display();

assistant.display();

techAssist.display();

skilledAsst.display();

//output

Details of All Employees are:

Labour's details

Employee no: 101

Employee name: Ram

Assistant's details

Employee no: 102

Employee name: Ravi

Wages: 500

TechAsst's details

Employee no: 103

Employee name: Ragu

Wages: 10000

Salary: 15000

SkilledAsst's details

Employee no: 104

Employee name: Vimal

Wages: 15000

Salary: 20000

Bonus: 10000

You might also like