Question: Debug the code below. DO NOT CHANGE IT BUT ONLY DEBUG THE ERORRS IN THE CODE! MAKE SURE THE CODE IS ERROR FREE AND TEST

Debug the code below. DO NOT CHANGE IT BUT ONLY DEBUG THE ERORRS IN THE CODE! MAKE SURE THE CODE IS ERROR FREE AND TEST IF IT WORKS BEFORE COMPLETING!!
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){
try {
Scanner scanner = new Scanner(new File("animal.txt"));
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){// Added this condition
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++; // Moved this line inside the if condition
}
}
}
}
writer.println("Total Animals: "+ animalsCount);
writer.println("Total Pets: "+ petsCount);
writer.println("Total Zoo Animals: "+ zooAnimalsCount);
scanner.close();
writer.close();
} catch (FileNotFoundException e){
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;
}
@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="+ super.id +
", type='"+ super.type +'\''+
", weight="+ super.weight +
", 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="+ super.id +
", type='"+ super.type +'\''+
", weight="+ super.weight +
", cageNumber="+ cageNumber +
", trainer='"+ trainer +'\''+
'}';
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!