File IntList.java contains code for an integer list class. Save it to your direc
ID: 3760099 • Letter: F
Question
File IntList.java contains code for an integer list class. Save it to your directory and study it; notice that the only things you can do are to create a list of a fixed size and add an element to a list. If the list is already full, a message will be printed.
File ListTest.java contains code for a class that creates an IntList, puts some values in it, and prints it. Save this to your directory and compile and run it to see how it works.
Now modify the add method in IntList.java such that its elements should always be in sorted order from largest to smallest. This means that when an element is inserted into a IntList it should be put into its sorted place, not just at the end of the array. To do this you'll need to do two things when you add a new element:
• Walk down the array until you find the place where the new element should go. Since the list is already
sorted you can just keep looking at elements until you find one that is at least as big as the one to be
inserted.
• Move down every element that will go after the new element, that is, everything from the one you stop
on to the end. This creates a slot in which you can put the new element. Be careful about the order in
which you move them or you'll overwrite your data!
Now you can insert the new element in the location you originally stopped on. All of this will go into your add method. Do not modify any other part of the program.
To ListTest.java test your class. When the list is printed, they should come out in sorted order.
// An (unsorted) integer list class with a method to add an
// integer to the list and a toString method that returns the contents
// of the list with indices.
//
// ****************************************************************
public class IntList
{
private int[] list;
private int numElements = 0;
//-------------------------------------------------------------
// Constructor -- creates an integer list of a given size.
//-------------------------------------------------------------
public IntList(int size)
{
list = new int[size];
}
//------------------------------------------------------------
// Adds an integer to the list. If the list is full,
// prints a message and does nothing.
//------------------------------------------------------------
public void add(int value)
{
if (numElements == list.length)
System.out.println("Can't add, list is full");
else
{
list[numElements] = value;
numElements++;
}
}
//-------------------------------------------------------------
// Returns a string containing the elements of the list with their
// indices.
//-------------------------------------------------------------
public String toString()
{
String returnString = "";
for (int i=0; i<numElements; i++)
returnString += i + ": " + list[i] + " ";
return returnString;
}
}
// ***************************************************************
// ListTest.java
//
// A simple test program that creates an IntList, puts some
// ints in it, and prints the list.
//
// ***************************************************************
import java.util.Scanner;
public class ListTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
IntList myList = new IntList(10);
int count = 0;
int num;
while (count < 10)
{
System.out.println("Please enter a number, enter 0 to quit:");
num = scan.nextInt();
if(num !=0)
{ myList.add(num);
count++;
}
else break;
}
System.out.println(myList);
}
}
(14 pt) Modify the add(int value) method to allow for insertion of the value in the appropriate (sorted) place in the array. You should be moving/bumping down the elements after the insertion point, to create a new slot for the element. The new "value" is inserted into the original array and no data is lost in the process.
(2 pt) No other code was modified to get the program working.
PLEASE CHECK IF THE CODE WORKS FOR ALL SITUATION BY RUNNING. THANKS
Explanation / Answer
I have updated add method for the above problem statement let me know your feedback so that I can update.
public void add(int value)
{
int count = 0;
if (numElements == list.length)
System.out.println("Can't add, list is full");
else
{
list[numElements] = value;
numElements++;
count++;
}
if(count >= 10)
{
int i = 0;
while(i < list.length && list[i] > value){
i++;
}
if(i < list.length){
for(int j = list.length-1; j > i; j--){
list[j] = list[j - 1];
}
list[i] = value;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.