File integerlist | Computer Science homework help

File integerlist | Computer Science homework help

File integerlist | Computer Science homework help

 
File IntegerList.java contains a Java class representing a list of integers. The following public methods areprovided:  IntegerList(int size)—creates a new list of size elements. Elements are initialized to 0.  void randomize()—fills the list with random integers between 1 and 100, inclusive.
void print()—prints the array elements and indicesint search(int target)—looks for value target in the list using a linear (also called sequential) searchalgorithm. Returns the index where it first appears if it is found, -1 otherwise.void selectionSort()—sorts the lists into ascending order using the selection sort algorithm.File IntegerListTest.java contains a Java program that provides menu-driven testing for the IntegerList class.Copy both files to your directory, and compile and run IntegerListTest to see how it works. For example, createa list, print it, and search for an element in the list. Does it return the correct index? Now look for an elementthat is not in the list. Now sort the list and print it to verify that it is in sorted order.Modify the code in these files as follows:1. Add a method void replaceFirst(int oldVal, int newVal) to the IntegerList class that replaces the firstoccurrence of oldVal in the list with newVal. If oldVal does not appear in the list, it should do nothing (butit’s not an error). If oldVal appears multiple times, only the first occurrence should be replaced. Note thatyou already have a method to find oldVal in the list; use it!Add an option to the menu in IntegerListTest to test your new method.2. Add a method void replaceAll(int oldVal, int newVal) to the IntegerList class that replaces all occurrencesof oldVal in the list with newVal. If oldVal does not appear in the list, it should do nothing (but it’s not anerror). Does it still make sense to use the search method like you did for replaceFirst, or should you doyour own searching here? Think about this.Add an option to the menu in IntegerListTest to test your new method.3. Add a method void sortDecreasing() to the IntegerList class that sorts the list into decreasing (instead ofincreasing) order. Use the selection sort algorithm, but modify it to sort the other way. Be sure you changethe variable names so they make sense!Add an option to the menu in IntegerListTest to test your new method.4. Add a method int binarySearchD (int target) to the IntegerList class that uses a binary search to find thetarget assuming the list is sorted in decreasing order. If the target is found, the method should return itsindex; otherwise the method should return –1. Your algorithm will be a modification of the binary searchalgorithm in listing Add an option to the menu in IntegerListTest to test your new method. In testing, make sure your methodworks on a list sorted in descending order then see what the method does if the list is not sorted(it shouldn’t be able to find some things that are in the list).
 


File integerlist | Computer Science homework help

 
File IntegerList.java contains a Java class representing a list of integers. The following public methods areprovided:  IntegerList(int size)—creates a new list of size elements. Elements are initialized to 0.  void randomize()—fills the list with random integers between 1 and 100, inclusive.
void print()—prints the array elements and indicesint search(int target)—looks for value target in the list using a linear (also called sequential) searchalgorithm. Returns the index where it first appears if it is found, -1 otherwise.void selectionSort()—sorts the lists into ascending order using the selection sort algorithm.File IntegerListTest.java contains a Java program that provides menu-driven testing for the IntegerList class.Copy both files to your directory, and compile and run IntegerListTest to see how it works. For example, createa list, print it, and search for an element in the list. Does it return the correct index? Now look for an elementthat is not in the list. Now sort the list and print it to verify that it is in sorted order.Modify the code in these files as follows:1. Add a method void replaceFirst(int oldVal, int newVal) to the IntegerList class that replaces the firstoccurrence of oldVal in the list with newVal. If oldVal does not appear in the list, it should do nothing (butit’s not an error). If oldVal appears multiple times, only the first occurrence should be replaced. Note thatyou already have a method to find oldVal in the list; use it!Add an option to the menu in IntegerListTest to test your new method.2. Add a method void replaceAll(int oldVal, int newVal) to the IntegerList class that replaces all occurrencesof oldVal in the list with newVal. If oldVal does not appear in the list, it should do nothing (but it’s not anerror). Does it still make sense to use the search method like you did for replaceFirst, or should you doyour own searching here? Think about this.Add an option to the menu in IntegerListTest to test your new method.3. Add a method void sortDecreasing() to the IntegerList class that sorts the list into decreasing (instead ofincreasing) order. Use the selection sort algorithm, but modify it to sort the other way. Be sure you changethe variable names so they make sense!Add an option to the menu in IntegerListTest to test your new method.4. Add a method int binarySearchD (int target) to the IntegerList class that uses a binary search to find thetarget assuming the list is sorted in decreasing order. If the target is found, the method should return itsindex; otherwise the method should return –1. Your algorithm will be a modification of the binary searchalgorithm in listing Add an option to the menu in IntegerListTest to test your new method. In testing, make sure your methodworks on a list sorted in descending order then see what the method does if the list is not sorted(it shouldn’t be able to find some things that are in the list).
 

Leave a Comment

Your email address will not be published. Required fields are marked *