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

Use RandomAccessFile instead of DataInputStream and DataOutputStream. (If you ne

ID: 3606894 • Letter: U

Question

Use RandomAccessFile instead of DataInputStream and DataOutputStream.(If you need class student i will paste it at the bottom of these page,all i want is the 5 case methods done, everything else is correct)

Assume that there are at most 100 students, and their ID numbers are integers in the range 1-100. Since there are at most 100 students, we reserve one empty location in the file for each potential student. The first record is reserved for student 1, the second record is reserved for student 2, and so on

Modify class Student so that each student record has 4 fields: id (int), name (at most 30 characters), age (int), major (at most 30 characters).

The program should offer the following options to the user:

                                           1. Add new student record

                                           2. Remove student record

                                           3. Update student record

                                           4. Display student record

                                           5. Display all student records

                                           6. Exit

import java.io.*;

import java.util.*;

class Student

{

private static final int NAMEMAX=30;

private static final int MAJORMAX=30;

private int id;

private char[] name=new char[NAMEMAX];

private int age;

private char[] major=new char[MAJORMAX];

public Student()

{

id = -1;

for (int j= 0; j< NAMEMAX; ++j)

name[j]=' ';

age = -1;

for (int j= 0; j< MAJORMAX; ++j)

major[j]=' ';  

}

public Student (int id, String s, int age, String m)

{

this.id = id;

for (int j=0; j<s.length(); ++j)

{

if (j<NAMEMAX) name[j]=s.charAt(j);

else break;

}

for (int j= s.length(); j< NAMEMAX; ++j)

name[j]=' ';

this.age = age;

for (int j=0; j<m.length(); ++j)

{

if (j<MAJORMAX) major[j]=m.charAt(j);

else break;

}

for (int j= m.length(); j< MAJORMAX; ++j)

major[j]=' ';

}

public void setId(int s)

{

id = s;

}

public int getId()

{

return id;

}

public void setName(String s)

{

for (int j=0; j<s.length(); ++j)

{

if (j<NAMEMAX) name[j]=s.charAt(j);

else return;

}

for (int j= s.length(); j< NAMEMAX; ++j)

name[j]=' ';

}

public String getName()

{

String s=new String(name);

return s.trim(); //line 34

}

public void setAge( int a )

{

age = a;

}

public int getAge()

{

return age;

}

public void setMajor(String m)

{

for (int j=0; j<m.length(); ++j)

{

if (j<MAJORMAX) major[j]=m.charAt(j);

else return;

}

for (int j= m.length(); j< MAJORMAX; ++j)

major[j]=' ';

}

public String getMajor()

{

String s=new String(major);

return s.trim();

}

public void write(RandomAccessFile f) throws IOException

{

f.writeInt(id);

for (int j= 0; j< NAMEMAX; ++j)

f.writeChar(name[j]);

f.writeInt(age);

for (int i= 0; i< MAJORMAX; ++i)

f.writeChar(major[i]);

}

public void read( RandomAccessFile f ) throws IOException

{

id = f.readInt();

for (int j= 0; j< NAMEMAX; ++j)

name [j] = f.readChar();

age = f.readInt();

for (int i= 0; i< MAJORMAX; ++i)

major [i] = f.readChar();

}

public static int size()

{

return 128; //4 (int) + 4 (int) + 50 (25 Unicode characters)

}

}

public class StudentDB

{

public static void main( String args[] ) throws FileNotFoundException, IOException

{

Student blank= new Student();

RandomAccessFile f = new RandomAccessFile("student.txt", "rw");

if(f.length()==0)

{

for ( int i = 0; i < 100; i++ )

blank.write(f);  

}

Scanner keyboard=new Scanner(System.in);

int choice=6;

do {

System.out.println(" 1. Add a Student record");

System.out.println(" 2. Remove a Student record");

System.out.println(" 3. Update a Student record");

System.out.println(" 4. Print a Student record");

System.out.println(" 5. Print all Student records");

System.out.println(" 6. Quit");

choice= keyboard.nextInt();

switch (choice)

{

case 1: addStudent(blank); break;

case 2: removeStudent(blank); break;

case 3: updateStudent(blank); break;

case 4: printStudent(blank); break;

//case 5: printAllStudent(blank); break;

default: break;

}

} while (choice!=6);

}

Explanation / Answer

Given below is the completed code for StudentDB class with output.


To indent code in eclipse, select code and press Ctrl+a and then Ctrl+i.
Please don't forget to rate the answer if it helped. Post a comment in case of any issues.


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
public class StudentDB
{
static Scanner keyboard=new Scanner(System.in);
public static void main( String args[] ) throws FileNotFoundException, IOException
{
Student blank= new Student();
RandomAccessFile f = new RandomAccessFile("student.txt", "rw");
if(f.length()==0)
{
for ( int i = 0; i < 100; i++ )
blank.write(f);  
}

int choice=6;
do {
System.out.println(" 1. Add a Student record");
System.out.println(" 2. Remove a Student record");
System.out.println(" 3. Update a Student record");
System.out.println(" 4. Print a Student record");
System.out.println(" 5. Print all Student records");
System.out.println(" 6. Quit");
choice= keyboard.nextInt();
switch (choice)
{
case 1: addStudent(f, blank); break;
case 2: removeStudent(f, blank); break;
case 3: updateStudent(f, blank); break;
case 4: printStudent(f, blank); break;
case 5: printAllStudent(f, blank); break;
default: break;
}
} while (choice!=6);
}
private static void printAllStudent(RandomAccessFile f, Student blank) throws IOException {
System.out.println("Displaying current list of students in file:");
System.out.println("---------------------------------------------");
f.seek(0);
for(int i = 0; i < 100; i++)
{
blank.read(f);
if(blank.getId() != -1)
System.out.println(blank.getId() + " " + blank.getName() + " " + blank.getAge() + " " + blank.getMajor());
}
System.out.println("---------------------------------------------");
}
private static void printStudent(RandomAccessFile f, Student blank) throws IOException {
int id;
System.out.println("Enter the ID of student to print: ");
id = keyboard.nextInt();
if(id >= 1 && id <= 100)
{
f.seek((id-1) * blank.size());
blank.read(f);
if(blank.getId() == -1)
System.out.println("No student data");
else
{
System.out.println(blank.getId() + " " + blank.getName() + " " + blank.getAge() + " " + blank.getMajor());
}
}
}
private static void updateStudent(RandomAccessFile f, Student blank) throws IOException {
int id, age;
String name, major;
System.out.print("Enter ID(1-100) of the student to be modified: ");
id = keyboard.nextInt();
System.out.println("Enter the new details for the student");
System.out.print("Enter name: ");
name = keyboard.next().trim();
System.out.print("Enter age: ");
age = keyboard.nextInt();
System.out.print("Enter major: ");
major = keyboard.next();
blank.setName(name);
blank.setAge(age);
blank.setId(id);
blank.setMajor(major);
if(id >= 1 && id <= 100)
{
f.seek(blank.size() * (id - 1));
blank.write(f);
System.out.println("Student with id " + id + " updated ");
}
else
System.out.println("Invalid id!");
}
private static void removeStudent(RandomAccessFile f, Student blank) throws IOException {
int id;
System.out.println("Enter ID of student to remove: ");
id = keyboard.nextInt();
if(id >= 1 && id <= 100)
{
blank.setId(-1);
blank.setName("");
blank.setMajor("");
blank.setAge(0);
f.seek(blank.size() * (id - 1));
blank.write(f);
System.out.println("Removed student with id " + id );
}
else
System.out.println("Invalid id!");
}
private static void addStudent(RandomAccessFile f, Student blank) throws IOException {
int id, age;
String name, major;
System.out.print("Enter ID(1-100): ");
id = keyboard.nextInt();
System.out.print("Enter name: ");
name = keyboard.next().trim();
System.out.print("Enter age: ");
age = keyboard.nextInt();
System.out.print("Enter major: ");
major = keyboard.next();
blank.setName(name);
blank.setAge(age);
blank.setId(id);
blank.setMajor(major);
if(id >= 1 && id <= 100)
{
f.seek(blank.size() * (id - 1));
blank.read(f);
if(blank.getId() == -1)
{
f.seek(blank.size() * (id - 1));
blank.write(f);
System.out.println("Student with id " + id + " added ");
}
else
System.out.println("Student with id " + id + " already exists");
}
else
System.out.println("Invalid id!");
}
}

output


1. Add a Student record
2. Remove a Student record
3. Update a Student record
4. Print a Student record
5. Print all Student records
6. Quit
5
Displaying current list of students in file:
---------------------------------------------
---------------------------------------------
1. Add a Student record
2. Remove a Student record
3. Update a Student record
4. Print a Student record
5. Print all Student records
6. Quit
1
Enter ID(1-100): 5
Enter name: Michael
Enter age: 20
Enter major: CSE
Student with id 5 added
1. Add a Student record
2. Remove a Student record
3. Update a Student record
4. Print a Student record
5. Print all Student records
6. Quit
5
Displaying current list of students in file:
---------------------------------------------
5 Michael 20 CSE
---------------------------------------------
1. Add a Student record
2. Remove a Student record
3. Update a Student record
4. Print a Student record
5. Print all Student records
6. Quit
4
Enter the ID of student to print:
3
No student data
1. Add a Student record
2. Remove a Student record
3. Update a Student record
4. Print a Student record
5. Print all Student records
6. Quit
1
Enter ID(1-100): 3
Enter name: Bob
Enter age: 21
Enter major: Math
Student with id 3 added
1. Add a Student record
2. Remove a Student record
3. Update a Student record
4. Print a Student record
5. Print all Student records
6. Quit
5
Displaying current list of students in file:
---------------------------------------------
3 Bob 21 Math
5 Michael 20 CSE
---------------------------------------------
1. Add a Student record
2. Remove a Student record
3. Update a Student record
4. Print a Student record
5. Print all Student records
6. Quit
4
Enter the ID of student to print:
3
3 Bob 21 Math
1. Add a Student record
2. Remove a Student record
3. Update a Student record
4. Print a Student record
5. Print all Student records
6. Quit
3
Enter ID(1-100) of the student to be modified: 3
Enter the new details for the student
Enter name: Alice
Enter age: 22
Enter major: CSE
Student with id 3 updated
1. Add a Student record
2. Remove a Student record
3. Update a Student record
4. Print a Student record
5. Print all Student records
6. Quit
5
Displaying current list of students in file:
---------------------------------------------
3 Alice 22 CSE
5 Michael 20 CSE
---------------------------------------------
1. Add a Student record
2. Remove a Student record
3. Update a Student record
4. Print a Student record
5. Print all Student records
6. Quit
2
Enter ID of student to remove:
5
Removed student with id 5
1. Add a Student record
2. Remove a Student record
3. Update a Student record
4. Print a Student record
5. Print all Student records
6. Quit
5
Displaying current list of students in file:
---------------------------------------------
3 Alice 22 CSE
---------------------------------------------
1. Add a Student record
2. Remove a Student record
3. Update a Student record
4. Print a Student record
5. Print all Student records
6. Quit
6