Java Classes Payment, Cash Payment, and Credit Card Payment
Not what you're looking for?
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.
CodeMate Hints
/**
* 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)
{
amount = paymentAmount;
}
/**
* Sets the payment amount
*/
public void setPayment(double paymentAmount)
{
amount = paymentAmount;
}
/**
* Returns the payment amount
*/
public double getPayment()
{
return amount;
}
/**
* Prints a description of the payment
*/
public void paymentDetails()
{
System.out.println("The payment amount is " + amount);
}
public static void main(String[] args)
{
// Create several test classes and invoke the paymentDetails method
Payment cash1 = new CashPayment(50.5);
Payment cash2 = new CashPayment(20.45);
Payment credit1 =
new CreditCardPayment(10.5, "Fred", "10/5/2010", "123456789");
Payment 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();
}
}
Purchase this Solution
Solution Summary
Java and Java compiler version 1.7.0_04 were used during development and testing of the attached program. Response mentions how the program can be compiled and tested at command line.
Solution Preview
Please rename attached 601665-Payment.java to Payment.java before you compile and execute the program.
Java and Java compiler version 1.7.0_04 were used during development and ...
Purchase this Solution
Free BrainMass Quizzes
Word 2010: Table of Contents
Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.
Basic Networking Questions
This quiz consists of some basic networking questions.
Inserting and deleting in a linked list
This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.
C# variables and classes
This quiz contains questions about C# classes and variables.
Word 2010: Tables
Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.