Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Instructions For this assignment, you will complete two separate classes which will be used to find statistics from arrays. The first class, NumStatsArray, needs to

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Instructions For this assignment, you will complete two separate classes which will be used to find statistics from arrays. The first class, NumStatsArray, needs to store an array of doubles as a member variable. This should be declared as private and final, so none of your methods will set this variable to point at a different array. The single constructor for this class has a parameter of a double array, and should set the member variable to point to this parameter array. You will need to complete the following methods in the NumStats Array class (you may assume that the member variable array is not empty when writing these methods): .toString - returns a String with the array contents in the form (1.0, 4.0,5.0). Note that there is a comma then a single space between each pair of elements, but there are no spaces/commas at the start/end, only curly braces. average - returns the arithmetic mean average of the numbers in the array as a double. This is found by dividing the sum of the numbers by the amount of numbers in the array (note: some values returned may be slightly inaccurate due to roundoff errors, which you do not need to worry about for this assignment). range - returns a double found by subtracting the smallest number in the array from the largest number in the array sortStatus-returns an int which is equal to 1 if the array is sorted in increasing order. - 1 if it is sorted in decreasing order or if it is not sorted. The array is sorted in increasing order if every number is greater than or equal to the previous number. The array is sorted in decreasing order if every element is less than or equal to the previous number. If neither of these is true then the list is unsorted. If every element in the array is the same, then it is counted as being in increasing order The second class, StringStatsArray, needs to store an array of Strings as a member variable. This should be declared as private and final, so none of your methods will set this variable to point at a different array. The single constructor for this class has a parameter of a String array, and should set the member variable to point to this parameter array. You will need to complete the following methods in the String StatsArray class: .toString - returns a String with the array contents in the form ("string 1", "string 2", "string 3"). Note that there are double quotes around each string, and that there is a comma then a single space between each pair of elements, but there are no spaces/commas at the start/end, only curly braces. averageLength-returns the arithmetic mean average of the length of the Strings in the array as a double (note: as before some values returned may have roundoff errors, which you do not need to worry about for this assignment). Search - has a single String parameter and returns an int which is equal to the index of the first appearance of the parameter String, or -1 if it does not appear in the array, sortStatus-returns an int which is equal to 1 if the array is sorted in alphabetical order. - 1 if it is sorted in reverse alphabetical order or Oif it is not sorted in either of these ways. Note that a String may appear twice in a row in an array which is sorted in alphabetical or reverse- alphabetical order. If every String in the array is the same, then it is counted as being in alphabetical order. You should use the runner_StatsArray class to test your classes. Do not add a main method to the NumstatsArrayjava or StringStats Array.java files, as it will cause your submission to be scored incorrectly. Milestones As you work on this assignment, you can use the milestones below to inform your development process: Milestone 1: Add the double array member variable to the NumStats Arrray class and complete the constructor. Write the toString method for the class and test that it formats the output string in the exact format described. Milestone 2: Write a loop in the average method which sums the values then use this to return the average. Write a loop to iterate through the array and find the minimum/maximum value in the range method, then use these values to calculate and return the range. Milestone 3: Write code in the sortStatus method which iterates through the array. Use separate flag variables to determine at the end of the array whether it is in increasing or decreasing order. Add return statements which return the correct values depending on ift/how the list is sorted. Milestone 4: Add the String array member variable to the String StatsArrray class and complete the constructor. Write the toString method for the class and test that it formats the output string in the exact format described. Milestone 5: Reuse and modify the code used for the average method in the NumStatsArray class to implement the averageLength method in StringStatsArray. Remember that the lengths of Strings are ints not doubles, but your method must return a double for the average length. Write a simple linear search algorithm in the search method to return the position of the parameter string, or -1 if it does not appear, Milestone 6: Reuse and modify the code used for the sortStatus method in the NumStatsArray class to write the sortStatus method for the StringStatsArray class. You may need to revise how to test whether one string appears before or after the other alphabetically, Files STATUS O NOT SUBMITTED SAVE SUBMIT 1 - public class NumStatsArray{ NumStatsArray.java //Add a final private variable to hold double array String StatsArray.java 00 von WN runner_StatsArray.java 5, public NumStatsarray(double [] a) { 6 //Write constructor code 7 } 8 9 // Implement all methods as described 10 public String toString() { 11 return; 12 } 13 14 public double average 15 return 0; 16 ) 17 18 public double range({ 19 return 0; } 21 22 public int sortStatus() { 23 return 0; 24 } 25 26 } 27 20 Files STATUS O NOT SUBMITTED SAVE SUBMIT NumStatsArray.java String StatsArray.java runner_StatsArray.java 1 - public class StringStatsArray 2 3 1/Add a final private variable to hold String array 4 5- public StringstatsArray(String[] a){ 6 I/Write constructor code 7 } 8 9 // Implement all methods as described 10- public String toString({ 11 return ""; 12 13 14 public double averageLength(){ 15 return 0; 16 } 17 18 public int search(String target) { 19 return 0; 20 } 21 22. public int sortStatus() { 23 return 0; 24 } 25 26 ) 27 Files NumStats nay.java String StatsArrayjava 6 runner_StatsArrayjava O NOT SUBMITTED 1 - Import java.util.Scanner; 2 3 - public class runner-StatsArray 4 5 public static Scanner scan - new Scanner(System.in); 7. public static void main(String[] args) { int ons ; whitecans - 3) Systen.out.println("Test which clessini - NurStatstroyinz - Stringstatsarray in3 - quit": ans - scan.nextInto: 12 1fCans - 1) testNums: 14 else if(ans 2) test StringsO; else if(ans 1-3) System.out.println("Not a valid choice"): Systen.out.printino: } 8 9- 10 11 } 15 16 17 18 19 20 21 22- 23 24 25 26 27 - 28 29 30 31 32 33 34 35 36 37 public static void testNSOL System.out.println("Enter array length: int len - scan.nextInto: double vals - nen double[len); System.out.println("Enter values:"); for(int i = 8; 1 39 40 41 42 43 44 - 45 46 47 48 49 58 51 52 53 54 55 56 ) 57 public static void testStrings System.out.println("Enter array length: "); int len-scan.nextInto: String() vals -new String[en]: scan.nextLine: System.out.println("Enter values:"); for(int ; 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems On GPUs In Databases

Authors: Johns Paul ,Shengliang Lu ,Bingsheng He

1st Edition

1680838482, 978-1680838480

More Books

Students also viewed these Databases questions