Question
I need 2 things done: 1. You see that your new IntListSorted can find the minimum and maximum values in the list in a much
I need 2 things done:
1. You see that your new IntListSorted can find the minimum and maximum values in the list in a much simpler and faster way than the superclass can. Override these methods with this faster approach. Modify your IntListTest class to use an IntListSorted instance instead of an IntList and try it out.
2. Besides overriding superclass functionality, subclasses can add functionality not available to the superclass. Write a method getMedian that returns the middle element of an IntListSorted. (Note: When the list contains an even number of items, just return the last element in the first half of the list, i.e., the element right before the "center" of the list. For example, in the case of [0, 1, 3, 10], 1 should be returned.) Demonstrate your code by adding additional statements to IntListTest.
Here is IntListTest:
public class IntListTest { public static void main(String[] args) { IntList list = new IntList(); list.add(5); list.add(4); list.add(3); System.out.println(list); System.out.println("Size: " + list.size()); System.out.println("Min: " + list.getMinimum()); System.out.println("Max: " + list.getMaximum()); } }
Here is IntListSorted:
public class IntListSorted extends IntList { /** * Adds a new item to this list, inserting it so that * the list remains sorted. */ @Override public void add(int newItem) { if (size() == 0 || get(size() - 1) <= newItem) { super.add(newItem); } else { int i = size(); while (i > 0 && newItem < get(i - 1)) { i -= 1; } // now i is 0, or newItem >= list[i - 1], so put the new // element at position i super.add(i, newItem); } } /** * Inserts a new item in this list, inserting it so that * the list remains sorted. (The given index is ignored.) */ @Override public void insert(int index, int newItem) { this.add(newItem); } }
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