In: Computer Science
this is one assignment
There will be a discussion in class about this assignment
What to do.
Create a simple database with three fields : ID, LastName, FirstName all as Strings. You should define a class called StudentRecord or DataBaseRecord with three private String elements (the three fields), along with the appropriate methods. Here's what mine looked like:
public class StudentRecord{
private String fname;
private String lname;
private String ID;
DataBaseRecord(String f,String l,String i)
{
this.fname=f;
this.lname=l;
this.ID=i;
}
public String toString()
{
return(fname+" "+lname+" "+ID);
}
public int compareTo(Student otherStudent)
{
return(LastName.compareTo(otherStudent.LastName));
}
}
You should also declare an object called DataBaseArray which is an array of DatabaseRecords. Records of type DataBaseRecord should be added at the end of the DataBaseArray. Create an IndexRecord class:
public class IndexRecord {
private String key;
private int where;
//other stuff here
}
Now create an IndexArray. This is an array of IndexRecord and is to be implemented That is, insertions must maintain the order in the array, where order is maintained by the key value. Note that this means you need to define a compareTo method in the IndexRecord object.
The OrderedArray class was an array of objects of type Student.
To print out the array, we included a method called printIt() that
printed out the entire array. But suppose we wanted to do the
following: we wish to retrieve an element from the array, do
something with it, and then retrieve the next element in the array.
Why (or when) you might wish to do this will become apparent as you
work on the programming assignment.
We need an iterator.
An iterator is an element that is part of the OrderedArray object;
it is a current pointer to an element in the array. We can
initialize this pointer to the beginning of the array or the end.
We can advance the pointer forward or backward.
Finally, and most importantly, we can retrieve the element in array
referenced by the iterator. The declaration of an iterator in our
example would be:
public class OrderedArray
{
private Student data[ ];
private int nextElem;
private int maxSize;
private int theIterator;
//we add an iterator;
.......
We can initialize the iterator to reference the beginning of the
array or the end of the array. We agree that if the array is empty,
either initialization should set the iterator to -1:
public void setIteratorBegin()
{
theIterator=(nextElem>0? 0 : -1);
}
public void setIteratorEnd()
{
theIterator=(nextElem>0 ? nextElem-1 :
-1);
}
Finally, there are three methods that return an instance of a
Student:
/* getIterator returns a reference to the Student currently
referenced by theIterator
/* if theIterator==1 return -1
public Student getIterator()
{
return(theIterator==-1 ? null
:data[theIterator]);
}
/* advance theIterator. If we run off end, set to -1
/* if theIterator==-1 return null, else the reference to the entry
in the array
public Student getIteratorNext()
{
theIterator=(theIterator==nextElem-1? -1:
theIterator+1);
return(theIterator==-1? null :
data[theIterator]);
}
/* decrement theIterator. If we run off
the beginning of the array, set to -1
/* if theIterator==-1 return null, else the reference to the entry
in the array
public Student getIteratorPrev()
{
theIterator=(theIterator==0? -1:
theIterator-1);
return
(theIterator==-1?null:data[theIterator]);
}
So what does all this get us ? See the following code to traverse
the array foward or in reverse:
public class UseOrderedArray {
public static void main(String[] args)
{
Student s;
OrderedArray myArray=new
OrderedArray(20);
myArray.insert(new
Student("smith",16,(float)2.7));
myArray.insert(new
Student("adams",15,(float)3.1));
myArray.insert(new
Student("morris",17,(float)3.3));
// print in reverse
order
myArray.setIteratorEnd();
for(s=myArray.getIterator();
s!=null;s=myArray.getIteratorPrev())
System.out.println(s);
// print in forward
order
myArray.setIteratorBegin();
for(s=myArray.getIterator();
s!=null;s=myArray.getIteratorNext())
System.out.println(s);
}
}
The data! Please note the format is: last_name first_name student_ID Read into your own file Dunn Sean 31111 Duong Geoffrey 29922 Fazekas Nicholas 31100 Prezioso Stefano 22223 Puvvada Mohana 11224 Ravikumar Rakhi 11226 Salyers Matthew 11227 Gillespie William 49587 Hess Caleb 29282 Armatis Jared 34512 Beckman Allan 35176 Wang Zhen 22113 Wingett Jordan 12345 Belt Keith 34987 Bixler Tyler 22234 Chambers Quentin 22567 Chinni Adithya 28456 Donheiser Michael 28456 Kondrashov Mikhail 33331 Kraus Laura 33332 Krupp Phillip 49888 Maass John 44112 McCarty Amanda 44223 Moldovan Gregory 44335 Oshiyoye Adekunle 44556 Pagalos Frank 33112 Perski Zackery 33221 Saunders Jordan 77556 Simpson Ashlynne 77665 Szalai Kyle 33112 Witting Robert 21354
/**
* This will be the main driver program for many of your programs.
Specifically,
* you will need to define a data structure and related algorithms
to use with this program.
* We will be using the data file I have provied for you: a file of
68 records. Each record is composed
* of three fields:
* String lastName
* String firstName
* String ID
* ID may be implemented as an integer, but it is easier to
implement as a string. Both lastName and firstName
* may not be unique, but the ID **is** unique.
*
*
*/
import java.util.*;
public class COSC311Driver
{
public static void main(String[] args)
{
/*The following
declaration declares a data structure that will change from one
assignment to the next. For example, you will need to
implement
* the following as
a doubly linked list, as well as a tree.
*/
DataBase d=new
DataBase();
int response;
Scanner keyboard=new
Scanner(System.in);
/* Read the data into
the database from the external disk file here
* IMPORTANT:
duplicate ID numbers should not be added. Disregard
* the entire
record for duplicate IDs
*/
do
{
System.out.println(" 1 Add a new student");
System.out.println(" 2 Delete a student");
System.out.println(" 3 Find a student by ID");
System.out.println(" 4 List students by ID increasing");
System.out.println(" 5 List students by first name
increasing");
System.out.println(" 6 List students by last name
increasing");
System.out.println(" 7 List students by ID decreasing");
System.out.println(" 8 List students by first name
decreasing");
System.out.println(" 9 List students by last name
decreasing");
System.out.println(" ");
System.out.println(" 0 End");
response=keyboard.nextInt();
switch (response)
{
case 1: d.addIt(); //Note: if the user enters an ID
already in use, issue a warning and return to the menu
break;
case 2: d.deleteIt(); //Note: output either "Deleted" or "ID not
Found" and return to menu
break;
case 3: d.findIt(); //Note: output the entire record or
the message "ID not Found" and return to menu
break;
case 4:
d.ListByIDAscending();
break;
case 5: d.ListByFirstAscending();
break;
case 6: d.ListByLastAscending();
break;
case 7: d.ListByIDDescending();
break;
case 8: d.ListByFirstDescending();
break;
case 9: d.ListByLastDescending();
break;
default:
}
} while
(response!=0);
}
}
import java.util.*;
public class COSC311Driver
{
public static void main(String[] args)
{
/*The following
declaration declares a data structure that will change from one
assignment to the next. For example, you will need to
implement
* the following as
a doubly linked list, as well as a tree.
*/
DataBase d=new
DataBase();
int response;
Scanner keyboard=new
Scanner(System.in);
/* Read the data into
the database from the external disk file here
* IMPORTANT:
duplicate ID numbers should not be added. Disregard
* the entire
record for duplicate IDs
*/
do
{
System.out.println(" 1 Add a new student");
System.out.println(" 2 Delete a student");
System.out.println(" 3 Find a student by ID");
System.out.println(" 4 List students by ID increasing");
System.out.println(" 5 List students by first name
increasing");
System.out.println(" 6 List students by last name
increasing");
System.out.println(" 7 List students by ID decreasing");
System.out.println(" 8 List students by first name
decreasing");
System.out.println(" 9 List students by last name
decreasing");
System.out.println(" ");
System.out.println(" 0 End");
response=keyboard.nextInt();
switch (response)
{
case 1: d.addIt(); //Note: if the user enters an ID
already in use, issue a warning and return to the menu
break;
case 2: d.deleteIt(); //Note: output either "Deleted" or "ID not
Found" and return to menu
break;
case 3: d.findIt(); //Note: output the entire record or
the message "ID not Found" and return to menu
break;
case 4:
d.ListByIDAscending();
break;
case 5: d.ListByFirstAscending();
break;
case 6: d.ListByLastAscending();
break;
case 7: d.ListByIDDescending();
break;
case 8: d.ListByFirstDescending();
break;
case 9: d.ListByLastDescending();
break;
default:
}
} while
(response!=0);
}
}
by far no error in this code