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 5 of 5

Thread: data structure assignment .. help me : |

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default data structure assignment .. help me : |

    Hi all .
    I have home work in data structure and I understood a little part of it
    so I need your experience

    this is the idea and the question



    Program Description

    Consider a program that manages the payroll of some organization. The three simplest operations performed by this program include:
    (i) adding a new employee to the payroll,
    (ii) searching for an employee, given some information about her, and
    (iii) deleting an employee who has left the organization.

    To keep things simple, let us assume that each employee record consists of three fields:
    (i) name,
    (ii) social security number (ssn), and
    (iii) salary.

    The three operations mentioned above, can be stated more precisely as follows: INSERT(record): to insert the given employee record into the collection of employee records.
     DELETE(ssn): to delete the employee whose ssn is given, from the collection.
     SEARCH(ssn): to search the collection for the employee whose ssn is given.

    Program Specifications

    We need to decide how the collection of records is actually stored in memory and how the above operations will be implemented. This kind of specification defines an abstract data type (ADT). Typically, an ADT can be implemented using one of several different data structures. A useful first step in deciding what data structure to use in a program is to specify an ADT for the program.

    Now we consider two alternate data structures for the above ADT:
    (i) Unordered array of records: the employee records are stored in an array, in no particular order. There is a variable, say n, that keeps track of the number of employees currently on the payroll.
    (ii) Ordered array of records: the employee records are stored in an array, in increasing order of ssn. Again, there is a variable, say n, that keeps track of the number of employees currently on the payroll.

    Tasks

    Referring to the provided Java code segments (pages 3-5) complete the following tasks (Whenever you see TO DO: add your code):

    (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.


    Record.java
    public class Record {
    	// data members
    	public int ssn;	// Primary Key
    	public String name;
    	public double pay;
    	// The constructor for this class
    	Record (int newSSN, String newName, double newPay) {
    		ssn = newSSN;
    		name = newName;
    		pay = newPay;
    	} // end of constructor
    } // end of class
     
    //--------------------------------------------------------------------------------
    RecordDB.java
    /*********************************************************
     * A class containing list of records with insert/delete *
     * and search functionality. 					   *
     *********************************************************/
    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;	
    		}
     
    		// TO DO: 
    	// 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
    		// Then, Insert the new record
    		// Finally, Increment current number of employees in the list	
    		// Print All records
    		printAll();
    	}
    //--------------------------------------------------------------------------------	//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) {
     
    		// TO DO:
    		//Check for underflow; if underflow, then don't delete
     
     
    		// TO DO:
    		// Scan the array searching for the record containing newSSN
    		// Then, Shift all records that are beyond slot i to the left by one slot
    		// Finally, Decrement current number of employees in the list	
     
     
    	// Print All records
    		printAll();
     
    	}
    //--------------------------------------------------------------------------------
    	//Search for the record with the given SSN, Parameter newSSN
    	public Record search (int newSSN) {
    		// Print All records
    		printAll();
     
    		// TO DO:
    		// Complete this Binary Search algorithm
    		int first=0, last=numRecords-1;
     
    	} // end of search function
    //-------------------------------------------------------------------------------
    	//Prints the list of records to the standard output;
    	private void printAll() {
     
    		System.out.println("---------------------------------");
    		for (int i=0; i<numRecords; i++) {
    			System.out.println(""+i+" "+recordList[i].ssn+
    			"  "+recordList[i].name+" " +recordList[i].pay);
    		}
    	}
    //--------------------------------------------------------------------------------
     
    // Main method - adds and deletes records and searches for some.
    	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(r == null)
    			System.out.println("Not found");
    		else 
    			System.out.println("Found");
     
    		r = recDB.search(34);
    		if(r == 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);
    	}
    }

    please help me in code writing
    Last edited by Noni; March 19th, 2011 at 11:07 AM.


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

    Default Re: data structure assignment .. help me : |

    where are you ..? I am really need you

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: data structure assignment .. help me : |

    i really have the same HW

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

    Default Re: data structure assignment .. help me : |

    where are you ..? I am really need you
    Everyone that contributes to the forums does so without pay...expecting something within a few hours of your post from contributors who are not paid and spend their free time doing so is asking quite a lot, and quite often might turn them away. Especially it the post is quite lengthy and has no defined question. Recommended reading:
    How To Ask Questions The Smart Way
    Short, Self Contained, Correct Example

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: data structure assignment .. help me : |

    Where specifically in the code writing do you need help with, and what is it that you don't quite understand?

Similar Threads

  1. What is the fastest and most memory-efficient data structure?
    By aussiemcgr in forum Collections and Generics
    Replies: 5
    Last Post: October 11th, 2012, 03:48 PM
  2. HI can some one tell me the size of the below structure.
    By sucheth13 in forum Java Native Interface
    Replies: 1
    Last Post: March 11th, 2011, 03:08 AM
  3. Data Structure for ordered binay tree
    By wash in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 23rd, 2010, 05:31 PM
  4. .xls data structure
    By helloworld922 in forum JDBC & Databases
    Replies: 3
    Last Post: August 20th, 2009, 07:12 PM