SlideShare a Scribd company logo
Modify the project so tat records are inserted into the random acess file in ascending order using
an insertion sort methodology with the SSN acting as the key value. This requires defining the
method compareTo() in the Personal and Student classes to be used in a modified method add()
in Database. The method finds a proper position for a record d, moves all the records in the file
to make room for d, and writres d into the file. With the new organization of the data files, find()
and modify() can also be modified. For example, find() stops its sequential search when it
encounters a record greater than the record looked for (or reaches the end of the file).
Database.java
import java.io.*;
public class Database {
private RandomAccessFile database;
private String fName = new String();;
private IOmethods io = new IOmethods();
Database() throws IOException {
System.out.print("File name: ");
fName = io.readLine();
}
private void add(DbObject d) throws IOException {
database = new RandomAccessFile(fName,"rw");
database.seek(database.length());
d.writeToFile(database);
database.close();
}
private void modify(DbObject d) throws IOException {
DbObject[] tmp = new DbObject[1];
d.copy(tmp);
database = new RandomAccessFile(fName,"rw");
while (database.getFilePointer() < database.length()) {
tmp[0].readFromFile(database);
if (tmp[0].equals(d)) {
tmp[0].readFromConsole();
database.seek(database.getFilePointer()-d.size());
tmp[0].writeToFile(database);
database.close();
return;
}
}
database.close();
System.out.println("The record to be modified is not in the database");
}
private boolean find(DbObject d) throws IOException {
DbObject[] tmp = new DbObject[1];
d.copy(tmp);
database = new RandomAccessFile(fName,"r");
while (database.getFilePointer() < database.length()) {
tmp[0].readFromFile(database);
if (tmp[0].equals(d)) {
database.close();
return true;
}
}
database.close();
return false;
}
private void printDb(DbObject d) throws IOException {
database = new RandomAccessFile(fName,"r");
while (database.getFilePointer() < database.length()) {
d.readFromFile(database);
d.writeLegibly();
System.out.println();
}
database.close();
}
public void run(DbObject rec) throws IOException {
String option;
System.out.println("1. Add 2. Find 3. Modify a record; 4. Exit");
System.out.print("Enter an option: ");
option = io.readLine();
while (true) {
if (option.charAt(0) == '1') {
rec.readFromConsole();
add(rec);
}
else if (option.charAt(0) == '2') {
rec.readKey();
System.out.print("The record is ");
if (find(rec) == false)
System.out.print("not ");
System.out.println("in the database");
}
else if (option.charAt(0) == '3') {
rec.readKey();
modify(rec);
}
else if (option.charAt(0) != '4')
System.out.println("Wrong option");
else return;
printDb(rec);
System.out.print("Enter an option: ");
option = io.readLine();
}
}
}
import java.io.*;
public interface DbObject {
public void writeToFile(RandomAccessFile out) throws IOException;
public void readFromFile(RandomAccessFile in) throws IOException;
public void readFromConsole() throws IOException;
public void writeLegibly() throws IOException;
public void readKey() throws IOException;
public void copy(DbObject[] db);
public int size();
}
import java.io.*;
public class IOmethods {
public void writeString(String s, RandomAccessFile out) throws IOException {
for (int i = 0; i < s.length(); i++)
out.writeChar(s.charAt(i));
}
public String readString(int len, RandomAccessFile in) throws IOException {
String s = "";
for (int i = 0; i < len; i++)
s += in.readChar();
return s;
}
public String readLine() throws IOException {
int ch;
String s = "";
while (true) {
ch = System.in.read();
if (ch == -1 || (char)ch == ' ') // end of file or end of line;
break;
else if ((char)ch != ' ') // ignore carriage return;
s = s + (char)ch;
}
return s;
}
}
import java.io.*;
public class Personal extends IOmethods implements DbObject {
protected final int nameLen = 10, cityLen = 10;
protected String SSN, name, city;
protected int year;
protected long salary;
protected final int size = 9*2 + nameLen*2 + cityLen*2 + 4 + 8;
Personal() {
}
Personal(String ssn, String n, String c, int y, long s) {
SSN = ssn; name = n; city = c; year = y; salary = s;
}
public int size() {
return size;
}
public boolean equals(Object pr) {
return SSN.equals(((Personal)pr).SSN);
}
public void writeToFile(RandomAccessFile out) throws IOException {
writeString(SSN,out);
writeString(name,out);
writeString(city,out);
out.writeInt(year);
out.writeLong(salary);
}
public void writeLegibly() {
System.out.print("SSN = " + SSN + ", name = " + name.trim()
+ ", city = " + city.trim() + ", year = " + year
+ ", salary = " + salary);
}
public void readFromFile(RandomAccessFile in) throws IOException {
SSN = readString(9,in);
name = readString(nameLen,in);
city = readString(cityLen,in);
year = in.readInt();
salary = in.readLong();
}
public void readKey() throws IOException {
System.out.print("Enter SSN: ");
SSN = readLine();
}
public void readFromConsole() throws IOException {
System.out.print("Enter SSN: ");
SSN = readLine();
System.out.print("Name: ");
name = readLine();
for (int i = name.length(); i < nameLen; i++)
name += ' ';
System.out.print("City: ");
city = readLine();
for (int i = city.length(); i < cityLen; i++)
city += ' ';
System.out.print("Birthyear: ");
year = Integer.valueOf(readLine().trim()).intValue();
System.out.print("Salary: ");
salary = Long.valueOf(readLine().trim()).longValue();
}
public void copy(DbObject[] d) {
d[0] = new Personal(SSN,name,city,year,salary);
}
}
import java.io.*;
public class Student extends Personal {
public int size() {
return super.size() + majorLen*2;
}
protected String major;
protected final int majorLen = 10;
Student() {
super();
}
Student(String ssn, String n, String c, int y, long s, String m) {
super(ssn,n,c,y,s);
major = m;
}
public void writeToFile(RandomAccessFile out) throws IOException {
super.writeToFile(out);
writeString(major,out);
}
public void readFromFile(RandomAccessFile in) throws IOException {
super.readFromFile(in);
major = readString(majorLen,in);
}
public void readFromConsole() throws IOException {
super.readFromConsole();
System.out.print("Enter major: ");
major = readLine();
for (int i = major.length(); i < nameLen; i++)
major += ' ';
}
public void writeLegibly() {
super.writeLegibly();
System.out.print(", major = " + major.trim());
}
public void copy(DbObject[] d) {
d[0] = new Student(SSN,name,city,year,salary,major);
}
}
import java.io.*;
public class UseDatabase {
static public void main(String a[]) throws IOException {
// (new Database()).run(new Personal());
(new Database()).run(new Student());
}
}
Solution
public category information non-public RandomAccessFile database;
personal String fName = new String();;
personal IOmethods io = new IOmethods();
Database() throws IOException
}
database.close();
System.out.println("The record to be changed isn't within the database");
}
personal Boolean find(DbObject d) throws IOException {
DbObject[] tmp = new DbObject[1];
d.copy(tmp);
information = new RandomAccessFile(fName,"r");
whereas (database.getFilePointer() < information.length()) come back true;
}
}
database.close();
come back false;
}
personal void printDb(DbObject d) throws IOException {
information = new RandomAccessFile(fName,"r");
whereas (database.getFilePointer() < information.length())
database.close();
}
public void run(DbObject rec) throws IOException
else if (option.charAt(0) == '2')
else if (option.charAt(0) != '4')
System.out.println("Wrong option");
else return;
printDb(rec);
System.out.print("Enter AN option: ");
possibility = io.readLine();
}
}
}
import java.io.*;
public interface DbObject
import java.io.*;
public category IOmethods
public String readString(int len, RandomAccessFile in) throws IOException (char)ch == ' ') //
{end of file or finish of line;
break;
else if ((char)ch != ' ') // ignore carriage return;
s = s + (char)ch;
}
return s;
}
}
import java.io.*;
public category Personal extends IOmethods implements DbObject ten, cityLen = 10;
protected String SSN, name, city;
protected int year;
protected long salary;
protected final int size = 9*2 + nameLen*2 + cityLen*2 + four + 8;
Personal()
Personal(String ssn, String n, String c, int y, long s) town = c; year = y; wage = s;
}
public int size() {
come back size;
}
public Boolean equals(Object pr)
public void writeToFile(RandomAccessFile out) throws IOException
public void writeLegibly()
}
import java.io.*;
public category Student extends Personal {
public int size() {
come back super.size() + majorLen*2;
}
protected String major;
protected final int majorLen = 10;
Student()
Student(String ssn, String n, String c, int y, long s, String m)
public void writeToFile(RandomAccessFile out) throws IOException
public void readFromFile(RandomAccessFile in) throws IOException
public void readFromConsole() throws IOException
public void copy(DbObject[] d)
}
import java.io.*;
public category UseDatabase
}

More Related Content

PDF
Application-Specific Models and Pointcuts using a Logic Meta Language
ESUG
 
PPTX
Anti patterns
Alex Tumanoff
 
PPSX
What's New In C# 7
Paulo Morgado
 
PDF
Not sure why my program wont run.Programmer S.Villegas helper N.pdf
wasemanivytreenrco51
 
PPT
3 database-jdbc(1)
hameedkhan2017
 
PDF
Please fix the java code (using eclipse)package hw4p1;import jav.pdf
info961251
 
PDF
An introduction into Spring Data
Oliver Gierke
 
PDF
Modify this code to change the underlying data structure to .pdf
adityaenterprise32
 
Application-Specific Models and Pointcuts using a Logic Meta Language
ESUG
 
Anti patterns
Alex Tumanoff
 
What's New In C# 7
Paulo Morgado
 
Not sure why my program wont run.Programmer S.Villegas helper N.pdf
wasemanivytreenrco51
 
3 database-jdbc(1)
hameedkhan2017
 
Please fix the java code (using eclipse)package hw4p1;import jav.pdf
info961251
 
An introduction into Spring Data
Oliver Gierke
 
Modify this code to change the underlying data structure to .pdf
adityaenterprise32
 

Similar to Modify the project so tat records are inserted into the random acess.pdf (20)

KEY
Node.js - Best practices
Felix Geisendörfer
 
PDF
What is the proper pseudocode for the following java codeimport j.pdf
artimagein
 
PDF
Basic file operations CBSE class xii ln 7
SATHASIVAN H
 
PDF
Sam wd programs
Soumya Behera
 
PPTX
Introduzione a C#
Lorenz Cuno Klopfenstein
 
PPTX
Why Sifu
LambdaWorks
 
PPTX
Why Sifu?
Sifu
 
PDF
JS Fest 2019 Node.js Antipatterns
Timur Shemsedinov
 
PDF
@author public class Person{   String sname, .pdf
aplolomedicalstoremr
 
PPTX
Groovy vs Boilerplate and Ceremony Code
stasimus
 
PPT
Oop lecture9 13
Shahriar Robbani
 
PDF
Data access 2.0? Please welcome: Spring Data!
Oliver Gierke
 
PDF
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
arjuncorner565
 
PDF
Active Software Documentation using Soul and IntensiVE
kim.mens
 
PPT
Tips and Tricks of Developing .NET Application
Joni
 
PDF
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
 
PPTX
Understanding java streams
Shahjahan Samoon
 
DOCX
Dotnet 18
dhruvesh718
 
PDF
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
mallik3000
 
PDF
Dpilot Source Code With ScreenShots
DeepAnshu Sharma
 
Node.js - Best practices
Felix Geisendörfer
 
What is the proper pseudocode for the following java codeimport j.pdf
artimagein
 
Basic file operations CBSE class xii ln 7
SATHASIVAN H
 
Sam wd programs
Soumya Behera
 
Introduzione a C#
Lorenz Cuno Klopfenstein
 
Why Sifu
LambdaWorks
 
Why Sifu?
Sifu
 
JS Fest 2019 Node.js Antipatterns
Timur Shemsedinov
 
@author public class Person{   String sname, .pdf
aplolomedicalstoremr
 
Groovy vs Boilerplate and Ceremony Code
stasimus
 
Oop lecture9 13
Shahriar Robbani
 
Data access 2.0? Please welcome: Spring Data!
Oliver Gierke
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
arjuncorner565
 
Active Software Documentation using Soul and IntensiVE
kim.mens
 
Tips and Tricks of Developing .NET Application
Joni
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
 
Understanding java streams
Shahjahan Samoon
 
Dotnet 18
dhruvesh718
 
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
mallik3000
 
Dpilot Source Code With ScreenShots
DeepAnshu Sharma
 
Ad

More from fcaindore (20)

PDF
You are the CIO of a medium size company tasked with modernizing the.pdf
fcaindore
 
PDF
Why are some goods only provided by the government Why are .pdf
fcaindore
 
PDF
When a supervisor comes to the HR manager to evaluate disciplinary a.pdf
fcaindore
 
PDF
What is the role of an ethical leader in corporate cultures a. A le.pdf
fcaindore
 
PDF
What is the main purpose of a project management planSolution.pdf
fcaindore
 
PDF
What is the difference between a balanace sheet and a net income she.pdf
fcaindore
 
PDF
What is involved in personalization and codification of tacit to exp.pdf
fcaindore
 
PDF
What dimensions of quality were highlighted in the Delta Airlines ba.pdf
fcaindore
 
PDF
What are the similarities and differences between the ETC of Photosy.pdf
fcaindore
 
PDF
What are non-tax costs of tax planningSolutionFollowings are .pdf
fcaindore
 
PDF
There is a host of sociological and cultural research that paints a r.pdf
fcaindore
 
PDF
True or False With argument passage by reference, the address of the.pdf
fcaindore
 
PDF
This is problem is same problem which i submitted on 22017, I just.pdf
fcaindore
 
PDF
SOme of functions of the eukaryotic orgenelles are performed in bact.pdf
fcaindore
 
PDF
please explain the global entreprenurship revolution for a flatter w.pdf
fcaindore
 
PDF
ourse O D. growth rate of currency in circulation-growth rate of the .pdf
fcaindore
 
PDF
Many hospitals have systems in place and are now or will in the futu.pdf
fcaindore
 
PDF
List the S + D and the organism that causes a vesicle, Gumma, purule.pdf
fcaindore
 
PDF
Let k be the number of ON-state devices in a group of n devices on a .pdf
fcaindore
 
PDF
In Java, write an assignment statement that takes the fifth power of.pdf
fcaindore
 
You are the CIO of a medium size company tasked with modernizing the.pdf
fcaindore
 
Why are some goods only provided by the government Why are .pdf
fcaindore
 
When a supervisor comes to the HR manager to evaluate disciplinary a.pdf
fcaindore
 
What is the role of an ethical leader in corporate cultures a. A le.pdf
fcaindore
 
What is the main purpose of a project management planSolution.pdf
fcaindore
 
What is the difference between a balanace sheet and a net income she.pdf
fcaindore
 
What is involved in personalization and codification of tacit to exp.pdf
fcaindore
 
What dimensions of quality were highlighted in the Delta Airlines ba.pdf
fcaindore
 
What are the similarities and differences between the ETC of Photosy.pdf
fcaindore
 
What are non-tax costs of tax planningSolutionFollowings are .pdf
fcaindore
 
There is a host of sociological and cultural research that paints a r.pdf
fcaindore
 
True or False With argument passage by reference, the address of the.pdf
fcaindore
 
This is problem is same problem which i submitted on 22017, I just.pdf
fcaindore
 
SOme of functions of the eukaryotic orgenelles are performed in bact.pdf
fcaindore
 
please explain the global entreprenurship revolution for a flatter w.pdf
fcaindore
 
ourse O D. growth rate of currency in circulation-growth rate of the .pdf
fcaindore
 
Many hospitals have systems in place and are now or will in the futu.pdf
fcaindore
 
List the S + D and the organism that causes a vesicle, Gumma, purule.pdf
fcaindore
 
Let k be the number of ON-state devices in a group of n devices on a .pdf
fcaindore
 
In Java, write an assignment statement that takes the fifth power of.pdf
fcaindore
 
Ad

Recently uploaded (20)

DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
CDH. pptx
AneetaSharma15
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Basics and rules of probability with real-life uses
ravatkaran694
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
CDH. pptx
AneetaSharma15
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 

Modify the project so tat records are inserted into the random acess.pdf

  • 1. Modify the project so tat records are inserted into the random acess file in ascending order using an insertion sort methodology with the SSN acting as the key value. This requires defining the method compareTo() in the Personal and Student classes to be used in a modified method add() in Database. The method finds a proper position for a record d, moves all the records in the file to make room for d, and writres d into the file. With the new organization of the data files, find() and modify() can also be modified. For example, find() stops its sequential search when it encounters a record greater than the record looked for (or reaches the end of the file). Database.java import java.io.*; public class Database { private RandomAccessFile database; private String fName = new String();; private IOmethods io = new IOmethods(); Database() throws IOException { System.out.print("File name: "); fName = io.readLine(); } private void add(DbObject d) throws IOException { database = new RandomAccessFile(fName,"rw"); database.seek(database.length()); d.writeToFile(database); database.close(); } private void modify(DbObject d) throws IOException { DbObject[] tmp = new DbObject[1]; d.copy(tmp); database = new RandomAccessFile(fName,"rw"); while (database.getFilePointer() < database.length()) { tmp[0].readFromFile(database); if (tmp[0].equals(d)) { tmp[0].readFromConsole(); database.seek(database.getFilePointer()-d.size()); tmp[0].writeToFile(database); database.close(); return;
  • 2. } } database.close(); System.out.println("The record to be modified is not in the database"); } private boolean find(DbObject d) throws IOException { DbObject[] tmp = new DbObject[1]; d.copy(tmp); database = new RandomAccessFile(fName,"r"); while (database.getFilePointer() < database.length()) { tmp[0].readFromFile(database); if (tmp[0].equals(d)) { database.close(); return true; } } database.close(); return false; } private void printDb(DbObject d) throws IOException { database = new RandomAccessFile(fName,"r"); while (database.getFilePointer() < database.length()) { d.readFromFile(database); d.writeLegibly(); System.out.println(); } database.close(); } public void run(DbObject rec) throws IOException { String option; System.out.println("1. Add 2. Find 3. Modify a record; 4. Exit"); System.out.print("Enter an option: "); option = io.readLine(); while (true) { if (option.charAt(0) == '1') { rec.readFromConsole();
  • 3. add(rec); } else if (option.charAt(0) == '2') { rec.readKey(); System.out.print("The record is "); if (find(rec) == false) System.out.print("not "); System.out.println("in the database"); } else if (option.charAt(0) == '3') { rec.readKey(); modify(rec); } else if (option.charAt(0) != '4') System.out.println("Wrong option"); else return; printDb(rec); System.out.print("Enter an option: "); option = io.readLine(); } } } import java.io.*; public interface DbObject { public void writeToFile(RandomAccessFile out) throws IOException; public void readFromFile(RandomAccessFile in) throws IOException; public void readFromConsole() throws IOException; public void writeLegibly() throws IOException; public void readKey() throws IOException; public void copy(DbObject[] db); public int size(); } import java.io.*; public class IOmethods { public void writeString(String s, RandomAccessFile out) throws IOException { for (int i = 0; i < s.length(); i++)
  • 4. out.writeChar(s.charAt(i)); } public String readString(int len, RandomAccessFile in) throws IOException { String s = ""; for (int i = 0; i < len; i++) s += in.readChar(); return s; } public String readLine() throws IOException { int ch; String s = ""; while (true) { ch = System.in.read(); if (ch == -1 || (char)ch == ' ') // end of file or end of line; break; else if ((char)ch != ' ') // ignore carriage return; s = s + (char)ch; } return s; } } import java.io.*; public class Personal extends IOmethods implements DbObject { protected final int nameLen = 10, cityLen = 10; protected String SSN, name, city; protected int year; protected long salary; protected final int size = 9*2 + nameLen*2 + cityLen*2 + 4 + 8; Personal() { } Personal(String ssn, String n, String c, int y, long s) { SSN = ssn; name = n; city = c; year = y; salary = s; } public int size() { return size; }
  • 5. public boolean equals(Object pr) { return SSN.equals(((Personal)pr).SSN); } public void writeToFile(RandomAccessFile out) throws IOException { writeString(SSN,out); writeString(name,out); writeString(city,out); out.writeInt(year); out.writeLong(salary); } public void writeLegibly() { System.out.print("SSN = " + SSN + ", name = " + name.trim() + ", city = " + city.trim() + ", year = " + year + ", salary = " + salary); } public void readFromFile(RandomAccessFile in) throws IOException { SSN = readString(9,in); name = readString(nameLen,in); city = readString(cityLen,in); year = in.readInt(); salary = in.readLong(); } public void readKey() throws IOException { System.out.print("Enter SSN: "); SSN = readLine(); } public void readFromConsole() throws IOException { System.out.print("Enter SSN: "); SSN = readLine(); System.out.print("Name: "); name = readLine(); for (int i = name.length(); i < nameLen; i++) name += ' '; System.out.print("City: "); city = readLine(); for (int i = city.length(); i < cityLen; i++)
  • 6. city += ' '; System.out.print("Birthyear: "); year = Integer.valueOf(readLine().trim()).intValue(); System.out.print("Salary: "); salary = Long.valueOf(readLine().trim()).longValue(); } public void copy(DbObject[] d) { d[0] = new Personal(SSN,name,city,year,salary); } } import java.io.*; public class Student extends Personal { public int size() { return super.size() + majorLen*2; } protected String major; protected final int majorLen = 10; Student() { super(); } Student(String ssn, String n, String c, int y, long s, String m) { super(ssn,n,c,y,s); major = m; } public void writeToFile(RandomAccessFile out) throws IOException { super.writeToFile(out); writeString(major,out); } public void readFromFile(RandomAccessFile in) throws IOException { super.readFromFile(in); major = readString(majorLen,in); } public void readFromConsole() throws IOException { super.readFromConsole(); System.out.print("Enter major: "); major = readLine();
  • 7. for (int i = major.length(); i < nameLen; i++) major += ' '; } public void writeLegibly() { super.writeLegibly(); System.out.print(", major = " + major.trim()); } public void copy(DbObject[] d) { d[0] = new Student(SSN,name,city,year,salary,major); } } import java.io.*; public class UseDatabase { static public void main(String a[]) throws IOException { // (new Database()).run(new Personal()); (new Database()).run(new Student()); } } Solution public category information non-public RandomAccessFile database; personal String fName = new String();; personal IOmethods io = new IOmethods(); Database() throws IOException } database.close(); System.out.println("The record to be changed isn't within the database"); } personal Boolean find(DbObject d) throws IOException { DbObject[] tmp = new DbObject[1]; d.copy(tmp); information = new RandomAccessFile(fName,"r"); whereas (database.getFilePointer() < information.length()) come back true; } }
  • 8. database.close(); come back false; } personal void printDb(DbObject d) throws IOException { information = new RandomAccessFile(fName,"r"); whereas (database.getFilePointer() < information.length()) database.close(); } public void run(DbObject rec) throws IOException else if (option.charAt(0) == '2') else if (option.charAt(0) != '4') System.out.println("Wrong option"); else return; printDb(rec); System.out.print("Enter AN option: "); possibility = io.readLine(); } } } import java.io.*; public interface DbObject import java.io.*; public category IOmethods public String readString(int len, RandomAccessFile in) throws IOException (char)ch == ' ') // {end of file or finish of line;
  • 9. break; else if ((char)ch != ' ') // ignore carriage return; s = s + (char)ch; } return s; } } import java.io.*; public category Personal extends IOmethods implements DbObject ten, cityLen = 10; protected String SSN, name, city; protected int year; protected long salary; protected final int size = 9*2 + nameLen*2 + cityLen*2 + four + 8; Personal() Personal(String ssn, String n, String c, int y, long s) town = c; year = y; wage = s; } public int size() { come back size; } public Boolean equals(Object pr) public void writeToFile(RandomAccessFile out) throws IOException public void writeLegibly() }
  • 10. import java.io.*; public category Student extends Personal { public int size() { come back super.size() + majorLen*2; } protected String major; protected final int majorLen = 10; Student() Student(String ssn, String n, String c, int y, long s, String m) public void writeToFile(RandomAccessFile out) throws IOException public void readFromFile(RandomAccessFile in) throws IOException public void readFromConsole() throws IOException public void copy(DbObject[] d) } import java.io.*; public category UseDatabase }