Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

File of farm.java: public class Farm { private Animal theFarmer; private Animal firstAnimal; private Animal secondAnimal; private Animal thirdAnimal; public Farm(Farmer f, Animal a1, Animal

image text in transcribedimage text in transcribed

File of farm.java:

public class Farm {

private Animal theFarmer; private Animal firstAnimal; private Animal secondAnimal; private Animal thirdAnimal;

public Farm(Farmer f, Animal a1, Animal a2, Animal a3) { theFarmer = f; firstAnimal = a1; secondAnimal = a2; thirdAnimal = a3; }

public String toString() { String s = theFarmer + " had a farm."; s += " And on that farm he had a " + firstAnimal; s += " And on that farm he had a " + secondAnimal; s += " And on that farm he had a " + thirdAnimal; return s; }

}

File of runner Farm.java

import java.util.Scanner;

public class runner_Farm {

private static Scanner scan;

public static void main(String[] args) { scan = new Scanner(System.in); System.out.println("First, what's your farmer's forename?"); String f = scan.nextLine(); System.out.println("And their surname?"); Farmer fm = new Farmer(f, scan.nextLine()); Animal a1 = addAnimal(); Animal a2 = addAnimal(); Animal a3 = addAnimal(); Farm theFarm = new Farm(fm, a1, a2, a3); System.out.println(); System.out.println(theFarm); }

private static Animal addAnimal() { String type = ""; Animal a = null; while(a == null){ System.out.println("Choose an animal: pig, cow or sheep."); type = scan.nextLine().toLowerCase(); if (type.equals("pig")) a = new Pig(); else if (type.equals("cow")) a = new Cow(); else if (type.equals("sheep")) a = new Sheep(); else System.out.println("Unrecognised animal."); } return a; }

}

File of Animal.java

public class Animal extends Farm {

private String name; public Animal(String n) { name = n; }

public String getName() { return name; }

public String speak() { return ""; }

public String toString() { return getName() + " (" + speak() + " " + speak() + ")."; }

}

File of famer.java

public class Farmer extends Animal{

private String forename; private String surname;

public Farmer(String f, String s){ forename = f; surname = s; }

public String toString(){ return forename + " " + surname; }

}

File of cow.java

public class Cow extends Animal {

public Cow() { super("cow"); }

public String speak(String s) { return s + "moo"; }

}

File of pig.java

public class Pig extends Animal {

public Pig() { super(); }

public String speak() { return "oink"; }

}

File of sheep.java

public class Sheep {

public Sheep() { String type = "sheep"; super(type); }

public String toString() { return "baa"; }

}

Instructions In this exercise you will debug a class hierarchy, which consists of the classes Farm, Farmer, Animal, Pig, Sheep and Cow. In this class hierarchy, a Farm object has Animals and a Farmer, while Pigs, Sheeps and Cows are all Animals. (Note that for the purpose of this assignment a Farmer is not an Animal). Look in particular for mistakes in the class declarations and constructors. When these are all fixed, everything should compile correctly, and the main method in the runner_Farm class should produce output as shown in the sample run. You do not need to debug the runner file, so if it does not compile or run correctly then there is at least one error in one of the other files. Do not add main methods to any of the other files, or your code will not be scored correctly. Sample Run: First, what's your farmer's forename? old And their surname? MacDonald Choose an animal: pig, cow or sheep. pig Choose an animal: pig, cow or sheep. COW Choose an animal: pig, cow or sheep. sheep Old MacDonald had a farm. And on that farm he had a pig (oink oink). And on that farm he had a cow (moo moo). And on that farm he had a sheep (baa baa). KY Files STATUS O NOT SUBMITTED SAVE SUBMIT INSTRUCTIONS RUN CODE GRADING HISTORY public class Farm 2-{ Farm.java runner Farm.java 3 4 5 6 7 8 9 In this class hierarchy, a Farm object has Animals and a Farmer, while Pigs, Sheeps and Cows are all Animals. (Note that for the purpose of this assignment a Farmer is not an Animal). private Animal theFarmer; private Animal firstAnimal; private Animal secondAnimal; private Animal thirdAnimal; public Farm(Farmer f, Animal al, Animal a2, Animal a3) { the Farmer = f; firstAnimal = al; secondAnimal = a2; thirdAnimal = a); Animal.java Farmer.java Cow.java 10 - 11 12 13 14 15 16 17 18 - 19 20 21 Pig.java Sheep.java public String toString) { String s = the Farmer + " had a farm."; S += " And on that farm he had a + firstAnimal; 5 += " And on that farm he had a + secondAnimal 3 5 += " And on that farm he had a + thirdAnimal; return s; } 22 23 24 25 26 27 Look in particular for mistakes in the class declarations and constructors. When these are all fixed, everything should compile correctly, and the main method in the runner_Farm class should produce output as shown in the sample run. You do not need to debug the runner file, so if it does not compile or run correctly then there is at least one error in one of the other files. Do not add main methods to any of the }

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

Seven NoSQL Databases In A Week Get Up And Running With The Fundamentals And Functionalities Of Seven Of The Most Popular NoSQL Databases

Authors: Aaron Ploetz ,Devram Kandhare ,Sudarshan Kadambi ,Xun Wu

1st Edition

1787288862, 978-1787288867

Students also viewed these Databases questions

Question

Explain how pancreatic secretions are regulated.

Answered: 1 week ago

Question

Define employer choice strategy.

Answered: 1 week ago