# Thread: Finding math.min/max and total

1. Member
Join Date
Mar 2012
Posts
2
Rep Power
0

## 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. ## 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. ## Re: Finding math.min/max and total

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

db

#### Posting Permissions

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