Question
import java.io.*; import java.util.*; class AllValues { int list[]; String s; int num; Scanner sc ; AllValues() { list = new int[100]; s = ;
import java.io.*; import java.util.*; class AllValues { int list[]; String s; int num; Scanner sc ; AllValues() { list = new int[100]; s = ""; num =0; sc = new Scanner(System.in); } void getInt() { try { System.out.println("Enter how many values?"); s = sc.nextLine(); num =Integer.parseInt(s); while( num <0) { System.out.println("Not an integer. Try again!"); s = sc.nextLine(); num =Integer.parseInt(s); } } catch(Exception e) { }; }
void initRand() { int k; Random r = new Random(); for(k=0; k int minGap() { int i, min=999; for(i=0;i void menu() { while(true) { int choice=8; do { System.out.println("Your options are:"); System.out.println("----------------------"); System.out.println("1) All even values?"); System.out.println("2) All unique values?"); System.out.println("3) Print min gap between values"); System.out.println("4) Statistics"); System.out.println("5) Print 80% percentile"); System.out.println("0) EXIT"); System.out.println("Please enter your option:"); choice = sc.nextInt(); } while (choice < 0 || choice > 5); switch(choice) { case 1: { if(AllEven()) System.out.println("All Even"); else System.out.println("Not Even"); break; } case 2: { if(isUnique()) System.out.println("All are unique"); else System.out.println("Not unique"); break; } case 3: { System.out.println("Minimum: "+ minGap()); break; } case 4: { AllEven(); break; } case 5: { isUnique(); break; } case 0: { System.exit(0); } } } } public static void main(String args[]) { AllValues a = new AllValues(); a.getInt(); a.initRand(); a.menu(); } } Task (reuse of code at work!): Rewrite the previous program and make it work when the list is populated with values found in an input file. The name of the file should be input from the user. Handle all possible cases (use try/catch statements). Have a constant for max size and read at most these many values from the file. Your output should be similar to the following samples: SAMPLE OUTPUT #1: Please input the name of the file to be opened: input.tx --- File Not Found! Exit program! --- SAMPLE OUTPUT #2: For this file holding all values of unwanted types: a nd fdsvfd sdfgvdf 3.5 3.23 sdafs asdasf erqwewr f Please input the name of the file to be opened: input.txt --- The file doesn't contain any integers. Exit program! --- SAMPLE OUTPUT #3: For this file holding mixed types: 34 a 55 18 47 89 b 45 67 59 abbbb 88 37 20 27 10 78 39 21 n m ghff Please input the name of the file to be opened: input2.txt The list size is: 16 The list is: 34 55 18 47 89 45 67 59 88 37 20 27 10 78 39 21 Your options are: ----------------- 1) All even values? 2) All unique values? 3) Print min gap between values 4) Statistics 5) Print 80% percentile 0) EXIT Please enter your option: 3 The minimum gap between 2 adjacent values is -51 Your options are: ----------------- 1) All even values? 2) All unique values? 3) Print min gap between values 4) Statistics 5) Print 80% percentile 0) EXIT Please enter your option: 2 All values/list are unique values. Your options are: ----------------- 1) All even values? 2) All unique values? 3) Print min gap between values 4) Statistics 5) Print 80% percentile 0) EXIT Please enter your option: 1 Some values/list are odd. Your options are: ----------------- 1) All even values? 2) All unique values? 3) Print min gap between values 4) Statistics 5) Print 80% percentile 0) EXIT Please enter your option: m Not an integer! Try again! Please enter your option: 5 The list sorted: 10 18 20 21 27 34 37 39 45 47 55 59 67 78 88 89 80%-percentile from this list: 89 88 78 Your options are: ----------------- 1) All even values? 2) All unique values? 3) Print min gap between values 4) Statistics 5) Print 80% percentile 0) EXIT Please enter your option: wer Not an integer! Try again! Please enter your option: 4 Statistics for this list: 34 55 18 47 89 45 67 59 88 37 20 27 10 78 39 21 The mean for this list is: 45.88 The variance for this list is: 584.11 The standard deviation for this list is: 24.17 Your options are: ----------------- 1) All even values? 2) All unique values? 3) Print min gap between values 4) Statistics 5) Print 80% percentile 0) EXIT Please enter your option: 0 Testing completed.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started