In this assignment, you study various ways to formulate a bigger array to replace one that has no room left. The point of this work is to understand that the choices you make matter greatly in terms of performance.
You will see that this is an abstract class, because the method int getNewSize() is missing. This is intentional, because we want to experiment with various ways of replacing the full array, an we do that by specifying the array's new size.We provide two extensions of Rarrays for you: Quadruple and TimesTen, which cause the new array to be 4 and 10 times the previous size, respectively. Each of those classes completes the Rarrays abstract class by providing the missing method int getNewSize().
Take a look.
protected int[] array;that you find in the Rarrays class. You can assign that instance variable to any integer array.
For example, the reset(Ticker) method assigns it thus:this.array = new int[2];
public void reset(Ticker ticker) { this.ticker = ticker; this.array = new int[2]; ticker.tick(2); }
In the code above, why do we write ticker.tick(2)?The lesson here is that a single line of Java code might take one operation, but it also might take considerably many more. A theme of this course is understanding the complexity of having a computer perform a given task.
- This is a very important point, and its understanding is a major goal of this studio.
- Why does new int[2] take two operations in our thinking?
- The statement new int[2] takes just one line of Java code. But what does Java do in response to that line?
- Java must allocate two elements for the array. Perhaps that takes just as single operation.
- But Java must also initialize those elements to 0.
- That cannot take a single operation and so its cost depends on the size of the array.
- Thus, for instantiating an array of two elements, we must tick twice.
- Generally, instantiating an array of n elements requires n ticks.
- Which group do you expect to finish first?
- Can you formalize, in terms of n the amount of work (ticks) that each group must do to write n in the form required for that group?
For example, if for some reason that two elements are insufficent for the array, the following would allow the instance variable to reference an 8-element array:However, reassignment of the array loses the reference to the previous array. Any data in that previous array would be lost.this.array = new int[8];
To preserve information as you provision for a larger array, you must hold on to both arrays, copy the old information into the newer one, and then reassign the instance variable.
Your first task is to complete this method until it passes the testGrowPreservesData test within the TestRarrays unit test.
The TestRarrays contains 3 test cases:![]()
- testInit should work as given to you.
- testGrowPreservesData is the one you are working toward now. When it's working you will see a green check box just as you see for testInit in the picture above.
- testGrowSufficientTicks is one you work on next. For now, don't worry about this one.
Work together to write the appropriate code below the comments, but don't worry about the ticks just yet. Ask for help from others or from a TA as needed.
Work on this until you get the green bar, indicating all tests are passing.
We are not yet interested in the output of those runs, but make sure they run without error.
If you don't know how to do this in Excel, here is some advice:
- Highlight all data including labels
- Go to the Insert tab on the Excel toolbar
- In the Charts header, click on Scatter Plot and select Scatter with Straight Lines and Markers
- The legend may flip the axes by default, in which the legend names will appear as Series1-5
- To resolve this go to the Chart Tools tab and select Switch Row/Column
- Save images of each graph as a JPEG or PNG file in your outputs folder
- How would you describe the curve you see?
- As a team, think about possible polynomail functions that could generate such a curve.
- Don't close the window as we'll return to it later.
- Does this data make sense in terms of how your doubling strategy works?
- While the data has a certain disconnected quality to it, can you nonetheless bound all of the plotted points between two straight lines?
But what about the cost of keeping the array so close to its necessary size?
For an array that eventually reaches size n, how much work is expended by each of the above approaches?
Discuss this with the instructor or TA.