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: