Java Polymorphism Abstract Classes Lab Intro to Computer S

Java - Polymorphism & Abstract Classes Lab

Intro to Computer Science Java Group Lab #2 Polymorphism & Abstract Classes (20 points) Introduction In this lab, you will create an abstract class called Shape which is the ancestor of several concrete classes. This class will have two instance variables representing the perimeter and area of the specific shape being described. You\'ll then create four concrete shape classes (Triangle S are, Rectangle, and Circle) along with several methods which among other things, calculate and store the perimeter and shape of the specific shape being defined Complete each of the steps below (they do not need to be performed in any particular order, but you may want to read through the lab first to decide how you want your group to proceed) Part 1: Create the abstract class named Shape. It will have: two instance variables, both private doubles, named area and perimeter a single constructor which takes no arguments and sets both instance variables to zero. the appropriate getter (accessor) and setter (mutator) methods for the two instance variables abstract methods with headings public String toString and epublic boolean equals (Object) a requirement of CS 112. abstract methods with headings public void calculatePerimiter and public void calculateArea a method with signature public int getNumberOfsides which will return the number of sides for a specific shape In this class, return a 0

Solution

//Shape.java
public abstract class Shape {
  
   private double area;
   private double perimeter;
  
  
   public Shape() {
       area=0;
       perimeter=0;
   }
  
   public void setarea(double area){
       this.area=area;
   }
  
   public void setperimeter(double perimeter){
       this.perimeter=perimeter;
   }
  
  
   public double getarea(){
       return area;
   }
  
   public double getperimeter(){
       return perimeter;
   }
  
   //abstract methods
   public abstract String toString();
   public abstract boolean equals(Object obj);
  
   public abstract void calculatePerimeter();
   public abstract void calculateArea();
  
   public int getNumberOfSides()
   {
       return 0;
   }
  
}//end of Shape

--------------------------------------------------------------------------------------------------------------------------------------------


//Triangle.java
public class Triangle extends Shape{


   private double a;
   private double b;
   private double c;

   public Triangle(double a,double b,double c) {
       setSide1(a);
       setSide2(b);
       setSide3(c);
       calculatePerimeter();
       calculateArea();
   }

   public void setSide1(double a) {
       this.a=a;      
   }

   public void setSide2(double b) {
       this.b=b;      
   }

   public void setSide3(double c) {
       this.c=c;  
   }

   public double getSide1() {
       return a;
   }

   public double getSide2() {
       return b;
   }

   public double getSide3() {
       return c;
   }

   public String toString() {      
       String desc= String.format
               (\"Triangle has sides %3.2f , %3.2f and %3.2f (%d sides).\ \",
               a,b,c,getNumberOfSides())
               +String.format
               (\"Its perimeter is %3.2f and its area is %3.2f.\",
               getperimeter(),getarea());
      
       return desc;
   }
  
   public int getNumberOfSides()
   {
       return 3;
   }


   public boolean equals(Object obj) {

       Triangle t=(Triangle)obj;

       return a==t.getSide1() &&
               b==t.getSide2() &&
               c==t.getSide3() ;
   }


   public void calculatePerimeter() {

       double perimeter=(a+b+c)/2.0;
       setperimeter(perimeter);

   }


   public void calculateArea() {
       double p=getperimeter();      
       double area=Math.sqrt(p*(p-a)*(p-b)*(p-c));
       setarea(area);
   }
}//end of Triangle

--------------------------------------------------------------------------------------------------------------------------------------------

//Rectangle.java
public class Rectangle extends Shape{

   private double height;
   private double width;
  
  
   public Rectangle(double height, double width) {
       setHeight(height);
       setWidth(width);
       calculatePerimeter();
       calculateArea();
   }
  
  
   public void setHeight(double height) {
       this.height=height;
   }

   public void setWidth(double width) {
       this.width=width;      
   }
  
   public double getHeight() {
       return height;
   }

   public double getWidth() {
       return width;      
   }


  
   public String toString() {      
       String desc= String.format
               (\"Rectangle has height %3.2f and width %3.2f (%d sides).\ \",
               height,width,getNumberOfSides())
               +String.format
               (\"Its perimeter is %3.2f and its area is %3.2f.\",
               getperimeter(),getarea());
      
       return desc;
   }

  
   public boolean equals(Object obj) {
       Rectangle r=(Rectangle)obj;
      
       return height==r.getHeight()&&
               width==r.getWidth();
   }
  
   public int getNumberOfSides()
   {
       return 4;
   }

  
   public void calculatePerimeter() {      
       setperimeter(2*(width+height));
      
   }

  
   public void calculateArea() {
       setarea(width*height);      
   }
}//end of Rectangle class

--------------------------------------------------------------------------------------------------------------------------------------------

//Square.java
public class Square extends Rectangle{
  
   public Square(double side) {
       super(side, side);
       calculatePerimeter();
       calculateArea();
   }
  
   public String toString() {      
       String desc= String.format
               (\"Square has side %3.2f (%d sides).\ \",getHeight(),getNumberOfSides())
               +String.format
               (\"Its perimeter is %3.2f and its area is %3.2f.\",
               getperimeter(),getarea());
      
       return desc;
   }
  
   public int getNumberOfSides()
   {
       return 4;
   }

  
   public boolean equals(Object obj) {
       Square s=(Square)obj;      
       return getHeight()==s.getHeight();              
   }

  
   public void calculatePerimeter() {      
       setperimeter(4*getHeight());
      
   }

  
   public void calculateArea() {
       setarea(getHeight()*getWidth());
      
   }

}//end of Square class

--------------------------------------------------------------------------------------------------------------------------------------------


public class Circle extends Shape{

  
   private double radius;
  
   public Circle(double radius) {
       setRadius(radius);
       calculatePerimeter();
       calculateArea();
   }
  
   public void setRadius(double radius){
       this.radius=radius;
   }
  
   public double getRadius(){
       return radius;
   }
  
   public String toString() {      
       String desc= String.format
               (\"Circle has radius %3.2f \ \",getRadius())
               +String.format
               (\"Its perimeter is %3.2f and its area is %3.2f.\",
               getperimeter(),getarea());
      
       return desc;
   }

  
   public boolean equals(Object obj) {
       Circle c=(Circle)obj;      
       return radius==c.getRadius();              
   }

  
   public void calculatePerimeter() {      
       setperimeter(2*Math.PI*radius);
      
   }

  
   public void calculateArea() {
       setarea(Math.PI*radius*radius);
      
   }
}//end of Circle

--------------------------------------------------------------------------------------------------------------------------------------------


public class RightTriangle extends Triangle{

  
   public RightTriangle(double a,double b) {
       super(a, b,Math.sqrt(a*a+b*b));  
       calculatePerimeter();
       calculateArea();
   }
  
   public void setSide1(double a) {
       super.setSide1(a);  
   }

   public void setSide2(double b) {
       super.setSide2(b);      
       setSide3(Math.sqrt(getSide1()*getSide1()+getSide2()*getSide2()));
   }

  
  
  
   public String toString() {      
       String desc= String.format
               (\"Right triangle has sides %3.2f , %3.2f and %3.2f (%d sides).\ \",
               getSide1(),getSide2(),getSide3(),getNumberOfSides())
               +String.format
               (\"Its perimeter is %3.2f and its area is %3.2f.\",
               getperimeter(),getarea());
      
       return desc;
   }
}//end of Right angle

--------------------------------------------------------------------------------------------------------------------------------------------


//Tester class
//Driver.java
public class Driver {

   public static void main(String[] args) {


       Shape[] shapes=new Shape[8];

       shapes[0]=new Rectangle(10.30, 8.0);
       shapes[1]=new Rectangle(10.30, 8.0);
       shapes[2]=new Square(20.0);
       shapes[3]=new Triangle(6.50, 14.90, 11.30);
       shapes[4]=new Circle(4.31);
       shapes[5]=new Circle(8.25);
       shapes[6]=new RightTriangle(3.0, 4.0);
       shapes[7]=new RightTriangle(5.0, 12.0);

       int i=1;

       for (Shape shape : shapes) {
           System.out.println(\"Shape\"+i);
           System.out.println(shape);
           i++;      
       }


       System.out.println(\"\ \ Test equals() methods\");

       if(shapes[0].equals(shapes[1]))
           System.out.println(\"Rectangles 1 and 2 are equal\");

       if(shapes[4].equals(shapes[5]))
           System.out.println(\"Circles 5 and 6 are equal\");
       else
           System.out.println(\"Circles 5 and 6 are not equal\");

       if(shapes[4].equals(shapes[4]))
           System.out.println(\"Circle 5 equal to itself ? yes\");
       else
           System.out.println(\"Circle 5 equal to itself ? No\");
      
      
       System.out.println(\"Test right-triangle reassignments\");
      
       RightTriangle r=new RightTriangle(3, 4);
      
       System.out.printf(\"Original :\");
      
       System.out.printf(\"Right triangle has sides %2.2f,%2.2f, and %2.2f\",r.getSide1(),
               r.getSide2(),r.getSide3());
      

       r.setSide1(6.0);
       r.setSide2(8.0);
      
      
       System.out.printf(\"\ Revised :\");
       System.out.printf(\"Right triangle has sides %2.2f,%2.2f, and %2.2f\",r.getSide1(),
               r.getSide2(),r.getSide3());

   }

}

--------------------------------------------------------------------------------------------------------------------------------------------

Sampleoutput:

Shape1
Rectangle has height 10.30 and width 8.00 (4 sides).
Its perimeter is 36.60 and its area is 82.40.
Shape2
Rectangle has height 10.30 and width 8.00 (4 sides).
Its perimeter is 36.60 and its area is 82.40.
Shape3
Square has side 20.00 (4 sides).
Its perimeter is 80.00 and its area is 400.00.
Shape4
Triangle has sides 6.50 , 14.90 and 11.30 (3 sides).
Its perimeter is 16.35 and its area is 34.34.
Shape5
Circle has radius 4.31
Its perimeter is 27.08 and its area is 58.36.
Shape6
Circle has radius 8.25
Its perimeter is 51.84 and its area is 213.82.
Shape7
Right triangle has sides 3.00 , 4.00 and 5.00 (3 sides).
Its perimeter is 6.00 and its area is 6.00.
Shape8
Right triangle has sides 5.00 , 12.00 and 13.00 (3 sides).
Its perimeter is 15.00 and its area is 30.00.


Test equals() methods
Rectangles 1 and 2 are equal
Circles 5 and 6 are not equal
Circle 5 equal to itself ? yes
Test right-triangle reassignments
Original :Right triangle has sides 3.00,4.00, and 5.00
Revised :Right triangle has sides 6.00,8.00, and 10.00

Note: Due to time constraint, little explanation is given.

Java - Polymorphism & Abstract Classes Lab Intro to Computer Science Java Group Lab #2 Polymorphism & Abstract Classes (20 points) Introduction In this
Java - Polymorphism & Abstract Classes Lab Intro to Computer Science Java Group Lab #2 Polymorphism & Abstract Classes (20 points) Introduction In this
Java - Polymorphism & Abstract Classes Lab Intro to Computer Science Java Group Lab #2 Polymorphism & Abstract Classes (20 points) Introduction In this
Java - Polymorphism & Abstract Classes Lab Intro to Computer Science Java Group Lab #2 Polymorphism & Abstract Classes (20 points) Introduction In this
Java - Polymorphism & Abstract Classes Lab Intro to Computer Science Java Group Lab #2 Polymorphism & Abstract Classes (20 points) Introduction In this
Java - Polymorphism & Abstract Classes Lab Intro to Computer Science Java Group Lab #2 Polymorphism & Abstract Classes (20 points) Introduction In this
Java - Polymorphism & Abstract Classes Lab Intro to Computer Science Java Group Lab #2 Polymorphism & Abstract Classes (20 points) Introduction In this
Java - Polymorphism & Abstract Classes Lab Intro to Computer Science Java Group Lab #2 Polymorphism & Abstract Classes (20 points) Introduction In this

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site