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...

  • 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....

  • 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....

  • 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...

  • 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...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • A java class named Book contains: instance data for the title, author, publisher and copyright year...

    A java class named Book contains: instance data for the title, author, publisher and copyright year a constructor to accept and initialize the instance data variables set and get methods a toString() method to return a formatted, multi-line description of the book Produce a child class that inherits from Book. The class is called Dictionary. Dictonary must include: instance data that describes the number of words in the dictionary a constructor that takes in all information needed to describe a...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and...

    Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and name variables (do not include topTrick). Note: The type refers to what the breed typically does; for example, a corgi would be a “cattle herding dog.” A Shiba Inu would be a “hunting dog.” Create the setTopTrick() mutator method Dog is parent class Corgi and Driver are subclasses Complete the Corgi class: Using the UML Class diagram, declare the instance variables. Create the two...

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
ADVERTISEMENT