3

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);
    }
}    
Connor.Littleton
  • 147
  • 1
  • 1
  • 12
  • Please show compilation error – Volodymyr Demenkov Jun 08 '17 at 07:15
  • 1
    If you look at your constructors you see that they expect `name` and `size` - when you call them you do not provide them. You should do `Lion l = new Lion("Leo", "XXL"); ` – Scary Wombat Jun 08 '17 at 07:16
  • 1
    The error message is clear and plain english, so how can this be unclear? It also has been answered multiple times, so even a little bit of research would help. Writing "New to java" is no excuse for ignoring the error message and avoiding own research at all costs. – Tom Jun 08 '17 at 07:20
  • Possible duplicate of [Java Error - Actual and formal argument lists differ in length](https://stackoverflow.com/questions/22813484/java-error-actual-and-formal-argument-lists-differ-in-length) – alkasm Jun 08 '17 at 08:57

5 Answers5

1

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.

Blasanka
  • 21,001
  • 12
  • 102
  • 104
0

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");
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

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")

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0

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"); 
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Ok awesome that worked, but now Im trying to figure out how I would use my set method and set the size and name of the animal without having to declare that inside the main function, but inside the animal class. Any suggestions? – Connor.Littleton Jun 08 '17 at 07:23
  • @Connor.Littleton I don't understand the question. Can you share an example of what you'd like your code to look like? – Mureinik Jun 08 '17 at 07:26
  • @sweeper yes that answers my question thank you so much! How would you call this inside main tho? by just printing out the hippo object? – Connor.Littleton Jun 08 '17 at 07:40
0

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");
Sweeper
  • 213,210
  • 22
  • 193
  • 313