Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Searching and Sorting In An Integer List File IntegerList.java contains a Java c

ID: 3771738 • Letter: S

Question

Searching and Sorting In An Integer List File IntegerList.java contains a Java class representing a list of integers. The following public methods are provided: 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 indices int search(int target) -- looks for value target in the list using a sequential search algorithm. 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, create a list, print it, and search for an element in the list. Does it return the correct index? Now look for an element that 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: Add a method void sortDecreasing() to the IntegerList class that sorts the list into decreasing (instead of increasing) order. Use the selection sort algorithm, but modify it to sort the other way. Be sure you change the variable names so they make sense! Add an option to the menu in IntegerListTest to test your new method. Add a method void replaceFirst(int oldVal, int newVal) to the IntegerList class that replaces the first occurrence of oldVal in the list with newVal. If oldVal does not appear in the list, it should do nothing (but it's not an error). If oldVal appears multiple times, only the first occurrence should be replaced. Note that you 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. Add a method void replaceAll(int oldVal, int newVal) to the IntegerList class that replaces all occurrences of oldVal in the list with newVal. If oldVal does not appear in the list, it should do nothing (but it's not an error). Does it still make sense to use the search method like you did for replaceFirst, or should you do your own searching here? Think about this. Add an option to the menu in IntegerListTest to test your new method. // **************************************************************** // IntegerList.java // // Define an IntegerList class with methods to create, fill, // sort, and search in a list of integers. // // **************************************************************** import cs1.Keyboard; public class IntegerList{ int[] list; //values in the list //------------------------------------------------------- //create a list of the given size //------------------------------------------------------- public IntegerList(int size) { list = new int[size]; } //------------------------------------------------------- //fill array with integers between 1 and 100, inclusive //------------------------------------------------------- public void randomize() { for (int i=0; i

Explanation / Answer

//Modification is in bold letters

// IntegerList.java
// Define an IntegerList class with methods to create, fill,
// sort, and search in a list of integers.
public class IntegerList
{

   int[] list; //values in the list

   //-------------------------------------------------------
   //create a list of the given size
   //-------------------------------------------------------
   public IntegerList(int size)
   {
       list = new int[size];
   }


   //-------------------------------------------------------
   //fill array with integers between 1 and 100, inclusive
   //-------------------------------------------------------
   public void randomize()
   {
       for (int i=0; i<list.length; i++)
           list[i] = (int)(Math.random() * 100) + 1;
   }


   //-------------------------------------------------------
   //print array elements with indices
   //-------------------------------------------------------
   public void print()
   {
       for (int i=0; i<list.length; i++)
           System.out.println(i + ": " + list[i]);
   }


   //-------------------------------------------------------
   //return the index of the first occurrence of target in the list.
   //return -1 if target does not appear in the list
   //-------------------------------------------------------
   public int search(int target)
   {
       int location = -1;
       for (int i=0; i<list.length && location == -1; i++)
           if (list[i] == target)
               location = i;
       return location;
   }


   //-------------------------------------------------------
   //sort the list into ascending order using the selection sort algorithm
   //-------------------------------------------------------
   public void selectionSort()
   {
       int minIndex;
       for (int i=0; i < list.length-1; i++)
       {
           //find smallest element in list starting at location i
           minIndex = i;
           for (int j = i+1; j < list.length; j++)
               if (list[j] < list[minIndex])
                   minIndex = j;

           //swap list[i] with smallest element
           int temp = list[i];
           list[i] = list[minIndex];
           list[minIndex] = temp;
       }
   }
  
  
   /**The method sortDecreasingOrder that sorts
       list into desending order using the selection sort algorithm */  
   public void sortDecreasingOrder()
   {
       int maxIndex;
       for (int i=0; i < list.length-1; i++)
       {
           //find maximum element in list starting at location i
           maxIndex = i;
          
           for (int j = i+1; j < list.length; j++)
               //check if element at j is greater than max element
               if (list[j] > list[maxIndex])
                   //reset maxIndex to j
                   maxIndex = j;

           //swap list[i] with maximum element
           int temp = list[i];
           list[i] = list[maxIndex];
           list[maxIndex] = temp;
       }
   }

}

---------------------------------------------------------------

/**
* The java program that modifies the selection sorts
* the list in decreasing order in the main method.
* */

//IntegerListTest.java
import java.util.Scanner;
public class IntegerListTest
{

   static IntegerList list = new IntegerList(10);
   static Scanner scanner=new Scanner(System.in);  
   // Create a list, then repeatedly print the menu and do what the
   // user asks until they quit  
   public static void main(String[] args)
   {
       printMenu();
       int choice = scanner.nextInt();
       while (choice != 0)
       {
           dispatch(choice);
           printMenu();
           choice = scanner.nextInt();
       }
   }  
   // menu of choices
   public static void dispatch(int choice)
   {
       int loc;
       switch(choice)
       {
       case 0:
           System.out.println("Bye!");
           break;
       case 1:
           System.out.println("How big should the list be?");
           int size = scanner.nextInt();
           list = new IntegerList(size);
           list.randomize();
           break;
       case 2:
           //Calling selectionSort method
           //to sort in ascending order
           list.selectionSort();
           break;
       case 3:
           //Calling sortDecreasingOrder method
           //to sort in desending order
           list.sortDecreasingOrder();
           break;

          
       case 4:
           System.out.print("Enter the value to look for: ");
           loc = list.search(scanner.nextInt());
           if (loc != -1)
               System.out.println("Found at location " + loc);
           else
               System.out.println("Not in list");
           break;
       case 5:
           list.print();
           break;
       default:
           System.out.println("Sorry, invalid choice");
       }
   }  
   // Print the user's choices  
   public static void printMenu()
   {

       System.out.println("    Menu   ");
       System.out.println("   ====");
       System.out.println("0: Quit");
       System.out.println("1: Create a new random list**)");
       System.out.println("2: Sort the list in increasing using selection sort");      
    //Add an option for descending order sort
       System.out.println("3: Sort the list in decreasing order using selection sort");

       System.out.println("4: Find an element in the list using sequential search");
       System.out.println("5: Print the list");
       System.out.print(" Enter your choice: ");
   }

}

------------------------------------------------------------------------

Sample Output:


   Menu
   ====
0: Quit
1: Create a new random list**)
2: Sort the list in increasing using selection sort
3: Sort the list in decreasing order using selection sort
4: Find an element in the list using sequential search
5: Print the list

Enter your choice: 1
How big should the list be?
10

   Menu
   ====
0: Quit
1: Create a new random list**)
2: Sort the list in increasing using selection sort
3: Sort the list in decreasing order using selection sort
4: Find an element in the list using sequential search
5: Print the list

Enter your choice: 5
0:   55
1:   50
2:   47
3:   62
4:   64
5:   92
6:   54
7:   81
8:   48
9:   78

   Menu
   ====
0: Quit
1: Create a new random list**)
2: Sort the list in increasing using selection sort
3: Sort the list in decreasing order using selection sort
4: Find an element in the list using sequential search
5: Print the list

Enter your choice: 3

   Menu
   ====
0: Quit
1: Create a new random list**)
2: Sort the list in increasing using selection sort
3: Sort the list in decreasing order using selection sort
4: Find an element in the list using sequential search
5: Print the list

Enter your choice: 5
0:   92
1:   81
2:   78
3:   64
4:   62
5:   55
6:   54
7:   50
8:   48
9:   47

   Menu
   ====
0: Quit
1: Create a new random list**)
2: Sort the list in increasing using selection sort
3: Sort the list in decreasing order using selection sort
4: Find an element in the list using sequential search
5: Print the list

Enter your choice: 2

   Menu
   ====
0: Quit
1: Create a new random list**)
2: Sort the list in increasing using selection sort
3: Sort the list in decreasing order using selection sort
4: Find an element in the list using sequential search
5: Print the list

Enter your choice: 5
0:   47
1:   48
2:   50
3:   54
4:   55
5:   62
6:   64
7:   78
8:   81
9:   92

   Menu
   ====
0: Quit
1: Create a new random list**)
2: Sort the list in increasing using selection sort
3: Sort the list in decreasing order using selection sort
4: Find an element in the list using sequential search
5: Print the list

Enter your choice: 3

   Menu
   ====
0: Quit
1: Create a new random list**)
2: Sort the list in increasing using selection sort
3: Sort the list in decreasing order using selection sort
4: Find an element in the list using sequential search
5: Print the list

Enter your choice: 5
0:   92
1:   81
2:   78
3:   64
4:   62
5:   55
6:   54
7:   50
8:   48
9:   47

   Menu
   ====
0: Quit
1: Create a new random list**)
2: Sort the list in increasing using selection sort
3: Sort the list in decreasing order using selection sort
4: Find an element in the list using sequential search
5: Print the list

Enter your choice:0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote