Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I need help on creating a method to handle minimum and maximum input values

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Wink I need help on creating a method to handle minimum and maximum input values

    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


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: I need help on creating a method to handle minimum and maximum input values

    just create a method of min(int i, int j) and max(int i, int j)
    in min method, return the variable that has the smaller value
    and in max method, return the variable with higher value

Similar Threads

  1. [SOLVED] Java Coding: Assistance with a loop that calculates maximum, minimum, and average of 10 inputted numbers.
    By Knowledge_Feeds_The_Mind in forum What's Wrong With My Code?
    Replies: 20
    Last Post: February 27th, 2014, 11:11 PM
  2. Help with method. Finding minimum value in Array
    By richardman54 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2013, 11:13 PM
  3. reading floats from file and output minimum and maximum temperatures
    By james_b in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 3rd, 2013, 11:10 PM
  4. maximum of two numbers method problem
    By lordofrandom in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 25th, 2013, 09:38 PM
  5. Java program to find the minimum and maximum values of input data
    By awake77 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 20th, 2008, 05:12 PM

Tags for this Thread