View difference between Paste ID: a6qVX2ua and ickhs4py
SHOW: | | - or go back to the newest paste.
1
package StudentSubject;
2
3
/* Write a program to define an abstract class Shapes. The Shapes class will be inherited by Circle and Rectangle classes. 
4
 * Circle and Rectangle will add and implement the abstract methods to calculate their areas and Perimeter. 
5
 * There will be a display() method, which will display the dimensions, area and perimenter of respective Shapes. 
6
 * Please use the below class diagram:
7
 * 
8
 *  -------------------------------
9
 *  | Shape :Abstract             |
10
 *  -------------------------------
11
 *  | # height: double			  |
12
 *  | # width: double			  |
13
 *  | # radius : double           |	
14
 *  | # areaVal: double		      |
15
 *  | # perimeterVal: double 	  |
16
 *  -------------------------------
17
 *  | + Area():void  			  |
18
 *  | + Perimeter():void          |
19
 *  | + Display():void            |
20
 *  -------------------------------
21
 *           |                         \
22
 *           |                          \
23
 *           |                           \    
24
 *  -------------------------------        -------------------------------
25
 *  | Circle                      |        | Rectangle                   |
26
 *  -------------------------------        -------------------------------
27
 *  | + Circle()                  |        | + Rectangle()               |
28
 *  | + Circle(double, double)    |        | + Rectangle(double, double) |
29
 *  | + Area(): void  			  |        | + Area(): void  			 |
30
 *  | + Perimeter(): void         |        | + Perimeter(): void         |
31
 *  | + Display(): void           |        | + Display(): void           |
32
 *  -------------------------------        -------------------------------
33
 * 
34
 * Hint:
35
 * 1. For area of circle use Math.PI, Area = PI*radius*radius
36
 * 2. Perimeter of circle = 2 * PI * radius
37
 * 3. Area of rectangle = height * width
38
 * 4. Perimeter of rectangle = 2(height + width)
39
 * 5. Check the main method before creating the Shapes, Circle and Rectangle class
40
 * 6. # means protected access
41
 *  
42
 */
43
public class Week8Program1 {
44
45
	public static void main(String[] args) {
46
		// Create Shape objects
47
		Circle circle = new Circle(4.0);
48
		Rectangle rectangle = new Rectangle(5.0,4.0);
49
		
50
		// Calculate the area and perimenter of Circle and Rectangle
51
		circle.area();
52
		circle.perimeter();
53
		
54
		rectangle.area();
55
		rectangle.perimeter();
56
		
57
		// Display the dimensions, area and perimeter of Circle and Rectangle
58
		circle.display();
59
		rectangle.display();
60
	}
61
}
62
63
//Define the abstract Shapes class
64
abstract class Shapes{
65
66
	protected double height;
67
	protected double width;
68
	protected double radius;
69
	
70
	protected double areaVal;
71
	protected double perimeterVal;
72
	
73
	abstract void area();
74
	abstract void perimeter();	
75
}
76
77
//Define Circle class which extends Shapes
78
class Circle extends Shapes{
79
	
80
	// Complete the code to define constructors
81
	Circle(){
82
		
83
	}
84
	
85
	Circle(double radius){
86
		this.radius = radius;
87
	}
88
	
89
	// Complete the code to define Area, Perimenter and Display methods
90
	void area(){
91
		this.areaVal = Math.PI * Math.pow(radius, 2)   ;
92
	}
93
	
94
	void perimeter(){
95
		this.perimeterVal =2 * Math.PI * radius ;
96
	}
97
	
98
	void display(){
99
		System.out.println("The Radius of Circle is: "+this.radius);
100
		System.out.println("Area of the Circle is: "+this.areaVal);
101
		System.out.println("Perimeter of the Circle is: "+this.perimeterVal);
102
	}
103
}
104
105
//Complete the code to define Circle class which extends Shapes
106
// 1. Define two constructors
107
// 2. Define the Area, Perimeter and Display method
108
// Hint: Refer to the Circle if required
109
class Rectangle extends Shapes{
110
	
111
	Rectangle(double a, double b)
112
	{
113
		this.height = a;
114
		this.width = b;
115
	}
116
117
	void area(){
118
		this.areaVal = height * width ;
119
	}
120
	
121
	void perimeter(){
122
		this.perimeterVal = 2 * (height + width) ;
123
	}
124
	
125
	void display(){
126
		System.out.println("The height & width of Rectangle is: ("+this.height + " ," + this.width + ")" );
127
		System.out.println("Area of the Rectangle is: "+this.areaVal);
128
		System.out.println("Perimeter of the Rectangle is: "+this.perimeterVal);
129
	}
130
	
131
}
132
133
/* The program should produce the following result
134
* Test case 1:
135
* Initialize the circle with radius: 4
136
* Initialize the rectangle with ht and wd: 5, 4
137
* 
138
* Expected output:
139
* The Radius of Circle is: 4.0
140
* Area of the Circle is: 50.26548245743669
141
* Perimeter of the Circle is: 25.132741228718345
142
*
143
* The height and width of Rectangle is: (5.0 ,4.0)
144
* Area of the Rectangle is: 20.0
145
* Perimeter of the Rectangle is: 18.0
146
*/