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

Thread: declaring in my mutator

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    23
    My Mood
    Daring
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question declaring in my mutator




    So this is a hw assignment and I'm having a little trouble with it. I am a beginner (first programming class EVER) but I'm a quick learner so bare with me lol :-)

    The assignment is to make 2 classes (done that part) one called gorilla and the other called cat. In gorilla are as follows:
    -instance variables weight and name
    -accessors and mutators for both instance variables

    The problem I'm having because my prof wants us to make sure the mutator for weight CAN'T be set to a negative number


    So here's my mutator for weight for gorilla:

     
     public void setGweight(int x) {
            x != -0; //clearly Java DOES NOT agree with this statement
        }

    Any ideas on how to fix this?

    Thanks in advance! From one beginner to the pros and all...I can't wait to get better at this!


  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: declaring in my mutator

    make sure the mutator for weight CAN'T be set to a negative number
    What comparison would detect the negative number? Use an if statement.
    What is the code supposed to do if it detects a negative number?

    Java DOES NOT agree with this statement
    What if the full text of the error message?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    23
    My Mood
    Daring
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: declaring in my mutator

    You know what...I totally forgot abt using an if statement! lol But she didn't say what the code is supposed to do if it detects a negative number, but to make sure that it CAN'T be a negative number (if that makes sense) after that part we are to use constructors to set the name and weight to default values... the full text of the error message says that
      public void setGweight(int x) {
            x != -0; //not a valid statement
        }

  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: declaring in my mutator

    I guess you could ignore making the change if the value is negative. Only change it if positive.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    23
    My Mood
    Daring
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: declaring in my mutator

    But when I type
      public void setGweight(int x) {
           if  (x != -0) {
    return false; //error reads saying: cannot return a value from method whose result type is void--Unnecessary return statement
       }
     }

    But I don't know how else to say that.

  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: declaring in my mutator

    The method's return type is void which means that it does not return anything. So just use return without a value.

    The if statement should use test if the value passed to the method is negative which means less than 0.
    I don't think there is a -0 value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with beginner mutator method
    By Dysun in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 06:32 PM
  2. Need Help with Accessor and Mutator Methods
    By ankur_032 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 9th, 2011, 10:00 AM
  3. Accesor and Mutator help
    By clevel211 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 5th, 2010, 05:06 PM
  4. <..> when declaring a class
    By Asido in forum Java Theory & Questions
    Replies: 1
    Last Post: September 8th, 2010, 08:47 AM
  5. What is the matching mutator method?
    By ssmith in forum Object Oriented Programming
    Replies: 1
    Last Post: November 3rd, 2009, 10:03 PM

Tags for this Thread