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.ioFile;
import java.ioFileNotFoundException;
import java.ioPrintWriter;
import java.util.Scanner;
public class ProgThree
public static void mainString args
try
Scanner scanner new Scannernew Fileanimaltxt;
PrintWriter writer new PrintWriteranimalouttxt;
int animalsCount petsCount zooAnimalsCount ;
while scannerhasNextLine
String line scanner.nextLine;
String data line.split;
if datalength Animal
int id Integer.parseIntdata;
String type data;
double weight Double.parseDoubledata;
if isValidAnimalid type, weight
Animal animal new Animalid type, weight;
writer.printlnanimal;
animalsCount;
else if datalength Pet or ZooAnimal
int id Integer.parseIntdata;
String type data;
double weight Double.parseDoubledata;
if id && id && isValidPetid type, weight, data data
Pet pet new Petid type, weight, data data;
writer.printlnpet;
petsCount;
else if id && id Added this condition
if isValidZooAnimalid type, weight, data data
ZooAnimal zooAnimal new ZooAnimalid type, weight, Integer.parseIntdata data;
writer.printlnzooAnimal;
zooAnimalsCount; Moved this line inside the if condition
writer.printlnTotal Animals: animalsCount;
writer.printlnTotal Pets: petsCount;
writer.printlnTotal Zoo Animals: zooAnimalsCount;
scanner.close;
writer.close;
catch FileNotFoundException e
eprintStackTrace;
private static boolean isValidAnimalint id String type, double weight
return id && id && type.length && weight ;
private static boolean isValidPetint id String type, double weight, String name, String owner
return id && id && type.length && weight && name.length && owner.length;
private static boolean isValidZooAnimalint id String type, double weight, String cageNumber, String trainer
try
int cageNum Integer.parseIntcageNumber;
return id && id && type.length && weight && cageNum && trainer.length;
catch NumberFormatException e
return false;
class Animal
private int id;
private String type;
private double weight;
public Animalint 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 Petint id String type, double weight, String name, String owner
superid 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 ZooAnimalint id String type, double weight, int cageNumber, String trainer
superid 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
