Page 1 of 2 12 LastLast
Results 1 to 20 of 30
  1. #1
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default 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:

    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 + ". ");
        }
    }
    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?

    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

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default

    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!

  3. #3
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    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() {
          ...
       }
       ...
    }

  4. #4
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    What lines are you seeing these errors?

    Hint- if your method has a return type, you have to return something of that type.
    That is exactly what I thought, but the class Payment71 is was supplied with the problem, so I assumed it was correct. I will put give it a return type and see what happpens.
    When in doubt, use a lightsaber

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Java Code:
    public class Payment71
    {
        public Payment()
    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.

  6. #6
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    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.

    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();
        }
    
    }
    In this code, there are four errors as follows:
    cannot find symbol class CashPayment71 (error appears 2x)
    cannot find symbol class CreditCardPayment71 (error appears 2x)

    The next file:
    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");
        }
    }
    In this file, the compiler "cannot find symbol, public class CashPayment71 extends Payment71"

    And the error in the last file is: "else without if", only, ther eis an if after the else.
    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 + ". ");
        }
    }
    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.

    Thanks guys,
    Mace
    When in doubt, use a lightsaber

  7. #7
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    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!

  8. #8
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    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

  9. #9
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    Are all your classes in the same package? If not, you'll need to import them. For instance:

    import myPackage.Payment71;

  10. #10
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    The errors with the file Payment71 are still the same.
    If you didn't compile Payment71 then compiler can not compile CashPayment71.... You must compile Payment71 in order to continue.
    First step, return to Payment71 and correct errors.

  11. #11
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Exclude everything from your main() in Payment71 and try to compile.
    If sucsessful, try to compile other classes.

  12. #12
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    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();
        }
    
    }
    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 + ". ");
        }
    }
    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???
    When in doubt, use a lightsaber

  13. #13
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    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

  14. #14
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    Ah, That defintely is a problem. I will fix that and see what happens. thanks
    When in doubt, use a lightsaber

  15. #15
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    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

  16. #16
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Just right click Payment7_1.java in File View (on the left) and rename it to Payment71.java

  17. #17
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Quote Originally Posted by MaceMan View Post
    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".
    The error message says everything you need to know. I have deleted parts of it that might have confused you, now it says:

    Payment7_1.java: class Payment71 should be declared in a file named Payment71.java

  18. #18
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    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:
    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.
    This is the General Output. the Build Output has an error that says:
    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.
    Anyone know why?
    BTW, thanks for all the pointers, I am so close to finishing!
    woohoo!
    When in doubt, use a lightsaber

  19. #19
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Maybe you're referencing Payment7_1.java somewhere in your build preferences?

  20. #20
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    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).

Page 1 of 2 12 LastLast

Similar Threads

  1. Inheritance Problem
    By kazumahits in forum New To Java
    Replies: 5
    Last Post: 01-11-2011, 03:46 PM
  2. Wierd problem with abstract and extends
    By g123456 in forum New To Java
    Replies: 6
    Last Post: 05-14-2010, 06:25 PM
  3. Inheritance Problem
    By g2beastie in forum New To Java
    Replies: 4
    Last Post: 03-25-2010, 08:23 PM
  4. inheritance problem
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 03-10-2010, 06:01 PM
  5. Problem with Inheritance
    By KronikAlkoholik in forum New To Java
    Replies: 4
    Last Post: 08-25-2009, 12:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •