This assignment requires you to implement at least four classes. The first class, Animal, stores basic data about an animal. The second class Pet, stores additional data about an animal which is a pet. The third class ZooAnimal, stores additional data about an animal which is kept in a zoo. The fourth class ProgThree has a main method which reads data about animals, pets and zoo animals and creates a report about animals, pets and zoo animals.
Your program must be developed following the guidelines previously discussed in class. These guidelines include appropriate variable names, use of comments to describe the purpose of the program and the purpose of each method, and proper use of indentation of all program statements.
Animal.java technical details:
One Constructor
Constructor #1. It will take all the parameters as listed:
int animalID.
String animalType.
double weight.
Instance variables for the Animal class
int animalID.
String animalType.
double weight.
Method which must be part of the Animal class:
A toString() method which displays information about an animal in the following format:
ID: 1015 Type: Monkey Weight: 38.6
Pet.java technical details:
One Constructor – the first three parameters are passed to the Animal class constructor in the first line of the Pet constructor by using the super reference.
Constructor. It will take all the parameters as listed:
int animalID.
String animalType.
double weight.
String name;
String owner:
Instance variables for the Pet class
String name;
String owner:
Method which must be part of the Pet class:
A toString() method which displays information about an animal in the following format:
ID: 5089 Type: Fish Weight: 0.2
Name: Sally Owner: Henry Santos
Note: The toString() Method gets the first line of output from the Animal class toString method.
Animal is a parent class of the Pet class.
ZooAnimal.java technical details:
One Constructor – the first three parameters are passed to the Animal class constructor in the first line of the ZooAnimal constructor by using the super reference.
Constructor #1. It will take all the parameters as listed:
int animalID.
String animalType.
double weight.
int cageNumber.
String trainer.
Instance variables for the ZooAnimal class
int cageNumber.
String trainer.
Method which must be part of the ZooAnimal class:
A toString() method which displays information about an animal in the following format:
ID: 8850 Type: Fish Weight: 0.2
Cage: 103 Trainer: Suzie Tran
Note: The toString() Method gets the first line of output from the Animal class toString method.
Animal is a parent class of the ZooAnimal class.
ProgThree.java Specifications:
ProgThree should read information about animals, pets and zoo animals. Initial data about pets is available in a text file named animal.txt. The program should read one line of input from the text file and process it independently. The program should continue until all lines of the input file have been processed.
The input file will contain a single line of data about each animal, pet or zoo animal. There are several lines in the input file and the animals, pets and zoo animals are mixed together. A input line will either contain three or five pieces of data. An input line with three pieces of data is an animal, and those three pieces of data will be animal id, type and weight, in order. Pets and zoo animals have the same first three pieces of data and then two additional pieces of data. If the animal id is between 3,000 and 7,999 the input record is about a pet and the last two pieces of data are the pet’s name and the owner’s name, both strings. If the animal id is between 8,000 and 9,999 the input record is about a zoo animal and the last two pieces of data are the cage number, an integer and the trainer’s name, a string.
The rules for valid input records, follow:
The first piece of data is animalID. It must be a four digit positive integer. Records whose ID numbers are between 3,000 and 7,999 should contain data about a “pet”. Records whose ID numbers are between 8,000 and 9,999 should contain data about a “zoo animal.” Records whose ID numbers are between 1,000 and 2,999 should contain data about an “animal.” Records whose ID numbers are less than 1,000 or greater than 9,999 should be considered to be invalid records.
animalType must be a string of at least three characters.
weight must be a valid positive real number.
For “Pet” objects, name and owner each must be strings of at least three characters.
For “Zoo Animal” objects, cageNumber must be a positive integer and trainer must be a string of at least three characters.
You may assume that input records will contain data of the data types needed by the program. For example, for input records of animals the record will contain an int, String and double in that order. You may not assume, however, that the input records will contain valid data. For example, the weight will be a real number (data type matches) but it may be negative making the input record invalid. In another input record, the animalType is a string but only contains two characters, making it an invalid record.
After reading in the data, determine if the data contained in the record is valid. If so, construct the appropriate type of object and write the object’s data to the output file, “animalout.txt”. If not, ignore the input record and proceed to the next record. When all of the animals have been processed, write to the output the total number of each, animals, pets and zoo animals written to the output file. Close the output file and end the program. You will need to use skills learned in modules two through seven and ten to complete this program.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class ProgThree {
public static void main(String[] args) {
File inputFile = new File("data/animal.txt");
if (!inputFile.exists()) {
System.err.println("Error: The file 'animal.txt' does not exist.");
return;
}
try {
Scanner scanner = new Scanner(inputFile);
PrintWriter writer = new PrintWriter("animalout.txt");
int animalsCount = 0, petsCount = 0, zooAnimalsCount = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] data = line.split(" ");
if (data.length == 3) { // Animal
int id = Integer.parseInt(data[0]);
String type = data[1];
double weight = Double.parseDouble(data[2]);
if (isValidAnimal(id, type, weight)) {
Animal animal = new Animal(id, type, weight);
writer.println(animal);
animalsCount++;
}
} else if (data.length == 5) { // Pet or ZooAnimal
int id = Integer.parseInt(data[0]);
String type = data[1];
double weight = Double.parseDouble(data[2]);
if (id >= 3000 && id <= 7999 && isValidPet(id, type, weight, data[3], data[4])) {
Pet pet = new Pet(id, type, weight, data[3], data[4]);
writer.println(pet);
petsCount++;
} else if (id >= 8000 && id <= 9999) {
if (isValidZooAnimal(id, type, weight, data[3], data[4])) {
ZooAnimal zooAnimal = new ZooAnimal(id, type, weight, Integer.parseInt(data[3]), data[4]);
writer.println(zooAnimal);
zooAnimalsCount++;
}
}
}
}
writer.println("Total Animals: " + animalsCount);
writer.println("Total Pets: " + petsCount);
writer.println("Total Zoo Animals: " + zooAnimalsCount);
scanner.close();
writer.close();
} catch (FileNotFoundException e) {
System.err.println("Error: The file 'animal.txt' was not found.");
e.printStackTrace();
}
}
private static boolean isValidAnimal(int id, String type, double weight) {
return id >= 1000 && id <= 2999 && type.length() >= 3 && weight > 0;
}
private static boolean isValidPet(int id, String type, double weight, String name, String owner) {
return id >= 3000 && id <= 7999 && type.length() >= 3 && weight > 0 && name.length() >= 3 && owner.length() >= 3;
}
private static boolean isValidZooAnimal(int id, String type, double weight, String cageNumber, String trainer) {
try {
int cageNum = Integer.parseInt(cageNumber);
return id >= 8000 && id <= 9999 && type.length() >= 3 && weight > 0 && cageNum > 0 && trainer.length() >= 3;
} catch (NumberFormatException e) {
return false;
}
}
}
class Animal {
private int id;
private String type;
private double weight;
public Animal(int id, String type, double weight) {
this.id = id;
this.type = type;
this.weight = weight;
}
public int getId() {
return id;
}
public String getType() {
return type;
}
public double getWeight() {
return weight;
}
@Override
public String toString() {
return "Animal{" +
"id=" + id +
", type='" + type + ''' +
", weight=" + weight +
'}';
}
}
class Pet extends Animal {
private String name;
private String owner;
public Pet(int id, String type, double weight, String name, String owner) {
super(id, type, weight);
this.name = name;
this.owner = owner;
}
@Override
public String toString() {
return "Pet{" +
"id=" + getId() +
", type='" + getType() + ''' +
", weight=" + getWeight() +
", name='" + name + ''' +
", owner='" + owner + ''' +
'}';
}
}
class ZooAnimal extends Animal {
private int cageNumber;
private String trainer;
public ZooAnimal(int id, String type, double weight, int cageNumber, String trainer) {
super(id, type, weight);
this.cageNumber = cageNumber;
this.trainer = trainer;
}
@Override
public String toString() {
return "ZooAnimal{" +
"id=" + getId() +
", type='" + getType() + ''' +
", weight=" + getWeight() +
", cageNumber=" + cageNumber +
", trainer='" + trainer + ''' +
'}';
}
}
So according to the one error I have in the code when testing it, it seems I am on the final step and this is the final error I need to debug it seems.
Error: The file ‘animal.txt’ does not exist.
I need insight on how to fix that error and make the code work.
READ THE INSTRUCTIONS AND MAKE SURE THE CODE FOLLOWS THE INSTRUCTIONS
DEBUG THE ERROR THE CODE HAS AND DON’T CHANGE IT ENTIRELY.
MAKE SURE THE CODE IS WORKING AND TEST IF IT WORKS
Trey 8 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.