Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Complete the methods below. These methods manipulate Arrays of Strings
- // For the draft, complete the first method
- //
- import java.util.Arrays;
- public class ArrayMethods
- {
- String[] list; //instance variable
- /**
- * Constructor for objects of class ArrayMethods
- */
- public ArrayMethods(String[] list)
- {
- // initialise instance variables
- this.list = list;
- }
- /**
- * Determines if the array is sorted (do not sort)
- * When Strings are sorted, they are in alphabetical order
- * Use the compareTo method to determine which string comes first
- * You can look at the String compareTo method in the Java API
- * @return true if the array is sorted else false.
- */
- public boolean isSorted()
- {
- boolean sorted = true;
- // TODO: Write the code to loop through the array and determine that each
- // successive element is larger than the one before it
- for (int i = 0; i < list.length - 1; i++)
- {
- if (list[i].compareTo(list[i + 1]) > 0)
- {
- sorted = false;
- }
- }
- return sorted;
- }
- /**
- * Replaces all but the first and last with the larger of its to neighbors
- * You can use the compareTo to determine which string is larger (later in alphabetical
- * order.
- * @return a string representation of the array. (do this with Arrays.toString(list))
- */
- public void replaceWithLargerNeighbor()
- {
- for (int i = 1; i < list.length - 1; i++)
- {
- String first = list[i];
- String second = list[i+1];
- if (first.compareTo(second) <= 0)
- {
- list[i] = second;
- }
- else
- {
- list[i+1] = first;
- }
- }
- }
- /**
- * Gets the number of duplicates in the array.
- * (Be careful to only count each duplicate once. Start at index 0. Does it match any of the other element?
- * Get the next word. It is at index i. Does it match any of the words with index > i?)
- * @return the number of duplicate words in the array.
- */
- public int countDuplicates()
- {
- int duplicates = 0;
- for (int i = 0; i < list.length - 1; i++)
- {
- for (int j = i + 1; j < list.length; j++)
- {
- String first = list[i];
- String second = list[i+1];
- if (list[i].equals(list[j]))
- {
- duplicates++;
- }
- }
- }
- return duplicates;
- }
- /**
- * Moves any word that starts with x, y, or z to the front of the array, but
- * otherwise preserves the order
- */
- public void xyzToFront()
- {
- int insertAt = 0;
- String word = "";
- for (int i = 0; i < list.length; i++)
- {
- word = list[i].substring(0, 1);
- if ("xyz".contains(word))
- {
- word = list[i];
- // remove
- for (int j = i - 1; j >= insertAt; j--)
- {
- list[j + 1] = list[j];
- }
- list[insertAt] = word;
- insertAt++;
- }
- }
- }
- /**
- * gets the string representation of this array
- * @returns the string representation of this array in
- * standard collection format
- */
- public String toString()
- {
- return Arrays.toString(list);
- }
- }
- //////////////////
- ///////////////
- ////////////////////
- //////////////////
- /////////////////
- //We want to get the average temperature in Anchorage, Alaska, in
- //January and February, 1955.
- //
- //But to get a better idea of the normal temperature,
- //we will discard the highest and lowest temeratures. The tester will
- //get the temperatures from the website
- //http://academic.udayton.edu/kissock/http/Weather/gsod95-current/AKANCHOR.txt
- //
- //It will put them into a double[]array that is passed to the constructor of
- //your class. You will complete the TemperatureNormalizer class below.
- //The TemperatureNormalizer class has a constructor that takes an array of
- //doubles as a parameter
- //
- //public TemperatureNormalizer(double[] list)
- //It also has methods:
- //public double getAdjustedAverage() - gets the average minus the max and min
- //public double getMax()
- //public double getMin()
- //public double getSum()
- //
- //For the draft, implement the getSum method. The other methods are implemented
- //as stubs
- //
- //Note: the tester uses code we have not covered to get the values from
- //the website. You can just ignore it and think of it as the plumbing that
- //gets you a double[]
- public class TemperatureNormalizer
- {
- private double[] data;
- /**
- /* Constructs a TemperatureNormalizer with the given array
- /* @param the array to process
- */
- public TemperatureNormalizer(double[] list)
- {
- data = list;
- }
- /**
- * Gets the adjusted average of the values in this array. The adjusted average
- * is calculated by removing the highest and lowest values and calculating
- * the average of the values that are left
- * @return the adjusted average
- */
- public double getAdjustedAverage()
- {
- double min = getMin();
- double max = getMax();
- //double num = 0.0;
- //for (int i = 0; i < data.length; i++)
- //{
- // num = data[i];
- // if (num == min || num == max)
- // {
- // // remove
- // for (int j = i + 1; j < data.length; j++)
- // {
- // data[j - 1] = data[j];
- // }
- // }
- //}
- int count = 0;
- double sum = 0.0;
- for (double temp: data)
- {
- sum += temp;
- count++;
- }
- sum = sum - min - max;
- count = count - 2;
- if (count == 0)
- {
- return 0;
- }
- return sum/count;
- }
- /**
- * Gets the maximum value in the array of doubles
- * @return the maximum value
- */
- public double getMax()
- {
- double max = Double.MIN_VALUE;
- for (double num: data)
- {
- if (max < num)
- {
- max = num;
- }
- }
- return max;
- }
- /**
- * Gets the minimum value in the array of doubles
- * @return the minimum value
- */
- public double getMin()
- {
- double min = Double.MAX_VALUE;
- for (double num: data)
- {
- if (min > num)
- {
- min = num;
- }
- }
- return min;
- }
- /**
- * Gets the sum of the values in the array
- * @return the sum of the values in the array
- */
- public double getSum()
- {
- double sum = 0;
- for (double num: data)
- {
- sum += num;
- }
- return sum;
- }
- }
- /////////////
- /////////////////////
- //////////////////////
- //////////////////////
- //This problem will use the same data as the previous one. This time you are
- //to complete the TemperatureDifferenceCalculator. It has two methods:
- //
- //public double maxDifference() - Calculates the maximum difference between
- //any two consecutive days. If Jan 7 temperature is 5 degrees and
- //Jan 8 is -10 degrees, the difference between the two temperatures is 15.
- //The temperature changed 15 degrees between one day and the next. The difference
- // is always the absolute value..
- //
- //public double minDifference() - Calculates the minimum difference between
- //any two consecutive days
- //
- //For the draft, provide the method stubs (in this case the headers and
- //a return value of 0)
- public class TemperatureDifferenceCalculator
- {
- private double[] data;
- /**
- * Constructs a TemperatureDifferenceCalculator with the given array
- * @param the array to process
- */
- public TemperatureDifferenceCalculator(double[] list)
- {
- data = list;
- }
- /**
- * Gets the maximum difference between any two consecutive values
- * @return the maximum difference
- */
- // TODO: add the stub for the maxDifference method. That is the header, the braces, and the return statement
- public double maxDifference()
- {
- double diff = Double.MIN_VALUE;
- for (int i = 0; i < data.length - 1; i++)
- {
- double first = data[i];
- double second = data[i+1];
- if (Math.abs(first - second) > diff)
- {
- diff = Math.abs(first - second);
- }
- }
- return diff;
- }
- /**
- * Gets the minimum difference between any two consecutive values
- * @return the minimum difference
- */
- // TODO: add the stub for the minDifference method. That is the header, the braces, and the return statement
- public double minDifference()
- {
- double diff = Double.MAX_VALUE;
- for (int i = 0; i < data.length - 1; i++)
- {
- double first = data[i];
- double second = data[i+1];
- if (Math.abs(first - second) < diff)
- {
- diff = Math.abs(first - second);
- }
- }
- return diff;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment