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

Help on Java Homework! Part A: Payment. Write three classes to represent a payme

ID: 3585836 • Letter: H

Question

Help on Java Homework!

Part A: Payment. Write three classes to represent a payment.

-A payment class represents a payment by the amount of money (as a double) and the date of the payment.

-A cash payment represents a payment by the amount and date.

-A credit card payment represents a payment by the amount, date, and also the name and credit card number.

In each class, include (or use inherited versions of): instance data variables getters and setters a toString method In the credit card payment class, include an equals method. Two payments are logically equivalent if they have the same amount, date, name, and credit card number.

Part B: Shapes. I have provided a PolyShape class that represents polygons by their number of sides and an array that contains their side lengths.

import java.util.Arrays;

public class PolyShape {
   private int numSides;
   private int[] sideLengths;
  
   /*
   * note: PolyShape constructor takes a variable-length parameter list;
   * this means it can be invoked with any number of integers;
   * inside the constructor, sideLengths is treated as an int[]
   *
   */
   public PolyShape(int ... sideLengths) {
       this.sideLengths = sideLengths;
       this.numSides = sideLengths.length;
   }
  
   public int getNumSides() {
       return numSides;
   }
   public int[] getSideLengths() {
       return Arrays.copyOf(sideLengths, sideLengths.length);
   }
   public int getPerimeter() {
       int perim = 0;
       for(int n : sideLengths)
           perim += n;
       return perim;
   }
   public String toString() {
       String s = "I am a shape with " + numSides + " sides of length: ";
       for(int length : sideLengths)
           s += length + " ";
       s += " I am a polygon.";
       return s;
   }
}

You will write child classes and a driver program to use PolyShape.

Write four classes that extend PolyShape (either directly or indirectly).

Quadrilateral

-This class represents polygons with four sides

Rectangle

-The class represents four-sided polygons where opposite sides are equal length.

-This class should have a method getArea() that takes no parameters and returns an integer.

Square

-This class represents four-sided polygons where all four sides are of equal length.

-This class should have a method getArea() that takes no parameters and returns an integer.

Triangle

-This class represents polygons with three sides

-This class should have methods isIsoceles and isEquilateral that returns a boolean if two or three (respectively) sides are of equal length

-Note you do not have to make sure the three side lengths represent a mathematically valid triangle!

In each class, include:

-one or more constructors

-getters and setters (if appropriate/necessary)

-toString method that returns the number of sides, the side lengths, and all possible names for a shape.

For example, a square might print "I have four sides of length 3, 3, 3, and 3. I am a polygon. I am a quadrilateral. I am a rectangle. I am a square."

-Write an interactive driver program. The program allows the user to repeatedly create a shape by entering its dimensions.

Note: you do not need to handle negative or decimal values in the driver program. You can assume the user will enter positive integer input only. The program prints the object created and the perimeter, area (if applicable), and isIsoceles/isEquilateral (if applicable) methods.

Think carefully about what kind of checks should go in the driver program and what kind of checks should go in the individual classes.

For Both Parts When designing your classes, consider the following.

-For all required methods, your class can either include the method directly or inherit it- whichever makes sense for that method!

-Think about what the parent-child relationship should be between the classes.

-Move common code as high up in the hierarchy as possible.

-Use super whenever possible.

-Avoid repeated code.

-Follow appropriate naming and style conventions.

-Follow good principals of object-oriented design and inheritance.

Extra Credit (10 points) Print out a text-based graphic of the shape created when the user creates a square or a rectangle. For example, a 4x5 rectangle might print:

*****

* *

* *

*****

Explanation / Answer

Please find my implementation.

I have not implemented extra credit part. Please repost in separate post.

public class PolyShape {

private int numSides;

private int[] sideLengths;

  

/*

   * note: PolyShape takes in an int[] because we don't know

   * in advance how many sides a polygon will have... but that

   * doesn't mean the child classes have to follow that same

   * setup- make sure the parameters of the child class

   * constructors make sense for that class!

   *

   * also note: there is no error checking here to make sure

   * that sideLengths has the appropriate number of values; for now,

   * we just assume the user will send in the proper information

   */

public PolyShape(int numSides, int[] sideLengths) {

this.numSides = numSides;

this.sideLengths = sideLengths;

}

  

public void setNumSides(int numSides) {

this.numSides = numSides;

}

public int getNumSides() {

return numSides;

}

public void setSideLengths(int[] sideLengths) {

this.sideLengths = sideLengths;

}

public int[] getSideLengths() {

return sideLengths;

}

public int getPerimeter() {

int perim = 0;

for(int n : sideLengths)

perim += n;

return perim;

}

  

public String toString() {

String s = "I am a shape with " + numSides + " sides of length: ";

for(int length : sideLengths)

s += length + " ";

s += " I am a polygon.";

return s;

}

}

class Quadrilateral extends PolyShape{

   public Quadrilateral(int sides[]) {

       super(sides.length, sides);

   }

   public String toString() {

String s = "I am a shape with " + getNumSides()+ " sides of length: ";

for(int length : getSideLengths())

s += length + " ";

s += " I am a Quadrilateral.";

return s;

}

}

////////////////////////////////////////////////////////////////////////////////

class Rectangle extends PolyShape{

   public Rectangle(int numSides, int[] sideLengths) {

       super(numSides, sideLengths);

   }

  

   public int getArea(){

       return getSideLengths()[0]*getSideLengths()[2];

   }

   public String toString() {

String s = "I am a shape with " + getNumSides()+ " sides of length: ";

for(int length : getSideLengths())

s += length + " ";

s += " I am a Rectangle.";

return s;

}

  

}

///////////////////////////////////////////////////////////////////////////////

class Square extends PolyShape{

   public Square(int numSides, int[] sideLengths) {

       super(numSides, sideLengths);

   }

  

   public int getArea(){

       return getSideLengths()[0]*getSideLengths()[2];

   }

   public String toString() {

String s = "I am a shape with " + getNumSides()+ " sides of length: ";

for(int length : getSideLengths())

s += length + " ";

s += " I am a Square.";

return s;

}

}

////////////////////////////////////////////////////////////////////////////////////

// Triangle class

class Triangle extends PolyShape{

   public Triangle(int numSides, int[] sideLengths) {

       super(numSides, sideLengths);

   }

  

   public boolean isIsoceles(){

       int sides[] = getSideLengths();

       if((sides[0]==sides[1]) || (sides[1]==sides[2]) || (sides[0]==sides[2]))

           return true;

       return false;

   }

  

   public boolean isEquilateral(){

       int sides[] = getSideLengths();

       if((sides[0]==sides[1]) && (sides[1]==sides[2]))

           return true;

       return false;

   }

   public String toString() {

String s = "I am a shape with " + getNumSides()+ " sides of length: ";

for(int length : getSideLengths())

s += length + " ";

s += " I am a Triangle.";

return s;

}

  

}

class Driver{

   public static void main(String[] args) {

       PolyShape[] shapes = {

           new Rectangle(4, new int[]{2,3,2,3}),

           new Square(4, new int[]{3,3,3,3}),

           new Triangle(3, new int[]{3,4,5}),

           new Triangle(3, new int[]{5,5,7})

       };

      

       for(PolyShape p : shapes){

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

           System.out.println(p.getPerimeter());

           if(p instanceof Rectangle)

               System.out.println(((Rectangle)p).getArea());

           if(p instanceof Square)

               System.out.println(((Square)p).getArea());

          

       }

   }

}