Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package StudentSubject;
- /* Write a program to define an abstract class Shapes. The Shapes class will be inherited by Circle and Rectangle classes.
- * Circle and Rectangle will add and implement the abstract methods to calculate their areas and Perimeter.
- * There will be a display() method, which will display the dimensions, area and perimenter of respective Shapes.
- * Please use the below class diagram:
- *
- * -------------------------------
- * | Shape :Abstract |
- * -------------------------------
- * | # height: double |
- * | # width: double |
- * | # radius : double |
- * | # areaVal: double |
- * | # perimeterVal: double |
- * -------------------------------
- * | + Area():void |
- * | + Perimeter():void |
- * | + Display():void |
- * -------------------------------
- * | \
- * | \
- * | \
- * ------------------------------- -------------------------------
- * | Circle | | Rectangle |
- * ------------------------------- -------------------------------
- * | + Circle() | | + Rectangle() |
- * | + Circle(double, double) | | + Rectangle(double, double) |
- * | + Area(): void | | + Area(): void |
- * | + Perimeter(): void | | + Perimeter(): void |
- * | + Display(): void | | + Display(): void |
- * ------------------------------- -------------------------------
- *
- * Hint:
- * 1. For area of circle use Math.PI, Area = PI*radius*radius
- * 2. Perimeter of circle = 2 * PI * radius
- * 3. Area of rectangle = height * width
- * 4. Perimeter of rectangle = 2(height + width)
- * 5. Check the main method before creating the Shapes, Circle and Rectangle class
- * 6. # means protected access
- *
- */
- public class Week8Program1 {
- public static void main(String[] args) {
- // Create Shape objects
- Circle circle = new Circle(4.0);
- Rectangle rectangle = new Rectangle(5.0,4.0);
- // Calculate the area and perimenter of Circle and Rectangle
- circle.area();
- circle.perimeter();
- rectangle.area();
- rectangle.perimeter();
- // Display the dimensions, area and perimeter of Circle and Rectangle
- circle.display();
- rectangle.display();
- }
- }
- //Define the abstract Shapes class
- abstract class Shapes{
- protected double height;
- protected double width;
- protected double radius;
- protected double areaVal;
- protected double perimeterVal;
- abstract void area();
- abstract void perimeter();
- }
- //Define Circle class which extends Shapes
- class Circle extends Shapes{
- // Complete the code to define constructors
- Circle(){
- }
- Circle(double radius){
- this.radius = radius;
- }
- // Complete the code to define Area, Perimenter and Display methods
- void area(){
- this.areaVal = Math.PI * Math.pow(radius, 2) ;
- }
- void perimeter(){
- this.perimeterVal =2 * Math.PI * radius ;
- }
- void display(){
- System.out.println("The Radius of Circle is: "+this.radius);
- System.out.println("Area of the Circle is: "+this.areaVal);
- System.out.println("Perimeter of the Circle is: "+this.perimeterVal);
- }
- }
- //Complete the code to define Circle class which extends Shapes
- // 1. Define two constructors
- // 2. Define the Area, Perimeter and Display method
- // Hint: Refer to the Circle if required
- class Rectangle extends Shapes{
- Rectangle(double a, double b)
- {
- this.height = a;
- this.width = b;
- }
- void area(){
- this.areaVal = height * width ;
- }
- void perimeter(){
- this.perimeterVal = 2 * (height + width) ;
- }
- void display(){
- System.out.println("The height & width of Rectangle is: ("+this.height + " ," + this.width + ")" );
- System.out.println("Area of the Rectangle is: "+this.areaVal);
- System.out.println("Perimeter of the Rectangle is: "+this.perimeterVal);
- }
- }
- /* The program should produce the following result
- * Test case 1:
- * Initialize the circle with radius: 4
- * Initialize the rectangle with ht and wd: 5, 4
- *
- * Expected output:
- * The Radius of Circle is: 4.0
- * Area of the Circle is: 50.26548245743669
- * Perimeter of the Circle is: 25.132741228718345
- *
- * The height and width of Rectangle is: (5.0 ,4.0)
- * Area of the Rectangle is: 20.0
- * Perimeter of the Rectangle is: 18.0
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement