Question

In: Computer Science

this is one assignment There will be a discussion in class about this assignment What to...

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

Solutions

Expert Solution

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


Related Solutions

It is like a class discussion and we're supposed to write a discussion about Vector spaces,...
It is like a class discussion and we're supposed to write a discussion about Vector spaces, subspaces and bases Overview (what to write on the discussion): So we're supposed to discuss and explain about these following points: Vector Spaces, Definitions, Examples, Non Examples Spanning Sets, Linear Independence Basis, Dimension Rank Change of Basis, Coordinates PUT EXAMPLES AND DEFINITIONS (EX: Showing examples to vector addition (closure under addition, etc) and scalar multiplication (distributive property, etc) Instructions: Add a new discussion topic....
2. Based on our discussion in class, what do you think about the ability for a...
2. Based on our discussion in class, what do you think about the ability for a corporation to bankrupt themselves in they are unable to pay their debts
YOU MUST COMPLETE THIS TO PASS THIS CLASS. Discussion Assignment: Referencing the ROME mnemonic listed below,...
YOU MUST COMPLETE THIS TO PASS THIS CLASS. Discussion Assignment: Referencing the ROME mnemonic listed below, prepare a factual submission article explaining the significance of the following lab results. Submit your results as your discussion contribution. Mrs. Breathless is a 42 year old female, just getting off a late shift. She reports to the ER in the early morning with shortness of breath. After several test, the following results are confirmed: ABG (arterial blood gases) lab results are: pH=7.44, PaCO2...
considered this discussion as practice for the assignment select one of the big 8 theorists under...
considered this discussion as practice for the assignment select one of the big 8 theorists under your unit 1 readings. discuss their contributions to the field of healthcare. how could you apply their ethical philosophy towards current issues facing healthcare?
For this assignment I need to do a discussion post about long term care insurance: Not...
For this assignment I need to do a discussion post about long term care insurance: Not too sure about this topic - The responds should be about two paragraphs. Consider the cost of long-term care insurance. Is there a cost-benefit to purchasing long-term care insurance? Would your assets be completely protected by this insurance option? Are there specific considerations for individuals or couples?
For this second class discussion, I want you to identify one vulnerable population that is at...
For this second class discussion, I want you to identify one vulnerable population that is at a high risk for being a victim of medication errors and answer the following questions: 1. What policies and procedures have been developed by hospitals and healthcare systems to help prevent medication errors for the vulnerable population you have chosen? 2. What steps can be taken by the healthcare team to ensure safe and proper medication administration for your chosen vulnerable population? 3. What...
Hello, I have an online discussion assignment due. and I don't have an idea about the...
Hello, I have an online discussion assignment due. and I don't have an idea about the questions. 1. Analyze economic choices and trade-offs 2. Analyze economic models. Would you help me with that? Typed answer is preferred. Thank you very much in advance! -------------------------------------------------------------------------- D1: Should Abortion Be Leagal Discussion Expectations Respond to at least two other peer postings.  Be substantive and follow up on your comments and questions. Do you agree or disagree and why.  Do you have questions based on...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main method. In the main method you should read in all of the rows of data from the Instructors Table in the Registration Database, then print out this data. Follow the steps outlined in class. Remember to use try-catch blocks for all database access code. The example in the book does not use Try-catch blocks, but you should
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.The Seller classUse the following class definition:class Seller { public:   Seller();   Seller( const char [], const char[], const char [], double );        void print();   void setFirstName( const char [] );   void setLastName( const char [] );   void setID( const char [] );   void setSalesTotal( double );   double getSalesTotal(); private:   char firstName[20];   char lastName[30];   char ID[7];   double salesTotal; };Data MembersThe data members for the class are:firstName holds the Seller's first namelastName holds the Seller's last nameID holds the Seller's id numbersalesTotal holds the Seller's sales totalConstructorsThis class has two constructors. The default constructor (the one that takes...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that will be part of an overall class named InstrumentDisplay. The classes are FuelGage, Odometer, MilesSinceLastGas, and CruisingRange. The FuelGage should assume a 15 gallon tank of gasoline and an average consumption of 1 gallon every 28 miles. It should increment in 1 gallon steps when you "add gas to the tank". It should decrement by 1 every 28 miles. It should display its current...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT