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

Thread: Another question abt mutators and accessors...

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

    Question Another question abt mutators and accessors...

    ok same assignment(discussed in the question abt mutators) new problem, in my class gorilla, we have to give it a pet cat with the accessors and mutators for the pet cat in the gorilla class... this is what I have so far
     
        private int Gweight;
        private String Gname;
        private Cat pet;
        private String cat; // Java made me add this variable for whatever the reason (the previous code used didn't work for some reason)
     
     
        public Cat getPet() {
            return pet;
        }
     
     
        public void setPet(String c) {
             this.cat = c; //this is the problem, I was wondering why it can't be this.pet=c; instead
        } 
    //the rest of the coding is fine my prof checked it earlier today


  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: Another question abt mutators and accessors...

    Please explain what problem you are having.
    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: Another question abt mutators and accessors...

    the setPet method in particular. maybe i'm misunderstanding the way the mutator works but I figured that
    public void setPet (String c) {
    this.pet = c;
    should work....but for some reason Java gives me an error message saying "incompatible types required: Cat found: String"

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Another question abt mutators and accessors...

    No, that shouldn't work, and the compiler is as usual correct. You're trying to assign a String to a Pet variable, and that won't work. You can only assign a Pet object to a Pet variable. Notice how you declare your pet variable:

    Cat pet;

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

    Default Re: Another question abt mutators and accessors...

    Ok that makes more sense but does it make a difference that the variable is like that because it's supposed to call on the cat class that is already written and correct since my prof checked it today. now the problem is calling those methods in my harness that says the gorilla has a pet cat here's what I have so far
     public static void main(String[] args) {
     
     
     
     
            Gorilla G1 = new Gorilla();
            Gorilla G2 = new Gorilla("Godzilla", 85);
     
     
     
     
            System.out.println(G1);
            System.out.println(G2);
            System.out.println("\n");
     
     
            Cat C1= new Cat();
            Cat C2 = new Cat();
     
            C2.setName("Rocket");
            C2.setColor("orange");
     
     
            G1.setPet("Brainy");
            G2.setPet("Twinkle Toes");
             System.out.println(G1.getPet().getName()); //THIS IS THE PROBLEM
     
            System.out.println(C1);
            System.out.println(C2);
            System.out.println("\n");
     
        }
    }
    The compiler will run it, but then Java gives me this error " Exception in thread "main" java.lang.NullPointerException at hwk11.tmarshall5.Hwk11Tmarshall5.main(Hwk11Tmarsha ll5.java:41) Java Result: 1"...and no matter what I do to fix it and still make sure the gorilla gets and pet and what the pet they get (if that makes sense)

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Another question abt mutators and accessors...

    The compiler can't compile that code, not unless you changed the Gorilla pet variable to a String variable, and hopefully you didn't do this. You need to show us how you "fixed" your Gorilla class.

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

    Default Re: Another question abt mutators and accessors...

    No I didn't change any of the variables

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Another question abt mutators and accessors...

    Oh, I see what you did. You need to get rid of that cat variable:

        private String cat; // Java made me add this variable for whatever the reason (the previous code used didn't work for some reason)

    And no, Java didn't make you add this variable. Just get rid of it and only use the pet variable in your setPet method. Give that method a Cat parameter and pass in a Cat object into that method as we discussed, not a String.

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

    Default Re: Another question abt mutators and accessors...

    Is this what you meant?
     public void setPet(String Cat) {
             this.pet = Cat;
        }
    Because if so, Java gives and error stating "incompatible types required: Cat found: String"

    --- Update ---

    Sorry just saw the post abt the gorilla class...here it is
    public class Gorilla {
     
        private int Gweight;
        private String Gname;
        private Cat pet;
     
     
        public Cat getPet() {
            return pet;
        }
     
     
        public void setPet(String Cat) {
             this.pet = Cat; //this is the error
        }
     
     
        public int getGweight() {
            return this.Gweight ;
        }
     
     
     
        public String getGname() {
            return this.Gname;
        }
     
     
        public void setGweight(int x) {
            this.Gweight = x; 
        }
     
     
        public void setGname(String g) {
            this.Gname = g;
        }
     
        public Gorilla() {
            this.Gname = "King Zeus";
            this.Gweight = 1;
        }
     
     
        public Gorilla(String g, int w) {
            this();
            this.Gname = g;
            this.Gweight = w;
        }
     
     
     
     
        public String toString() {
            return "The Gorilla's name is " + Gname + ", and the weight of that Gorilla is:  " + Gweight;
        }
     
    }

  10. #10
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Another question abt mutators and accessors...

    Quote Originally Posted by Vessel06 View Post
    Is this what you meant?
     public void setPet(String Cat) {
             this.pet = Cat;
        }
    Because if so, Java gives and error stating "incompatible types required: Cat found: String"
    Yep, and again the compiler is right. Again, don't use a String parameter for this method. What parameter type should you use instead?

    Note: you should not have a parameter named "Cat" as this will be very confusing since you have a class of the same name.

  11. The Following User Says Thank You to curmudgeon For This Useful Post:

    Vessel06 (March 12th, 2013)

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

    Default Re: Another question abt mutators and accessors...

    But when I simply put
    public void setPet(c) {
             this.pet = c; //this is the error
        }
    Java gives another error stating that an identifier still needs to be made...unsure what I should use in the parameter to compile

  13. #12
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Another question abt mutators and accessors...

    Quote Originally Posted by Vessel06 View Post
    But when I simply put
    public void setPet(c) {
             this.pet = c; //this is the error
        }
    Java gives another error stating that an identifier still needs to be made...unsure what I should use in the parameter to compile
    Have a look at the method tutorial to see how to write methods. The parameter always needs it's type declared, and in fact you've done this with your other methods, and in fact already did it with this method, but you just had the wrong type (String). Now you've given the parameter *no* type, and this can't work. Try to give it a *correct* type. You can do it -- just look at your notes or the tutorial.

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

    Vessel06 (March 12th, 2013)

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

    Default Re: Another question abt mutators and accessors...

    YOU SIR ROCK!!!! I passed in the right identifier
     public void setPet(Cat c) {
             this.pet = c; 
        }

    Now the problem is to make sure it all runs in the harness when I give the gorilla a pet cat as I showed earlier in one of the previous post showing my Gorilla Class

  16. #14
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Another question abt mutators and accessors...

    Quote Originally Posted by Vessel06 View Post
    YOU SIR ROCK!!!! I passed in the right identifier
     public void setPet(Cat c) {
             this.pet = c; 
        }
    There you go!

    Now the problem is to make sure it all runs in the harness when I give the gorilla a pet cat as I showed earlier in one of the previous post showing my Gorilla Class
    You should be able to fix your harness so that it now works.

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

    Default Re: Another question abt mutators and accessors...

    Ok...this is the prof's example/instruction "In your harness, check that your new accessor and mutator work. For instance, if you have a Gorilla koko and a Cat fluffy, you should be able to say
    koko.setPet(fluffy);
    //And if you then said
     
    System.out.println(koko.getPet().getName());
    //you should get the same result as
    System.out.println(fluffy.getName());"

    however those codes aren't working for me (and they do have the appropriate variable names from my project)

    the one that is working for me is
            G1.setPet(C2);
            G2.setPet(C1);
            System.out.println(G1.getPet());

    My question is if that code above says that a gorilla gets a pet cat...because in my output it just reprints a previous statement

    --- Update ---

    Just in case you need to see the rest of my harness...here it is
    public static void main(String[] args) {
     
     
     
     
            Gorilla G1 = new Gorilla();
            Gorilla G2 = new Gorilla("Godzilla", 85);
     
     
     
     
            System.out.println(G1);
            System.out.println(G2);
            System.out.println("\n");
     
     
            Cat C1= new Cat();
            Cat C2 = new Cat();
     
            C2.setName("Rocket");
            C2.setColor("orange");
     
     
            G1.setPet(C2);
            G2.setPet(C1);
            System.out.println(G1.getPet());
     
     
            System.out.println(C1);
            System.out.println(C2);
            System.out.println("\n");
     
        }
    }

  18. #16
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Another question abt mutators and accessors...

    You should show what is wrong with your current code. What output are you seeing vs. what are you expecting.

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

    Default Re: Another question abt mutators and accessors...

    This is the current code giving the issue
            G1.setPet(C2);
            G2.setPet(C1);
            System.out.println(G1.getPet().getGname);
     
     
    The output I'm getting is " The Gorilla's name is King Zeus, and the weight of that Gorilla is:  1
    The Gorilla's name is Godzilla, and the weight of that Gorilla is:  85
     
     
    The cat's name is Rocket, and the color of the cat is: orange
    The cat's name is Mr. Fluffmiester, and the color of the cat is: tuxedo
    The cat's name is Rocket, and the color of the cat is: orange" //This one doesn't say anything about the gorilla HAVING either one of cats as a pet cat

Similar Threads

  1. Constructor/Accessors/methods problem!!
    By gpelefty90 in forum Object Oriented Programming
    Replies: 4
    Last Post: September 26th, 2011, 12:56 AM

Tags for this Thread