Question

In: Computer Science

In JAVA please... Define a class named Payment that contains an instance variable "paymentAmount" (non-static member...

In JAVA please...

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.

Solutions

Expert Solution


public class Payment {
   double paymentAmount;

   public Payment(double paymentAmount) {
       super();
       this.paymentAmount = paymentAmount;
   }

   public double getPaymentAmount() {
       return paymentAmount;
   }

   public void setPaymentAmount(double paymentAmount) {
       this.paymentAmount = paymentAmount;
   }
  
   public String paymentDetails()
   {
       return "The Payment Amount is "+paymentAmount+"\n";
   }

   public String toString() {
       return paymentDetails();
   }
  
   public static void main(String[] args)
   {
       CashPayment cash1 = new CashPayment(250.00,"dollar");
       CashPayment cash2 = new CashPayment(520.00,"peso");
       CreditCardPayment credit1 = new CreditCardPayment(125.25,"Peter","May-2025","2541 2568 1258 4568");
       CreditCardPayment credit2 = new CreditCardPayment(285.00,"Stark","Sep-2030","5684 2256 1445 2589");
       System.out.println(cash1.toString());
       System.out.println(cash2.toString());
       System.out.println(credit1.toString());
       System.out.println(credit2.toString());
   }
  
}

class CashPayment extends Payment{
   String currency;

   public CashPayment(double paymentAmount, String currency) {
       super(paymentAmount);
       this.currency = currency;
   }

   public String getCurrency() {
       return currency;
   }

   public void setCurrency(String currency) {
       this.currency = currency;
   }
  
   public String paymentDetails()
   {
       return "The Payment Amount is "+paymentAmount+" "+currency+"\n";
   }
  
   public String toString() {
       return paymentDetails();
   }
  
}

class CreditCardPayment extends Payment{
  
   String nameOnCard;
   String expDate;
   String cardNumber;
     
   public CreditCardPayment(double paymentAmount, String nameOnCard, String expDate, String cardNumber) {
       super(paymentAmount);
       this.nameOnCard = nameOnCard;
       this.expDate = expDate;
       this.cardNumber = cardNumber;
   }

   public String getNameOnCard() {
       return nameOnCard;
   }

   public void setNameOnCard(String nameOnCard) {
       this.nameOnCard = nameOnCard;
   }

   public String getExpDate() {
       return expDate;
   }

   public void setExpDate(String expDate) {
       this.expDate = expDate;
   }

   public String getCardNumber() {
       return cardNumber;
   }

   public void setCardNumber(String cardNumber) {
       this.cardNumber = cardNumber;
   }
  
   public String paymentDetails()
   {
       return "The Payment Amount is "+paymentAmount+"\nName on the card: "+nameOnCard+"\nExpiry date: "+expDate+"\nCredit card number: "+cardNumber+"\n";
   }
  
   public String toString() {
       return paymentDetails();
   }
  
}


/*Output:
The Payment Amount is 250.0 dollar

The Payment Amount is 520.0 peso

The Payment Amount is 125.25
Name on the card: Peter
Expiry date: May-2025
Credit card number: 2541 2568 1258 4568

The Payment Amount is 285.0
Name on the card: Stark
Expiry date: Sep-2030
Credit card number: 5684 2256 1445 2589

*/


Related Solutions

Define a class named Document that contains an instance variable of type String named text that...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document.
IN JavaDefine a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document.
Code in Java Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value.Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document.
IN JAVADefine a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value.Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
Define a class named Document that contains a member variable of type String named text that stores any textual content for the document.
Define a class named Document that contains a member variable of type String named text that stores any textual content for the document. Create a method named toString() that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes member variables for the sender, recipient, and title of an email message. Implement appropriate accessor and mutator methods. The body of the email message should...
I need this done in JAVA. Define a class named Cash. The class contains the following...
I need this done in JAVA. Define a class named Cash. The class contains the following public elements: A Double stored property that contains the amount of money (dollars and cents) described by an object of the class. A read-only, computed property. It calculates and returns the minimum number of U.S. bills and coins that add up to the amount in the stored property.  The return value is an Int array of length 9 that contains (beginning with index 0 of...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
java Define the Circle2D class that contains: • Two double data fields named x and y...
java Define the Circle2D class that contains: • Two double data fields named x and y that specify the center of the circle with get methods. A data field radius with a get method. • A data field radius with a get method. • A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius. • A constructor that creates a circle with the specified x, y, and radius. • A method getArea()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT