Results 1 to 3 of 3
  1. #1
    jrandaz is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Finding math.min/max and total

    I am working on a project for my java class and got stumped on a few questions. I am requesting guidance on figuring out the math.min, math.max and total of scores. Should I be creating a method and calling it to find the max, min, and total or should they be included in getScoreWithinRange?
    Java Code:
    // import necessary packages
    import java.util.Scanner;
    
    // class declaration
    public class ProjectApp
    {
    
        // main method declaration
        public static void main(String[] args)
        {
    		// start of while loop
    		String choice = "y";
    		while (choice.equalsIgnoreCase("y"))
    		{
    			// create scanner
    			Scanner sc = new Scanner(System.in);
    			int testNumber = getTestNumberWithinRange(sc, "How many scores would you like to enter? ",
    			6, 10);
    			double score = getScoreWithinRange(sc, 1, 10, testNumber);
    
    
    
    		}
    	}
    
    	public static int getTestNumber(Scanner sc, String prompt)
    	{
    		int testNumber = 0;
    		boolean isValid = false;
    		while (isValid == false)
    		{
    			System.out.print(prompt);
    			if (sc.hasNextInt())
    			{
    				testNumber = sc.nextInt();
    				isValid = true;
    			}
    			else
    			{
    				System.out.println("Error! Invalid number. Try Again.");
    			}
    			sc.nextLine();
    		}
    		return testNumber;
    	}
    	public static int getTestNumberWithinRange(Scanner sc, String prompt,
    	int min, int max)
    	{
    		int testNumber = 0;
    		boolean isValid = false;
    		while (isValid == false)
    		{
    			testNumber = getTestNumber(sc, prompt);
    			if (testNumber < min)
    			{
    				System.out.println("Error! Number must be greater than " + min + ".");
    			}
    			else if (testNumber > max)
    			{
    				System.out.println("Error! Number must be less than " + max + ".");
    			}
    			else
    				isValid = true;
    		}
    		return testNumber;
    	}
    
    	public static double getScore(Scanner sc, String prompt)
    		{
    			double score = 0;
    			boolean isValid = false;
    			while (isValid == false)
    			{
    				System.out.println(prompt);
    				if (sc.hasNextDouble())
    				{
    					score = sc.nextDouble();
    					isValid = true;
    				}
    				else
    				{
    					System.out.println("Error! Invalid number. Try Again.");
    				}
    				sc.nextLine();
    			}
    			return score;
    		}
    	public static double getScoreWithinRange(Scanner sc, double min, double max, int testNumber)
    	{
    		double score = 0;
    		boolean isValid = false;
    		while (isValid == false)
    		{
    			for (int i = 1; i <= testNumber; i++)
    			{
    				System.out.print("Enter score #" + i + ": ");
    				score = sc.nextDouble();
    
    				
    				if (score <= max && score >= min)
    				{
    					isValid = true;
    
    				}
    				else if (score >= max)
    				{
    					System.out.println("Error! Number must be 10.00 or less");
    						i--;
    				}
    				else if (score <= min)
    				{
    					System.out.println("Error! Number must be 1.00 or more");
    					i--;
    				}
    			}
    		}
    		return score;
    	}
    
    
    } // end class

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,494
    Rep Power
    6

    Default Re: Finding math.min/max and total

    Generally you would write a method to do one thing only. So you could write separate method s to do each task. However, in this case you can determine the max and min as each value is read in. Read in a new value and compare it to the old max value. If the new value is higher make it the max value.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    10,096
    Rep Power
    17

    Default Re: Finding math.min/max and total

    Heyyy the Grand Poobah's back! Where've you been, Junky?

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

Similar Threads

  1. Running total little help
    By silverspoon34 in forum Forum Lobby
    Replies: 9
    Last Post: 02-27-2011, 09:19 PM
  2. Calculating Total?
    By ethemartian in forum New To Java
    Replies: 6
    Last Post: 02-21-2011, 04:09 AM
  3. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 06:23 AM
  4. Replies: 2
    Last Post: 12-10-2009, 12:06 PM
  5. Total noob
    By J_Walker in forum New To Java
    Replies: 9
    Last Post: 04-24-2009, 03:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •