331,499 views
17 votes
17 votes
Define a class named Payment that contains an instance variable of type double that stores the amount of the payment and appropriate get and set methods. Also, create a method named paymentDetails that outputs an English sentence to describe the payment amount. 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. Define a class named PaymentTest class that contains the main() method that creates two CashPayment and two CreditCardPayment objects with different values and calls paymentDetails for each. (5 pts)

User Takis
by
2.8k points

1 Answer

13 votes
13 votes

Answer:

The code is given below:

class Payment

{

private double amount;

public Payment( )

{

amount = 0;

}

public Payment(double amount)

{

this.amount = amount;

}

public void setPayment(double amount)

{

this.amount = amount;

}

public double getPayment( )

{

return amount;

}

public void paymentDetails( )

{

System.out.println("The payment amount is " + amount);

}

}

class CashPayment extends Payment

{

public CashPayment( )

{

super( );

}

public CashPayment(double amt)

{

super(amt);

}

public void paymentDetails( )

{

System.out.println("The cash payment amount is "+ getPayment( ));

}

}

class CreditCardPayment extends Payment

{

private String name;

private String expiration;

private String creditcard;

public CreditCardPayment()

{

super( );

name = " ";

expiration = " ";

creditcard = "";

}

public CreditCardPayment(double amt, String name, String expiration, String creditcard)

{

super(amt);

this.name = name;

this.expiration = expiration;

this.creditcard = creditcard;

}

public void paymentDetails( )

{

System.out.println("The credit card payment amount is " + getPayment( ));

System.out.println("The name on the card is: " + name);

System.out.println("The expiration date is: " + expiration);

System.out.println("The credit card number is: " + creditcard);

}

}

class Question1Payment

{

public static void main(String[ ] args)

{

CashPayment cash1 = new CashPayment(50.5), cash2 = new CashPayment(20.45);

CreditCardPayment credit1 = new CreditCardPayment(10.5, "Fred", "10/5/2010",

"123456789");

CreditCardPayment credit2 = new CreditCardPayment(100, "Barney", "11/15/2009",

"987654321");

System.out.println("Cash 1 details:");

cash1.paymentDetails( );

System.out.println( );

System.out.println("Cash 2 details:");

cash2.paymentDetails( );

System.out.println( );

System.out.println("Credit 1 details:");

credit1.paymentDetails( );

System.out.println( );

System.out.println("Credit 2 details:");

credit2.paymentDetails( );

System.out.println( );

}

}

Output:

Define a class named Payment that contains an instance variable of type double that-example-1
User Lab Bhattacharjee
by
2.8k points