Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Prof. Dave Pitts CS585A: Practical Java Programming Spring 2016 Programming Assi

ID: 3688724 • Letter: P

Question

Prof. Dave Pitts CS585A: Practical Java Programming Spring 2016 Programming Assignment 4 For this assignment, you will follow the formatting guidelines found in "Program Readability and Formatting Guidelines" on Canvas posted with the PDF for this assignment. Your programs will be graded according the rubric posted with this assignment on Canvas. Please use the exact class and package names specified for this assignment. Part 1: Polygon.java, Triangle.java, Square.java, Pentagon.java, Hexagon.java, Heptagon.java, Octagon.java, and PolygonPlay.java (in package pa4) For this program, you'll implement a class hierarchy that represents regular polygons: geometric shapes with three or more sides where the sides are equal. The base class will be a public class Polygon. You'll extend Polygon to implement the classes Triangle (three sided polygon), Square (four sided polygon), Pentagon (five sided polygon), Hexagon (six sided polygon), Heptagon (seven sided polygon), and Octagon (eightsided polygon). Polygon should be an abstract base that implements the Comparable interface. The Polygon class should define the following methods: public Polygon(double s) throws ZeroSideForPolygonException A constructor which sets the size of the polygon side to s. All of the other shapes will rely on this size. public abstract double area(); This abstract method must be implemented in the extensions to return the area of the polygon public abstract double perimeter(); This abstract method must be implemented in the extensions to returns the area of the polygon public int compareTo(Polygon p) Compares the calling polygon with the polygon p by comparing the area of the calling polygon to the area of the polygon p. Returns negative in the calling polygon area is less that that of polygon p, zero in the areas are equal, and positive in the calling polygon area is greater than that of polygon p. public boolean equals(Polygon p) Returns true in the area of the calling polygon is equal to that of polygon p and false otherwise. public abstract String toString(); This forces the extensions to override the toString method. public double getSide() Returns the value of the polygon's side. Classes Triangle, Square, Pentagon, Hexagon, Heptagon, and Octgaon all should be public classes (in the package pa4) and should extend the Polygon class. They must implement the area(), perimeter(), and toString() methods. The formulas for the classes are as follows (where a is the value for the polygon's side): • Triangle Page 1 of 3 Prof. Dave Pitts CS585A: Practical Java Programming Spring 2016 area = 3 a 2 2 perimeter = 3a • Square area = a 2 perimeter = 4 a • Pentagon area = 25+105 a 2 4 perimeter = 5a • Hexagon area = 33 a 2 2 perimeter = 6a • Heptagon area = 7 4 a 2 cot( 7 ) , where cot(x )= 1 tan (x) perimeter = 7a • Octagon area = a 2 (2+22) perimeter = 8a The format for your polygons that should be returned by toString is: XXXXXXX That is, a square with side 10 should display a string: Square<10, 100, 40> Note that the Polygon class constructor throws an exception called ZeroSizeForPolygonException. The Polygon class constructor should throw this exception if the size parameter is zero or negative. You must implement the ZeroSizeForPolygonException as we did in class. You can make is a non-public class in the Polygon.java file. Submission of your Assignment Your classes must be written to be in the package called pa4 and they must have the names used above. Your class methods must have exactly the signatures (definitions) described above. Deliverables You will submit your classes as a ZIP file through Canvas by the due date. Your .java source files should be at the top level of the ZIP file. That is, your Java source should not be zipped in the package folder (or any other folder). Again, you will lose marks if your ZIP file does Page 2 of 3 Prof. Dave Pitts CS585A: Practical Java Programming Spring 2016 not conform to these requirements. Page 3 of 3

Explanation / Answer

Here is the code. In the question provided,reference to one Exception is given. You missed providing it. So, I have provided the rest of working code. Please incorporate the code for ZeroException in provided program. . I believe it's a simple chnage

abstract class Polygon implements Comparable<Polygon>
{
    public double s;
    public Polygon(double s)
    //throws ZeroSideForPolygonException
    {
        this.s=s;
    }
    public abstract double area();
    public abstract double perimeter();
    public abstract String toString();
    public boolean equals(Polygon p)
    {
        if(this.area()==p.area())
        return true;
        else
        return false;
    }
    public int compareTo(Polygon p)
    {
        if(this.area()==p.area())
        return 0;
        else
        if(this.area()<p.area())
        return -1;
        else
        return 1;
    }
    public double getSide()
    {
        return s;
    }

}

public class Triangle extends Polygon
{
    Triangle(double a)
    {
        super(a);
    }
     public double area()
     {
         return (Math.sqrt(3) * s *s)/2;
     }
    public double perimeter()
    {
        return 3*s;
    }
    public String toString()
    {
        return " "+this.getClass().getSimpleName()+"<"+s+", "+ area()+","+ perimeter()+">";
    }
}

public class Square extends Polygon
{
    Square(double a)
    {
        super(a);
    }
     public double area()
     {
         return s*s;
     }
    public double perimeter()
    {
        return 4*s;
    }
    public String toString()
    {
        return " "+this.getClass().getSimpleName()+"<"+s+", "+ area()+","+ perimeter()+">";
    }
}

public class Pentagon extends Polygon
{
    Pentagon(double a)
    {
        super(a);
    }
     public double area()
     {
         return (Math.sqrt(25+(10*Math.sqrt(5))) * s)/4;
     }
    public double perimeter()
    {
        return 5*s;
    }
    public String toString()
    {
       return " "+this.getClass().getSimpleName()+"<"+s+", "+ area()+","+ perimeter()+">";
    }
}

public class Hexagon extends Polygon
{
    Hexagon(double a)
    {
        super(a);
    }
     public double area()
     {
         return (3*Math.sqrt(3) * s *s)/2;
     }
    public double perimeter()
    {
        return 6*s;
    }
    public String toString()
    {
        return " "+this.getClass().getSimpleName()+"<"+s+", "+ area()+","+ perimeter()+">";
    }
}

public class Heptagon extends Polygon
{
    Heptagon(double a)
    {
        super(a);
    }
     public double area()
     {
         return (7*s*s/Math.tan(180/7))/4;
     }
    public double perimeter()
    {
        return 7*s;
    }
    public String toString()
    {
        return " "+this.getClass().getSimpleName()+"<"+s+", "+ area()+","+ perimeter()+">";
    }
}
public class Octagon extends Polygon
{
    Octagon(double a)
    {
        super(a);
    }
     public double area()
     {
         return (s*s*(2 +(2*Math.sqrt(2))))/4;
     }
    public double perimeter()
    {
        return 8*s;
    }
    public String toString()
    {
       return " "+this.getClass().getSimpleName()+"<"+s+", "+ area()+","+ perimeter()+">";
    }
}

public class HelloWorld{

     public static void main(String []args){
     Polygon[] shapes=new Polygon[6];
     double input=3;
shapes[0]=new Triangle(input);
shapes[1] = new Square(input);
shapes[2] = new Pentagon(input);
shapes[3] = new Hexagon(input);
shapes[4] = new Heptagon(input);
shapes[5] = new Octagon(input);

for(Polygon p:shapes){
     System.out.println(p.toString());
}

     }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote