View History Bookmarks Window Help drive 223102 ooo 2016F CP

View History Bookmarks Window Help drive. 2231-02 ooo. 2016F CPS2231.02 Google D. Exercise Lab 3 Inheritance Given an abstract class Shape as designed below. class Shape private String myName; constructor public shape String name) myName name; public string getName() f return myName; public double area freturn -1;} 42 Page 38 MacBook Pro

Solution

//Circle.java

public class Circle extends Shape {

   private double myRadius;

  

   public Circle(double radius, String name) {

       super(name);

       this.myRadius = radius;

   }

  

   public double area() { return Math.PI*myRadius*myRadius; }

  

   @Override

   public String toString() {

       return \"Circle. Name: \" + this.getName() + \" Area: \" + this.area();      

   }

}

Square.java

public class Square extends Shape{

private double mySide;

  

   public Square(double side, String name) {

       super(name);

       this.mySide = side;

   }

  

   public double area() { return mySide*mySide; }

  

   @Override

   public String toString() {

       return \"Square. Name: \" + this.getName() + \" Area: \" + this.area();      

   }

}

//Main.java

public class Main {

   public static void main(String args[])

   {

       Circle c = new Circle(2, \"Austin Circle\");

       System.out.println(c.toString());

      

       Square s = new Square(2,\"Austin Square\");

       System.out.println(s.toString());

      

       Square s2 = new Square(3, \"Trump Square\");

       System.out.println(s2.toString());

   }

}

 View History Bookmarks Window Help drive. 2231-02 ooo. 2016F CPS2231.02 Google D. Exercise Lab 3 Inheritance Given an abstract class Shape as designed below. c
 View History Bookmarks Window Help drive. 2231-02 ooo. 2016F CPS2231.02 Google D. Exercise Lab 3 Inheritance Given an abstract class Shape as designed below. c

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site