Question

In: Computer Science

).Modify the project so that records are inserted into the random access file in ascending order...

).Modify the project so that records are inserted into the random access file in ascending order using an insertion sort methodology with the Social Security Number 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 writes d into the file.

2. With the new organization of the data files. find() and modify() must also be modified. Both methods should now stop their sequential search when they encounter a record greater than the record looked for, or they reach the end of the file.

Can somebody check the compareTo() and help me out with the add() method in this question.

Personal.java

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Comparator;

public class Personal extends IOmethods implements DbObject, Comparable {
   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 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);
   }

   // compare by ssn
   public static Comparator ssnComparator = new Comparator() {

       public int compare(Personal p1, Personal p2) {
           String ssnP1 = p1.SSN.toUpperCase();
           String ssnP2 = p2.SSN.toUpperCase();

           // ascending order
           return ssnP1.compareTo(ssnP2);

       }
   };

   @Override
   public int compareTo(Personal o) {

       if (Integer.parseInt(this.SSN) < Integer.parseInt(o.SSN)) {

           return -1;
       } else if (Integer.parseInt(this.SSN) > Integer.parseInt(SSN)) {

           return 1;
       } else {
           return 0;
       }

   }
}


Student.java

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);
}
public int compareTo() {
  
}
}

DbObject.java

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();
}

IOmethods,java

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 == '\n') // end of file or end of line;
break;
else if ((char)ch != '\r') // ignore carriage return;
s = s + (char)ch;
}
return s;
}
}
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();
}
}
}

UseDatabase.java

import java.io.IOException;

public class UseDatabase {
static public void main(String a[]) throws IOException {
// (new Database()).run(new Personal());
(new Database()).run(new Student());
}
}

Solutions

Expert Solution

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Comparator;

public class Personal extends IOmethods implements DbObject, Comparable {
   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 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);
   }

   // compare by ssn
   public static Comparator ssnComparator = new Comparator() {

       public int compare(Personal p1, Personal p2) {
           String ssnP1 = p1.SSN.toUpperCase();
           String ssnP2 = p2.SSN.toUpperCase();

           // ascending order
           return ssnP1.compareTo(ssnP2);

       }
   };

  

@Override
   public int compareTo(Personal o) {

// first convert the integers into i1 and i2

//use the inbuilt compareTo() in the Integer classs

   Integer i1=Integer.parseInt(this.SSN);

   Integer i2=Integer.parseInt(SSN);

   return i1.compareTo(i2);

   }

}

add methods looks good in the program. Please let me know if you need any changes

Note : If you like my answer please rate and help me it is very Imp for me


Related Solutions

Modify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order.
Programming Language : JavaModify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order.NOTE: Do not move the last element into the first element of the array. You must treat the algorithm as if the pivot is actually sitting in the last location of the array.After it has been sorted in descending order, go through all the items in the array and make sure...
Modify this program so that it takes in input from a TEXT FILE and outputs the...
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)! Program works just need to modify it to take in input from a text file and output the results in an output file. ________________________________________________________________________________________________ #include <stdio.h> #include <string.h> // Maximum string size #define MAX_SIZE 1000 int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; char ch; int count,len,a[26]={0},p[MAX_SIZE]={0},temp; int i,j; //Take...
describe the mechanism that supports random file access in Java.
describe the mechanism that supports random file access in Java.
C++ 1. Modify this program to open the file "Customers.dat" so that all data is written...
C++ 1. Modify this program to open the file "Customers.dat" so that all data is written to the end of the file AND to allow the file to be read. 2. Create a method called "getLargestCustomerNumber" and call it after the "Customers.dat" file has been opened. Read all the existing customerNumbers and determine the largest customerNumber - do not assume the last record in the file is the largest number. Use this number as the base when adding new customer...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates the Fibonacci sequence and writes the sequence to the shared-memory object. The Consumer.c file should then output the sequence. Producer.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/mman.h> #include <zconf.h> int main() { /* The size (in bytes) of shared-memory object */ const int SIZE = 4096; /* The name of shared-memory object */ const char *Obj =...
in JAVA, Project: Displaying Words in Ascending Alphabetical Order Write a program that reads words from...
in JAVA, Project: Displaying Words in Ascending Alphabetical Order Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. 1. You must use one of following Concrete class to store date from the input.txt file. Vector, Stack, ArrayList, LinkedList 2. To sort those words, you should use one of existing interface methods available in Collection class.
For each of the following file processing operations, indicate whether a sequential file, indexed random file, virtual storage access method, hashing,
For each of the following file processing operations, indicate whether a sequential file, indexed random file, virtual storage access method, hashing, or pointer structure would work best. You may choose as many as you wish for each step. Also, indicate which would perform the least optimally.a. Retrieve a record from the file based on its primary key value.b. Update a record in the file. c. Read a complete file of records. d. Find the next record in a file. e....
•Modify p4.c so that the output file p4.output is created but also displayed to standard output...
•Modify p4.c so that the output file p4.output is created but also displayed to standard output ( the screen ). This should be done by another instance of exec(). •Implement the pipe() command to do the following: $> grep –o else p4.c | wc –l p4.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include <sys/wait.h> int main(int argc, char *argv[]) { int rc = fork(); if (rc < 0) {     // fork failed     fprintf(stderr, "fork...
Why is a device such as a tape-drive a poor secondary storage device for Random/Direct access to a file?
Why is a device such as a tape-drive a poor secondary storage device for Random/Direct access to a file?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT