Test Average and Letter grade

Loops, input validation

Page 1 of 1

3 Replies - 13115 Views - Last Post: 29 October 2009 - 05:07 PM Rate Topic: -----

#1 LCTR89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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 class TestAverageAndLetterGrade
{
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.
System.out.println("Test1: "+determineGrade(score1));
System.out.println("Test2: "+determineGrade(score2));
System.out.println("Test3: "+determineGrade(score3));
System.out.println("Test4: "+determineGrade(score4));
System.out.println("Test5: "+determineGrade(score5));

// Assign the average of all 5 test to calcAverage.
System.out.println("Average: " + " "
+ calcAverage( score1, score2, score3, score4, score5));
// Assign the letter grade of the average to determineGrade.
System.out.println("Average letter grade: "
+determineGrade((score1+score2+score3+score4+score5)/5));
}
/**
The determineGrade method accepts an argument and then
displays the value of the parameter.
@param scoreValue The score is given a letter grade.
*/
public static char determineGrade(double scoreValue)
{
char grade;
// Determine grade based on score.
if (scoreValue >=0 && scoreValue< 60)
grade = 'F';
else if (scoreValue >=60 && scoreValue < 70)
grade = 'D';
else if (scoreValue >=70 &&scoreValue < 80)
grade = 'C';
else if (scoreValue >=80 &&scoreValue< 90)
grade = 'B';
else if (scoreValue >=90 &&scoreValue <= 100)
grade = 'A';
else
grade='X';
return grade;
}
/**
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  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is online

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

Reputation: 8325
  • View blog
  • Posts: 31,857
  • 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);


Was This Post Helpful? 0
  • +
  • -

#4 LCTR89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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): ");
			  while((grade<0) || (grade>100))
			  {
				   validate
			   }
				counter++;
				return grade;
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1