Individual directions for each method are in the comments above the method header for each method. Make sure you read them carefully to see what behavior your method should have. Exceptions to this are the promptForNumbers and findMedians methods whose instructions are too involved to place in the method header - its requirements are below.
The promptForNumbers method takes a Scanner as input. It should prompt the user to enter integers and end when the user enters a 0. You may assume that the user will enter only integer values. When the user enters a 0 the method should return back a list containing all of the numbers the user entered (except for the 0) in the same order that the user entered them. A transcript of what the output should look like is below - remember that user entries are in BOLD:
Enter a number (0 to quit): 15Enter a number (0 to quit): -3Enter a number (0 to quit): 77Enter a number (0 to quit): 0The list returned by the above transcript would be .
The findMedian method requires the computation of the median of a list of numbers. Remember - the rule for computing the median of a sorted list of numbers are:
If the list has an odd length, the median is the value in the center of the list.If the list has an even length, the median is the average of the two middle values in the list.The median of an empty list is 0.Read the findMedian method carefully - you do NOT need to perform your own sort for this method!
For the method histogramPlot you must print a histogram. A histogram is a kind of bar chart that shows the frequency of values within a set of ranges. For this problem the method histogramPlot takes an array of int values and prints a plot based on their values. For example, given the array {10, 3, 9, 14, 33, 66, 102, 51, 27, 29, 5, -12, 0} the histogramPlot method should print:
10-19: **20-29: **30-39: *49-49:50+ : ***NOTE: There is the companion method histogramCounts that given an array returns an array of counts of the numbers in the ranges given above. For full credit your histogramPlot method MUST use histogramCounts.
Note that the method intsFromFile requires you to read from a file of integers. A file named `numbers.txt is included in the zyBooks files and should be used as your input file. The contents of that file are given below for debugging purposes if you have difficulty downloading the file to your system:
314-5507586301510248904090-238490234
If you have written your methods correctly, running your program with the default main method should produce the following output (as always, user inputs are in BOLD)::
check1: truecheck2: falseThe median of [3, 3, 11, 12, 54] is 11The median of [3, 19, 33, 35] is 26[true, true, false, true, true]SB : This is a string that we can use for testingSUB: s is aSUB: tSUB: This is a striSB : This is a string that we can use for testing and appendINDEX i: 2INDEX w: 22INDEX z: -1Enter a number (0 to quit): 15Enter a number (0 to quit): -3Enter a number (0 to quit): 77Enter a number (0 to quit): 0NUMBERS: [15, -3, 77]NUMBERS: [3, 14, -55, 0, 75, 86, 3, 0, 15, 1024, 8904090, -238490234]NUMBERS: [][5, 2, 2, 1, 0, 3]10-19: **20-29: **30-39: *49-49:50+ : ***10-19: *20-29: *30-39: *49-49: *50+ : ***10-19: *20-29: *30-39: *49-49: *50+ : **---------------------------------------------------------------------------------------/*** @author YOUR NAME HERE* @version DATE HERE*/import java.util.Arrays;import java.util.Scanner;import java.util.List;import java.util.ArrayList;import java.util.Collections;import java.io.File;import java.io.FileNotFoundException;
public class VariousMethods2 {/** * Reads a list of numbers from the file inFile into a new List * and returns the list. Returns an empty list if the file cannot * be read. REMEMBER: You cannot change the method header and * receive full credit for this problem! * * @param inFileName name of the file to read numbers from * @return List of numbers from inFileName if inFileName can be read, an * empty List otherwise. */public static List intsFromFile(String inFileName) {// TODO: Implement this method
// TODO: remove the following line before completing// this line is only here to allow the template// to compile.return new ArrayList();}
/** * Returns true if every element of list1 is in list2, otherwise * returns false regardless of order. For example, if list1 * is and list2 is then * it should return true. If list2 is then it would * return false. Note that duplicates do not matter - if list1 is * and list2 is it would still * return true. NOTE: You MUST NOT use the containsAll method of * List for this implementation. * * @param list1 the first list to evaluate * @param list2 the second list to evaluate * @return true if every element of list1 is in list2, false otherwise */public static boolean allIn(List list1, List list2) { // TODO: Implement this method// TODO: remove the following line before completing// this line is only here to allow the template// to compile.return false;}/** * Returns true if every element of list1 is in list2 and if every * element of list2 is in list1. You MUST make use of the allIn * method to receive full credit. * * @param list1 the first list to evaluate * @param list2 the second list to evaluate * @return true if every element of list1 is in list2, false otherwise */public static boolean allInBoth(List list1, List list2) { // TODO: Implement this method // TODO: remove the following line before completing// this line is only here to allow the template// to compile.return false;}/** * Returns the median of the List list1, where the median is * defined as in the description for this exam. Note that * the ONLY change you can make to the List list1 should be * to sort it. DO NOT WRITE YOUR OWN SORT! The first line of * this method sorts the list for you. * * @param list1 the list to find the median of * @return the median of list1 */public static int findMedian(List list1) { Collections.sort(list1); // Your code below this line!// TODO: Implement this method// TODO: remove the following line before completing// this line is only here to allow the template// to compile.return -1;}/** * Returns an array of booleans that are set true or * false based on the associated values in the array * arr using the following rules: * 1. If arr[i] is divisible by three then the boolean * value in the the array returned at the same position * should be true * 2. Unless the values in arr[i] is also divisible by 5, * then the value returned in that position should be false. * 3. It should also be false for any values not divisible * by 3. * So for example, if arr = {3, 15, 21, 7, 10} then the * array returned would be {true, false, true, false, false}. * NOTE: The original array must NOT be changed by this method! * * @param arr array holding the int values to test * @return a boolean array as described above */public static boolean[] divByThree(int[] arr) {// TODO: Implement this method// TODO: remove the following line before completing// this line is only here to allow the template// to compile. return new boolean[0];}/** * Returns an array of counts of values in the array * values. The positions of the return array should * contain the following counts: * position 0: count of values less than 10 * position 1: count of values in the range 10-19 * position 2: count of values in the range 20-29 * position 3: count of values in the range 30-39 * position 4: count of values in the range 40-49 * position 5: count of values greater than 49 * The array values must NOT be changed by this method. * * For example, if values = {39, 23, 22, 11, 19, 54, 1024} then * the value returned by this method would be {0, 2, 2, 1, 0, 2} * * @param values integer values to obtain counts for * @return an integer array of counts as described above. */public static int[] histogramCounts(int[] values) {// TODO: Implement this method// TODO: remove the following line before completing// this line is only here to allow the template// to compile.return new int[0];}/** * Prints a histogram plot of the array values. * See the exam instructions for what the format of this * plot should be. * * @param values the values to plot as a histogram */public static void histogramPlot(int[] values) {
String[] labels = { " // TODO: Implement this method // YOUR CODE GOES HERE - DO NOT CHANGE THE LINE ABOVE. // HINT: Use nested loops and the labels array above to print your histogram!}
/** * See the exam write-up for details on this method * @param input a Scanner to read input from * @return an array of Strings. */public static List promptForNumbers(Scanner input) {// TODO: Implement this method
// TODO: remove the following line before completing// this line is only here to allow the template// to compile.return new ArrayList<>();}
// NOTE: DO NOT CHANGE ANYTHING IN THIS METHOD!// This method will produce the output that// zyBooks is looking for for some of its automated// tests.public static void runTests(Scanner input) {List list1 = Arrays.asList(3,54,3,12,11);List list2 = Arrays.asList(11,54,12,5,3);boolean check1 = allIn(list1,list2);boolean check2 = allIn(list2,list1);System.out.println("check1: "+check1);System.out.println("check2: "+check2);int median = findMedian(list1);System.out.println("The median of "+list1+" is "+median);List list5 = Arrays.asList(19,35,33,3);median = findMedian(list5);System.out.println("The median of "+list5+" is "+median);int[] arr = new int[list1.size()];for (int i=0; iarr[i] = list1.get(i);}boolean[] byThree = divByThree(arr);System.out.println(Arrays.toString(byThree));SimpleStringBuilder sb = new SimpleStringBuilder("This is a string that we can use for testing");String sub = sb.substring(3,9);System.out.println("SB : "+sb);System.out.println("SUB: "+sub);sub = sb.substring(11, 12);System.out.println("SUB: "+sub);sub = sb.substring(0,14);System.out.println("SUB: "+sub);sb.append(" and append");System.out.println("SB : "+sb);char[] tests = {'i','w','z'};for (int i = 0; iint idx = sb.indexOf(tests[i]);System.out.println("INDEX "+tests[i]+": "+idx);}List numbers = promptForNumbers(input);System.out.println("NUMBERS: "+numbers);numbers = intsFromFile("numbers.txt");System.out.println("NUMBERS: "+numbers);numbers = intsFromFile("nosuchfile.txt");System.out.println("NUMBERS: "+numbers);int[] arr1 = {10, 3, 9, 14, 33, 66, 102, 51, 27, 29, 5, -12, 0};int[] counts = histogramCounts(arr1);System.out.println(Arrays.toString(counts));histogramPlot(arr1);int[] arr2 = {0, 10, 20, 30, 40, 50, 60, 70};histogramPlot(arr2);int[] arr3 = {0, 9, 19, 29, 39, 49, 59, 69};histogramPlot(arr3);}// NOTE: You may comment out the call to runTests// and enter your own main code. However before// making your final submission make sure you// have put this method back to how it started!public static void main(String[] args) {//Set up the ScannerScanner keyboard = new Scanner(System.in);
// Call runTests (comment this out to create your own small tests,// but uncomment it and remove your tests before submitting)runTests(keyboard);// Close the Scanner.keyboard.close();}}