I will also be creating a 4th class with a main method, but I mainly need help w
ID: 3753213 • Letter: I
Question
I will also be creating a 4th class with a main method, but I mainly need help with these first 3 classes as I feel like I am doing them wrong:
Animal class– will have four instance variables (legs and eyes both integers) and (eat and sound both strings). All four will protected.
You will have two constructors: one will accept no parameters and will have the four instance variables assigned to either 0 or “nothing”, appropriately. The other will accept four incoming parameters and will set the instance variables appropriately to the values of the incoming parameters.
This class will also have a method called printAnimal that has no incoming parameters. It will simply print information about the current state (i.e. value) of each instance variable. For this print statement you must precede the string that you are going to print out with something like “From Animal: ” to indicate that the printing is being conducted within the class Animal. This method will not return anything.
Finally, there will be a method called setLegs that accepts one incoming parameter and will assign its value to the instance variable legs. This method will not return anything. You are now to do the following sequence of statements:
Spider class – this will be a subclass (child) of the super class Animal (parent). No instance variables are declared.
You will have two constructors: one will accept no parameters. The other will accept four incoming parameters. Both constructors will invoke the parent constructor (i.e. of the Animal class) appropriately.
This class will also have a method called printAnimal that will override the method with the same name within the class Animal. This method will also have no incoming parameters. It will simply print information about the current state (i.e. value) of each instance variable. For this print statement you must precede the string that you are going to print out with something like “From Spider: ” to indicate that the printing is being conducted within the class Spider. Also, this method must invoke the method printAnimal within the superclass/parent (i.e. Animal class). This method will not return anything.
This class will have another method called getNumEyes that will have no incoming parameters but will return the value of the instance variable eyes.
This class will have another method called setLegs that will accept one incoming parameter. This method will then set the value of the instance variable legs by calling its parent’s (i.e. Animal class) own method setLegs to do this task. This method will not return anything.
Lion class – this will be a subclass (child) of the super class Animal (parent). No instance variables are declared.
You will have one constructor: It will accept four incoming parameters. This constructor will invoke the parent constructor (i.e. of the Animal class) appropriately.
This class will also have a method called printAnimal that will override the method with the same name within the class Animal. This method will also have no incoming parameters. It will simply print information about the current state (i.e. value) of each instance variable. For this print statement you must precede the string that you are going to print out with something like “From Lion:” to indicate that the printing is being conducted within the class Lion. Also, this method must invoke the method printAnimal within the superclass/parent (i.e. Animal class). This method will not return anything.
This class will have another method called getNumEyes that will have no incoming parameters but will return the value of the instance variable eyes.
This class will have another method called setLegs that will accept one incoming parameter. This method will then set the value of the instance variable legs by calling its parent’s (i.e. Animal class) own method setLegs to do this task. This method will not return anything.
This class will have another method called setEyes that will accept one incoming parameter. This method will then set the value of the instance variable eyes to the value of the incoming parameter. This method will not return anything.
Explanation / Answer
1. Animal Class:
package com.practice.animalexample;
public class Animal {
//4 instance variables
protected int eyes;
protected int legs;
protected String eat;
protected String sound;
//Constructor without fields
public Animal() {
eyes = 0;
legs = 0;
eat = "";
sound = "";
}
//Constructor with fields
public Animal(int eyes, int legs, String eat, String sound) {
this.eyes = eyes;
this.legs = legs;
this.eat = eat;
this.sound = sound;
}
//Print Animal Function
public void printAnimal() {
System.out.println("From Animal: ");
System.out.println("eyes: " + eyes);
System.out.println("legs: " + legs);
System.out.println("eat: " + eat);
System.out.println("sound: " + sound);
}
//Set Legs Function
public void setLegs(int legs) {
this.legs = legs;
}
}
2. Spider Class:
package com.practice.animalexample;
//Spider class as sub class of Animal
public class Spider extends Animal {
//Constructor without fields
public Spider() {
super(); //calling parent class (Animal) constructor without fields
}
//Constructor with fields
public Spider(int eyes, int legs, String eat, String sound) {
super(eyes, legs, eat, sound); //calling parent class (Animal) constructor to initialize variables
}
//Print Animal Function
public void printAnimal() {
System.out.println("From Spider: ");
super.printAnimal(); //Calling parent class (Animal) printAnimal Function
}
//Get NumEyes Function
public int getNumEyes() {
return eyes;
}
//Set legs Function
public void setLegs(int legs) {
super.setLegs(legs); // setting the value by calling parent class setLegs function
}
}
3. Lion Class:
package com.practice.animalexample;
//Lion class sub class of Animal Class
public class Lion extends Animal {
//Constructor with fields
public Lion(int eyes, int legs, String eat, String sound) {
super(eyes, legs, eat, sound); //calling parent class (Animal) constructor to initialize fields
}
//Print Animal Function
public void printAnimal() {
System.out.println("From Lion: ");
super.printAnimal(); //Calling parent class (Animal) printAnimal Function
}
//Get NumEyes function
public int getNumEyes() {
return eyes;
}
//Set legs function
public void setLegs(int legs) {
super.setLegs(legs); //setting the value by calling parent class setLegs function
}
//Set Eyes function to set the value for eyes of lion class
public void setEyes(int eyes) {
this.eyes = eyes;
}
}