Define a class named Payment that contains a member variable of type double that
ID: 3553938 • Letter: D
Question
Define a class named Payment that contains a member variable of type double that stores
the amount of the payment and appropriate accessor and mutator methods. Also create a
method named paymentDetails that outputs an English sentence to describe the amount of
the payment. 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 member 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 in the printout. Create a
main method that creates at least two CashPayment and two CreditCardPayment objects
with different values and calls paymentDetails for each.
ANSWER HINT******
/**
* This program introduces inheritance through a problem of
* creating two types of Payments, Cash and Credit. The
* paymentDetails method outputs in English a sentence that describes
* the payment.
*/
/**
* Base class that holds payment amount provides a method for returning
* a description of the payment.
*/
public class Payment
{
//Payment amount
private double amount;
//Constructor to initialize amount to 0
public Payment()
{
amount = 0.0;
}
/**
* Constructor to initialize payment amount
*/
public Payment(double paymentAmount)
{
/**Must be your own original code. No copy and paste from internet. Easy to locate copied code. Also, must have java comments, state below (1). Points rewarded once ALL requirements are met. Thanks. **/
1. Comments within your code. NO COMMENTS, NO POINTS. Using Eclipse IDE, you can have Eclipse create the comments sections for you automatically. You should have method documentations as well as inline documentations. Example:
Explanation / Answer
import java.io.*;
public class Payment
{
//Payment amount
private double amount;
//Constructor to initialize amount to 0
public Payment()
{
amount = 0.0;
}
/**
* Constructor to initialize payment amount
*/
public Payment(double paymentAmount)
{
amount=paymentAmount;
}
public void paymentDetails()
{
System.out.println("The amount paid is "+amount);
}
}
//Cash Class
public class cashPayment extends Payment
{
//Payment amount
private double amount;
/**
* Constructor to initialize payment amount
*/
public cashPayment(double paymentAmount)
{
amount=paymentAmount;
}
public void paymentDetails() //Displays amount payment in cash
{
System.out.println("The amount is paid in cash. Amount paid "+amount);
}
}
//Credit Class
public class creditPayment extends Payment
{
//Payment amount
private double amount;
//data member to stores name on the card, expiration date and card number.
private String name,exdate;
private long cardnumber;
/*
* Constructor to initialize the card with its details and amount paid.
*/
public creditPayment(String nm,String expdate,long no,double paymentAmount)
{
name=nm;
exdate=expdate;
cardnumber=no;
amount = paymentAmount;
}
public void paymentDetails() //Displays amount payment in credit and card details
{
System.out.println("The amount is paid using credit card. Amount paid "+amount);
System .out.println("Card Details:");
System.out.println("Name: "+name+" Expiration Date:"+exdate+" Card Number:"+cardnumber);
}
}
//Main Class
public class Main
{
public static void main(String args[])
{
cashPayment cp1=new cashPayment(100.0);
cashPayment cp2=new cashPayment(157.56);
creditPayment crp1=new creditPayment("Alex","26/12/2020",145679,146.7);
creditPayment crp2=new creditPayment("Max","21/02/2015",15573,6786.7);
cp1.paymentDetails();
cp2.paymentDetails();
crp1.paymentDetails();
crp2.paymentDetails();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.