Reputation: 49
Im a beginner, can you tell me what am i doing wrong? Why can't i use those methods properly?
IDE shows: Return type required/ Cannot return a value from a method with a void result type
` public class Human
{
public int Age = 0;
public int Weight = 0;
public int Height = 0;
private String name = "";
boolean isMale;
}
public getAge()
{
return Age;
}
public getWeight()
{
return Weight;
}
public getHeight()
{
return Height;
}
public Human(int Age, int Weight, int Height, String name, boolean
isMale)
{
} `
Upvotes: 0
Views: 1953
Reputation: 28
Great job so far, you're just missing the return type statements for your methods. Your methods should be written as follows:
class Human {
private int Age = 0;
private int Weight = 0;
private int Height = 0;
private String name = "";
private boolean isMale;
public int getAge() {
return Age;
}
public int getWeight() {
return Weight;
}
public int getHeight() {
return Height;
}
public Human(int Age, int Weight, int Height, String name, boolean isMale) {
this.Age = Age;
this.Weight = Weight;
this.Height = Height;
this.name = name;
this.isMale = isMale;
//returns nothing
}
}
Notice that each method has some sort of return type. Since getAge() returns Age (which you have specified above as type int), you need to explicitly put in the method declaration statement that you are returning an int. Same holds true if you were to return a String or a Boolean, etc.
The last method (Human()), however, is a constructor function that gets called when you instantiate a new Human object:
Human myHuman = new Human(31, 155, 68, "Bob", true);
Notice how in the codeblock above, I am passing values in the order that I have them in the constructor function and the constructor function sets the attributes of the object based on what is being passed. The big takeaway here, per your question, is that it does not return anything (with the exception of a new object...discussion for another time).
Generally speaking, if you are not returning anything, put void in the return type. Just as you do in the Main function.
Last thing that I would like to point out, is the use of public vs. private for your attributes and methods. Generally speaking, you would set your attributes to private and then make your public get/set methods to return or alter these attributes. Just something to look out for in the future.
Hope that helps! Feel free to message me if you have another question or need some clarification.
Upvotes: 1
Reputation: 1276
You need to specify a return value after each method modifier.
For example,
public int getAge() {
return age;}
Upvotes: 0
Reputation: 3267
You need to have a return type for each function from where you want to return a value. ex:
public int getAge()
{
return Age;
}
public int getWeight()
{
return Weight;
}
public int getHeight()
{
return Height;
}
Upvotes: 0