9A
Write a SimpleLineArrayManager
class which manages SimpleLine
objects. A SimpleLine
represents a line in 2-d space. It is simpler than the one in the graphics library. Copy it into your project. You can look at the documentation for the class by opening the class in the editor window. On the right-hand side where it says "Source Code", click the down arrow and select "Documentation."
The SimpleLineArrayManager
has an instance variable SimpleLine[]
. Call the instance variable lines. Its constructor takes an array of SimpleLine
objects as a parameter
Provide these method
public double sum()
gets the sum of length of all the lines in the array. Remember the Pythagorean theorem. I made a private helper method getLength
because we need to calculate the length in multiple places and do not want to repeat the work.public void swap(int index1, int index2)
- swaps the element at index1 with the element at index2. If either index is out of bounds, do not changing anything.public SimpleLine shortest()
gets the shortest line. Remember the Pythagorean theorem. I made a private helper method getLength
because I needed to calculate the length in two places and did not want to repeat the work. If the array has a length of 0, it is empty so return nullpublic String toString()
gets a string representation of the array - provided. (This is why the instance variable must be called lines.) Provide Javadoc
For the draft, define the instance variable, implement the constructor and the sum method
9B
You are going to to work with a partially filled array in this problem. Write a class to manage an array of Strings. Call the class GrowableArray. GrowableArray
constructor takes no parameters but initializes the array so it can hold 8 elements. Use the constant provided, CAPACITY.
Provide methods:
Hint: The algorithms to implement the methods is explained in your text book
public void add(String toAdd)
adds the given string at the end of the partially filled array.public void add(String toAdd, int index)
adds the String at the specified index. Grow the array if necessary. Shift the elements with a higher index and then add the elementpublic void remove(int index)
removes the element at the given index by shifting elements at a higher index to the next lower index. private void growIfNeeded()
checks to see if the array is full. If it is, it doubles the size of the array and copies the elements to a the new array in the same order. This is provided for youpublic String toString()
returns a string representation of the partially filled array. It does not necessarily include all elements. This is provided for yoProvide Javadoc
For the draft, implement the constructor and the first add method
9C
Weather.com maintains a table of the average high temperatures by year for various cities. TemperatureTable
manages a 2d array of ints representing these temperatures.
The table for 3 years for San Jose might look like this
2017 | 2010 | 2000 | |
---|---|---|---|
Jan | 58 | 59 | 40 |
Feb | 62 | 60 | 45 |
Mar | 66 | 65 | 65 |
Apr | 70 | 72 | 68 |
May | 75 | 73 | 72 |
Jun | 80 | 81 | 83 |
Jul | 83 | 84 | 90 |
Aug | 82 | 82 | 87 |
Sept | 81 | 80 | 81 |
Oct | 74 | 75 | 71 |
Nov | 65 | 66 | 68 |
Dec | 58 | 56 | 59 |
For the South Pole, since the temperatures do not vary much, they only record 3 values per year
2017 | 2000 | |
---|---|---|
Nov - Feb | -17 | -15 |
Mar - Jun | -58 | -63 |
Jul - Oct | -65 | -80 |
The constructor that takes two parameters
Provide the constructor and instance variable.
Provide these methods
last(
) Gets the element in the last column of the last row. Do not use a loopgetName()
Gets the name of the city getHighest()
Gets the largest value in the arraypublic boolean contains(int target)
Returns true if the target is in the array, otherwise falseFor the draft, provide the constructor, instance variables, and the last()
method.