import java.util.Scanner; public class Histogram { private static final int SENTINAL = 999; private static final...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
import java.util.Scanner; public class Histogram { private static final int SENTINAL = −999; private static final int MAX_NUMBERS = 20; private static final double UPPER_BOUND = 100.0; private static final double LOWER_BOUND = 0.0; private static final int NUM_BINS = 10%; //private static final int BIN_SIZE = ?? // sentinal value to signal endo of input // maximum number of numbers to input // largest numbers accepted as data // smallest numbers accepted as adata // number of bins in range [0..100] // size of each bin * Method to show an example of using StringBuilder. * * You will also notice that this method is called from the * main function. * */ public static String getHeaderAsString( String me ) { // Create an instance of the StringBuilder class // which allows us to create an object of // a series of strings that can then be converted // into one large string via the toString method. // StringBuilder sb=new StringBuilder(); " + me + "!" ); sb.append( System.getProperty("line.separator") ); sb.append( "Welcome to the Histogram Program me = getFirstName (me); " sb.append( System.getProperty("line.separator") ); sb.append( System.getProperty("line.separator") ); sb.append( "This program will print out a histogram of the numbers" i sb.append( System.getProperty("line.separator") ); sb.append( "input by you + getFirstName (me) + sb.append( System.getProperty("line.separator") ); sb.append( System.getProperty("line.separator") ); sb.append( "Please enter up to + MAX NUMBERS + sb.append( System.getProperty("line.separator") ); ); " doubles or + SENTINAL + to stop input!" ); return sb.toString(); } * Method to return the first name of the user in case *the full name was entered. */ public static String getFirstName (String name ) { // Note that add the " " to string to be sure // there is something to split return (name+" ").split(" ")[0]; } * * local main test driver * */ public static void main(String[] args) { } /* * // Connect to the keyboard as the input stream Scanner userInput = new Scanner(System.in); System.out.print( "And who am I working with today? String user = userInput.nextLine(); String heading = getHeaderAsString( user ); // Print out welcome message System.out.println( heading ); // Call the method which prompts the user // to input the numbers that will be used // to build the histogram. //double[] numbers = inputNumbers( userInput ); // Call the method to reate the array histogram //int[] histogram = calculateHistogram( numbers ); // Print the historgram //System.out.println( toString( histogram ) ); * COMPLETE YOUR METHODS * } // end of class ); For this problem, write a program Histogram. java which implements a static class named Histogram. This class will provide a series of static methods which will allow a client program to produce and display a histogram of a series of floating point values. Note A histogram is a graphical representation depicting the frequency in which numbers occur within specified ranges. Example: [0..10], (10..20], (20..30],...., (90..100], where [a..b] denotes the set of numbers x such that: a <= x <= b and (a..b] denotes the set of numbers x such that: a < x <= b. Example, assuming the following series of numbers: 1 11 11.123 41 47 51 61.7 71 81 91 2.5 12 22 44.3 42.9 52 62 72 82 92 The resulting Histogram of Values in Decades from 0 to 100 is: [0..10]: ** (10..20]: *** (20..30]: * (30..40]: (40..50]: **** ** (50..60]: (60..70]: ** (70..80]: (80..90]: ** ** (90..100]: ** Important guidelines: ■ The numbers that will be used to compute the histogram should be stored in an array of doubles. The array should be large enough to store the maximum possible inputs. ■ For testing purposes, the array can be expicitly populated up to its' maximum size, but your program must also provide a method which populates the array through user input. See below. ■ The histogram itself (i.e. counts for the various ranges) should also be stored as an array. Think what the data type of this array which represents the histogram should be. ■ Make sure to follow the method headers for each method you need to implement as specified below. Altering the method signature in any way will prevent us from testing your methods. 1. Write a method calulateHistogram that computes and returns an array that represents a histogram created from thearray of numbers passed to the method. // This method should create and return an array of integers // that represents the resulting histogram from the numbers // entered and passed to the method. // public static int ☐ calculateHistogram( double ☐ numbers ) { } .... 2. Write a method findBin that determines and returns the bin (i.e. index of the Histogram array) that the number (i.e. argument num passed to the method) belongs in. public static int findBin( double num ) { } // There are a few ways to do this, one is a series of conditional // statements to find the correct bin. But, if you know the range // of numbers that belong in a specific bin... think about it. // 3. Write a method toString to construct and return a String representation of the histogram (similar to the toString method of the Arrays class). public static String toString( int ☐ histogram ) { } // The histogram can be visualzed as a series of // buckets, where each bucket represents one range // of the histogram: [0..10]: **** (10..20]: ** (20..30]: *** (30..40]: * ] // The string returned should only contain the string representation // of the histogram and no other verbeage. It should function // like the toString method of the Array class but specific to // creating a histogram. // You may want to create an instance of the // StringBuilder class to assist you in this method. // Follow the code in the method getHeaderAsString // as a guide. You can also use string concatenation. 4. Write a method to determine if the integer passed to the method is in the legal range of valid inputs as specified by the static variables of the class, LOWER_BOUND and UPPER_BOUND: public static boolean valid Input( double num ) { } 5. Write a method that performs the user input. This method accepts an object of the scanner class, uses this object to perform user input by calling the apporopriate methods, and returns an array of the floating point values that were input. The Scanner object passed to this method should be created in the main method and passed to this method. The array returned from this method will be passed to the calculateHistogram method to create the Histogram. See the template code provided. public static double ☐☐ inputNumbers( Scanner scan ) { } A few guidelines on user input: * The user may input at most the maximum amount of numbers as specified by the static variable `MAX_NUMBERS`. Each number entered must be between the valid bounds. This method should do error checking and reporting accordingly: Any input number outside the range is an input error, and the program should report the error and ask for a correct input. * Consider the correct data types and methods of Scanner class that should be used. * Your method will also need to determine how to keep track of how many numbers you have read in (i.e. input) so you do not exceed the maximum numbers to be entered. * To not force the user to enter the maximum inputs allowed, you can make use of a *sentinal* value to signal end of intput. For examp continue to accept input values until the value as specified by the static variable `SENTINAL` is entered (or off course until the maximum number of inputs has been reached). The logic here can get more complicated then you may think. Think through all the possib cases carefully. *The array that this method returns should only be as large as the number of inputs read in. 6. Finally, make sure you use the static variables which have been declared in the template provided. You should NOT have the numbers 20 or 10 occur anywhere in your program except in the declarations of these variables. You MUST do this so that if you decide to change one of these, you don't have the "multiple update" problem. Here is an example of what you should use: public class Histogram { public static final int SENTINAL public static final int MAX_NUMBERS public static final int NUM_BINS = = -999; 20; = 10; // sentinal value used to // maximum number of numb // number of bins in rang // UPPER_BOUND to represent the largest possible number in the rang // LOWER_BOUND to represent the smalles possible number in the rang // BIN_SIZE how many different values fall into each bin public static void main(....) etc. Important The values assigned to MAX_NUMBERS and NUM_BINS determine the range of values represented by each bin. For example: number of bins size of bins 10 5 10 i.e., [0..10], (10..20], 20 i.e., [0..20], (20..40] (90..100] (80...100] 2 50 i.e., [0..50], (50..100] It is clear when NUM_BINS is 10, then so is the size, but this is a coincidence. Your program should determine the size (i.e. range) of each bin. Consider how another variable could be used here. Don't worry about the possible error when the number of bins does not divide evenly into 100 (we'll run your program as is, and won't change the number of bins from 10 to something else). Sample executions of Histogram.java: Sample Run #1 ☐ Console X <terminated> Histogram [Java Application] /Library/Java/JavaVirtual Machines/jdk1.8.0_131.jdk/Contents/Home/bin/java (Jul 7, 2017, 3:25:46 PM) Welcome to the Histogram Program! This program will print out a histogram of the numbers input by the user; enter up to 20 doubles in the range [0.. 100]; enter 1 to end. 13657F655MN 45 56.7 54.9 34.9 2 99 8 -1 You input 13 numbers: [1.0, 3.0, 6.0, 5.0, 7.0, 45.0, 65.0, 56.7, 54.9, 34.9, 2.0, 99.0, 8.0] Histogram of Values in Decades from 0 to 100: [0..10]: (10..20]: ******* (20..30]: (30..40]: (40..50]: (50..60]: **** (60..70]: (70..80]: (80..90]: (90..100]: This sample run uses -1 as the sentinal value, you just need to use the variable as set by the class variable SENTINAL import java.util.Scanner; public class Histogram { private static final int SENTINAL = −999; private static final int MAX_NUMBERS = 20; private static final double UPPER_BOUND = 100.0; private static final double LOWER_BOUND = 0.0; private static final int NUM_BINS = 10%; //private static final int BIN_SIZE = ?? // sentinal value to signal endo of input // maximum number of numbers to input // largest numbers accepted as data // smallest numbers accepted as adata // number of bins in range [0..100] // size of each bin * Method to show an example of using StringBuilder. * * You will also notice that this method is called from the * main function. * */ public static String getHeaderAsString( String me ) { // Create an instance of the StringBuilder class // which allows us to create an object of // a series of strings that can then be converted // into one large string via the toString method. // StringBuilder sb=new StringBuilder(); " + me + "!" ); sb.append( System.getProperty("line.separator") ); sb.append( "Welcome to the Histogram Program me = getFirstName (me); " sb.append( System.getProperty("line.separator") ); sb.append( System.getProperty("line.separator") ); sb.append( "This program will print out a histogram of the numbers" i sb.append( System.getProperty("line.separator") ); sb.append( "input by you + getFirstName (me) + sb.append( System.getProperty("line.separator") ); sb.append( System.getProperty("line.separator") ); sb.append( "Please enter up to + MAX NUMBERS + sb.append( System.getProperty("line.separator") ); ); " doubles or + SENTINAL + to stop input!" ); return sb.toString(); } * Method to return the first name of the user in case *the full name was entered. */ public static String getFirstName (String name ) { // Note that add the " " to string to be sure // there is something to split return (name+" ").split(" ")[0]; } * * local main test driver * */ public static void main(String[] args) { } /* * // Connect to the keyboard as the input stream Scanner userInput = new Scanner(System.in); System.out.print( "And who am I working with today? String user = userInput.nextLine(); String heading = getHeaderAsString( user ); // Print out welcome message System.out.println( heading ); // Call the method which prompts the user // to input the numbers that will be used // to build the histogram. //double[] numbers = inputNumbers( userInput ); // Call the method to reate the array histogram //int[] histogram = calculateHistogram( numbers ); // Print the historgram //System.out.println( toString( histogram ) ); * COMPLETE YOUR METHODS * } // end of class ); For this problem, write a program Histogram. java which implements a static class named Histogram. This class will provide a series of static methods which will allow a client program to produce and display a histogram of a series of floating point values. Note A histogram is a graphical representation depicting the frequency in which numbers occur within specified ranges. Example: [0..10], (10..20], (20..30],...., (90..100], where [a..b] denotes the set of numbers x such that: a <= x <= b and (a..b] denotes the set of numbers x such that: a < x <= b. Example, assuming the following series of numbers: 1 11 11.123 41 47 51 61.7 71 81 91 2.5 12 22 44.3 42.9 52 62 72 82 92 The resulting Histogram of Values in Decades from 0 to 100 is: [0..10]: ** (10..20]: *** (20..30]: * (30..40]: (40..50]: **** ** (50..60]: (60..70]: ** (70..80]: (80..90]: ** ** (90..100]: ** Important guidelines: ■ The numbers that will be used to compute the histogram should be stored in an array of doubles. The array should be large enough to store the maximum possible inputs. ■ For testing purposes, the array can be expicitly populated up to its' maximum size, but your program must also provide a method which populates the array through user input. See below. ■ The histogram itself (i.e. counts for the various ranges) should also be stored as an array. Think what the data type of this array which represents the histogram should be. ■ Make sure to follow the method headers for each method you need to implement as specified below. Altering the method signature in any way will prevent us from testing your methods. 1. Write a method calulateHistogram that computes and returns an array that represents a histogram created from thearray of numbers passed to the method. // This method should create and return an array of integers // that represents the resulting histogram from the numbers // entered and passed to the method. // public static int ☐ calculateHistogram( double ☐ numbers ) { } .... 2. Write a method findBin that determines and returns the bin (i.e. index of the Histogram array) that the number (i.e. argument num passed to the method) belongs in. public static int findBin( double num ) { } // There are a few ways to do this, one is a series of conditional // statements to find the correct bin. But, if you know the range // of numbers that belong in a specific bin... think about it. // 3. Write a method toString to construct and return a String representation of the histogram (similar to the toString method of the Arrays class). public static String toString( int ☐ histogram ) { } // The histogram can be visualzed as a series of // buckets, where each bucket represents one range // of the histogram: [0..10]: **** (10..20]: ** (20..30]: *** (30..40]: * ] // The string returned should only contain the string representation // of the histogram and no other verbeage. It should function // like the toString method of the Array class but specific to // creating a histogram. // You may want to create an instance of the // StringBuilder class to assist you in this method. // Follow the code in the method getHeaderAsString // as a guide. You can also use string concatenation. 4. Write a method to determine if the integer passed to the method is in the legal range of valid inputs as specified by the static variables of the class, LOWER_BOUND and UPPER_BOUND: public static boolean valid Input( double num ) { } 5. Write a method that performs the user input. This method accepts an object of the scanner class, uses this object to perform user input by calling the apporopriate methods, and returns an array of the floating point values that were input. The Scanner object passed to this method should be created in the main method and passed to this method. The array returned from this method will be passed to the calculateHistogram method to create the Histogram. See the template code provided. public static double ☐☐ inputNumbers( Scanner scan ) { } A few guidelines on user input: * The user may input at most the maximum amount of numbers as specified by the static variable `MAX_NUMBERS`. Each number entered must be between the valid bounds. This method should do error checking and reporting accordingly: Any input number outside the range is an input error, and the program should report the error and ask for a correct input. * Consider the correct data types and methods of Scanner class that should be used. * Your method will also need to determine how to keep track of how many numbers you have read in (i.e. input) so you do not exceed the maximum numbers to be entered. * To not force the user to enter the maximum inputs allowed, you can make use of a *sentinal* value to signal end of intput. For examp continue to accept input values until the value as specified by the static variable `SENTINAL` is entered (or off course until the maximum number of inputs has been reached). The logic here can get more complicated then you may think. Think through all the possib cases carefully. *The array that this method returns should only be as large as the number of inputs read in. 6. Finally, make sure you use the static variables which have been declared in the template provided. You should NOT have the numbers 20 or 10 occur anywhere in your program except in the declarations of these variables. You MUST do this so that if you decide to change one of these, you don't have the "multiple update" problem. Here is an example of what you should use: public class Histogram { public static final int SENTINAL public static final int MAX_NUMBERS public static final int NUM_BINS = = -999; 20; = 10; // sentinal value used to // maximum number of numb // number of bins in rang // UPPER_BOUND to represent the largest possible number in the rang // LOWER_BOUND to represent the smalles possible number in the rang // BIN_SIZE how many different values fall into each bin public static void main(....) etc. Important The values assigned to MAX_NUMBERS and NUM_BINS determine the range of values represented by each bin. For example: number of bins size of bins 10 5 10 i.e., [0..10], (10..20], 20 i.e., [0..20], (20..40] (90..100] (80...100] 2 50 i.e., [0..50], (50..100] It is clear when NUM_BINS is 10, then so is the size, but this is a coincidence. Your program should determine the size (i.e. range) of each bin. Consider how another variable could be used here. Don't worry about the possible error when the number of bins does not divide evenly into 100 (we'll run your program as is, and won't change the number of bins from 10 to something else). Sample executions of Histogram.java: Sample Run #1 ☐ Console X <terminated> Histogram [Java Application] /Library/Java/JavaVirtual Machines/jdk1.8.0_131.jdk/Contents/Home/bin/java (Jul 7, 2017, 3:25:46 PM) Welcome to the Histogram Program! This program will print out a histogram of the numbers input by the user; enter up to 20 doubles in the range [0.. 100]; enter 1 to end. 13657F655MN 45 56.7 54.9 34.9 2 99 8 -1 You input 13 numbers: [1.0, 3.0, 6.0, 5.0, 7.0, 45.0, 65.0, 56.7, 54.9, 34.9, 2.0, 99.0, 8.0] Histogram of Values in Decades from 0 to 100: [0..10]: (10..20]: ******* (20..30]: (30..40]: (40..50]: (50..60]: **** (60..70]: (70..80]: (80..90]: (90..100]: This sample run uses -1 as the sentinal value, you just need to use the variable as set by the class variable SENTINAL import java.util.Scanner; public class Histogram { private static final int SENTINAL = −999; private static final int MAX_NUMBERS = 20; private static final double UPPER_BOUND = 100.0; private static final double LOWER_BOUND = 0.0; private static final int NUM_BINS = 10%; //private static final int BIN_SIZE = ?? // sentinal value to signal endo of input // maximum number of numbers to input // largest numbers accepted as data // smallest numbers accepted as adata // number of bins in range [0..100] // size of each bin * Method to show an example of using StringBuilder. * * You will also notice that this method is called from the * main function. * */ public static String getHeaderAsString( String me ) { // Create an instance of the StringBuilder class // which allows us to create an object of // a series of strings that can then be converted // into one large string via the toString method. // StringBuilder sb=new StringBuilder(); " + me + "!" ); sb.append( System.getProperty("line.separator") ); sb.append( "Welcome to the Histogram Program me = getFirstName (me); " sb.append( System.getProperty("line.separator") ); sb.append( System.getProperty("line.separator") ); sb.append( "This program will print out a histogram of the numbers" i sb.append( System.getProperty("line.separator") ); sb.append( "input by you + getFirstName (me) + sb.append( System.getProperty("line.separator") ); sb.append( System.getProperty("line.separator") ); sb.append( "Please enter up to + MAX NUMBERS + sb.append( System.getProperty("line.separator") ); ); " doubles or + SENTINAL + to stop input!" ); return sb.toString(); } * Method to return the first name of the user in case *the full name was entered. */ public static String getFirstName (String name ) { // Note that add the " " to string to be sure // there is something to split return (name+" ").split(" ")[0]; } * * local main test driver * */ public static void main(String[] args) { } /* * // Connect to the keyboard as the input stream Scanner userInput = new Scanner(System.in); System.out.print( "And who am I working with today? String user = userInput.nextLine(); String heading = getHeaderAsString( user ); // Print out welcome message System.out.println( heading ); // Call the method which prompts the user // to input the numbers that will be used // to build the histogram. //double[] numbers = inputNumbers( userInput ); // Call the method to reate the array histogram //int[] histogram = calculateHistogram( numbers ); // Print the historgram //System.out.println( toString( histogram ) ); * COMPLETE YOUR METHODS * } // end of class ); For this problem, write a program Histogram. java which implements a static class named Histogram. This class will provide a series of static methods which will allow a client program to produce and display a histogram of a series of floating point values. Note A histogram is a graphical representation depicting the frequency in which numbers occur within specified ranges. Example: [0..10], (10..20], (20..30],...., (90..100], where [a..b] denotes the set of numbers x such that: a <= x <= b and (a..b] denotes the set of numbers x such that: a < x <= b. Example, assuming the following series of numbers: 1 11 11.123 41 47 51 61.7 71 81 91 2.5 12 22 44.3 42.9 52 62 72 82 92 The resulting Histogram of Values in Decades from 0 to 100 is: [0..10]: ** (10..20]: *** (20..30]: * (30..40]: (40..50]: **** ** (50..60]: (60..70]: ** (70..80]: (80..90]: ** ** (90..100]: ** Important guidelines: ■ The numbers that will be used to compute the histogram should be stored in an array of doubles. The array should be large enough to store the maximum possible inputs. ■ For testing purposes, the array can be expicitly populated up to its' maximum size, but your program must also provide a method which populates the array through user input. See below. ■ The histogram itself (i.e. counts for the various ranges) should also be stored as an array. Think what the data type of this array which represents the histogram should be. ■ Make sure to follow the method headers for each method you need to implement as specified below. Altering the method signature in any way will prevent us from testing your methods. 1. Write a method calulateHistogram that computes and returns an array that represents a histogram created from thearray of numbers passed to the method. // This method should create and return an array of integers // that represents the resulting histogram from the numbers // entered and passed to the method. // public static int ☐ calculateHistogram( double ☐ numbers ) { } .... 2. Write a method findBin that determines and returns the bin (i.e. index of the Histogram array) that the number (i.e. argument num passed to the method) belongs in. public static int findBin( double num ) { } // There are a few ways to do this, one is a series of conditional // statements to find the correct bin. But, if you know the range // of numbers that belong in a specific bin... think about it. // 3. Write a method toString to construct and return a String representation of the histogram (similar to the toString method of the Arrays class). public static String toString( int ☐ histogram ) { } // The histogram can be visualzed as a series of // buckets, where each bucket represents one range // of the histogram: [0..10]: **** (10..20]: ** (20..30]: *** (30..40]: * ] // The string returned should only contain the string representation // of the histogram and no other verbeage. It should function // like the toString method of the Array class but specific to // creating a histogram. // You may want to create an instance of the // StringBuilder class to assist you in this method. // Follow the code in the method getHeaderAsString // as a guide. You can also use string concatenation. 4. Write a method to determine if the integer passed to the method is in the legal range of valid inputs as specified by the static variables of the class, LOWER_BOUND and UPPER_BOUND: public static boolean valid Input( double num ) { } 5. Write a method that performs the user input. This method accepts an object of the scanner class, uses this object to perform user input by calling the apporopriate methods, and returns an array of the floating point values that were input. The Scanner object passed to this method should be created in the main method and passed to this method. The array returned from this method will be passed to the calculateHistogram method to create the Histogram. See the template code provided. public static double ☐☐ inputNumbers( Scanner scan ) { } A few guidelines on user input: * The user may input at most the maximum amount of numbers as specified by the static variable `MAX_NUMBERS`. Each number entered must be between the valid bounds. This method should do error checking and reporting accordingly: Any input number outside the range is an input error, and the program should report the error and ask for a correct input. * Consider the correct data types and methods of Scanner class that should be used. * Your method will also need to determine how to keep track of how many numbers you have read in (i.e. input) so you do not exceed the maximum numbers to be entered. * To not force the user to enter the maximum inputs allowed, you can make use of a *sentinal* value to signal end of intput. For examp continue to accept input values until the value as specified by the static variable `SENTINAL` is entered (or off course until the maximum number of inputs has been reached). The logic here can get more complicated then you may think. Think through all the possib cases carefully. *The array that this method returns should only be as large as the number of inputs read in. 6. Finally, make sure you use the static variables which have been declared in the template provided. You should NOT have the numbers 20 or 10 occur anywhere in your program except in the declarations of these variables. You MUST do this so that if you decide to change one of these, you don't have the "multiple update" problem. Here is an example of what you should use: public class Histogram { public static final int SENTINAL public static final int MAX_NUMBERS public static final int NUM_BINS = = -999; 20; = 10; // sentinal value used to // maximum number of numb // number of bins in rang // UPPER_BOUND to represent the largest possible number in the rang // LOWER_BOUND to represent the smalles possible number in the rang // BIN_SIZE how many different values fall into each bin public static void main(....) etc. Important The values assigned to MAX_NUMBERS and NUM_BINS determine the range of values represented by each bin. For example: number of bins size of bins 10 5 10 i.e., [0..10], (10..20], 20 i.e., [0..20], (20..40] (90..100] (80...100] 2 50 i.e., [0..50], (50..100] It is clear when NUM_BINS is 10, then so is the size, but this is a coincidence. Your program should determine the size (i.e. range) of each bin. Consider how another variable could be used here. Don't worry about the possible error when the number of bins does not divide evenly into 100 (we'll run your program as is, and won't change the number of bins from 10 to something else). Sample executions of Histogram.java: Sample Run #1 ☐ Console X <terminated> Histogram [Java Application] /Library/Java/JavaVirtual Machines/jdk1.8.0_131.jdk/Contents/Home/bin/java (Jul 7, 2017, 3:25:46 PM) Welcome to the Histogram Program! This program will print out a histogram of the numbers input by the user; enter up to 20 doubles in the range [0.. 100]; enter 1 to end. 13657F655MN 45 56.7 54.9 34.9 2 99 8 -1 You input 13 numbers: [1.0, 3.0, 6.0, 5.0, 7.0, 45.0, 65.0, 56.7, 54.9, 34.9, 2.0, 99.0, 8.0] Histogram of Values in Decades from 0 to 100: [0..10]: (10..20]: ******* (20..30]: (30..40]: (40..50]: (50..60]: **** (60..70]: (70..80]: (80..90]: (90..100]: This sample run uses -1 as the sentinal value, you just need to use the variable as set by the class variable SENTINAL import java.util.Scanner; public class Histogram { private static final int SENTINAL = −999; private static final int MAX_NUMBERS = 20; private static final double UPPER_BOUND = 100.0; private static final double LOWER_BOUND = 0.0; private static final int NUM_BINS = 10%; //private static final int BIN_SIZE = ?? // sentinal value to signal endo of input // maximum number of numbers to input // largest numbers accepted as data // smallest numbers accepted as adata // number of bins in range [0..100] // size of each bin * Method to show an example of using StringBuilder. * * You will also notice that this method is called from the * main function. * */ public static String getHeaderAsString( String me ) { // Create an instance of the StringBuilder class // which allows us to create an object of // a series of strings that can then be converted // into one large string via the toString method. // StringBuilder sb=new StringBuilder(); " + me + "!" ); sb.append( System.getProperty("line.separator") ); sb.append( "Welcome to the Histogram Program me = getFirstName (me); " sb.append( System.getProperty("line.separator") ); sb.append( System.getProperty("line.separator") ); sb.append( "This program will print out a histogram of the numbers" i sb.append( System.getProperty("line.separator") ); sb.append( "input by you + getFirstName (me) + sb.append( System.getProperty("line.separator") ); sb.append( System.getProperty("line.separator") ); sb.append( "Please enter up to + MAX NUMBERS + sb.append( System.getProperty("line.separator") ); ); " doubles or + SENTINAL + to stop input!" ); return sb.toString(); } * Method to return the first name of the user in case *the full name was entered. */ public static String getFirstName (String name ) { // Note that add the " " to string to be sure // there is something to split return (name+" ").split(" ")[0]; } * * local main test driver * */ public static void main(String[] args) { } /* * // Connect to the keyboard as the input stream Scanner userInput = new Scanner(System.in); System.out.print( "And who am I working with today? String user = userInput.nextLine(); String heading = getHeaderAsString( user ); // Print out welcome message System.out.println( heading ); // Call the method which prompts the user // to input the numbers that will be used // to build the histogram. //double[] numbers = inputNumbers( userInput ); // Call the method to reate the array histogram //int[] histogram = calculateHistogram( numbers ); // Print the historgram //System.out.println( toString( histogram ) ); * COMPLETE YOUR METHODS * } // end of class ); For this problem, write a program Histogram. java which implements a static class named Histogram. This class will provide a series of static methods which will allow a client program to produce and display a histogram of a series of floating point values. Note A histogram is a graphical representation depicting the frequency in which numbers occur within specified ranges. Example: [0..10], (10..20], (20..30],...., (90..100], where [a..b] denotes the set of numbers x such that: a <= x <= b and (a..b] denotes the set of numbers x such that: a < x <= b. Example, assuming the following series of numbers: 1 11 11.123 41 47 51 61.7 71 81 91 2.5 12 22 44.3 42.9 52 62 72 82 92 The resulting Histogram of Values in Decades from 0 to 100 is: [0..10]: ** (10..20]: *** (20..30]: * (30..40]: (40..50]: **** ** (50..60]: (60..70]: ** (70..80]: (80..90]: ** ** (90..100]: ** Important guidelines: ■ The numbers that will be used to compute the histogram should be stored in an array of doubles. The array should be large enough to store the maximum possible inputs. ■ For testing purposes, the array can be expicitly populated up to its' maximum size, but your program must also provide a method which populates the array through user input. See below. ■ The histogram itself (i.e. counts for the various ranges) should also be stored as an array. Think what the data type of this array which represents the histogram should be. ■ Make sure to follow the method headers for each method you need to implement as specified below. Altering the method signature in any way will prevent us from testing your methods. 1. Write a method calulateHistogram that computes and returns an array that represents a histogram created from thearray of numbers passed to the method. // This method should create and return an array of integers // that represents the resulting histogram from the numbers // entered and passed to the method. // public static int ☐ calculateHistogram( double ☐ numbers ) { } .... 2. Write a method findBin that determines and returns the bin (i.e. index of the Histogram array) that the number (i.e. argument num passed to the method) belongs in. public static int findBin( double num ) { } // There are a few ways to do this, one is a series of conditional // statements to find the correct bin. But, if you know the range // of numbers that belong in a specific bin... think about it. // 3. Write a method toString to construct and return a String representation of the histogram (similar to the toString method of the Arrays class). public static String toString( int ☐ histogram ) { } // The histogram can be visualzed as a series of // buckets, where each bucket represents one range // of the histogram: [0..10]: **** (10..20]: ** (20..30]: *** (30..40]: * ] // The string returned should only contain the string representation // of the histogram and no other verbeage. It should function // like the toString method of the Array class but specific to // creating a histogram. // You may want to create an instance of the // StringBuilder class to assist you in this method. // Follow the code in the method getHeaderAsString // as a guide. You can also use string concatenation. 4. Write a method to determine if the integer passed to the method is in the legal range of valid inputs as specified by the static variables of the class, LOWER_BOUND and UPPER_BOUND: public static boolean valid Input( double num ) { } 5. Write a method that performs the user input. This method accepts an object of the scanner class, uses this object to perform user input by calling the apporopriate methods, and returns an array of the floating point values that were input. The Scanner object passed to this method should be created in the main method and passed to this method. The array returned from this method will be passed to the calculateHistogram method to create the Histogram. See the template code provided. public static double ☐☐ inputNumbers( Scanner scan ) { } A few guidelines on user input: * The user may input at most the maximum amount of numbers as specified by the static variable `MAX_NUMBERS`. Each number entered must be between the valid bounds. This method should do error checking and reporting accordingly: Any input number outside the range is an input error, and the program should report the error and ask for a correct input. * Consider the correct data types and methods of Scanner class that should be used. * Your method will also need to determine how to keep track of how many numbers you have read in (i.e. input) so you do not exceed the maximum numbers to be entered. * To not force the user to enter the maximum inputs allowed, you can make use of a *sentinal* value to signal end of intput. For examp continue to accept input values until the value as specified by the static variable `SENTINAL` is entered (or off course until the maximum number of inputs has been reached). The logic here can get more complicated then you may think. Think through all the possib cases carefully. *The array that this method returns should only be as large as the number of inputs read in. 6. Finally, make sure you use the static variables which have been declared in the template provided. You should NOT have the numbers 20 or 10 occur anywhere in your program except in the declarations of these variables. You MUST do this so that if you decide to change one of these, you don't have the "multiple update" problem. Here is an example of what you should use: public class Histogram { public static final int SENTINAL public static final int MAX_NUMBERS public static final int NUM_BINS = = -999; 20; = 10; // sentinal value used to // maximum number of numb // number of bins in rang // UPPER_BOUND to represent the largest possible number in the rang // LOWER_BOUND to represent the smalles possible number in the rang // BIN_SIZE how many different values fall into each bin public static void main(....) etc. Important The values assigned to MAX_NUMBERS and NUM_BINS determine the range of values represented by each bin. For example: number of bins size of bins 10 5 10 i.e., [0..10], (10..20], 20 i.e., [0..20], (20..40] (90..100] (80...100] 2 50 i.e., [0..50], (50..100] It is clear when NUM_BINS is 10, then so is the size, but this is a coincidence. Your program should determine the size (i.e. range) of each bin. Consider how another variable could be used here. Don't worry about the possible error when the number of bins does not divide evenly into 100 (we'll run your program as is, and won't change the number of bins from 10 to something else). Sample executions of Histogram.java: Sample Run #1 ☐ Console X <terminated> Histogram [Java Application] /Library/Java/JavaVirtual Machines/jdk1.8.0_131.jdk/Contents/Home/bin/java (Jul 7, 2017, 3:25:46 PM) Welcome to the Histogram Program! This program will print out a histogram of the numbers input by the user; enter up to 20 doubles in the range [0.. 100]; enter 1 to end. 13657F655MN 45 56.7 54.9 34.9 2 99 8 -1 You input 13 numbers: [1.0, 3.0, 6.0, 5.0, 7.0, 45.0, 65.0, 56.7, 54.9, 34.9, 2.0, 99.0, 8.0] Histogram of Values in Decades from 0 to 100: [0..10]: (10..20]: ******* (20..30]: (30..40]: (40..50]: (50..60]: **** (60..70]: (70..80]: (80..90]: (90..100]: This sample run uses -1 as the sentinal value, you just need to use the variable as set by the class variable SENTINAL
Expert Answer:
Answer rating: 100% (QA)
The answer provided below has been developed in a clear step by step manner Step 1 inptuElements met... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
This question concerns lexical grammars. (a) Tree Adjoining Grammars contain two types of elementary tree. (i) What are these trees called? [1 mark] (ii) If one were building a grammar for English...
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
Describe the role of the police in contemporary society? 2. What does Alex Vitale argue? Do you agree with him? 3. Discuss the impact of the #Black Lives Matters protests. 4. How do you see the...
-
The following solvency ratios are available for Drira Corporation: 2012 2011 Debt to total assets.......................50%......................45% Times interest earned...............10...
-
Rewrite 4x 2 by using an absolute value.
-
In the past five years, there have been significant innovations in technology such as smartphones and tablets. Technology companies rely on intellectual property (IP) rights, such as patents,...
-
The balance sheet of Hutter Amalgamated is shown below. If the 12/31/2010 value of operations is $756 million, what is the 12/31/2010 intrinsic market value of equity? Balance Sheet, December 31,...
-
A 5-year housing project costing P500,000 is projected to give the following annual returns: Year 1 - P 75,000 Year 2 - 150,000 Year 3 - 225,000 Year 4 - 300,000 Year 5 - 350,000 Will you accept the...
-
The Great Gatsby's Rolls-Royce repair is a small business on Long Island that specializes in Rolls Royce repair and service. Jay Gatsby owns the business. Nick Carraway is a repair man and does some...
-
Equipment acquired on January 5, 2009, at a historical cost of $380,000 has accumulated depreciation of $95,625 as of december 31, 2012. What is the journal entry to record the sale assuming the...
-
Mahaffey Enterprises has a temporary difference resulting in future deductible amounts of $500,000, income taxes payable of $800,000, and a tax rate of 20 percent. What should they record as their...
-
Craig bought stock for $10,000 and gifted it to Homer when the stock was valued at $5,000. Homer sold the stock 2 years later for $8,000. Homers taxable gain on the sale of stock is?
-
Explain what accrual basis accounting is and what cash basis accounting is. Understand revenue realization principle.
-
1. Consider cases of utilitarianism that justify the potentially immoral ends of leadership, while contributing to the happiness or well-being of others outside their particular group/organization....
-
Portal Manufacturing has total fixed costs of $520,000. A unit of product sells for $15 and variable costs per unit are $11. a). Prepare a contribution margin income statement showing predicted net...
-
Suppose we have a string holding the text of an entire book. If we want to analyze segments of the text, it is inefficient to make substrings. Instead, we should just store a reference to the...
-
What is the maximum number of nodes in a red-black tree with black height bh?
-
Write methods: that test whether a point is inside or on the boundary of an ellipse. Add the methods to the class Geometry. public static boolean isInside (Point2D.Double p, Ellipse2D.Double e)...
-
Prove the second part of Theorem 3.8. That is let \(\left\{X_{n}ight\}_{n=1}^{\infty}\) be a sequence of random variables, \(X\) be a random variable, and \(g\) be a Borel function on \(\mathbb{R}\)....
-
Prove the first part of Theorem 3.7. That is let \(\left\{X_{n}ight\}_{n=1}^{\infty}\) be a sequence of random variables, \(c\) be a real constant, and \(g\) be a Borel function on \(\mathbb{R}\)...
-
Let \(\left\{X_{n}ight\}_{n=1}^{\infty},\left\{Y_{n}ight\}_{n=1}^{\infty}\), and \(\left\{Z_{n}ight\}_{n=1}^{\infty}\) be independent sequences of random variables that converge in probability to the...
Study smarter with the SolutionInn App