JAVA homework help thank you Part A: Payment (40 points) Write three classes to
ID: 3676441 • Letter: J
Question
JAVA homework help thank you
Part A: Payment (40 points)
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:
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.
Note: you are not required to submit a driver program, but I strongly recommend you write one to test your classes!
Part B: Shapes (60 points)
I have provided a PolyShape class that represents polygons by their number of sides and an array that contains their side lengths. You will write child classes and a driver program to use PolyShape.
Do not modify the PolyShape class.
Write four classes that extend PolyShape.
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
In each class, include:
one or more constructors
getters and setters
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.
I have provided a sample program so you can see how the driver program works. To run the sample:
On a MAC, open a terminal window. On a PC, open a command window (type "cmd" in the search box).
Move to the folder where you downloaded the file (you can use "cd full/file/path").
Then use this command: java -jar ShapeCreator.jar
Your classes should work with your driver program, but they should be designed to work with other drivers, too (for example, a driver I might create).
Think carefully about what kind of checks should go in the driver program and what kind of checks should go in the individual classes.
Remember that object-oriented design states that each object should be in charge of its own information.
You will be graded both on your syntax but also your program design. For full credit, consider all of the design elements listed below.
PolyShape class
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;
}
}
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:
*****
* *
* *
*****
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.
Think about what information should be sent as parameters and what information can be hard-coded (using constants) into a class.
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.
Submission
Zip all files together and upload that file. Include the PolyShape file in your zip.
You will receive no credit for files that do not compile.
Include your name in the title of the zip file.
You can work in groups of up to four students on all projects. If you work in a group, submit only one submission and include all names in the comments section of the java file.
Late projects can be submitted up to a three days late with a 10% late penalty.
Explanation / Answer
//Payment.java
public class Payment
{
//declare variables as protected
protected double amount;
protected String date;
//defatult constructor
public Payment()
{
amount=0;
date=" ";
}
//parameterized cosntructor
public Payment(double amount , String date )
{
this.amount=amount;
this.date=date;
}
//Getter methods
public double getAmount()
{
return amount;
}
public String getDate()
{
return date;
}
//Returns the string representation of Payment object
@Override
public String toString() {
return "amount : "+amount+" date:"+date;
}
}
---------------------------------------------------------------------------------------------------
//CashPayment.java
public class CashPayment extends Payment
{
//Constructor that calls the base class
//Payment constructor
public CashPayment(double amount , String date )
{
super(amount, date);
}
//Returns the amount
public double getAmount()
{
return amount;
}
//Returns the date
public String getDate()
{
return date;
}
//Returns the string representation of Payment object
@Override
public String toString() {
return super.toString();
}
}
---------------------------------------------------------------------------------------------------
//CreditCardPayment.java
public class CreditCardPayment extends Payment
{
//declare variables for credit card payment
private String name;
private String creditCardNumber;
//Constructor that sets the name and credit card number
public CreditCardPayment(double amount , String date,
String name, String creditCardNumber)
{
//calling super class Payment constructor
super(amount, date);
this.name=name;
this.creditCardNumber=creditCardNumber;
}
//Equals method that returns true if both credit
//card attributes are same otherwise returns false
public boolean equals(CreditCardPayment creditObject) {
return amount==creditObject.getAmount()
&& date.endsWith(creditObject.getDate())
&& name.equals(creditObject.getName())
&&creditCardNumber.endsWith(creditObject.getCreditCard());
}
//Getter methods for name and credit card number
public String getName()
{
return name;
}
public String getCreditCard()
{
return creditCardNumber;
}
//Returns the string representation of Payment object
@Override
public String toString() {
return "Name :"+name+"Cradit Card Number : "+creditCardNumber+
super.toString();
}
}
---------------------------------------------------------------------------------------------------------------
/**The java PaymentDriver program that creates instance of
* Payment , CashPayment and CreditCardPayment and
* prints the payment, cash payment and credit card payment
* And also checks if two credit card payments are equal.*/
//PaymentDriver.java
public class PaymentDriver
{
public static void main(String[] args)
{
//Create an instance of Payment
Payment payment =new Payment(5000, "01-03-2016");
System.out.println("payment");
System.out.println(payment.toString());
//Create an instance of CashPayment
CashPayment cashPayment=new CashPayment(1000, "02-1-2016");
System.out.println("cash payment");
System.out.println(cashPayment.toString());
//Create an instance of CreditCardPayment
CreditCardPayment creditcard=new CreditCardPayment(2000, "01-02-2016",
"dinesh", "123-456-7890");
System.out.println("Credit card payment");
System.out.println(creditcard.toString());
CreditCardPayment creditcard2=new CreditCardPayment(2000, "01-02-2016",
"dinesh", "123-456-7890");
System.out.println("Credit card payment");
System.out.println(creditcard2.toString());
//Check if two credit card payments are equal
if(creditcard.equals(creditcard2))
System.out.println("Equal credit card payments");
else
System.out.println("Different credit card payments");
}
}
-------------------------------------------------------------------------------------------------------------------
Sample output:
payment
amount : 5000.0 date:01-03-2016
cash payment
amount : 1000.0 date:02-1-2016
Credit card payment
Name :dineshCradit Card Number : 123-456-7890amount : 2000.0 date:01-02-2016
Credit card payment
Name :dineshCradit Card Number : 123-456-7890amount : 2000.0 date:01-02-2016
Equal credit card payments
--------------------------------------------------------------------------------------------------------
//PolyShape.java
public class PolyShape
{
private int numSides;
private int[] sideLengths;
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;
}
}
----------------------------------------------------------------------------------------------------
//Quadrilateral.java
public class Quadrilateral extends PolyShape
{
//Constructor that sets the number of side and
//side lengths and calls the super class PolyShape
public Quadrilateral(int numSides, int[] sideLengths) {
super(numSides, sideLengths);
}
//Returns the string representation of Quadrilateral
@Override
public String toString() {
return "I am a Quadrilateral and "+super.toString();
}
}
----------------------------------------------------------------------------------------------------
//Rectangle.java
public class Rectangle extends PolyShape
{
//Constructor that calls the PolyShap that calls
//constructor of PolyShape
public Rectangle(int numSides, int[] sideLengths) {
super(numSides, sideLengths);
}
//Returns the area of rectangle
public int getArea()
{
//get sides array
int sides[]=getSideLengths();
//since side1 and side2 equal to side3 and side4
//returns sides[0] *sides[1]
return sides[0]*sides[1];
}
//Returns the string representation of rectangle
@Override
public String toString() {
return "I am a rectangle "+super.toString();
}
}
----------------------------------------------------------------------------------------------------
//Square.java
public class Square extends PolyShape
{
//Constructor that sets the number of side and
//side lengths and calls the super class PolyShape
public Square(int numSides, int[] sideLengths)
{
super(numSides, sideLengths);
}
//Returns the string representation of Quadrilateral
@Override
public String toString() {
return "I am a square and "+super.toString();
}
}
----------------------------------------------------------------------------------------------------
//Triangle.java
public class Triangle extends PolyShape
{
//Constructor that sets the number of side and
//side lengths and calls the super class PolyShape
public Triangle(int numSides, int[] sideLengths) {
super(numSides, sideLengths);
}
//Returns the string representation of Quadrilateral
@Override
public String toString() {
return "I am a triangle and "+super.toString();
}
}
----------------------------------------------------------------------------------------------------
//Tester program that tests the child classes
//of PolyShape
//ShapeCreator.java
public class ShapeCreator
{
public static void main(String[] args)
{
int quadralateralLengths[]={3,4,5,6};
//Create an instance of Quadrilateral
Quadrilateral quadralateral=new Quadrilateral(4, quadralateralLengths);
System.out.println(quadralateral.toString());
int sideLengths[]={3,3,3,3};
//Create an instance of Square
Square square=new Square(4, sideLengths);
System.out.println(square.toString());
int triangleLengths[]={3,3,3};
//Create an instance of Triangle
Triangle triangle=new Triangle(3, triangleLengths);
System.out.println(triangle.toString());
int rectLengths[]={3,4,3,4};
//Create an instance of Rectangle
Rectangle rectangle=new Rectangle(4, rectLengths);
System.out.println(rectangle.toString());
System.out.println("Rectangle area : "+rectangle.getArea());
}
}
sample output:
I am a Quadrilateral and I am a shape with 4 sides of length: 3 4 5 6
I am a polygon.
I am a square and I am a shape with 4 sides of length: 3 3 3 3
I am a polygon.
I am a triangle and I am a shape with 3 sides of length: 3 3 3
I am a polygon.
I am a rectangle I am a shape with 4 sides of length: 3 4 3 4
I am a polygon.
Rectangle area : 12
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.