In this program we will be using inheritance, abstract classes, the Comparable i
ID: 3903724 • Letter: I
Question
In this program we will be using inheritance, abstract classes, the Comparable interface and writing to a file. You will also need to create a custom exception which you must use and catch.
Create an abstract class Animal. This class should contain instance variables as follows:
String name – must be at least 1 character but not more than 10 characters.
int paws – must be from 0 to 4
methods
abstract speak() – returns a String
toString() – returns name and number of paws
getters/setters
constructors – as needed
Should implement the Comparable interface. Compare on the name of the animal
Create a Dog class that extends Animal
Implement the abstract speak method – return “Woof, woof!”
Create a toString() method – should print the type of animal add the Animal toString() and append the results of the speak method.
Create a Cat class that extends Animal
Implement the abstract method – return “Meow”.
Create a toString() method – should print the type of animal add the Animal toString() and append the results of the speak method.
Create an InvalidAnimalException which will be used if an invalid animal is entered.
Create an AnimalTester class.
Create an array of Animals – at least 5 Animals.
Using a Random assignment – Assign a mixture of Cats and Dogs to the array.
You will need a method to allow users to enter the name and number of paws. This cannot be static.
Write the original array to a file.
Sort the array.
Write the sorted array to the same file.
Read the file and print the results.
You must use your custom exception to tell the user if their entry is invalid.
Explanation / Answer
// the code is almost completed you can run it once,
// one problem remains, that the graph location on the axes is not correct, I am rectifying that now
// output is attached
// plz comment if you need any clarification
// hit like if you liked it
// CODE
package chegg.june;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
// AnimalTester class.
public class Chegg339 {
private static final Scanner in = new Scanner(System.in);
public static void main(String... args) throws FileNotFoundException, InvalidAnimalException {
Chegg339 animalTester = new Chegg339();
Animal[] animals = new Animal[5];
for(int i = 0; i < 5; i++) {
animals[i] = animalTester.createAnimal();
}
System.out.println(" Animals are....");
// write the contents of original array to the file
try (PrintStream p = new PrintStream(new File("animals.txt"))) {
for(Animal animal : animals) {
if(animal instanceof Dog) {
p.println(((Dog)animal).toString());
System.out.println(((Dog)animal).toString());
} else if(animal instanceof Cat) {
p.println(((Cat)animal).toString());
System.out.println(((Cat)animal).toString());
} else {
throw new InvalidAnimalException();
}
}
}
System.out.println(" Sorting Animals...");
// sort the array
Arrays.sort(animals);
System.out.println("Writing Sorted Animals to file...");
// write the contents of sorted array to the file
try (PrintStream p = new PrintStream(new File("animals.txt"))) {
for(Animal animal : animals) {
p.println(animal);
}
}
System.out.println(" Sorted Animals are....");
Scanner fin = new Scanner(new File("animals.txt"));
int i = 0;
while(fin.hasNext()) {
StringTokenizer st = new StringTokenizer(in.nextLine());
if(st.hasMoreTokens()) {
switch (st.nextToken()) {
case "Dog":
animals[i] = new Dog(st.nextToken(), Integer.valueOf(st.nextToken()));
break;
case "Cat":
animals[i] = new Cat(st.nextToken(), Integer.valueOf(st.nextToken()));
break;
default:
throw new InvalidAnimalException();
}
System.out.println(animals[i]);
i++;
}
}
}
private Animal createAnimal() {
System.out.println("Enter the name and number of paws for animal : ");
if(Math.random()%2 == 0) {
return new Dog(in.next(), in.nextInt());
} else
return new Cat(in.next(), in.nextInt());
}
}
/**
* Custom Exception class
* @author ram bablu
*/
class InvalidAnimalException extends Exception {
public InvalidAnimalException() {
super("Illegal Animal Type Given");
}
}
/**
* Description of Animal class
* @author ram bablu
*/
abstract class Animal implements Comparable {
private String name;
private int paws;
public Animal(String name, int paws) {
this.name = name;
this.paws = paws;
}
public abstract String speak();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPaws() {
return paws;
}
public void setPaws(int paws) {
this.paws = paws;
}
@Override
public String toString() {
return name + " " + paws ;
}
@Override
public int compareTo(Object obj) {
Animal that = (Animal) obj;
return this.name.compareTo(that.name);
}
}
class Dog extends Animal {
public Dog(String name, int paws) {
super(name, paws);
}
@Override
public String speak() {
return "Woof, woof!";
}
@Override
public String toString() {
return "Dog " +super.toString()+ " "+speak();
}
}
class Cat extends Animal {
public Cat(String name, int paws) {
super(name, paws);
}
@Override
public String speak() {
return "Meaw";
}
@Override
public String toString() {
return "Cat " +super.toString()+ " "+speak();
}
}
/*
Using a Random assignment – Assign a mixture of Cats and Dogs to the array.
You will need a method to allow users to enter the name and number of paws. This cannot be static.
Write the original array to a file.
Sort the array.
Write the sorted array to the same file.
Read the file and print the results.
You must use your custom exception to tell the user if their entry is invalid.
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.