forked from SaketJNU/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeOverloading.java
More file actions
70 lines (59 loc) · 2.01 KB
/
ShapeOverloading.java
File metadata and controls
70 lines (59 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//Program for a class Shape is defined with two overloading constructors in it. Another class Test1 is partiallydefined which inherits the class Shape. The class Test1 should include two overloading constructors as appropriate for some object instantiation. You should define the constructors using the super class constructors. Also, override the method calculate() in Test1 to calculate the volume of a Shape.
import java.util.Scanner;
class Shape
{
double length, breadth;
Shape(double l, double b) // Constructor with two arguments
{
length = l;
breadth= b;
}
Shape(double len) // Constructor with one arguments
{
length = breadth = len;
}
double calculate() // To calculate the area of a shape object
{
return length * breadth ;
}
}
class Test1 extends Shape
{
double height;
Test1(double l, double h) //Derived class constructor which can call the one parametrized constructor of the base class
{
super(l);
this.length = l;
this.height = h;
}
Test1(double l, double b, double h) //Derived class constructor which can call the two parametrized constructor of the base class
{
super(l,b);
this.length=l;
this.breadth=b;
this.height=h;
}
double calculate() //Override the method calculate() in the derived class to find the volume of a shape instead of finding the area of a shape
{
return length*breadth*height;
}
}
class ShapeOverloading
{
public static void main(String args[])
{
System.out.println("\nEnter the length, breadth and height\n");
Scanner sc = new Scanner(System.in); //Create an object to read input
double l=sc.nextDouble(); //Read length
double b=sc.nextDouble(); //Read breadth
double h=sc.nextDouble(); //Read height
Test1 myshape1 = new Test1(l,h);
Test1 myshape2 = new Test1(l,b,h);
double volume1;
double volume2;
volume1 = myshape1.calculate();
volume2=myshape2.calculate();
System.out.print("\nThe volume using length and height is: "+volume1);
System.out.print("\nThe volume using all 3 parameters is: "+volume2);
}
}