This is what my code is about(Homework question):
1. Define a class named Payment that contains an instance variable of type double that stores the amount of payment and appropriate accessor and mutator methods. Also create a method named PaymentDetails that outputs an english sentence to describe the amount of payment.
2. 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.
3. 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. Finally, redefine the paymentDetails method to include all credit card information in the print out.
Create a main method that creates at least two CashPayment and two CreditCardPayment objects with different values and calls paymentDetails for each.
I'm completely new to java so I'll sound like a newbie.
Here's my code:
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(); } }
I have a lot of errors that I can't figure out:
ddddd.java:13: illegal start of expression
public double getPayment(){
^
ddddd.java:13: ';' expected
public double getPayment(){
^
ddddd.java:16: illegal start of expression
public void paymentDetails(){
^
ddddd.java:16: illegal start of expression
public void paymentDetails(){
^
ddddd.java:16: ';' expected
public void paymentDetails(){
ddddd.java:7: illegal start of expression
public Payment(double amount){
^
ddddd.java:7: '.class' expected
public Payment(double amount){
^
ddddd.java:7: ';' expected
public Payment(double amount){
^
ddddd.java:10: illegal start of expression
public void setPayment(double amount){
^
ddddd.java:10: illegal start of expression
public void setPayment(double amount){
^
ddddd.java:10: ';' expected
public void setPayment(double amount){
^
ddddd.java:10: ';' expected
public void setPayment(double amount){
^
ddddd.java:13: illegal start of expression
public double getPayment(){
^
ddddd.java:13: ';' expected
public double getPayment(){
^
ddddd.java:73: reached end of file while parsing
}}
^
13 errors