Reputation: 147
I need help creating a setter method for my code. I have created two setter methods for both of my string values, but I am A) not sure if they are implemented correct, and B) not sure how to call them so that they appear on the screen. Essentially I would like to be able to just call my lion and hippo classes and have them already have a name and a size, and not have to implement them inside my main function directly by inserting something like Hippo h = new Hippo("Tom", "42")
package game2;
public class Game2 {
public static void main(String[] args) {
//I am getting the error here, what I want to do is figure out how to
//get this to work and then declare a name and size for the animal
Hippo h = new Hippo();
Lion l = new Lion();
}
}
package game2;
public abstract class Animal {
private String name;
private String Size;
public String getName() {
return name;
}
public String getSize() {
return Size;
}
public void setName(String name) {
name = "Tom";
}
public void setSize(String name) {
name = "42";
}
public Animal(String theName, String theSize) {
name = theName;
Size = theSize;
}
}
package game2;
public class Hippo extends Animal {
public Hippo(String name, String Size) {
super(name, Size);
}
}
package game2;
public class Lion extends Animal{
public Lion(String name, String Size) {
super(name, Size);
}
}
Upvotes: 3
Views: 18038
Reputation: 271585
If you want to decide the name and size after you have created your animal, you need a parameterless constructor. Currently all the constructors require you to pass 2 arguments. So write one!
Here is an example:
public Hippo() {
setSize(insert default size here);
setName(insert default name here);
}
Then, you can create a hippo like this:
Hippo hippo = new Hippo();
hippo.setName("Tom");
Upvotes: 0
Reputation: 22437
Remember when implement constructor by your own, you are overload the default constructor. So you need to pass two arguments.
You didnot pass the arguments for constructors when you instantiate:
Hippo h = new Hippo();
Lion l = new Lion();
Because your class constructors expect two parameters.
public Hippo(String name, String Size) {
super(name, Size);
}
And:
public Lion(String name, String Size) {
super(name, Size);
}
Solutions:
Either you can pass arguments when you instantiating objects:
Hippo h = new Hippo("name", "33");
Lion l = new Lion("name", "22");
Or you need to implement overloaded constructors for these.
Read this to learn more about constructor overloading.
Upvotes: 1
Reputation: 311348
Both Hippo
's and Lion
's constructors expect two string arguments - the name and the size. You need to pass them when you call the respective constructors. E.g.:
Hippo h = new Hippo("Happy", "Huge");
Lion l = new Lion("Leo", "Big");
Upvotes: 0
Reputation: 58772
new Hippo()/Lion() is calling a function you don't have (empty constructor)
You have function Hippo(String name, String Size) which need 2 parameters
try new Hippo("Billy", "10")
Upvotes: 0
Reputation: 48258
your code is not compiling because you are not using the right constructor, Both Hippo's and Lion's constructors expect two string arguments so you have to do something like:
Hippo h = new Hippo("myHippo", "220");
Lion l = new Lion("Simba", "400");
Upvotes: 0