File IntList.java contains code for an integer list class. Save it to your direc
ID: 3532731 • 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 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 smallest to largest. 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:
Explanation / Answer
Check and Tell if it works :
IntList.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
// 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()
{
list = new int[1];
}
private int[] growArray(int[] arr)
{
int[] arr2=new int[list.length+1];
System.arraycopy(arr, 0, arr2, 0, arr.length);
return arr2;
}
//------------------------------------------------------------
// 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)
{
list=growArray(list);
}
if(numElements==0)
{
list[numElements]=value;
numElements++;
}
else
{
int correctPos=Arrays.binarySearch(list, value);
if(correctPos==1)
{
System.out.println("Array already has the element !! Try again");
}
else
{
//InsertPos=-correctPos-1
int insertPos=-(correctPos+1);
//Shift
for(int i=insertPos; i<list.length-1; i++)
{
list[i+1] = list[i];
growArray(list);
}
//Assign
list[insertPos] = value;
numElements++;
}
}
}
//-------------------------------------------------------------
// Returns a string containing the elements of the list with their
// indices.
//-------------------------------------------------------------
public String toString()
{
StringBuffer returnString = new StringBuffer("");
for (int i=0; i<numElements; i++)
{
returnString.append(i + ": " + list[i] + " ");
}
return returnString.toString();
}
}
ListTest.java
// ***************************************************************
// 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();
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);
}
System.out.println(myList);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.