Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this question, you will write several classes, and then create and use instances of those classes in order to simulate a sheep farm. (a)

For this question, you will write several classes, and then create and use instances of those classes in order to simulate a sheep farm. (a) (7 points) Write a class Dog. A Dog has the following private attributes: A String name A String breed The dog class also contains the following public methods: A constructor that takes as input the name and breed of the dog getName which returns the name of the dog. A herd method that returns the number of sheep the dog can herd. This will depend on the type of dog doing the herding. All dogs of the Collie breed can herd 20 sheep, while all Shepherds can herd 25 sheep. Kelpies and Teruvens can each herd up to 30 sheep. All other breeds of dogs can herd 10 sheep. For example, a Dog of breed White Shepherd or belgian shepherd, or just shepherd would be able to herd 25 sheep. Hint: Think about you can use the toLowerCase() and contains() methods to do this check in a case-insensitive manner. (b) (8 points) Write a class Sheep. A Sheep has the following private attributes: A String name An int age A boolean hasWool Page 5 A static Random numberGenerator Initialize the Random numberGenerator with seed 123. The Sheep class should also have the following public methods: getName(): returns the name of a Sheep getAge(): returns the age of a Sheep A constructor that takes as input the name and age of a Sheep. It should set hasWool to be true shear(): This method returns a random double between 6 and 10 (inclusive), representing the amount of wool (in pounds) taken from the sheep. You can only shear a sheep if it presently hasWool. This method sets hasWool to false after a sheep is sheared. This method should return 0 if the sheep has no wool on it. Use the static Random numberGenerator and nextDouble in generating the random number. (Do not use Math.random()). Note that by using the Random class, you will get the same sequence of random numbers every time you run your code. (c) (15 points) Write a class Farm. This class has three private attributes: An array of Sheep A Dog A String name It also has the following public methods: A constructor that takes as input the name of the farm, a Dog who will be responsible for herding the sheep, and an array of sheep. It initializes the Sheep array and copies the Sheep references from the input array to the Farm array. Throw an IllegalArgumentException if there are more Sheep than the Dog can herd. getName that returns the name of the farm. getNumSheep that returns the number of sheep on the farm. printFarm that prints the name of the farm, the name of the dog on the farm, as well as the names and ages of all of the sheep. getWool it returns (in pounds) the amount of wool obtained from shearing all sheep on the farm. (d) (10 points) In the provided class WoolFactory write a main method. Here, you will write a main method that creates a farm where the name of the farm, the name and breed of dog, and number of sheep are chosen by the user via a Scanner object. In order to facilitate generating your Sheep array, we have provided methods to generate ran- dom Sheep ages, and to select Sheep names randomly from a provided array of potential Sheep names (you are welcome to use your own names or modify this code). Then, shear all of the sheep on the farm. Display the number of sheep on the farm, and then use the printFarm() method to give them complete farm information, and then print how much money they made. Sheeps wool sells for $1.45 per pound.

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_2

Step: 3

blur-text-image_3

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2018 Dublin Ireland September 10 14 2018 Proceedings Part 1 Lnai 11051

Authors: Michele Berlingerio ,Francesco Bonchi ,Thomas Gartner ,Neil Hurley ,Georgiana Ifrim

1st Edition

3030109240, 978-3030109240

More Books

Students also viewed these Databases questions

Question

How will the members be held accountable?

Answered: 1 week ago

Question

a. How will the leader be selected?

Answered: 1 week ago