Problem Statement:
Write an inheritance hierarchy for classes Shapes are shown in figure. Write a menu
driven program to accept user input and output each objects area. Use a reference variable of Shape class and instanceof operator to determine the area.
Shapes
TwoDimensional ThreeDimensional
Circle Square Triangle Sphere Cube
Input and Output Requirements:
Program should read through keyboard input data for circle, square, triangle, sphere and cube objects. It must display area of the corresponding object.
Algorithm:
Begin
Define a class Shape with methods for getting input data and finding the area and displaying it.
Define classes two dimensional and three dimensional both inherited from shape class and overrides the method for getting data and finding area and displaying it.
Define classes circle, square and triangle inherited from two dimensional class . Calculate area of each shape.
Define classes sphere and cube inherited from three dimensional class and find area of each shape.
Create a menu driven program and accept the choice.
Create object according to user choice and use instanceof operator for dynamic method invocation.
End
Program:
import java.io.IOException; import java.util.Scanner; class shape { Scanner in = new Scanner (System.in); float radius,side; } class twoD extends shape { float height; } class circle extends twoD { void getradius() { System.out.print(" Enter the radius : " ); radius = in.nextFloat(); } void Area() { double area = 3.1416 * radius * radius; System.out.println("Area of Circle : " + area); } } class square extends twoD { void getside() { System.out.print(" Enter the side : " ); side = in.nextFloat(); } void Area() { double area = side * side; System.out.println("Area of Square : " + area); } } class triangle extends twoD { void getheight() { System.out.print(" Enter the height : " ); height = in.nextFloat(); } void getside() { System.out.print(" Enter the base : " ); side = in.nextFloat(); } void Area() { double area = 0.5 * side * height; System.out.println("Area of Triangle : " + area); } } class sphere extends threeD { void getradius() { System.out.print(" Enter the radius : " ); radius = in.nextFloat(); } void Area() { double area = 4 * 3.1416 * radius * radius; System.out.println("Area of Circle : " + area); } } class cube extends threeD { void getside() { System.out.print(" Enter the side : " ); side = in.nextFloat(); } void Area() { double area = 6 * side * side; System.out.println("Area of Cube : " + area); } } public class inheritance { public static void main(String[] args) throws IOException { int ch; Scanner in = new Scanner (System.in); for(;;) { System.out.println("1. Circle"); System.out.println("2. Square"); System.out.println("3. Triangle"); System.out.println("4. Sphere"); System.out.println("5. Cube"); System.out.println("6. Exit"); System.out.println(" Enter ur Choice :" ); ch = in.nextInt(); if(ch == 1) { circle c = new circle(); get_right_obj(c); } if(ch == 2) { square c = new square(); get_right_obj(c); } if(ch == 3) { triangle c = new triangle(); get_right_obj(c); } if(ch == 4) { sphere c = new sphere(); get_right_obj(c); } if(ch == 5) { cube c = new cube(); get_right_obj(c); } if(ch >= 6) { System.out.println("Program Exiting....."); System.exit(0); } } } public static void get_right_obj(shape s) throws IOException { if(s instanceof circle) { circle c = new circle(); c.getradius(); c.Area(); } if(s instanceof square) { square c = new square(); c.getside(); c.Area(); } if(s instanceof triangle) { triangle c = new triangle(); c.getheight(); c.getside(); c.Area(); } if(s instanceof sphere) { sphere c = new sphere(); c.getradius(); c.Area(); } if(s instanceof cube) { cube c = new cube(); c.getside(); c.Area(); } } } |
Output:
1. Circle
2. Square
3. Triangle
4. Sphere
5. Cube
6. Exit
Enter ur Choice :1
Enter the radius : 7
Area of Circle : 153.9384
1. Circle
2. Square
3. Triangle
4. Sphere
5. Cube
6. Exit
Enter ur Choice :2
Enter the side : 5
Area of Square : 25.0
1. Circle
2. Square
3. Triangle
4. Sphere
5. Cube
6. Exit
Enter ur Choice :3
Enter the height : 7
Enter the base : 9
Area of Triangle : 31.5
1. Circle
2. Square
3. Triangle
4. Sphere
5. Cube
6. Exit
Enter ur Choice :4
Enter the radius : 4
Area of Circle : 201.0624
1. Circle
2. Square
3. Triangle
4. Sphere
5. Cube
6. Exit
Enter ur Choice :5
Enter the side : 9
Area of Cube : 486.0
1. Circle
2. Square
3. Triangle
4. Sphere
5. Cube
6. Exit
Enter ur Choice :6
Program Exiting…..
Leave a Reply