In java language Part A: Payment (40 points) Write three classes to represent a
ID: 3675772 • Letter: I
Question
In java language
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.
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.
Explanation / Answer
Answer Part A)
// Payment.java
import java.util.Calendar;
public class Payment
{
private double amount;
private Calendar date;
public Payment()
{
this.amount = 0.0;
this.date = null;
}
public Payment(double amount, Calendar date)
{
this.amount = amount;
this.date = date;
}
public double getAmount()
{
return amount;
}
public void setAmount(double amount)
{
this.amount = amount;
}
public Calendar getDate()
{
return date;
}
public void setDate(Calendar date)
{
this.date = date;
}
public String toString()
{
return "Amount: " + amount + ", Date: " + date.getTime();
}
}
---------------------------------------------------------------
// CashPayment.java
import java.util.Calendar;
public class CashPayment extends Payment
{
public CashPayment()
{
super();
}
public CashPayment(double amount, Calendar date)
{
super(amount, date);
}
public String toString()
{
return super.toString();
}
}
------------------------------------------------------------------
// CreditCardPayment.java
import java.util.Calendar;
public class CreditCardPayment extends Payment
{
private String name;
private String cardNumber;
public CreditCardPayment()
{
super();
name = "no name yet";
cardNumber = "no card yet";
}
public CreditCardPayment(double amount, Calendar date, String name, String cardNumber)
{
super(amount, date);
this.name = name;
this.cardNumber = cardNumber;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getCardNumber()
{
return cardNumber;
}
public void setCardNumber(String cardNumber)
{
this.cardNumber = cardNumber;
}
public boolean equals(CreditCardPayment other)
{
if(getAmount() != other.getAmount())
return false;
if(getDate().compareTo(other.getDate()) != 0)
return false;
if(name.compareToIgnoreCase(other.name) != 0)
return false;
if(cardNumber.compareToIgnoreCase(other.cardNumber) != 0)
return false;
return true;
}
public String toString()
{
return super.toString() + ", Name: " + name + ", Card Number: " + cardNumber;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.