Java Programming Question File IntList.java contains code for an integer list cl
ID: 3562852 • Letter: J
Question
Java Programming Question
File IntList.java contains code for an integer list class. 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. Write a class SortedIntList that extends IntList. SortedIntList should be just like IntList except that its elements should always be in sorted order from smallest to largest. This means that when an element is inserted into a SortedIntList it should be put into its sorted place, not just at the end of the array. To do this you
Explanation / Answer
===================================================
Class for IntList
===================================================
// ****************************************************************
// IntList.java
//
// 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 {
protected int[] list;
protected 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 = "Integer List is ";
for (int i = 0; i < numElements; i++)
returnString += "Element at index "+ i + " is : " + list[i] + " ";
return returnString;
}
}
==========================================================================
Class for SortedIntList
===========================================================================
public class SortedIntList extends IntList {
// -------------------------------------------------------------
// Constructor -- creates an integer list of a given size.
// -------------------------------------------------------------
public SortedIntList(int size) {
super(size);
}
// -------------------------------------------------------------
// Adds an integer to the list. If the list is full,
// prints a message and does nothing.
// -------------------------------------------------------------
@Override
public void add(int value) {
if (numElements == list.length)
System.out.println("Can't add, list is full ");
else {
boolean indexFound=false;
int i;
for(i=0;i<numElements;i++){
if(value <=list[i]) {
indexFound =true;
break;
}
}
if(indexFound==false) {
list[numElements] = value;
} else {
//For inserting into sorted order
for(int j=numElements;j!=i;j--) {
list[j] = list[j-1];
}
list[i] = value;
}
numElements++;
}
}
// -------------------------------------------------------------
// Returns a string containing the elements of the list with their
// indices.
// -------------------------------------------------------------
public String toString() {
String returnString = "Sorted Integer List is ";
for (int i = 0; i < numElements; i++)
returnString += "Element at index "+ i + " is : " + list[i] + " ";
return returnString;
}
}
============================================================================
Class for ListTest
============================================================================
// ****************************************************************
// ListTest.java
//
// A simple test program that creates an IntList, puts some
// ints in it, and prints the list.
//
// ****************************************************************
public class ListTest {
public static void main(String[] args) {
System.out.println("==============================");
IntList myList = new IntList(10);
myList.add(100);
myList.add(50);
myList.add(200);
myList.add(25);
System.out.println(myList);
System.out.println("==============================");
//SortedIntList with size 5
SortedIntList sortIntList = new SortedIntList(5);
sortIntList.add(100);
sortIntList.add(50);
sortIntList.add(200);
sortIntList.add(25);
sortIntList.add(1000);
System.out.println(sortIntList);
System.out.println("==============================");
//sortIntList having 5 elements if we add one more element to it
//it will print error message
sortIntList.add(2000);
}
}
===================================================================
Sample Input and Output
====================================================================
==============================
Integer List is
Element at index 0 is : 100
Element at index 1 is : 50
Element at index 2 is : 200
Element at index 3 is : 25
==============================
Sorted Integer List is
Element at index 0 is : 25
Element at index 1 is : 50
Element at index 2 is : 100
Element at index 3 is : 200
Element at index 4 is : 1000
==============================
Can't add, list is full
===============================================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.