Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: what is wrong with my code?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what is wrong with my code?

    the run does not like what i want and all the statment correct ??
    what the quastion want :
    (i) Task1: Complete the code, so your program will support insert, delete, and search. After you finish, make another copy of this program using unordered array to insert a new record at the end, sequential search.

    (ii) Task2: Implement a function called payRangeQuery that takes two double parameters low and high and print all records of employees whose pay is between low and high. The payRangeQuery function should be implemented as a public method in the RecordDB class.

    (iii) Task3: Write a search function that is similar to the currently implemented search function, except that the new search function returns an integer. If the given SSN is found, then the function returns a non-negative integer that is the index of the slot in which the SSN was found. If the given SSN is not found, then the function should return a negative integer, whose magnitude (absolute value) is the index of the slot in which the SSN would have been found, had it been in the array. For example, if the social security numbers in the array are: 10, 20, 30, 40 and we were looking for 22, and then the function should return -2 because if 22 were present in the array it would be in slot 2.

    (iv) Task4: Re-implement the “insert” and “delete” functions so that they call the new search function. You would have noticed that both insert and delete currently perform a linear scan of the array; insert does it in order to find a slot to insert the new record in and delete does it in order to find the record with the given SSN. Having implemented the new search function, it is possible to simplify and speed-up both insert and delete by making appropriate calls to the new “search” function.

    This is class RecordDB >>>>>>>>>
    PHP Code:
     /*
    import java.util.*;
    public class RecordDB {
            private static int maxRecords = 200;
        private int numRecords = 0;
        private Record recordList[] = new Record[maxRecords];
    //--------------------------------------------------------------------------------
    //Inserts a record into the ordered array in a sorted fashion.
    //Parameter rec:  The record to be inserted.
        public void insert (Record rec) {
    // Check for overflow; if overflow, then don't insert
            if (numRecords == maxRecords-1){
                return; }
                      else{
                        int o;
                        for(o=0;o<numRecords;o++){
                            if(recordList[o].SSN>rec.SSN){
                                break;
                            }
    //Find the position to insert in, by scanning the array left-to-right
    // until a large SSN is located
    // Then, Shift all records one space down
                            for(int j=numRecords-1;j>=o;j--){
                                recordList[j+1]=recordList[j]; //shifting
                            }
    // Then, Insert the new record
                            recordList[o]=rec;
                                numRecords++;      // Finally, Increment current number of employees in the list
                            } //end for first
                          }  //end else
    // Print All records
                    printAll();
                int v= search0(rec.SSN);// task 4
                for(int i=0;i<numRecords;i++){
                    if (i==v){
                        int p;
                        for( p=numRecords;p==i;p--){
                        recordList[p]=recordList[p+1];}
                        recordList[p]=rec[i];
                    }
                      else {
                        int positive= v*-1;
                        if (i==positive){
                            int p;
                        for( p=numRecords;p==i;p--){
                        recordList[p]=recordList[p+1];}
                        recordList[p]=rec[i];
                              }
                           }
                }
     }// end insert
            //---------------------------------------------------------------------------------    //Deletes a record from the list based on the key (SSN).
           // Performs a linear scan of the array of records until
           //a record is found with the given SSN
        //Parameter SSN
        public void delete (int newSSN) {

            //Check for underflow; if underflow, then don't delete
    if (numRecords==0){
        return;
    }
    // Scan the array searching for the record containing newSSN
    int c;
    for(c=0;c<numRecords;c++){
        if(recordList[c].SSN == newSSN){
          break;
        }
    }
     for(int j=numRecords-1;j>=c;j--){
          recordList[c]=recordList[c+1];  // Then, Shift all records that are beyond slot i to the left by one slot
     }
     numRecords--; // Finally, Decrement current number of employees in the list

        // Print All records
            printAll();
                    int v= search0(newSSN);// task 4
                for(int i=0;i<numRecords;i++){
                    if (i==v){
                       for(int u=i;u<numRecords;u--){
                       recordList[u]=recordList[u+1];}
                    }
                      else {
                        int positive= v*-1;
                        if (i==positive){
                            for(int u=i;u<numRecords;u--){
                                recordList[u]=recordList[u+1];}
                              }
                        else return;
                        }
                           }
        }
    //--------------------------------------------------------------------------------
        //Search for the record with the given SSN, Parameter newSSN
        public Record search (int newSSN) {
            // Print All records
                    printAll();
            // Complete this Binary Search algorithm
            int first=0, last=numRecords-1, mid=0;
                    boolean sure =false;
                    while (!sure && first<=last){
                        mid=(first+last)/2;
                        if(recordList[mid].SSN== newSSN ){
                            sure=true;
                                 }
                         else{
                               if( newSSN < recordList[mid].SSN){
                                last=mid-1;
                                   }
                                    else first=mid+1; 
                              }
                        }
                    if(sure)
                        return recordList[mid];
                    else
                        return null;
    } // end of search function
    //------------------TASK 2-------------------------------------------------------------
            public void payRangeQuery(double low,double high){
                   for(int r=0; low < recordList[r].pay && recordList[r].pay< high ;r++){
                      System.out.println(""+r+" "+recordList[r].SSN+
                "  "+recordList[r].name+" " +recordList[r].pay);
                   }
            }
       //-------------------------------------------------------------------------------
            public int search0 (int newSSN) {
            // Print All records
                    printAll();
            // Complete this Binary Search algorithm
            int first=0, last=numRecords-1, mid=0;
                    boolean sure =false;
                    while (!sure && first<=last){
                        mid=(first+last)/2;
                        if(recordList[mid].SSN== newSSN ){
                            sure=true;
                                 }
                         else{
                               if( newSSN < recordList[mid].SSN){
                                last=mid-1;
                                   }
                                    else first=mid+1; 
                              }
                        }
                    if(sure)
                        return mid;
                    else{
                        int q;
                        for( q=0; q<numRecords;q++){
                            if(recordList[q].SSN > recordList[q+1].SSN ){
                               break;
                            }
                        }
                        return -q;
                       }// end else
            } // ehd search function in task 3;
    //-------------------------------------------------------------------------------
        //Prints the list of records to the standard output;
        private void printAll() {

            System.out.println("---------------------------------");
                    System.out.println("i SSN Name pay");
            for (int i=0; i<numRecords; i++) {
                System.out.println(""+i+" "+recordList[i].SSN+
                "  "+recordList[i].name+" " +recordList[i].pay);
            }
    }

    -----------------------------------------------------------------
    This is class Record
    PHP Code:
    public class Record {
    // data members
        
    public  int SSN;    // Primary Key
        
    public String name;
        public 
    double pay;
        
    // The constructor for this class
        
    Record (int newSSNString newNamedouble newPay) {
            
    SSN newSSN;
            
    name newName;
            
    pay newPay;
        } 
    // end of constructor
    public int getSSN(){
        return 
    SSN;
    }
    public 
    String getName(){
        return 
    name;
    }
    public 
    double getPay(){
        return 
    pay;
    }

        
    int compareTo(Record record) {
            throw new 
    UnsupportedOperationException("Not yet implemented");
        }


    -----------------------------------------------------------------------------
    The main
    PHP Code:
    public class company {

        public static 
    void main(String[] args) {
               
    RecordDB recDB = new RecordDB();

            
    recDB.insert(new Record(3"John"500));
            
    recDB.insert(new Record(22"Mike"2500));
            
    recDB.insert(new Record(13"Mark"1760));
            
    recDB.insert(new Record(19"Bob"500));
            
    recDB.insert(new Record(7"Cathy"500));

                    
    Record r  recDB.search(22);
            if(
    == null)
                
    System.out.println("Not found");
            else
                
    System.out.println("Found");

            
    recDB.search(34);
            if(
    == null)
                
    System.out.println("Not found");
            else
                
    System.out.println("Found");

            
    recDB.delete(19);
            
    recDB.delete(7);
            
    recDB.delete(3);
            
    recDB.delete(13);
            
    recDB.delete(21);
            
    recDB.delete(22);
            
    recDB.delete(3);
        }
        } 


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is wrong with my code?

    Will not forgive anyone who takes a my work

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: what is wrong with my code?

    I am posting this link in the hopes it might help you phrase your posts in such a way that encourages contributors to want to help you...
    How To Ask Questions The Smart Way
    ...mainly because your post lacks a specific question, and semi-non-sense large-font posts can be considered rude by many, and will not help your chances (rather hurt them)

Similar Threads

  1. what's wrong with this code
    By gcsekhar in forum Java Networking
    Replies: 5
    Last Post: July 21st, 2011, 06:01 AM
  2. what's wrong with my code
    By gcsekhar in forum Java Networking
    Replies: 2
    Last Post: July 19th, 2011, 09:31 AM
  3. What is Wrong with my code
    By xew123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 04:59 AM
  4. What is wrong with my code???
    By nine05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 09:59 AM
  5. What is wrong with my code?
    By phantom06 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2011, 05:21 AM