Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 4 : Create a Horse class In this lab you will create a Horse class in the sp

ID: 663763 • Letter: L

Question

Lab 4 : Create a Horse class

In this lab you will create a Horse class in the spirit of the Table class. This will be the first of many classes we will create. Serious Java programs are created from multiple classes.

Your Horse should have the following fields:

private String name; // horse's name

private int age; // age in years

private String breed;

***************************************************

You should create a constructor that let's fill in the fields of our horse.

Each of the above fields should have an accessor and mutator.

Your horse class should have the following general methods (I will write the first one for you):

public void eat()
{
System.out.println( name + " is eating" ); // prints the content of the name field followed by the words is eating.
}

public void sleep()

public void walk()

public void trot()

Each of these methods should be written in the style of eat(), i.e., each method should print the name of the horse and what it is doing.

Create a test class, similar to TableTest, and create at least two horses and try them out.

Explanation / Answer

class Horse {

private String name; // horse's name

private int age; // age in years

private String breed;

//Accessors and Mutators

Horse(String theName, int theAge, theBreed )
{
setHorsename(theName);
setHorseage(theAge);
setHorsebreed(theBreed);
}
public String getHorsename()
{
return this.name;
}
public String setHorsename(theName);
{
this.name;
}
  
public int getHorseage()
{
return this.age;
}
public int setHorseage(theAge);
{
this.age;
}
public String getHorsebreed()
{
return this.breed;
}
public String setHorsebreed(theBreed);
{
this.breed;
}
  
public void eat()
{
System.out.println( getHorsename() + " is eating" ); // prints the content of the name field followed by the words is eating.
}
  
public void sleep()
{
System.out.println( getHorsename() + " is sleeping" );
}
  
public void walk()
{
System.out.println( getHorsename() + " is walking" );
}
public void trot()
{
System.out.println( getHorsename() + " is troting" );
}
}

class Test
{
public static void main(String args[])
{
Horse h1 =new Horse("XYZ");
Horse h2 =new Horse("ABC");
  
  
h1.eat();
h1.sleep();
h1.walk();
h1.trot();
h2.eat();
h2.sleep();
h2.walk();
h2.trot();
  
}
}