In Java, allow a user to enter a 3-digit IDnumber, name, year of birth and IQ sc
ID: 3678367 • Letter: I
Question
In Java, allow a user to enter a 3-digit IDnumber, name, year of birth and IQ score. Store the information in a sorted list (either array or linked list). Allow the user to indicate when all the ID’s have been entered and then write all the information in the list to a text file named “iq.txt”. Subsequent runs bring back the data created in the earlier run and allow the user to continue adding to the file. Create a second program that reads in the text file created above. Allow the user to retrieve the information for a given ID and display the information.
Explanation / Answer
Below is the Java Code. I have used serializable interface to store objects to file and ream them back.
import java.io.*;
import java.util.*;
// Serializable interface is used to directly write objects to file
class Record implements Serializable
{
int IDnumber;
String name;
String dob;
double iq;
public Record(int id, String name, String dob, double iq)
{
this.name=name;
IDnumber=id;
this.dob=dob;
this.iq=iq;
}
public String toString()
{
return "ID:"+IDnumber+" Name: "+name+" DOB: "+dob+" IQ: "+iq;
}
}
public class MainClass
{
public static void main(String a[])
{
try
{
ArrayList<Record> records;
File file=new File("iq.txt");
// Read already saved records from file (If available)
if(file.exists())
{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
records = (ArrayList<Record>) ois.readObject();
ois.close();
}
else
records=new ArrayList();
int c=1;
Scanner sc=new Scanner(System.in);
int id;
double iq;
String name, dob;
Record r;
//ArrayList records=new ArrayList();
while(c!=0)
{
System.out.println("Enter 1 to add record and 0 to exit");
c=sc.nextInt();
if(c==1)
{
System.out.println("Enter Id");
id=sc.nextInt();
System.out.println("Enter Name");
name=sc.next();
System.out.println("Enter Dob");
dob=sc.next();
System.out.println("Enter IQ");
iq=sc.nextDouble();
r=new Record(id,name,dob,iq);
records.add(r);
}
}
// Sort the list based on ID
Collections.sort(records, new Comparator<Record>() {
@Override public int compare(Record p1, Record p2) {
return p1.IDnumber - p2.IDnumber; // Ascending
}
});
System.out.println("Savinf Records..... Done");
// Write the updated list to file
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(records);
oos.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
// Read records
import java.io.*;
import java.util.*;
public class ReadRecords
{
public static void main(String a[])
{
try
{
ArrayList<Record> records;
File file=new File("iq.txt");
if(file.exists())
{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
records = (ArrayList<Record>) ois.readObject();
ois.close();
for(int i=0; i<records.size();i++)
{
System.out.println("Record#"+i+" "+records.get(i));
}
}
else
{
System.out.println("Records not found");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.