# Test Average and Letter grade

Page 1 of 1

## 3 Replies - 25650 Views - Last Post: 29 October 2009 - 05:07 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=135037&amp;s=e4824a5c547b98e7d39eed85ad1387fe&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 LCTR89

Reputation: 1
• Posts: 3
• Joined: 27-October 09

# Test Average and Letter grade

Posted 28 October 2009 - 10:40 AM

My professor wants actual validation somethin like:

" Invalid ! Please enter a score between 0-100

How can I do that using do-while loops? ( I dont want to mess up what i already have.. (it works fine when it runs on netbeans)

After this done he wants it modified in order for it to drop the lowest score and calculate the new average score using the the 4 remaining scores and assign it a letter grade...I'm not sure how to do that

import java.util.Scanner; // Needed for Scanner class

/* A demonstration of how to Create Methods, use Loops, and Input Validation
* Luis Loli
* Cop2250_U05 Project_4
* #7 on page 276-277 of the textbook
*
*/

/**
*
*
*/
{
public static void main(String[] args)
{
int score1 ,score2,score3,score4,score5; // Test Score.
String input; // Accept the users input.

Scanner keyboard = new Scanner(System.in);

// Get the score for Test 1.
do
{
System.out.println("Enter score for Test1: ");
input = keyboard.nextLine();
score1 = Integer.parseInt(input);
}while (score1 < 0 || score1 > 100);

// Get the score for Test 2.
do
{
System.out.print("Enter score for Test2: ");
input = keyboard.nextLine();
score2 = Integer.parseInt(input);
}while (score2 < 0 || score2 > 100);

// Get the score for Test 3.
do
{
System.out.print("Enter score for Test3: ");
input = keyboard.nextLine();
score3 = Integer.parseInt(input);
}while (score3 < 0 || score3 > 100);
// Get the score for Test 4.
do
{
System.out.print("Enter score for Test4: ");
input = keyboard.nextLine();
score4 = Integer.parseInt(input);
}while (score4 < 0 || score4 > 100);
// Get the score for Test 5.
do
{
System.out.print("Enter score for Test5: ");
input = keyboard.nextLine();
score5 = Integer.parseInt(input);
}while (score5 < 0 || score5 > 100);

// Display the average of all 5 test and it's letter grade.
System.out.println("Here are the grades and the average test score:" );
System.out.println("===============================================" );
// Assign a letter grade to the input score.

// Assign the average of all 5 test to calcAverage.
System.out.println("Average: " + " "
+ calcAverage( score1, score2, score3, score4, score5));
}
/**
The determineGrade method accepts an argument and then
displays the value of the parameter.
@param scoreValue The score is given a letter grade.
*/
{
// Determine grade based on score.
if (scoreValue >=0 && scoreValue< 60)
else if (scoreValue >=60 && scoreValue < 70)
else if (scoreValue >=70 &&scoreValue < 80)
else if (scoreValue >=80 &&scoreValue< 90)
else if (scoreValue >=90 &&scoreValue <= 100)
else
}
/**
The calcAverage method returns the average of its parameters divided by .
@param score1 input value of first score.
@param score2 input value of second score.
@param score3 input value of third score.
@param score4 input value of fourth score.
@param score5 input value of fifth score.
@return The average of all 5 tests.
*/
public static double calcAverage(double score1, double score2,double
score3,double score4,double score5)
{
// calculate the average testscore
double average = 0.0;
// Display the value in number.
average = (score1+score2+score3+score4+score5)/5.0;
return average;

Is This A Good Question/Topic? 1

## Replies To: Test Average and Letter grade

### #2 Laythe

Reputation: 6
• Posts: 76
• Joined: 09-June 09

## Re: Test Average and Letter grade

Posted 28 October 2009 - 02:55 PM

Dream.In.Code has a policy by which we prefer to see a

good faith effort on your part before providing source

code for homework assignments. Please post the code you

have written in an effort to resolve the problem, and our

members would be happy to provide some guidance. Be sure

to include a description of any errors you are

encountering as well.

Please post you code like this:

```Paste Your Code Between These Tags
```

Thank you for helping us helping you.

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Test Average and Letter grade

Posted 28 October 2009 - 07:54 PM

Just add an if before testing the end of the loop
```do
{
System.out.print("Enter score for Test4: ");
input = keyboard.nextLine();
score4 = Integer.parseInt(input);
if(score4 < 0 || score4 > 100)
System.out.println("Invalid number");
}while (score4 < 0 || score4 > 100);

```

### #4 LCTR89

Reputation: 1
• Posts: 3
• Joined: 27-October 09

## Re: Test Average and Letter grade

Posted 29 October 2009 - 05:07 PM

I spoke to my professor and he doesn't want us to use if statements for validation, he wants us to create a separate method for the validation.

Would something like the following work with the code I currently have, I implemented it but I it is not working; I doesn't validate.

I believe I need to re-do my original code in order for it to work.

```
public static int valdscore()
{

int score;
system.out.print("Please enter score" + counter + " (0-100): ");
{
validate
}
counter++;