Question
A Sorted Integer List File IntList.java contains code for an integer list class. Save it to your directory and study it; notice that the only
A Sorted Integer List
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 write a class SortedIntList that extends IntList. SortedIntList should be just like IntList except that its elements should always be sorted. 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. Think carefully about what methods and instance variables you have to define in SortedIntList and what you can inherit directly from IntList -- don't override anything you don't have to.
To test your class, modify ListTest.java so that after it creates and prints the IntList, it creates and prints a SortedIntList containing the same elements.
// **************************************************************** // 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 = ""; for (int i=0; i
// **************************************************************** // ListTest.java // // A simple test program that creates an IntList, puts some // ints in it, and prints the list. // // **************************************************************** import IntList; public class ListTest { public static void main(String[] args) { IntList myList = new IntList(10); myList.add(100); myList.add(50); myList.add(200); myList.add(25); System.out.println(myList); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started