Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help with java. Create a Farm class Included methods: Farm() Farm(int cows,int chickens, int sheep) setAnimals(int cows,int chickens, int sheep) getCows() getChickens() getSheep() addCows(int

need help with java.

Create a Farm class

Included methods:

Farm()

Farm(int cows,int chickens, int sheep)

setAnimals(int cows,int chickens, int sheep)

getCows()

getChickens()

getSheep()

addCows(int num)

addChickens(int num)

addSheep(int num)

removeCows(int num)

removeChickens(int num)

removeSheep(int num)

equals(Farm)

toString()

You will create a farm class that will create the farm object. This farm object will contain 3 ints that store the values of the number of cows, chickens, and sheep.

Part 2: create a tester class

Using the testFarm class create tests that will make sure that your Farm works as intended. The basic outline of the class is already given to you, make sure your output matches what is given to you.

public class Farm {

/*

* class properties: one for the cows, chickens, and sheep

*/

private int cows;

private int chickens;

private int sheep;

/**

* this constructor will create a farm object with no animals ( 0 of each animal)

*/

public Farm() {

cows = 0;

chickens = 0;

sheep = 0;

}

/**

* @param cows - number of cows

* @param chickens - number of chickens

* @param sheep - number of sheep

*

* this constructor will create a farm object with the specified

* number of each animals

*/

public Farm(int cows, int chickens, int sheep) {

setAnimals(cows, chickens, sheep);

}

/**

* @param cows

* @param chickens

* @param sheep

*/

void setAnimals(int cows, int chickens, int sheep) {

this.sheep = sheep;

this.chickens = chickens;

this.cows = cows;

}

/**

* @return

*/

int getCows() {

return cows;

}

/**

* @return

*/

int getChickens() {

return chickens;

}

/**

* @return

*/

int getSheep() {

return sheep;

}

/**

* @param num

*/

void addCows(int num) {

cows += num;

}

/**

* @param num

*/

void addChickens(int num) {

chickens += num;

}

/**

* @param num

*/

void addSheep(int num) {

sheep += num;

}

/**

* @param num

*/

void removeCows(int num) {

cows = Math.max(0, cows - num);

}

/**

* @param num

*/

void removeChickens(int num) {

chickens = Math.max(0, chickens - num);

}

/**

* @param num

*/

void removeSheep(int num) {

sheep = Math.max(0, sheep - num);

}

/**

* @param farm

* @return will return a boolean that states weather the farm

* has the same exact number of each chickens, cows, and sheep

*/

boolean equals(Farm farm) {

return (cows == farm.cows && chickens == farm.chickens && sheep == farm.sheep);

}

/**

* will return a String in the form

* "x cows, y chickens, z sheep"

* where x, y, and z are the respective number of each animals

*/

public String toString() {

return String.format("%d cows, %d chickens, %d sheep", cows, chickens, sheep);

}

}

this is what I have right now. I know have to make another class but I do not know how.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions

Question

What is Aufbau's rule explain with example?

Answered: 1 week ago

Question

Write Hund's rule?

Answered: 1 week ago