Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: BlueJ inheritance Issue. Please Help.

  1. #1
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default BlueJ inheritance Issue. Please Help.

    Hello,

    I'm working on my coursework and the task is to create inheritance using "super"
    Screenshot_2.png
    This is the structure.

    public class Gadget
        {
            public String model;
            public double price;
            public int weight;
            public String size;
     
        public Gadget(String themodel, double theprice, int theweight, String thesize)
        {
            price = theprice;
            model = themodel;
            weight = theweight;
            size = thesize;
        }
     
        //Return The price of a gadget.
        public double getPrice()
        {
            return this.price;
        }
     
        //Return The model of a gadget.
        public String getModel()
        {
            return this.model;
        }
     
        //Return The weight of a gadget.
        public int getWeight()
        {
            return this.weight;
        }
     
        //Return The size of a gadget.
        public String getSize()
        {
            return this.size;
        }
    }
    This is my Gadget class.
    For my Phone class i have this task:
    The Mobile class is a subclass of the Gadget class and has just one attribute, which corresponds to the (whole) number of minutes of calling credit remaining. The attribute is initialized in the constructor by being assigned the value of one of the constructor's five parameters and it has a corresponding accessor method. The other parameters of the constructor represent the model the price, the weight and the size of the mobile phone and these four values are passed to the constructor of the Gadget class.

    public class Phone extends Gadget
    {
        private int credits;
        private int balance;
     
        public void Gadget(String themodel, double theprice, int theweight, String thesize)
        {
            super();
            credits = theCredits;
        }   
    }
    This is my Phone class and the same error i keep getting. I've checked my code 1000 times and it look 100% correct. I just can not understand why the "super" function doesn't want to work for me. I reinstalled BlueJ but it's still the same. Please i need your help because i can't even begin the task with the most basic function.
    It's telling me that the super call must be the first statement but it already is! Please help
    Attached Images Attached Images
    Last edited by JoeBG; January 9th, 2019 at 11:56 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    the same error i keep getting.
    Please copy the full text of the error messages and paste it here. Not as images.

    Check that the code is defining a constructor vs a method. Constructors do not have a return type.
    Also constructors have the same name as the class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    The error code is "call to super must be first statement in constructor". But i believe that it's already the first statement..
    There is also another message:
    constructor Gadget in class Gadget cannot be applied to given types;
    required: java.lang.String,double,int,java.lang.String
    found: no arguments
    reason: actual and formal argument lists differ in length


    This is a big hurdle for me and i can't do anything else until i get this fixed..
    Last edited by JoeBG; January 9th, 2019 at 08:47 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    call to super must be first statement in constructor
    See my previous post about constructors vs methods.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    I'm sorry but i can't understand. Can you please give me an example? Am i supposed to add something before the super call?
    I probably look stupid right now but i really need to understand what my issue is.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    Some examples from the Gadget code:
    public class Gadget
        {
            public String model;
            public double price;
            public int weight;
            public String size;
     
        public Gadget(String themodel, double theprice, int theweight, String thesize)  //<<<<< the Constructor
     
     ....
     
        //Return The price of a gadget.
        public double getPrice()                                               //<<< defines a method
        {
            return this.price;
        }

    Also see the tutorial: https://docs.oracle.com/javase/tutor...structors.html

    https://docs.oracle.com/javase/tutor...O/methods.html
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    Well i think that i already have a constructor and a method but it still doesn't want to Compile..
    public class Phone extends Gadget
    {
       private int credits;
       private int balance;
     
       public void Gadget(String themodel, double theprice, int theweight, String thesize)        //<<<<< Constructor
       {
           super();
           credits = theCredits;
       }
     
       public String getCredits()                     //<<<<<< Method
       {
           return theCredits;
       }
    }

    Am i wrong?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    From post#2:
    Constructors do not have a return type. void is a return type
    Also constructors have the same name as the class.

    public void Gadget(String themodel, double theprice, int theweight, String thesize)  // defines a method named Gadget

    The constructor for the Phone class needs to be named Phone not Gadget.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    Ok so i removed the return and changed the constructor to Phone instead of Gadget but it still doesn't compile
    public class Phone extends Gadget
    {
       private int credits;
       private int balance;
     
       public Phone(String themodel, double theprice, int theweight, String thesize)
       {
           super();
           credits = theCredits;
       }
     
       //Return the current balance of credits.
       public String getCredits()
       {
           return theCredits;
       }
    }

    The solution is probably very simple but i just can't get my mind on it..

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    still doesn't compile
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    There are several problems in your code but you need to learn to figure them out. The first hint is it doesn't compile which means it must have a compilation error message. What does it say? Paste the whole think in a post. Then we can help you interpret it.

    Regards,
    Jim

  12. #12
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    The error message i'm getting now is:
    constructor Gadget in class Gadget cannot be applied to given types;
    required: java.lang.String,double,int,java.lang.String
    found: no arguments
    reason: actual and formal argument lists differ in length


    My Gadget class compiles without any problem. So i think i'm doing something wrong with the inheritance..

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    The error messages should have the source line and line number for where the error happens.
    These two line describe the problem:
    required: java.lang.String,double,int,java.lang.String
    found: no arguments
    The constructor takes the 4 arguments listed in the message but the code calling the constructor did not have any arguments.
    Change the calling code so that it provides the required arguments: java.lang.String,double,int,java.lang.String
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    JoeBG (January 9th, 2019)

  15. #14
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    Oh ok, so i've added the arguments to the super call and the error disappeared. Now i have another error for the 2nd part "credits = theCredits;" saying:
    cannot find symbol - variable theCredits

    public class Phone extends Gadget
    {
       private int credits;
       private int balance;
     
       public Phone(String themodel, double theprice, int theweight, String thesize)
       {
           super(themodel, theprice, theweight, thesize);
           credits = theCredits;
       }
     
       //Return the current balance of credits.
       public String getCredits()
       {
           return theCredits;
       }
    }

    should i remove the "credits = theCredits;" statement outside the constructor?
    According to the examples i've seen it looks correct as it is..
    Last edited by JoeBG; January 9th, 2019 at 11:00 AM.

  16. #15
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    Ok. I am confused about something. You should have also gotten the error.

    The constructor gadget() is undefined.

    When you invoked super() in the subclass, Phone, the default gadget() constructor was not defined (unless you put it there later and didn't tell us about it). It is normally there by default UNLESS you add an explicit constructor. Then the default constructor goes away.

    Regards,
    Jim

  17. #16
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    Quote Originally Posted by jim829 View Post
    Ok. I am confused about something. You should have also gotten the error.

    The constructor gadget() is undefined.

    When you invoked super() in the subclass, Phone, the default gadget() constructor was not defined (unless you put it there later and didn't tell us about it). It is normally there by default UNLESS you add an explicit constructor. Then the default constructor goes away.

    Regards,
    Jim
    I have't added anything extra. This is my entire code for the Phone class
    public class Phone extends Gadget
    {
       private int credits;
     
       public Phone(String themodel, double theprice, int theweight, String thesize)
       {
           super(themodel, theprice, theweight, thesize);
           credits = theCredits;
       }
     
       //Return the current balance of credits.
       public String getCredits()
       {
           return theCredits;
       }
    }

    But since Phone extends Gadget i don't think i should make another constructor for Gadget..

  18. #17
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    You no longer need a default constructor in Gadget since you don't call it. But earlier you should have received an error message when you invoked super() in the Phone class.

    Here is an example:

    class Foo {
       int v;
     
       public Foo(int v) {
    	  this.v = v;
       }
    }
     
    class Bar extends Foo {
       public Bar(int v) {
    	  super(); <<-- error because the constructor Foo() no longer exists.
       }
    }

    Anyway, sorry for the distraction.

    Regards,
    Jim

  19. The Following User Says Thank You to jim829 For This Useful Post:

    JoeBG (January 9th, 2019)

  20. #18
    Junior Member
    Join Date
    Jan 2019
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: BlueJ inheritance Issue. Please Help.

    Ok so i manage to compile everything.
    My current code is:
    public class Phone extends Gadget
    {
       private int credits;
     
       public Phone(String themodel, double theprice, int theweight, String thesize, int theCredits)
       {
           super(themodel, theprice, theweight, thesize);
           credits = theCredits;
       }
     
       //Return the current balance of credits.
       public int getCredits()
       {
           return credits;
       }
    }

    Thank you very much for your help. I'll try to do the rest from here.

Similar Threads

  1. BlueJ program !
    By fevdjyg in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 16th, 2013, 04:35 PM
  2. BLUEJ Program Help
    By threlot in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 5th, 2013, 09:42 PM
  3. Bluej help
    By kiara4 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 13th, 2012, 05:29 PM
  4. Bluej issue
    By BeardedAxeWound in forum Object Oriented Programming
    Replies: 16
    Last Post: August 26th, 2012, 03:07 AM
  5. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM