To help you better understand the use of constructors, accessor methods, mutator methods, and the toString method you will be doing a series of simple classes designed to give you practice in creating these special methods. These assignments are going to be fairly trivial so you can concentrate on the process of creating the methods. In this assignment you are going to implement two classes: a FarmAnimal class and a Farm class. The Farm class will be a client of the FarmAnimal class. It will also contain the main method for the program.
The FarmAnimal class is going to keep track of how many farm animals a farmer has on his farm. Specifically, you are going to track the number of cows, horses, pigs, and chickens on the farm. On this first assignment, we will be walking through the process together.
Create a file named FarmAnimal.java and add the following code.
public class FarmAnimal { // instance variables // constructors // accessor methods }
The first step is to add our instance variables to the class. We will need a variable to store the farmer's name and one variable for each animal. Add the following code to the instance variables section.
private String name; // The farmer's name private int cows; // number of cows private int horses; // number of horses private int pigs; // number of pigs private int chickens; // number of chickens
Next we are going to define two constructors for the class. Recall that a constructor is a special method designed to give initial values to the instance variables. By defining two constructors we make our class more flexible and easier to use.
Here is the code for our first constructor. Add the code to the FarmAnimal class under the constructors section.
public FarmAnimal() { name = ""; cows = 0; horses = 0; pigs = 0; chickens = 0; }
This constructor is called the default constructor since it does not have any parameters in its parameter list. The default constructor typically sets the instance variables to default values like 0 and "" (the null string). The default constructor is called when you instantiate an object using the following format.
FarmAnimal farm1 = new FarmAnimal();
*do not add this code to the FarmAnimal class. It is used in the Farm class.
This code creates a FarmAnimal object, named farm1, and calls the default constructor to initialize its instance variables. It sets the instance variables for that object to the following
name = "" cows = 0 horses = 0 pigs = 0 chickens = 0
Here is the code for our second constructor.
public FarmAnimal(String n, int c, int h, int p, int ch) { name = n; cows = c; horses = h; pigs = p; chickens = ch; }
We are providing this constructor, to the clients who use our class, so they have more flexibility in the way they can instantiate FarmAnimal objects. Instead of just using the default constructor which sets all of the instance variables to default values, they can use this constructor to provide their own values to the instance variables. Here is an example of how they might use this constructor.
FarmAnimal farm2 = new FarmAnimal("John Chisum", 50, 12, 5, 10);
*do not add this code to the FarmAnimal class. It is used in the Farm class.
This constructor will instaniate a FarmAnimal object, named farm2, and set the instance variables for that object to the following
name = "John Chisum" cows = 50 horses = 12 pigs = 5 chickens = 10
Accessor methods provide clients access to the instance variables of a class so that they can see the contents of the instance variables. There are 5 instance variables in the FarmAnimal class so we will include 5 accessor methods. Add the following code to the FarmAnimal class under the accessor methods section.
public String getName() { return name; } public int getCows() { return cows; } public int getHorses() { return horses; } public int getPigs() { return pigs; } public int getChickens() { return chickens; }
The purpose of the Farm class is to test the fuctionality of the FarmAnimal class. To do this it instantiates two FarmAnimal objects and then displays information about each object.
Create a file named Farm.java and add the following code.
public class Farm { public static void main(String[] args) { } }
The first step is to instantiate two FarmAnimal objects. The first object, farm1, is instantiated using the default constructor, the second object, farm2, is instantiated using the second constructor. Add the following code to the Farm class's main method.
FarmAnimal farm1 = new FarmAnimal(); FarmAnimal farm2 = new FarmAnimal("John Chisum", 50, 12, 5, 10);
The next step is to display the information about each farm. To do this we are going to utilize the accessor methods of FarmAnimal class. Add the following code.
System.out.println("Farmer Name : " + farm1.getName()); System.out.println("Cows : " + farm1.getCows()); System.out.println("Horses : " + farm1.getHorses()); System.out.println("Pigs : " + farm1.getPigs()); System.out.println("Chickens : " + farm1.getChickens()); System.out.println(); System.out.println("Farmer Name : " + farm2.getName()); System.out.println("Cows : " + farm2.getCows()); System.out.println("Horses : " + farm2.getHorses()); System.out.println("Pigs : " + farm2.getPigs()); System.out.println("Chickens : " + farm2.getChickens()); System.out.println();
Compile and run the program.
FarmAnimal.java
Farm.java
Farmer Name : Cows : 0 Horses : 0 Pigs : 0 Chickens : 0 Farmer Name : John Chisum Cows : 50 Horses : 12 Pigs : 5 Chickens : 10