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 10 of 10

Thread: Exercise 95 (I KNOW, I'm at an earlier exercise)

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Wink Exercise 95 (I KNOW, I'm at an earlier exercise)

    So basically, I decided to go back in time over my winter break to catch up on some exercises and knowledge where I've fallen behind. I went back to some earlier text book stuff to try to catch up over winter break. So now, I'm back at exercise 95 and need help, which I would totally appreciate. Here are the intructions:

    I need help with b:

     
    You may have noticed that the Person class's talk instance method causes a small decrease to occur in the value of the instance variable myWeight each time it is called.
     
    If eric is an instance of the Person class with an initial weight of 67.2kg, what will his weight be after five calls to talk?
     
    Modify the following code by adding an accessor method for the instance variable myWeight. Then run the program and verify your answer to part (a).
     
      public class Person 
      { 
        private double myHeight; 
        private double myWeight; 
        private String myHair; 
     
        public Person( double height, double weight, String hair )  
        { 
          myHeight = height; 
          myWeight = weight; 
          myHair = hair; 
        } 
     
        public void sleep( int hours ) 
        { 
          for ( int i = 0 ; i < hours ; i++ ) 
            System.out.println( "Sleeping ... " ); 
        } 
     
        public void talk() 
        { 
          myWeight -= 0.01; 
          System.out.println( "Talking ... " ); 
        }

    The above is code I'm not allowed to edit. Here is what I wrote:

     
    public void getWeight(){
        return myWeight;
    } 
     
    public static MyWeightIs (double myWeight)
    {
        Person John = new Person (2.5, 40.2, "multicolored");
     
        return John.getWeight() );
    }&#65279;

    Here is the code that comes after that I can't edit:

     
        public String getHair() 
        {   
          return myHair; 
        } 
      } 
     
      public static void main( String[] args ) 
      { 
        Person eric = new Person( 1.57, 67.2, "brown" ); 
        for ( int i = 0 ; i < 5 ; i++ ) 
          eric.talk(); 
     
        System.out.println( "Eric now weighs " + eric.getWeight() + "kg" ); 
      }

    So whatever I can't edit is written in stone essentially. I'll try any suggestion. Thanks so much.

    NOTE: ignore the earlier "& #65279" because that just appeared in the preview window, in case it shows up. I didn't write that part.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,400
    My Mood
    Mellow
    Thanks
    143
    Thanked 600 Times in 587 Posts

    Default Re: Exercise 95 (I KNOW, I'm at an earlier exercise)

    Hints: The accessor the exercise is looking for should be called getMyWeight() (class names begin with capital letters, variable and method names do not), should return a value (that's why it's called an accessor) and should not take a parameter. It also shouldn't create a new instance of Person (that's an odd thing to do). You can use the getHair() method as a model.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,400
    My Mood
    Mellow
    Thanks
    143
    Thanked 600 Times in 587 Posts

    Default Re: Exercise 95 (I KNOW, I'm at an earlier exercise)

    Any progress?

  4. #4
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 95 (I KNOW, I'm at an earlier exercise)

    Here is what I've got from what your telling me:

    public String getWeight(){
        return myWeight;
    } 
     
    public static myWeightIs (){
     
        return getWeight() );
    }

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,400
    My Mood
    Mellow
    Thanks
    143
    Thanked 600 Times in 587 Posts

    Default Re: Exercise 95 (I KNOW, I'm at an earlier exercise)

    Let's see what I said and compare that to what you got.

    What I said:
    1. The accessor the exercise is looking for should be called getMyWeight()

    What you got:
    public String getWeight()
    public static myWeightIs()

    Can you see that what I said and what you got don't match? Also, the second method signature is not valid.

    What I said:
    2. should return a value

    What you got:
    return myWeight;
    return getWeight() );

    Good job minus. Both attempt to return a value. I'm not sure why there are two methods, one returning the other, and can you see that the second statement is syntactically incorrect?

    What I said:
    3. should not take a parameter

    What you got:
    public String getWeight()
    public static myWeightIs()

    Winner! (minus). Still not sure why there are two methods.

    What I said:
    4. shouldn't create a new instance of Person

    What you got:
    return myWeight;
    return getWeight() );

    Winner! (minus). Still not sure why there are two methods.

    Overall, you made positive progress with some cleanup to do.

  6. #6
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 95 (I KNOW, I'm at an earlier exercise)

    Thanks. How about this:

    public static getMyWeight () {
        return John.myWeight() );
    }

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,400
    My Mood
    Mellow
    Thanks
    143
    Thanked 600 Times in 587 Posts

    Default Re: Exercise 95 (I KNOW, I'm at an earlier exercise)

    I know this OOP stuff is confusing, but look at the bigger picture. There are several instances of the class Person, let's say each instance is the name of the person, sally, john, joe, jill, etc. Each instance of the Person class is a separate object with its own instance variables, including the one called myWeight. When the myWeight variable of a specific Person instance is needed, it will be recovered from memory using the notation:

    weight = sally.getMyWeight();
    weight = john.getMyWeight();
    weight = joe.getMyWeight();
    weight = etc.getMyWeight();

    (separate calls for example, they wouldn't all be used at the same time and assigned to weight)

    That will call the method getMyWeight() for that object, and the method will return myWeight for that instance variable and assign it to the variable called weight. Therefore, the name of the instance IS NOT included in the return statement. It's already known to be the "current instance of the Person object" which can also be referred to as 'this' instance, as in this.myWeight. In the case of that simple method, even 'this' is not necessary, because there's no confusion between variables named myWeight.

    I hope this helps. I know it's a lot to absorb, and you're not quite there yet, but you're hopefully getting wetter.

  8. #8
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 95 (I KNOW, I'm at an earlier exercise)

    I tried a few different things. Here's what I have now. I have this:

     
    public static getWeight () {
        return Person.myWeight();
    }

    I'm starting to think I shouldn't return "myWeight" as a method, but I'm not sure. I get the error, "invalid declaration type; return type required" no matter how I do it. I think what your saying (which I think matches up), is that it has to do with this line:

     
    public static getWeight () {

    Since I'm getting the error, I think it matters how I'm declaring the method or it wouldn't show up every time. Am I wrong? Should I focus on just that line?

    I tried changing it, and it still didn't work, like this:

     
    public static getWeight () {
        return myWeight;
    }

    So, its almost like it doesn't matter what I put in the body. Lol.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,400
    My Mood
    Mellow
    Thanks
    143
    Thanked 600 Times in 587 Posts

    Default Re: Exercise 95 (I KNOW, I'm at an earlier exercise)

    You'll have to define "didn't work." Show code, show errors, something.

    I've told you explicitly (more than once) what the 'getter' should be called, what should and should not be in its body, yet you've tried almost every possible combination of code EXCEPT the right one, and you've tried the same wrong code more than once. We're almost into the definition of insanity, yet I'm beginning to think it applies to me rather than you. I keep telling you the nearly the same thing, expecting the outcome to be different. It appears the outcome will never be the right one, even though it should have happened accidentally by now.

    Good luck.

  10. #10
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 95 (I KNOW, I'm at an earlier exercise)

    I made it work. Lol, I feel like such a moron:

     
    public double getWeight(){
        return myWeight;
    }&#65279;


    --- Update ---

    Lol, if you had said I had to make it a double, I would have figured it out. Haha. It's not your fault. It's the way I think...

Similar Threads

  1. Exercise 86
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 7th, 2013, 10:31 PM
  2. Exercise
    By keepStriving in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 21st, 2013, 06:58 PM
  3. Help with exercise.
    By javapol in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2013, 08:40 PM
  4. Exercise
    By phite2009 in forum Member Introductions
    Replies: 3
    Last Post: September 30th, 2011, 08:51 AM
  5. Is this what my exercise want?
    By Arkeshen in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 04:51 PM

Tags for this Thread