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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 34

Thread: Setters and getters

  1. #1
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Setters and getters

    I'm trying to try and set the cost of the pizza, in one class i have :

       public double MargheritaCost;
    public void setMargheritaCost(double MargheritaCost) {
    		this.MargheritaCost = MargheritaCost;
    		}
     
    	public double getMargheritaCost() {
    		return MargheritaCost;
    		}
    then in another class i have
     void Margherita ()
    	{
    		Pizza p=new Pizza();
    		p.setMargheritaCost(5.49);
    	}
    this does not set the cost and i get an error message informing me to create a method

    anyone got any ideas where i'm going wrong? thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Setters and getters

    get an error message
    Please copy the full text of the compiler's error message and paste it here.

    Is the correct version of the Pizza class accessible to the compiler so it sees the method definition?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Setters and getters

    Quote Originally Posted by maths94 View Post
    then in another class i have
     void Margherita ()
    	{
    		Pizza p=new Pizza();
    		p.setMargheritaCost(5.49);
    	}
    The 'p' is a local variable. If method Margherita contains only these 2 instructions .... it has not much sense.

    And finally one question: why a class like Pizza (which seems to have a generic meaning) should have a setMargheritaCost method?
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  4. #4
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Setters and getters

    Quote Originally Posted by Norm View Post
    Please copy the full text of the compiler's error message and paste it here.

    Is the correct version of the Pizza class accessible to the compiler so it sees the method definition?
    The method setMargheritaCost(double) is undefined for the type Pizza

    I'm using extends


    public class PizzaMenu extends Pizza {

    void Margherita ()
    {
    Pizza p=new Pizza();
    p.setMargheritaCost(5.49);
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Setters and getters

    The method setMargheritaCost(double) is undefined for the type Pizza
    Is the correct version of the Pizza class accessible to the compiler so it sees the method definition?

    Why create an instance of the Pizza class, set a value in it and then immediately discard that instance? As andbin said, that makes no sense.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Setters and getters

    Quote Originally Posted by Norm View Post
    Is the correct version of the Pizza class accessible to the compiler so it sees the method definition?
    I may sound like an idiot, but how do you check =/

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Setters and getters

    Use a file explorer type program, find and delete all old versions of the class file and then compile the new version.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Setters and getters

    Quote Originally Posted by Norm View Post
    Use a file explorer type program, find and delete all old versions of the class file and then compile the new version.
    I've deleted the others, but it's definitely the correct one

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Setters and getters

    Then why isn't the compiler finding the definition for that method in the Pizza class?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Setters and getters

    Quote Originally Posted by Norm View Post
    Then why isn't the compiler finding the definition for that method in the Pizza class?
    Are you sure i don't it correctly ? sorry i'm just becoming really aggravated, i haven't progressed in 3 days and it needs to be done for friday. Becoming worried ;p

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Setters and getters

    Is the setMargheritaCost() method defined in the Pizza class that the compiler sees when it tries to compile the code with the call to setMargheritaCost()?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Setters and getters

    Quote Originally Posted by Norm View Post
    Is the setMargheritaCost() method defined in the Pizza class that the compiler sees when it tries to compile the code with the call to setMargheritaCost()?
    I kinda just copied the way i done with my other getters and setters. So in my Pizza class the only thing i have related to this is
    static public double MargheritaCost;

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Setters and getters

    static public double MargheritaCost;
    That defines a class variable and should not be related to calling a method in the class.

    Having it static means all instances of the class that it is in will share the same value instead of each instance of the class having its own value.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Setters and getters

    Quote Originally Posted by Norm View Post
    That defines a class variable and should not be related to calling a method in the class.

    Having it static means all instances of the class that it is in will share the same value instead of each instance of the class having its own value.
    The method is defined in the PizzaBase class, so how can i define it in both, i'm required to use extend on pizza because it contains all the toppings that i'll need

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Setters and getters

    The method is defined in the PizzaBase class
    Does the Pizza class extend that class so that the PizzaBase methods are available in instances of the Pizza class?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Setters and getters

    Quote Originally Posted by Norm View Post
    Does the Pizza class extend that class so that the PizzaBase methods are available in instances of the Pizza class.
    It won't allow me to do that, receive an error message "Implicit super constructor PizzaBase() is undefined for default constructor. Must define an explicit constructor"

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Setters and getters

    This way of trying to fix your problems is not going to work. You need to make a small, complete set of classes that show the problem and paste them here along with the error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Setters and getters

    Quote Originally Posted by Norm View Post
    This way of trying to fix your problems is not going to work. You need to make a small, complete set of classes that show the problem and paste them here along with the error messages.
    Is it ok that when you've looked at it, and if the problem is resolved. Can i edit my post and remove the code? don't want others stealing you see..

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Setters and getters

    Make a small special set of classes for testing, don't use the classes from your assignment.

    There are two or three classes and a few methods each. Nothing more is needed.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Setters and getters

    Quote Originally Posted by Norm View Post
    Make a small special set of classes for testing, don't use the classes from your assignment.

    There are two or three classes and a few methods each. Nothing more is needed.
    How can i do my testing class =/ cheers

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Setters and getters

    How can i do my testing class
    Same way you create any class. With an editor or IDE.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Setters and getters

    Quote Originally Posted by Norm View Post
    Same way you create any class. With an editor or IDE.
    I know how to create a class =/ i mean i don't know how to test each class, just call the class?

  23. #23
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Setters and getters

    public class TestClass
    {
     
     
    public static void main(String[] args)
    {
     
    PizzaMenu pm = new PizzaMenu();
     
    // call methods and stuff with pm object
     
    }
    }

  24. #24
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Setters and getters

    Quote Originally Posted by GoodbyeWorld View Post
    public class TestClass
    {
     
     
    public static void main(String[] args)
    {
     
    PizzaMenu pm = new PizzaMenu();
     
    // call methods and stuff with pm object
     
    }
    }
    I've already got this down at the moment, but different object name. Can you show an example of using pm with method, please?

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Setters and getters

    example of using pm with method
      pm.aMethodInPizzaMenuClass();  // call a method in PizzaMenu class
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. help with setters and getters
    By takira in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 28th, 2013, 12:34 PM
  2. help with setters and getters
    By takira in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2013, 04:10 PM
  3. Can anyone explain the Getters and setters in Java
    By sivasubramaniam in forum Java Theory & Questions
    Replies: 4
    Last Post: July 30th, 2013, 06:47 AM
  4. Encapsulation (getters and setters) Tips?
    By Robertgif in forum Java Theory & Questions
    Replies: 4
    Last Post: March 2nd, 2013, 06:36 AM
  5. Problems with input/output and getters/setters
    By robbiep551 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: January 5th, 2012, 11:44 PM