Question: Please Use Java Please Follow Instructions Program 6: Sorting an Array of Objects This assignment will add the ability to sort the data and update
Please Use Java
Please Follow Instructions
Program 6: Sorting an Array of Objects
This assignment will add the ability to sort the data and update data in the Array of Object Program from Program 5 in the previous module.
A. Include the following requirements from Program 5:
- Create a Class for the Unit of One of your database. The information for this Class must include a minimum of 3 fields. Within the Class, include the following methods:
- A constructor that accepts the information about one item and assigns it to the instance data of the method.
- A method that prints the information about one item of your collection. Name this method toString. NOTE: It is important that you name this method toString because it's one of Java's built-in methods that allows you print out your Object.
- Keep all of the Gets and Sets. There should be one of each for each field in the Unit of One Class.
- Create another Class that instantiates an array of the Unit of One Objects with the following methods:
- Create an array of the Objects from the Class of the Unit of One.
- Create a constructor to initialize the number of items in the array to zero.
- Create a method to add one item at a time to the array.
- Create a method to display the contents of the entire array. Call this method toString.
- Create a method to increase the size of the array if it is getting full.
- Include the method that reads the data from the external file and populates the Array of Objects.
- Remember - when the program first executes - open the file, read from the file and populate the array, then close the file.
- Include the method that writes the Array of Objects to the same external file. Remember - do not use toString when writing the data to the external file.
- Remember - when the user selects Exit from the menu - open the file, write to the file, then close the file.
B. Add the following requirements:
- Add a method that will Sort the data in your Array of Objects.
- Write your code from the Selection Sort algorithm in the lecture.
- Pick one default field to sort the data by.
- Display a menu that will allow the User to select at least one other field to sort the data by.
- DO NOT use any of Java's built-in sorting methods. YOU MUST write your own using the Selection Sort algorithm discussed in the lecture.
C. Be sure to also include the following:
- Create a Driver Class with a Menu with the following options displayed like so:
- Add an item to the array.
- Update an item in the array.
- Print out the list of all items.
- Sort the list.
- Exit.
- Include a sub-menu for Sort option that allows the User to select which field they wish to sort by.
- When your program is first executed, read data from an external file and place it in your array. Include a minimum of 5 sets of data in the file. Submit the external file with your program.
- Save the data back to the external file when the Exit option is chosen from the menu in the Driver.
NOTE: You MUST read your data from an external file! You MUST save your data back to that SAME external file when done.
Selection Sort Algo:
Data:
outerLoop: Outer loop counter innerLoop: Inner loop counter location: Location of smallest value n = number of items in the array
Logic:
outerLoop runs from 0 to (number of items in array 1) initialize the location of the smallest value to the position indicated by the outerLoop value innerLoop runs from (outerLoop + 1) to the number of items in array IF the value in the array at position innerLoop is less than the smallest value at position location THEN replace the location of the smallest value into the location of the value in the array END IF end innerLoop swap the value of the new smallest with the value located at the position indicated by the outerLoop END outerLoop
Logic converted to pseudo-code:
FOR (outerLoop = 0 to (n 1) ) let location = outerLoop FOR (innerLoop = ((outerLoop + 1) to n) IF (value at array[innerLoop] is less than array[location]) THEN location = innerLoop END IF END FOR innerLoop swap array[outerLoop] with array[location] END FOR outerLoop
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
