Results 1 to 7 of 7
Like Tree2Likes
  • 2 Post By Iron Lion

Thread: Making a method exercise question

  1. #1
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Making a method exercise question

    The SingleFamily class represents a single family home that is either owner-occupied, or not (rented out). The main method of the SingleFamilyTester class creates a SingleFamily object and prints out some information about it.

    public class SingleFamilyTester {

    public static void main(String[] args) {
    System.out.println("I own a single-family dwelling.");
    SingleFamily house = new SingleFamily(true);
    System.out.print("The house is ");
    if (!house.getOwnerOcc()) {
    System.out.print("not ");
    }
    System.out.println("owner-occupied.");
    System.out.println("I have just moved.");
    house.setOwnerOcc(false);
    System.out.print("Now the house is ");
    if (!house.getOwnerOcc()) {
    System.out.print("not ");
    }
    System.out.println("owner-occupied.");
    }
    }



    Assignment is to complete the driver so it works properly.

    public class SingleFamily {

    private boolean ownerOccupied;

    public SingleFamily(boolean yesNo) {
    ownerOccupied = yesNo;
    }

    public void setOwnerOcc(boolean yesNo) {
    ownerOccupied = yesNo;
    }
    public void getOwnerOcc(boolean yesNo){
    ownerOccupied=yesNo;

    }
    }


    Bold part is the part that I added. But it says I have given my method the wrong name or parameters. I'm guessing it the parameters? But Im not certain what I change...

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: Making a method exercise question

    Please use code tags when posting.

    I don't see any reason why the compiler would object to that, but it's not very useful as a getter method, since it does exactly the same as the setter method. For a getter method, you want it to return a value, and you don't need it to take any arguments.

  3. #3
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Making a method exercise question

    Sorry, I'll remember to use code tags next time!

    in this program though, isnt the getOwnerOcc a boolean because it's just saying if the house is occupied or not?
    The program is supposed to write:

    "I own a single-family dwelling.
    The house is owner-occupied.
    I have just moved.
    Now the house is not owner-occupied."

    If it had a return, I would write:
    public String getOwnerOcc(String a){
    return a;
    } ....but this doesn't work

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Making a method exercise question

    You want to return the value of ownerOccupied. So first look to see what the type is for this variable, then be sure that the method signatures shows that the method returns this type (and not String since it's not a String), and then inside the method, return the variable. Also, it makes little sense for a method to return something that is passed into it -- otherwise it's a futile method. Most getter methods (methods that look like public Type getXXX() ) don't take any parameters.

  5. #5
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Making a method exercise question

    ok, I know the type isn't void and you said it's not a string. So it's boolean right?

    public boolean getOwnerOcc(){
    return
    }

    And I need to return something...but I don't get what I return.

  6. #6
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: Making a method exercise question

    Doesn't the name of the method suggest the name of the variable that you want to return?
    Fubarable and katiebear128 like this.

  7. #7
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Making a method exercise question

    thanks Iron Lion! I fixed it ! It was right in front of me...I guess I was looking at the problem too long. thanks for all your help too fubarable.

Similar Threads

  1. Replies: 8
    Last Post: 08-25-2010, 05:22 PM
  2. Swing question: Making a visual tic tac toe
    By alacn in forum New To Java
    Replies: 8
    Last Post: 07-05-2010, 03:42 AM
  3. SCPJ6 Study Guide - Question about exercise
    By HoChiMinh in forum Java Certification
    Replies: 1
    Last Post: 01-03-2009, 04:27 AM
  4. Replies: 1
    Last Post: 10-02-2008, 04:06 AM
  5. exercise of recursive method
    By amexudo in forum New To Java
    Replies: 2
    Last Post: 03-09-2008, 06:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •