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...
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.
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
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.
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.
Re: Making a method exercise question
Doesn't the name of the method suggest the name of the variable that you want to return?
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.