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...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that stores the int value represented by this object. • A constructor that creates a MyInteger object for a default int value. What default value did you choose? What other values did you consider? Why did you make this choice? • A constructor that creates a MyInteger object for a specified int value. • A getter method, valueOf(), that returns value. • A setter method,...
Draw UML diagram Define a class named Document that contains a member variable of type string...
Draw UML diagram 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 function named getText that returns the text field and a way to set this value. Next, define a class for Email that is derived from Document and that includes member variables for the sender , recipient , and title of an e-mail message. Implement appropriate accessor and mutator functions. The body of...
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...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT