Question

Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type...

Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails().

Define a class named CashPayment that is derived from Payment. This class will have an additional instance variable of type String (a non-static member variable) that specifies a type of currency, such as "dollar", "euro", "peso", "yen", etc. This class should override the paymentDetails method to indicate that the payment is in cash, and the type of currency used. Include appropriate constructor(s) and accessor/mutator methods for all instance variablesand. Override toString() with contents of cash payment details (should still call paymentDetails()). Define a class named CreditCardPayment that is derived from Payment. This class should contain instance variables (member variables) for the name on the card, expiration date, and credit card number. Include appropriate constructor(s) and accessor/mutator methods for all instance variables.

Finally, redefine thepaymentDetails method to include all credit card information in the printout. Override toString() method with contents of credit payment details (should still call paymentDetails()). Create a main method within Payment that creates at least two CashPayment and two CreditCardPayment objects with different values and payment amounts. Call toString() methods from each object to print the details of each payment.

For an additional challenge: Use an array of Payment type references to represent your objects. Use a "for" loop to show the contents of each Payment.

Programming Language for this problem needs to be in JAVA. Hopefully it runs without any errors on Eclipse.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Program:

package paymentdriver;

//Payment class

class Payment {
private double paymentAmount;

//Default Constructor
public Payment() {
this.paymentAmount = 0;
}
  
//Arg Constructor
public Payment(double paymentAmount) {
this.paymentAmount = paymentAmount;
}
  
//Properties
public double getPaymentAmount() {
return paymentAmount;
}

public void setPaymentAmount(double paymentAmount) {
this.paymentAmount = paymentAmount;
}
  
//Payment details method
public void paymentDetails()
{
System.out.println("\n Payment Amount: " + this.paymentAmount);
}

@Override
public String toString() {
return "Payment{" + "paymentAmount=" + paymentAmount + '}';
}
}

//Child class 1


class CashPayment extends Payment
{
private String type;

public CashPayment(String type) {
this.type = type;
}

public CashPayment(String type, double paymentAmount) {
super(paymentAmount);
this.type = type;
}
  
//Payment details method
public void paymentDetails()
{
System.out.println("\n Cash Payment Amount: " + getPaymentAmount() + " " + this.type);
}

@Override
public String toString() {
return "CashPayment{" + "Amount: " + getPaymentAmount() + ", type=" + type + '}';
}
}

//Child class 2


class CreditCardPayment extends Payment
{
private String nameOnCard;
private String expDate;
private String creditCardNumber;

public CreditCardPayment(String nameOnCard, String expDate, String creditCardNumber) {
this.nameOnCard = nameOnCard;
this.expDate = expDate;
this.creditCardNumber = creditCardNumber;
}

public CreditCardPayment(String nameOnCard, String expDate, String creditCardNumber, double paymentAmount) {
super(paymentAmount);
this.nameOnCard = nameOnCard;
this.expDate = expDate;
this.creditCardNumber = creditCardNumber;
}
  
//Payment details method
public void paymentDetails()
{
System.out.println("\n Card Payment: \n Name on Card: " + this.nameOnCard + "\n Card Number: " + this.creditCardNumber + "\n Expiry Date: " + this.expDate + " Amount: " + getPaymentAmount() + " \n");
}

@Override
public String toString() {
return "CreditCardPayment{ Amount: " + getPaymentAmount() + ", nameOnCard=" + nameOnCard + ", expDate=" + expDate + ", creditCardNumber=" + creditCardNumber + '}';
}
}

public class PaymentDriver {
public static void main(String[] args) {
  
//Creating objects
CashPayment cObj1 = new CashPayment("dollar");
CashPayment cObj2 = new CashPayment("euro");
  
//Assigning payment
cObj1.setPaymentAmount(250);
cObj2.setPaymentAmount(345);
  
System.out.println(cObj1);
System.out.println(cObj2);
  
CreditCardPayment crObj1 = new CreditCardPayment("John Nick", "08-06-2022", "3265 6478 9855 6547");
CreditCardPayment crObj2 = new CreditCardPayment("Mary Lee", "01-02-2032", "5984 6598 6664 8955");
  
//Assigning payment
crObj1.setPaymentAmount(980);
crObj2.setPaymentAmount(748);
  
System.out.println(crObj1);
System.out.println(crObj2);
  
}
}
____________________________________________________________________________________________

Sample Run:

Output - PaymentDriver (run) * run: CashPayment Amount: 250.0, type=dollar) CashPayment Amount: 345.0, type=euro} CreditCardP

Add a comment
Know the answer?
Add Answer to:
Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Code the following Program in C++ Define a class named Payment that contains a member variable...

    Code the following Program in C++ Define a class named Payment that contains a member variable of type float that stores the amount of the payment and appropriate accessor and mutator methods.    Also create a member function named paymentDetails that outputs an English sentence that describes the amount of the payment. Next define a class named CashPayment that is derived from Payment. This class should redefine the paymentDetails function to indicate that the payment is in cash. Include appropriate constructor(s)....

  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

  • Create a class called Retangle.java that contains two double-precision instance variables named width and height. The...

    Create a class called Retangle.java that contains two double-precision instance variables named width and height. The class should include all kinds of overloaded constructors. Additionally, there should be two accessor methods, mutator methods, class method named area() that returns the area of a Rectangle object. Inside main(),fully test all methods.

  • IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named...

    IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...

  • 7. PersonData and CustomerData classes Design a class  named PersonData with the following member variables...

    7. PersonData and CustomerData classes Design a class  named PersonData with the following member variables : lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables . Next, design a class  named CustomerData, which is derived from the PersonData class . The CustomerData class should have the following member variables : customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool....

  • Create a class Circle with one instance variable of type double called radius. Then define an...

    Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...

  • 7. PersonData and CustomerData classes Design a class named PersonData with the following member variables: lastName...

    7. PersonData and CustomerData classes Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData, which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailinglist The customer Number variable will be used to hold a unique integer for each customer. The mailing List variable should be a bool....

  • Define an interface named Shape with a single method named area that calculates the area of...

    Define an interface named Shape with a single method named area that calculates the area of the geometric shape:        public double area(); Next, define a class named Circle that implements Shape. The Circle class should have an instance variable for the radius, a constructor that sets the radius, an accessor and a mutator method for the radius, and an implementation of the area method. Define another class named Rectangle that implements Shape. The Rectangle class should have instance variables...

  • Design a class for python named PersonData with the following member variables: lastName firstName address city...

    Design a class for python named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData , which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool . It will be...

  • In Java For the following questions, “define a class” means the following: • Create an appropriately-named...

    In Java For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT