Results 1 to 20 of 30
- 04-07-2011, 03:57 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
problem with two extends of Inheritance
Hi guys,
I am currently doing a problem that is as follows ( I have not yet put in the Main method):
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 that describes 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.
My code so far is as follows:
I get three error messages right now, which say "Payment7_1.java:34: invalid method declaration; return type required" at lines 34, 40, and 72. Does anyone know why?Java Code:import java.util.Scanner; public class Payment71 { //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 class CashPayment extends Payment71 { Scanner keyboard = new Scanner(System.in); char answer = null; char answer1 = 'Y'; char answer2 = 'N'; public Payment() { System.out.println("If payment is in cash, please enter Y."); System.out.ptinln("If payment is not in cash, please enter N."); answer = keyboard.nextChar(); if (answer.equalsIgnoreCase(answer1)) { System.out.println("Please enter amount of cash payment."); amount = keyboard.nextDouble(); } else if (answer.equalsIgnoreCase(answer2)) { System.out.println("Payment is not in cash."); } } public void paymentDetails() { System.out.println("The payment amount is " + amount + " and is cash"); } } public class CreditCardPayment extends Payment71 { Scanner keyboard = new Scanner(system.in); String CCname; double CCexperation; int CCnumber; public String getCCname() { System.out.println("Enter credit card name."); CCname = keyboard.nextLine(); } public double getCCexperation() { System.out.println("Enter credit card experation date."); CCexperation = keyboard.nextLine(); } public int getCCnubmer() { System.out.println("Enter credit card number."); CCnumber = keyboard.nextInt(); } public void paymentDetails() { System.out.println("The payment amount is " + amount + " and is cash"); } public double getAmount(double amount) { System.out.println("Please enter amount payment."); amount = keyboard.nextDouble(); } public void paymentDetails() { System.out.println("The payment amount is " + amount + " and is credit."); System.out.println("The credit card holders name is " + CCname + ". "); System.out.println("The date of experation on the card is " + CCexperation + ". "); System.out.println("The credit card number is " + CCnumber + ". "); } }
And secondly, how would the problem know if I payed in cash or credit? Do I need to implement some IF loops, or will the program go through all classes?
Thanks for any help, and you can be sure once fix these two questions there will be more. :)When in doubt, use a lightsaber
- 04-07-2011, 04:04 PM #2
What lines are you seeing these errors?
Hint- if your method has a return type, you have to return something of that type.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-07-2011, 04:05 PM #3
Member
- Join Date
- Jan 2011
- Location
- Gainesville, FL
- Posts
- 45
- Rep Power
- 0
The method name for the constructors of each class need to be the same as the class name.
Java Code:public class Payment71 { public Payment71() { ... } ... }
- 04-07-2011, 04:07 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
- 04-08-2011, 12:17 AM #5
As pointed out above your constructor does not have the correct name. Since it is not the same as the class name, the compiler assumes it is a method and wants you to add the return type.Java Code:public class Payment71 { public Payment()
- 04-08-2011, 03:59 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Okay, I looked at the driver I am supposed to use, and had to rewrite all my classes because of it. (stuipid me)
So here are my new classes. they are in all differnet files, which is how they are supposed to be.
In this code, there are four errors as follows:Java Code:public class Payment71 { //Payment amount protected double amount; //Constructor to initialize amount to 0 public Payment71() { amount = 0.0; } //Constructor to initialize payment amount public Payment71(double paymentAmount) { amount = paymentAmount; } //Sets the payment amount public void setPayment71(double paymentAmount) { amount = paymentAmount; } //Returns the payment amount public double getPayment71() { return amount; } //Prints a description of the payment public void payment71Details() { System.out.println("The payment amount is " + amount); } public static void main(String[] args) { // Create several test classes and invoke the paymentDetails method Payment71 cash1 = new CashPayment71(50.5); Payment71 cash2 = new CashPayment71(20.45); Payment71 credit1 = new CreditCardPayment71(10.5, "Fred", "10/5/2010", "123456789"); Payment71 credit2 = new CreditCardPayment71(100, "Barney", "11/15/2009", "987654321"); System.out.println("Cash 1 details:"); cash1.payment71Details(); System.out.println(); System.out.println("Cash 2 details:"); cash2.payment71Details(); System.out.println(); System.out.println("Credit 1 details:"); credit1.payment71Details(); System.out.println(); System.out.println("Credit 2 details:"); credit2.payment71Details(); System.out.println(); } }
cannot find symbol class CashPayment71 (error appears 2x)
cannot find symbol class CreditCardPayment71 (error appears 2x)
The next file:
In this file, the compiler "cannot find symbol, public class CashPayment71 extends Payment71"Java Code:public class CashPayment71 extends Payment71 { double amount; public CashPayment71(Double paymentAmount) { super(paymentAmount); if (paymentAmount < 0); { System.out.println("Fatal error: creating an illegal payment."); } } public void payment71Details() { System.out.println("The payment amount is " + amount + " and is cash"); } }
And the error in the last file is: "else without if", only, ther eis an if after the else.
Any help would be great, but I would like to learn how to correctly do code like this, so if you could just point the problems out and why they are there, I can try and fix it.Java Code:public class CreditCardPayment71 extends Payment71 { String CCname = null; int CCexperation = 0; int CCnumber = 0; public CreditCardPayment71(double paymentAmount, String theCCname, int theCCexperation, int theCCnumber) { super(paymentAmount); if((theCCexperation > 0) && (theCCnumber > 0) && (theCCname != null)); { CCexperation = theCCexperation; CCnumber = theCCnumber; CCname = theCCname; } else if((theCCexperation < 0) && (theCCnumber < ) && (theCCname = null)); { System.out.println("Fatal error: creating an illegal credit card payment."); System.exit(0); } } public String getName() { return CCname; } public int getExperation() { return CCexperation; } public int getNumber() { return CCnumber; } public void payment71Details() { System.out.println("The payment amount is " + amount + " and is credit."); System.out.println("The credit card holders name is " + CCname + ". "); System.out.println("The date of experation on the card is " + CCexperation + ". "); System.out.println("The credit card number is " + CCnumber + ". "); } }
Thanks guys,
MaceWhen in doubt, use a lightsaber
- 04-08-2011, 05:14 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 34
- Rep Power
- 0
Make sure the name of your java files match the name of your class. For instnace, Payment71.java, CashPayment71.java, and CreditCardPayment71.java. Also, you may need to add imports for your other classes.
In your if else problem, you have a semi colon at the end of your if and before your open {. Might as well get rid of the semi colon after your first if too.
I'd recommend you download and use Eclipse for java development. It makes life a whole lot easier!
- 04-08-2011, 07:52 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Okay, I took out those two semicolons and it fixed thosde problems. Now the files CreditCardPayment71 and CashPayment71 have the same error, "cannot find symbol, public class CashPayment71 extends Payment71" and "cannot find symbol, public class creditCardPayment71 extends payment71".
The errors with the file Payment71 are still the same.When in doubt, use a lightsaber
- 04-08-2011, 08:23 PM #9
Member
- Join Date
- Apr 2011
- Posts
- 34
- Rep Power
- 0
Are all your classes in the same package? If not, you'll need to import them. For instance:
import myPackage.Payment71;
- 04-08-2011, 08:33 PM #10
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
If you didn't compile Payment71 then compiler can not compile CashPayment71.... You must compile Payment71 in order to continue.The errors with the file Payment71 are still the same.
First step, return to Payment71 and correct errors.
- 04-08-2011, 08:38 PM #11
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Exclude everything from your main() in Payment71 and try to compile.
If sucsessful, try to compile other classes.
- 04-09-2011, 12:31 AM #12
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Okay, I did some work on it, and now the files CashPayment71 and CreditCardPayment71 do not have any errors! :)
And the file Payment71 has only two errors:
cannot find symbol constructor CreditCardPayment71(int,java.lang.String,java.lang .String,java.lang.String)
(error happens at lines 65 and 67)
I listed the two files again that the error concerns, as I made a few twiks.
Java Code:public class Payment71 { //Payment amount protected double amount; //Constructor to initialize amount to 0 public Payment71() { amount = 0.0; } //Constructor to initialize payment amount public Payment71(double paymentAmount) { amount = paymentAmount; } //Sets the payment amount public void setPayment71(double paymentAmount) { amount = paymentAmount; } //Returns the payment amount public double getPayment71() { return amount; } //Prints a description of the payment public void payment71Details() { System.out.println("The payment amount is " + amount); } public static void main(String[] args) { // Create several test classes and invoke the paymentDetails method Payment71 cash1 = new CashPayment71(50.5); Payment71 cash2 = new CashPayment71(20.45); Payment71 credit1 = new CreditCardPayment71(10.5, "Fred", "10/5/2010", "123456789"); Payment71 credit2 = new CreditCardPayment71(100, "Barney", "11/15/2009", "987654321"); System.out.println("Cash 1 details:"); cash1.payment71Details(); System.out.println(); System.out.println("Cash 2 details:"); cash2.payment71Details(); System.out.println(); System.out.println("Credit 1 details:"); credit1.payment71Details(); System.out.println(); System.out.println("Credit 2 details:"); credit2.payment71Details(); System.out.println(); } }Right now, all I want is for the code to compile. After that, I can work on the logic. Anyone know why it still has a problem locating CreditCard{Payment71???Java Code:public class CreditCardPayment71 extends Payment71 { String CCname = null; int CCexperation = 0; int CCnumber = 0; public CreditCardPayment71(double paymentAmount, String theCCname, int theCCexperation, int theCCnumber) { super(paymentAmount); if((theCCexperation > 0) && (theCCnumber > 0) && (theCCname != null)) { CCexperation = theCCexperation; CCnumber = theCCnumber; CCname = theCCname; } else if((theCCexperation < 0) || (theCCnumber < 0) || (theCCname == null)) { System.out.println("Fatal error: creating illegal credit card payment information."); System.exit(0); } } public String getName() { return CCname; } public int getExperation() { return CCexperation; } public int getNumber() { return CCnumber; } public void payment71Details() { System.out.println("The payment amount is " + amount + " and is credit."); System.out.println("The credit card holders name is " + CCname + ". "); System.out.println("The date of experation on the card is " + CCexperation + ". "); System.out.println("The credit card number is " + CCnumber + ". "); } }When in doubt, use a lightsaber
- 04-09-2011, 08:20 AM #13
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
You have declared your CreditCardPayment71 to take double, String and two ints as parameters:
Java Code:public CreditCardPayment71(double paymentAmount, String theCCname, int theCCexperation, int theCCnumber)
But when you are testing it you put: double and three Strings as parameters:
Java Code:new CreditCardPayment71(10.5, "Fred", "10/5/2010", "123456789"); new CreditCardPayment71(100, "Barney", "11/15/2009", "987654321");
Thats why compiler is complaining:
Java Code:cannot find symbol constructor CreditCardPayment71(int,java.lang.String,java.lang .String,java.lang.String)
You must provide your CardCreditPayement71 constructor with correct parameters in Pyment71's main metod or made another one with next signature:
Java Code:CreditCardPayment71(int,java.lang.String,java.lang .String,java.lang.String)
What solution it will be it depends on your needs and design.
br
- 04-09-2011, 03:30 PM #14
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Ah, That defintely is a problem. I will fix that and see what happens. thanks
When in doubt, use a lightsaber
- 04-09-2011, 08:59 PM #15
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Thanks for the help Milo, that fixed those two problems. Now I have a single problem with Payment71, which says:
"Payment7_1.java:26: class Payment71 is public, should be declared in a file named Payment71.java
public class Payment71".
^
1 error
Process completed.
I am using JCreator, and created this class using, "New project". Is this wrong, should I have created it using, "New File"?
Both other classes I created using "New File".When in doubt, use a lightsaber
- 04-09-2011, 09:03 PM #16
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Just right click Payment7_1.java in File View (on the left) and rename it to Payment71.java
- 04-09-2011, 09:10 PM #17
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
- 04-10-2011, 08:57 PM #18
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Okay, I accidentally had an extra file called payment7-1, so I deleted it, and now have payment71. Payment71 now works correctly, and the output is as follows:
This is the General Output. the Build Output has an error that says:Java Code:Fatal error: creating an illegal payment. Fatal error: creating an illegal payment. Cash 1 details: The payment amount is 0.0 and is cash Cash 2 details: The payment amount is 0.0 and is cash Credit 1 details: The payment amount is 10.5 and is credit. The credit card holders name is Fred. The date of experation on the card is 10/5/2010. The credit card number is 123456789. Credit 2 details: The payment amount is 100.0 and is credit. The credit card holders name is Barney. The date of experation on the card is 11/15/2009. The credit card number is 987654321. Process completed.
Anyone know why?Java Code:avac: file not found: C:\Users\Mackenna\Documents\JCreator LE\MyProjects\Problem7.1\Payment7.1\src\Payment7_1.java Usage: javac <options> <source files> use -help for a list of possible options Process completed.
BTW, thanks for all the pointers, I am so close to finishing!
woohoo!When in doubt, use a lightsaber
- 04-10-2011, 09:45 PM #19
Maybe you're referencing Payment7_1.java somewhere in your build preferences?
- 04-10-2011, 10:07 PM #20
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Obviously javac is looking for file that you had deleted. That file is probably still in the list of source files somewhere in the JCreator's project-worspace files. When you want to delete file do it from JCreator's File View.
Try to show all files in your workspace (right click on project name in File View and choose Show All Files from popup menu). If you find problematic file listed try to delete it again (again: right click on that file in File View and choose Delete)
If there is no such file listed, I'm afraid you must start from begining (make new project and import or copy-paste all necessary files).
Similar Threads
-
Inheritance Problem
By kazumahits in forum New To JavaReplies: 5Last Post: 01-11-2011, 03:46 PM -
Wierd problem with abstract and extends
By g123456 in forum New To JavaReplies: 6Last Post: 05-14-2010, 06:25 PM -
Inheritance Problem
By g2beastie in forum New To JavaReplies: 4Last Post: 03-25-2010, 08:23 PM -
inheritance problem
By er1c550n20 in forum New To JavaReplies: 2Last Post: 03-10-2010, 06:01 PM -
Problem with Inheritance
By KronikAlkoholik in forum New To JavaReplies: 4Last Post: 08-25-2009, 12:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks