Question: Write a program that reads from a file created by the program in the previous programming project and displays the following information on the screen:
Write a program that reads from a file created by the program in the previous programming project and displays the following information on the screen: the data for the species having the smallest population and the data for the species having the largest population. Do not assume that the objects in the file are in any particular order. The user gives the file name.
The previous program:
class Species
private String name;
private int population;
private double growthRate;
public Species
name null;
population ;
growthRate ;
public Species String name, int population,
double growthRate
this.name name;
this.population population;
this.growthRate growthRate;
public String toString
return Name name
"Population population
"Growth rate growthRate ;
public class species
public static void mainString args
ObjectOutputStream outputStream null;
String fileName "species.records";
try
outputStream new ObjectOutputStreamnew FileOutputStreamfileName;
catch IOException e
System.out.printlnError opening output file fileName ;
System.exit;
Species califCondor new SpeciesCalif Condor", ;
Species blackRhino new SpeciesBlack Rhino", ;
try
outputStream.writeObjectcalifCondor;
outputStream.writeObjectblackRhino;
outputStream.close;
catch IOException e
System.out.printlnError fileName ;
System.exit;
try
inputStream new ObjectInputStreamnew FileInputStreamspeciesrecords";
catch IOException e
System.out.printlnSorryThere was an Error opening the requested file fileName ;
System.exit;
Species readOne null, readTwo null;
try
readOne Species inputStream.readObject;
readTwo Species inputStream.readObject;
inputStream.close;
catch Exception e
System.out.printlnError reading from file fileName ;
System.exit;
System.out.printlnThe following was read
"from file fileName ;
System.out.printlnreadOne;
System.out.println;
System.out.printlnreadTwo;
System.out.printlnEnd of program.";
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
