In Java, you will write several classes, and then create and use instances of th
ID: 3845081 • Letter: I
Question
In Java, 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 • 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. Sheep’s wool sells for $1.45 per pound.
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class WoolFactory {
public static void main(String[] args) {
String farmName;
String dogName;
String dogBreed;
int numOfSheep;
Sheep[] sheeps;
Dog dog;
Farm farm;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Farm Name :");
farmName = sc.next();
System.out.println("Enter Dog Name :");
dogName = sc.next();
System.out.println("Enter Dog Breed :");
dogBreed = sc.next();
System.out.println("Enter Number of Sheep :");
numOfSheep = sc.nextInt();
sc.close();
sheeps = generateRandonSheeds(numOfSheep);
dog = new Dog(dogName, dogBreed);
farm = new Farm(farmName, dog, sheeps);
double wool = farm.getWool();
farm.printFarm();
System.out.println("Total Earnig : $"+(wool*1.45d));
sc.close();
}
private static Sheep[] generateRandonSheeds(int num){
Random rand = new Random();
String str = "ljaeruinmautwebajadfjduioenajfhfdnnbyefasutnaj";
Sheep[] sheeps = new Sheep[num];
for(int i=0;i<num;i++){
int pos1 = Math.abs(rand.nextInt()%str.length()-1)+1;
int pos2 = Math.abs(rand.nextInt()%str.length()-1)+1;
if(pos1>pos2){
int temp =pos1;
pos1= pos2;
pos2= temp;
}
sheeps[i] = new Sheep((str.substring(pos1, pos2)), Math.abs(rand.nextInt()%20));
}
return sheeps;
}
}
//===============================================================
public class Farm {
private Sheep[] sheeps;
private Dog dog;
private String name;
public Farm(String n,Dog d,Sheep[] s){
if(d.herd()<s.length) new IllegalArgumentException();
sheeps = new Sheep[s.length];
for(int i=0;i<s.length;i++)
sheeps[i] = s[i];
setDog(d);
setName(n);
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumSheep(){
return sheeps.length;
}
public void printFarm(){
System.out.println("Farm Name : "+name);
System.out.println("Dog name : "+dog.getName());
System.out.println("Sheep In Farm :");
for(int i=0;i<sheeps.length;i++){
System.out.println(" Name : +"+sheeps[i].getName()+" Age : "+sheeps[i].getAge());
}
}
public double getWool(){
double totalWool = 0.0d;
for(int i=0;i<sheeps.length;i++){
totalWool = sheeps[i].shear();
}
return totalWool;
}
}
//===============================================================
import java.util.Random;
public class Sheep {
private String name;
private int age;
private boolean hasWool;
private static Random numberGenerator = new Random(123);
public Sheep(String n,int a){
setName(n);
setAge(a);
hasWool = true;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isHasWool() {
return hasWool;
}
public void setHasWool(boolean hasWool) {
this.hasWool = hasWool;
}
public static Random getNumberGenerator() {
return numberGenerator;
}
public static void setNumberGenerator(Random numberGenerator) {
Sheep.numberGenerator = numberGenerator;
}
public double shear(){
if(hasWool){
hasWool = false;
return (Math.abs(numberGenerator.nextDouble())%5.0d)+6.0;
}
else
return 0.0d;
}
}
//===============================================================
import java.beans.beancontext.BeanContext;
public class Dog {
private String name;
private String breed;
public Dog(String n,String b){
setName(n);
setBreed(b);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public int herd(){
if(breed.toLowerCase().contains("collie"))
return 20;
else if(breed.toLowerCase().contains("shepherds"))
return 25;
else if(breed.toLowerCase().contains("kelpies")||breed.toLowerCase().contains("teruvens"))
return 30;
else
return 10;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.