Question
I need help with this Java programming assignment. Help would be appreciated! Project 22-2: Animal List Create an application that collects and displays various animals.
I need help with this Java programming assignment. Help would be appreciated!
Project 22-2: Animal List
Create an application that collects and displays various animals.
Console:
Specifications (This program must meet all of them)
Create an abstract class named Animal. This class should have a private name variable of type String, and contain these constructors and methods:
public Animal(String name) public String getName() public void setName(String name) public String getNameAndType() public abstract void speak() protected void speak(Consumerconsumer)
The getNameAndType() method should return the animal's name concatenated with 'the' and the type of the class (example: 'Percy the Cat').
Create subclasses named Dog, Cat, and Turtle that extend the Animal class. These classes should have constructors that call the constructor of the parent class.
When the subclasses override the abstract speak() method, they should call the protected speak() method of the parent class and pass it a function. This function contains the functionality for the speak() method for that subclass.
You should be able to easily modify this class so an animal 'speaks' by printing data to the console or by displaying a GUI dialog box.
After the user has entered all the animals they want, each animal that they've entered should 'speak'.
Use the Console class from chapter 7 or a variation of it to validate the user's input.
Code for Console Class from chapter 7:
import java.util.Scanner;
public class Console {
private static Scanner sc = new Scanner(System.in);
public static String getString(String prompt) {
System.out.print(prompt);
String s = sc.next(); // read user entry
sc.nextLine(); // discard any other data entered on the line
return s;
}
public static int getInt(String prompt) {
int i = 0;
boolean isValid = false;
while (!isValid) {
System.out.print(prompt);
if (sc.hasNextInt()) {
i = sc.nextInt();
isValid = true;
} else {
System.out.println("Error! Invalid integer. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
public static int getInt(String prompt, int min, int max) {
int i = 0;
boolean isValid = false;
while (!isValid) {
i = getInt(sc, prompt);
if (i
System.out.println("Error! Number must be greater than " + min + ".");
} else if (i >= max) {
System.out.println("Error! Number must be less than " + max + ".");
} else {
isValid = true;
}
}
return i;
}
public static double getDouble(String prompt) {
double d = 0;
boolean isValid = false;
while (!isValid) {
System.out.print(prompt);
if (sc.hasNextDouble()) {
d = sc.nextDouble();
isValid = true;
} else {
System.out.println("Error! Invalid number. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return d;
}
public static double getDouble(String prompt, double min, double max) {
double d = 0;
boolean isValid = false;
while (!isValid) {
d = getDouble(sc, prompt);
if (d
System.out.println("Error! Number must be greater than " + min + ".");
} else if (d >= max) {
System.out.println("Error! Number must be less than " + max + ".");
} else {
isValid = true;
}
}
return d;
}
}
Welcome to the Aninal List Type of animol: 1 Dog 2 - Cat 3 Turtle Choose type:2 Enter animal's name: Percy Continue? (y): y Type of animal: 1 Dog 2 Cat 3 Turtle Choose type: 3 Enter animal's name: Yertle Continue? (y): n And now let's hear the animals speak Percy the Cat says "Meow Yertle waves! (turtles don't have vocal cords)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