hi I need help with this home work my code seems to be working fine but I'm having problems on finding out how to handle the minimum and maximum values. I need to have my program calculate the minimum and maximum values user inputs but cant find a way to do that, it should be written as a method.
this is what I have as a code:
import java.io.*; import java.lang.Math; import java.lang.Double; import java.util.Scanner; public class LabFive { public static void main (String [] arguments)throws IOException { Scanner keyboard = new Scanner (System.in); //Declare variables String operator, userInput1, userInput2; double number1, number2; boolean userWantsToContinue = true, flag = true; //accumulators int additionCounter, subtractionCounter, multiplicationCounter; int divisionCounter, modulusCounter, powerCounter, totalCounter; // Set accumulators to 0 additionCounter = 0; subtractionCounter = 0; multiplicationCounter = 0; divisionCounter = 0; modulusCounter = 0; powerCounter = 0; //Prompt user for file Name System.out.println( "Enter File Name: "); String outputFileName = keyboard.nextLine(); final boolean OVERWRITE = false; final boolean APPEND = true; FileWriter outputFileWriter = new FileWriter( outputFileName , APPEND ); PrintWriter outputPrintWriter = new PrintWriter( outputFileWriter ); do { //Prompt user for operation and numbers System.out.print(" Enter first number: "); userInput1 = keyboard.nextLine(); System.out.print(" Choose operation to be performed: + , - , * , / , or ^" + "\n" + "(Hit <E> to exit)" + "\n" + "(Hit <H> for Help)"); operator = keyboard.nextLine (); System.out.print(" Enter Second number: "); userInput2 = keyboard.nextLine (); number1 = Double.parseDouble(userInput1 ); number2 = Double.parseDouble(userInput2 ); if (operator.equalsIgnoreCase("H") && operator.trim().length()>0) { System.out.print("1- Enter first number for operation" + "\n" + "2- Enter operator:" + "\n" + "<+> to add" + "\n" + "<-> to subtract" + "\n" + "<*> to multiply" + "\n" + "</> to divide" + "\n" + "<%> to get the remainder of a division" + "\n" + "<^> to raise the first number to the power of the second number" + "\n" + "3- Enter second number for operation" + "\n" + "Or enter <E> to exit"); } if (operator.equalsIgnoreCase("+") && operator.trim().length()>0) { additionCounter = additionCounter +1; System.out.print(number1 + " + " + number2 + " = " + "\n" + computeAddition(number1, number2) + "\n" ); } if (operator.equalsIgnoreCase("-") && operator.trim().length()>0) { subtractionCounter = subtractionCounter +1; System.out.print(number1 + "-" + number2 + "=" + "\n" + computeSubtraction(number1, number2) + "\n" ); } if (operator.equalsIgnoreCase("*")&& operator.trim().length()>0) { multiplicationCounter = multiplicationCounter +1; System.out.print(number1 + "*" + number2 + "=" + "\n" + computeMultiplication(number1, number2) + "\n" ); } if (operator.equalsIgnoreCase("/") && operator.trim().length()>0) { divisionCounter = divisionCounter +1; if (number2 >=1) { System.out.print(number1 + "/" + number2 + "=" + "\n" + computeDivision(number1, number2) + "\n" ); }else if (number2 == 0) { System.out.println("Error: Operation cannot be performed with second number as zero, please try again"); while(flag) { System.out.println("Enter second number"); number2 = keyboard.nextDouble (); if(number2>=1) { System.out.print(number1 + "/" + number2 + "=" + "\n" + computeDivision(number1, number2) + "\n" ); flag=false; }else if(number2 ==0) { System.out.println("Error: Operation cannot be performed with second number as zero, please try again"); } } } } if (operator.equalsIgnoreCase("%")) { modulusCounter = modulusCounter +1; if (number2 >=1) { System.out.print(number1 + "%" + number2 + "=" + "\n" + computeModulus(number1, number2) + "\n" ); }else if (number2 == 0) { System.out.println("Error: Operation cannot be performed with second number as zero, please try again"); while(flag) { System.out.println("Enter second number"); number2 = keyboard.nextDouble (); if(number2>=1) { System.out.print(number1 + "%" + number2 + "=" + "\n" + computeModulus(number1, number2) + "\n" ); flag=false; }else if(number2 ==0) { System.out.println("Error: Operation cannot be performed with second number as zero, please try again"); } } } } if (operator.equalsIgnoreCase("^")) { powerCounter = powerCounter +1; System.out.print(number1 + "^" + number2 + "=" + "\n" + computePower(number1, number2) + "\n" ); } else userWantsToContinue = false; }while (userWantsToContinue = true && (!operator.equalsIgnoreCase("E") && operator.trim().length()>0)); // Statistics printout totalCounter = (additionCounter + subtractionCounter + multiplicationCounter + divisionCounter + modulusCounter); System.out.println ("Total number of additions performed: " + additionCounter); System.out.println ("Total number of subtractions performed: " + subtractionCounter); System.out.println ("Total number of multiplications performed: " + multiplicationCounter); System.out.println ("Total number of divisions performed: " + divisionCounter); System.out.println ("Total number of modulus operations performed: " + modulusCounter); System.out.println ("Total number of exponential operations performed: " + powerCounter); System.out.println ("Total number of operations performed: " + totalCounter); } public static double computeAddition( double number1, double number2) { return (number1 + number2); } public static double computeSubtraction( double number1, double number2) { return (number1 - number2); } public static double computeMultiplication( double number1, double number2) { return (number1 * number2); } public static double computeDivision( double number1, double number2) { return (number1 / number2); } public static double computeModulus( double number1, double number2) { return (number1 % number2); } public static double computePower(double number1, double number2) { return Math.pow(number1, number2); } }
--- Update ---
this is the code with the solution:
import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.lang.Math; import java.lang.Double; import java.util.Scanner; public class LabFive { public static void main (String [] arguments)throws IOException { Scanner keyboard = new Scanner (System.in); //Declare variables String operator, userInput1, userInput2; double number1, number2; boolean userWantsToContinue = true, flag = true; //accumulators int additionCounter, subtractionCounter, multiplicationCounter; int divisionCounter, modulusCounter, powerCounter, totalCounter; // Set accumulators to 0 additionCounter = 0; subtractionCounter = 0; multiplicationCounter = 0; divisionCounter = 0; modulusCounter = 0; powerCounter = 0; //Prompt user for file Name System.out.println( "Enter File Name: "); String outputFileName = keyboard.nextLine(); final boolean OVERWRITE = false; final boolean APPEND = true; FileWriter outputFileWriter = new FileWriter( outputFileName , APPEND ); PrintWriter outputPrintWriter = new PrintWriter( outputFileWriter ); //Add list to hold all user inputs List<Double> userInputsList = new ArrayList<>(); do { //Prompt user for operation and numbers System.out.print(" Enter first number: "); userInput1 = keyboard.nextLine(); System.out.print(" Choose operation to be performed: + , - , * , / , or ^" + "\n" + "(Hit <E> to exit)" + "\n" + "(Hit <H> for Help)"); operator = keyboard.nextLine (); System.out.print(" Enter Second number: "); userInput2 = keyboard.nextLine (); number1 = Double.parseDouble(userInput1 ); number2 = Double.parseDouble(userInput2 ); //Add userInputs to the List userInputsList.add(number1); userInputsList.add(number2); if (operator.equalsIgnoreCase("H") && operator.trim().length()>0) { System.out.print("1- Enter first number for operation" + "\n" + "2- Enter operator:" + "\n" + "<+> to add" + "\n" + "<-> to subtract" + "\n" + "<*> to multiply" + "\n" + "</> to divide" + "\n" + "<%> to get the remainder of a division" + "\n" + "<^> to raise the first number to the power of the second number" + "\n" + "3- Enter second number for operation" + "\n" + "Or enter <E> to exit"); } if (operator.equalsIgnoreCase("+") && operator.trim().length()>0) { additionCounter = additionCounter +1; System.out.print(number1 + " + " + number2 + " = " + "\n" + computeAddition(number1, number2) + "\n" ); } if (operator.equalsIgnoreCase("-") && operator.trim().length()>0) { subtractionCounter = subtractionCounter +1; System.out.print(number1 + "-" + number2 + "=" + "\n" + computeSubtraction(number1, number2) + "\n" ); } if (operator.equalsIgnoreCase("*")&& operator.trim().length()>0) { multiplicationCounter = multiplicationCounter +1; System.out.print(number1 + "*" + number2 + "=" + "\n" + computeMultiplication(number1, number2) + "\n" ); } if (operator.equalsIgnoreCase("/") && operator.trim().length()>0) { divisionCounter = divisionCounter +1; if (number2 >=1) { System.out.print(number1 + "/" + number2 + "=" + "\n" + computeDivision(number1, number2) + "\n" ); }else if (number2 == 0) { System.out.println("Error: Operation cannot be performed with second number as zero, please try again"); while(flag) { System.out.println("Enter second number"); number2 = keyboard.nextDouble (); if(number2>=1) { System.out.print(number1 + "/" + number2 + "=" + "\n" + computeDivision(number1, number2) + "\n" ); flag=false; }else if(number2 ==0) { System.out.println("Error: Operation cannot be performed with second number as zero, please try again"); } } } } if (operator.equalsIgnoreCase("%")) { modulusCounter = modulusCounter +1; if (number2 >=1) { System.out.print(number1 + "%" + number2 + "=" + "\n" + computeModulus(number1, number2) + "\n" ); }else if (number2 == 0) { System.out.println("Error: Operation cannot be performed with second number as zero, please try again"); while(flag) { System.out.println("Enter second number"); number2 = keyboard.nextDouble (); if(number2>=1) { System.out.print(number1 + "%" + number2 + "=" + "\n" + computeModulus(number1, number2) + "\n" ); flag=false; }else if(number2 ==0) { System.out.println("Error: Operation cannot be performed with second number as zero, please try again"); } } } } if (operator.equalsIgnoreCase("^")) { powerCounter = powerCounter +1; System.out.print(number1 + "^" + number2 + "=" + "\n" + computePower(number1, number2) + "\n" ); } else userWantsToContinue = false; }while (userWantsToContinue = true && (!operator.equalsIgnoreCase("E") && operator.trim().length()>0)); // Statistics printout totalCounter = (additionCounter + subtractionCounter + multiplicationCounter + divisionCounter + modulusCounter); System.out.println ("Total number of additions performed: " + additionCounter); System.out.println ("Total number of subtractions performed: " + subtractionCounter); System.out.println ("Total number of multiplications performed: " + multiplicationCounter); System.out.println ("Total number of divisions performed: " + divisionCounter); System.out.println ("Total number of modulus operations performed: " + modulusCounter); System.out.println ("Total number of exponential operations performed: " + powerCounter); System.out.println ("Total number of operations performed: " + totalCounter); //Use the Collections interface to obtain the Min and Max System.out.print("Max input value: " + Collections.max(userInputsList)); System.out.print("Min input value: " + Collections.min(userInputsList)); } public static double computeAddition( double number1, double number2) { return (number1 + number2); } public static double computeSubtraction( double number1, double number2) { return (number1 - number2); } public static double computeMultiplication( double number1, double number2) { return (number1 * number2); } public static double computeDivision( double number1, double number2) { return (number1 / number2); } public static double computeModulus( double number1, double number2) { return (number1 % number2); } public static double computePower(double number1, double number2) { return Math.pow(number1, number2); } }
I hope this helps someone I know I was going crazy with this