Answered step by step
Verified Expert Solution
Question
1 Approved Answer
5. Consider writing a program to be used to manage information about the animals on a farm. The farm has three kinds of animals: cows,
5. Consider writing a program to be used to manage information about the animals on a farm. The farm has three kinds of animals: cows, pigs, and goats. The cows are used to produce both milk and meat. The goats are used only to produce milk, and the pigs are used only to produce meat. Assume that an Animal class has been defined. Which of the following is the best wasy to represent the remaining data? A. Define two subclasses of the Animal class: Milk Producer and Meat Producer. Define two subclasses of the Milk Producer class: Cow and Goat; and define two subclasses of the MeatProducer class: Cow and Pig. B. Define three subclasses of the Animal class: Cow, Goat, and Pig. Also define two interfaces: Milk Producer and Meat Producer. Define the Cow and Goat classes to implement the Milk Producer interface, and define the Cow and Pig classes to implement the Meat Producer interface. C. Define five new classes, not related to the Animal class: Cow, Goat, Pig, MilkProducer, and Meat Producer. D. Define five subclasses of the Animal class: Cow, Goat, Pig, MilkProducer, and Meat Producer. E. Define two subclasses of the Animal class: Milk Producer and Meat Producer. Also define three interfaces: Cow, Goat, and Pig. Define the MilkProducer class to implement the Cow and Goat interfaces, and define the Meat Producer class to implement the Cow and Pig interfaces. Questions 6-8 refer to the following incomplete definitions of the Book and TextBook classes (note that Textbook is a subclass of Book) public class Book { private String name; private double price; //constructor public Book (String n, double p) { name = n; price = p; System.out.println("created a book"); } public static Book cheapestbook (Book[ ] booklist) { 1/precondition: booklist.length > 0 //return the book with the lowest price .... missing code }} public class TextBook extends Book { private String classUsedin; public TextBook (String n, double p, String class) { //constructor super(n, p); classUsedin = class; System.out.println("created a textbook"); } public static TextBook getBookForClass(String classname, Textbook[ ] textbooklist) { //return the first textbook in textbooklist that is used in the given class (or null if there is no such textbook) ......missing code }} 6. Assume that the following declarations have been made: Book b; Book[ ] B; TextBook tb; TextBook[ ] TB; And that all four variables have been properly initialized. Which of the following statements will not compile? A. b = Book.cheapestBook(B); B. b = Book.cheapestBook(TB); C. b = TextBook.getBookForClass("Intro to Java", TB); D. tb = TextBook.getBookForClass("Intro to Java", B); E. tb = TextBook.getBookForClass("Intro to Java", TB); 7. Note that methods cheapestBook and getBookFor Class are declared static. Which of the following statements is true? A. It is appropriate for the two methods to be static because they both operate on arrays passed as parameters rather than on fields of the two classes. B. Although the code works as is, because the two methods return single objects rather than arrays, they really shouldn't be declared static. C. It is appropriate for the cheapestBook method to be either static or nonstatic, but because TextBook is a subclasss of Book, the getBookForClass method must be static if the cheapestBook method is static. D. Because the two methods are static, the missing code for the method bodies cannot contain any recursive calls. E. It is appropriate for the two methods to be static, but if they had return type void they could not be static. 8. Consider the following declaration and initialization: Book oneBook = new Textbook("ABC, 10.50, kindergarten"); Which of the following statements about this code is true? A. The code will not compile because the type of the left-hand side of the assignment is Book, and the type of the right-hand side is Textbook. B. The code will compile, but there will be a runtime error when it is executed because the type of the left-hand side of the assignment is Book, and the type of the right-hand side is TextBook. C. The code will compile and execute without error; only created a book will be printed. D. The code will compile and execute without error; only created a textbook will be printed. E. The code will compile and execute without error, and both created a book and created a textbook will be printed
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started