File IntegerList.java contains a Java class representing a list of integers. Eac
ID: 3673971 • Letter: F
Question
File IntegerList.java contains a Java class representing a list of integers. Each IntegerList object created is an array of integers. Open it an examine the data field, constructor, and methods that are already defined. 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. • Modify the code in these files as follows
// ****************************************************************
// IntegerList.java
//
// Define an IntegerList class with methods to create, fill,
// sort, and search in a list of integers.
//
// ****************************************************************
public class IntegerList
{
private 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;
}
}
/****************NEW STUFF*****************/
}
// ****************************************************************
// IntegerList.java
//
// Define an IntegerList class with methods to create, fill,
// sort, and search in a list of integers.
//
// ****************************************************************
public class IntegerList
{
private 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;
}
}
/****************NEW STUFF*****************/
}
: 1. 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 the code for case 3 in IntegerListTest to test your new method.
2. 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 a specific value in the list; use it! Add a case and option to the menu in IntegerListTest to test your new method. Add as #6 in the menu. Note that you will have to prompt the user to enter in which value they would like to replace, as well as what they want the new value to be.
3. 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). Again use methods that already exist. If you find yourself typing the same code that exists in another method, call the other method instead!!!! Add a case and option to the menu in IntegerListTest to test your new method. Add as #7 in the menu. Note that you will have to prompt the user to enter in which value they would like to replace, as well as what they want the new value to be.
4. Add an increaseSize method. Create a new array that is one longer than the old array. Copy everything from the old array to the new array. Make sure you make your list refer (point) to the new array! Since you do not have any way to add elements to the list, you won't need to call increaseSize yet inside of IntegerListTest.
5. Add a method void addElement(int newVal) to the IntegerList class that adds an element to the list. At the beginning of addElement, call increaseSize before you do anything else. Put the new value at the end of the new array. Add a case and option to the menu in IntegerListTest to test your new method. Add as #8 in the menu. Note that you will have to prompt the user to enter in which value they would like to add.
6. Add a method void removeFirst(int newVal) to the IntegerList class that removes the first occurrence of a value from the list. If the value does not appear in the list, it should do nothing (but it's not an error). Since the array needs to stay full, if the element is there to be removed we will have to create a new, smaller array. So you'll need to do the following: a. Create a new array that is one shorter than the old array. b. Copy everything except the first occurrence of the value given from the old array to the new array. c. Make list refer to the new array. Add a case and option to the menu in IntegerListTest to test your new method. Add as #9 in the menu. Note that you will have to prompt the user to enter in which value they would like to remove.
7. Add a method removeAll(int newVal) to the IntegerList class that removes all occurrences of a value from the list. If the value does not appear in the list, it should do nothing (but it's not an error). Utilize methods that already exist! Add a case and option to the menu in IntegerListTest to test your new method. Add as #10 in the menu. Note that you will have to prompt the user to enter in which value they would like to remove.
8. Add a method void addInOrder(int newVal) to the IntegerList class that assumes that the list is sorted in increasing order and adds the given element in its correct (sorted) position. So if the list contained the values 10 20 30 40 50 and you added 25, the new list would be 10 20 25 30 40 50. Don't just stick the value on the end and then sort! Sorting is expensive -- actually look through the list, figure out where the new value should go, and put it there. Note that you are increasing the size of the list at the same time. Add a case and option to the menu in IntegerListTest to test your new method. Add as #11 in the menu. Note that you will have to prompt the user to enter in which value they would like to add. Note: when you test this method, you must first select the sort option before selecting this option.
Explanation / Answer
IntegerList.java
public class IntegerList
{
int[] list; // values in the list
int numElements;
// -------------------------------------------------------
// 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;
numElements = list.length;
}
// -------------------------------------------------------
// 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;
}
}
// -----------------------------------------------------------------------
// Sort the list into descending order using the selection sort algorithm
// -----------------------------------------------------------------------
public void sortDecreasing()
{
int maxIndex;
for (int i = 0; i < list.length - 1; i++)
{
// find largest element in list starting at location i
maxIndex = i;
for (int j = i + 1; j < list.length; j++)
if (list[j] > list[maxIndex])
maxIndex = j;
// swap list[i] with largest element
int temp = list[i];
list[i] = list[maxIndex];
list[maxIndex] = temp;
}
}
// ----------------------------------------------------------
// Replace the first occurrence of oldVal with newVal; do
// nothing if oldVal is not in the list
// ----------------------------------------------------------
public void replaceFirst(int oldVal, int newVal)
{
int location = search(oldVal);
if (location != -1)
list[location] = newVal;
}
// ----------------------------------------------------------
// Replace all occurrences of oldVal with newVal; do
// nothing if oldVal is not in the list
// ----------------------------------------------------------
public void replaceAll(int oldVal, int newVal)
{
for (int i = 0; i < list.length; i++)
if (list[i] == oldVal)
list[i] = newVal;
}
// ----------------------------------------------------------
// Search for the target in a list that is sorted in
// decreasing order. Return -1 if the target is not found;
// otherwise return the index of its location.
// ----------------------------------------------------------
public int binarySearchD(int target)
{
int min = 0;
int max = list.length - 1;
int mid = 0;
boolean found = false;
while (!found && min <= max)
{
mid = (min + max) / 2;
if (list[mid] == target)
found = true;
else if (target > list[mid])
max = mid - 1;
else
min = mid + 1;
}
if (found)
return mid;
else
return -1;
}
// --------------------------------------------------------------
// Doubles the size of the list.
// --------------------------------------------------------------
private void increaseSize()
{
int[] temp = new int[list.length * 2];
for (int i = 0; i < list.length; i++)
temp[i] = list[i];
list = temp;
}
// --------------------------------------------------------------
// Adds an element to the list. If the array is full its size
// is doubled first.
// --------------------------------------------------------------
public void addElement(int newVal)
{
if (numElements == list.length)
increaseSize();
list[numElements] = newVal;
numElements++;
}
// ---------------------------------------------------------------
// Removes the first occurrence of newVal from the list.
// ---------------------------------------------------------------
public void removeFirst(int newVal)
{
// find the first instance of the element
boolean found = false;
int location = 0;
while (location < list.length && !found)
if (list[location] == newVal)
found = true;
else
location++;
if (found)
{
// move all elements after location up one spot
for (int i = location; i < numElements - 1; i++)
list[i] = list[i + 1];
numElements--;
}
}
// -------------------------------------------------------------
// Removes all occurrences of newVal from the list.
// -------------------------------------------------------------
public void removeAll(int newVal)
{
int i = 0;
while (i < numElements)
{
if (list[i] == newVal)
{
for (int j = i; j < numElements - 1; j++)
list[j] = list[j + 1];
numElements--;
}
else
i++;
}
}
public void addInOrder(int newVal)
{
if (numElements == list.length)
increaseSize();
// Find the correct place
int index = 0;
boolean found = false;
while (index < numElements && !found)
{
if (list[index] > newVal)
found = true;
else
index++;
}
// Shift elements down to make room at position index
for (int i = numElements; i > index; i--)
list[i] = list[i - 1];
list[index] = newVal;
numElements++;
}
}
IntegerListTest.java
import java.util.Scanner;
public class IntegerListTest
{
static IntegerList list = new IntegerList(10);
static Scanner scan = 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 = scan.nextInt();
while (choice != 0)
{
dispatch(choice);
printMenu();
choice = scan.nextInt();
}
}
// -------------------------------------------------------
// Do what the menu item calls for
// -------------------------------------------------------
public static void dispatch(int choice)
{
int loc;
int oldVal, newVal;
switch (choice)
{
case 0:
System.out.println("Bye!");
break;
case 1:
System.out.println("How big should the list be?");
int size = scan.nextInt();
list = new IntegerList(size);
list.randomize();
break;
case 2:
list.selectionSort();
break;
case 3:
System.out.print("Enter the value to look for: ");
loc = list.search(scan.nextInt());
if (loc != -1)
System.out.println("Found at location " + loc);
else
System.out.println("Not in list");
break;
case 4:
list.print();
break;
case 5:
list.sortDecreasing();
break;
case 6:
System.out.print("Enter the old value: ");
oldVal = scan.nextInt();
System.out.print("Enter the new value: ");
newVal = scan.nextInt();
list.replaceFirst(oldVal, newVal);
break;
case 7:
System.out.print("Enter the old value: ");
oldVal = scan.nextInt();
System.out.print("Enter the new value: ");
newVal = scan.nextInt();
list.replaceAll(oldVal, newVal);
break;
case 8:
System.out.print("Enter the value to look for: ");
loc = list.binarySearchD(scan.nextInt());
if (loc != -1)
System.out.println("Found at location " + loc);
else
System.out.println("Not in list");
break;
case 9:
System.out.print("Enter the value to add to the list: ");
newVal = scan.nextInt();
list.addElement(newVal);
break;
case 10:
System.out.print("Enter the value to delete: ");
oldVal = scan.nextInt();
list.removeFirst(oldVal);
break;
case 11:
System.out.print("Enter the value to delete: ");
oldVal = scan.nextInt();
list.removeAll(oldVal);
break;
case 12:
System.out.print("Enter the value to be added to the list: ");
newVal = scan.nextInt();
list.addInOrder(newVal);
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 list (** do this first!! **)");
System.out.println("2: Sort the list using selection sort");
System.out
.println("3: Find an element in the list using sequential search");
System.out.println("4: Print the list");
System.out.println("5: Sort the list in decreasing order");
System.out.println("6: Replace a value in the list");
System.out.println("7: Replace all occurrences of a value in the list");
System.out
.println("8: Find an element in a list sorted in decreasing order");
System.out.println("9: Add an element to the list");
System.out.println("10: Remove a value from the list");
System.out
.println("11: Remove all occurrences of a value from the list");
System.out.println("12: Add an element to a sorted list");
System.out.print(" Enter your choice: ");
}
}
output
0: Quit
1: Create a new list (** do this first!! **)
2: Sort the list using selection sort
3: Find an element in the list using sequential search
4: Print the list
5: Sort the list in decreasing order
6: Replace a value in the list
7: Replace all occurrences of a value in the list
8: Find an element in a list sorted in decreasing order
9: Add an element to the list
10: Remove a value from the list
11: Remove all occurrences of a value from the list
12: Add an element to a sorted list
Enter your choice: 1
How big should the list be?
3
Menu
====
0: Quit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.