Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can\'t figure this out Concepts tested by this program: Arrays, ArrayLists Creat

ID: 646364 • Letter: C

Question

  can't figure this out

Concepts tested by this program:

            Arrays, ArrayLists

            Create a Data Element/Worker class

                        Instance variables

                        Getters and setters

            Create a Data Manager class

                        Methods to access the data    

Create a GUI driver class

            Frame

            Panel

            Event programming

            Textfields

            Labels

            Buttons

            JOptionPane. showMessageDialog

            Read from a file

The Local Driver

Explanation / Answer

import java.io.*;
class studentRecords
{
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
public void addRecords() throws IOException
{
// Create or Modify a file for Database
PrintWriter pw = new PrintWriter(new BufferedWriter(new
FileWriter("studentRecords.txt",true)));
String name, Class, fname, mname, address, dob;
int age;
long telephoneNo;
String s;
boolean addMore = false;
// Read Data
do
{
System.out.print(" Enter name
name = br.readLine();
System.out.print("Father's Name
:
fname = br.readLine();
System.out.print("Mother's Name
:
mname = br.readLine();
System.out.print("Address
:
address = br.readLine();
System.out.print("Class
:
Class = br.readLine();
System.out.print("Date of Birth (dd/mm/yy) :
dob = br.readLine();
System.out.print("Age
:
age = Integer.parseInt(br.readLine());
System.out.print("Telephone No.
:
telephoneNo = Long.parseLong(br.readLine());
// Print to File
pw.println(name);
pw.println(fname);
pw.println(mname);
pw.println(address);
pw.println(Class);
pw.println(dob);
pw.println(age);
pw.println(telephoneNo);
System.out.print(" Records added successfully ! Do
you want to add more records ? (y/n) : ");
s = br.readLine();
if(s.equalsIgnoreCase("y"))
{
addMore = true;
System.out.println();
}
else
addMore = false;
}
while(addMore);
pw.close();
showMenu();
}
public void readRecords() throws IOException
{
try
{
// Open the file
BufferedReader file = new BufferedReader(new
FileReader("studentRecords.txt"));
String name;
int i=1;
// Read records from the file
while((name = file.readLine()) != null)
{
System.out.println("S.No. : " +(i++));
System.out.println("-------------");
System.out.println(" Name
: " +name);
System.out.println("Father's Name : "
+file.readLine());
System.out.println("Mother's Name : "
+file.readLine());
System.out.println("Address
: "
+file.readLine());
System.out.println("Class
: "
+file.readLine());
System.out.println("Date of Birth : "
+file.readLine());
System.out.println("Age
: "
+Integer.parseInt(file.readLine()));
System.out.println("Tel. No.
: "
+Long.parseLong(file.readLine()));
System.out.println();
}
file.close();
showMenu();
}
catch(FileNotFoundException e)
{
System.out.println(" ERROR : File not Found !!!");
}
}
public void clear() throws IOException
{
// Create a blank file
PrintWriter pw = new PrintWriter(new BufferedWriter(new
FileWriter("studentRecords.txt")));
pw.close();
System.out.println(" All Records cleared successfully !");
for(int i=0;i<999999999;i++); // Wait for some time
showMenu();
}
public void showMenu() throws IOException
{
System.out.print("1 : Add Records 2 : Display Records 3 :
Clear All Records 4 : Exit Your Choice : ");
int choice = Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
addRecords();
break;
case 2:
readRecords();
break;
case 3:
clear();
break;
case 4:
System.exit(1);
break;
default:
System.out.println(" Invalid Choice !");
break;
}
}
public static void main(String args[]) throws IOException
{
studentRecords call = new studentRecords();
call.showMenu();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote