Define a class named Payment that contains an instance variable of type double t
ID: 3545299 • Letter: D
Question
Define a class named Payment that contains an instance variable of type double
that stores the amount of the payment and appropriate accessor and mutator
methods. Also create a method named paymentDetails that outputs an English
sentence to describe the amount of the payment.
Next, define a class named CashPayment that is derived from Payment . This class
should redefine the paymentDetails method to indicate that the payment is in
cash. Include appropriate constructor(s).
Define a class named CreditCardPayment that is derived from Payment . This
class should contain instance variables for the name on the card, expiration date,
and credit card number. Include appropriate constructor(s). Finally, redefine the
paymentDetails method to include all credit card information in the printout.
Create a main method that creates at least two CashPayment and two
CreditCardPayment objects with different values and calls paymentDetails for each.
Explanation / Answer
Hi here are the classes that you need.
class Payment{
public double amtPayment;
public void setPaymentAmount(double payAmt){
amtPayment= payAmt;
}
public double getPaymentAmount(){
return amtPayment;
}
public void paymentDetails(){
System.out.println(" The amount for your payment is $" + this.getPaymentAmount());
}
}
class CashPayment extends Payment{
public CashPayment(double amt){
amtPayment=amt;
}
public void paymentDetails(){
System.out.println(" The amount for your cash payment is $" + this.getPaymentAmount() );
}
}
class CreditCardPayment extends Payment{
public String nameOnCard;
public String expirationDate;
public String creditCardNumber;
public CreditCardPayment(double amt, String name, String exp, String number){
amtPayment=amt;
nameOnCard=name;
expirationDate=exp;
creditCardNumber=number;
}
public void paymentDetails(){
System.out.println(" The amount for your credit card payment is $" + this.getPaymentAmount() + "You are using the card under the name " + this.nameOnCard + " with card number " + this.creditCardNumber + " which expires on " + this.expirationDate);
}
}
public static void main(String args[]){
CashPayment cash1= new CashPayment(100);
CashPayment cash2= new CashPayment(200);
CreditCardPayment ccredit1= new CreditCardPayment(100);
CreditCardPayment ccredit2= new CreditCardPayment(200);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.