Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Scanner; public class Grades { //------------------------------

ID: 3629437 • Letter: I

Question

import java.util.Scanner;

public class Grades
{
//-------------------------------------------------------------------
// Reads in and processes grades until a negative number is entered.
//-------------------------------------------------------------------
public static void main (String[] args)
{
double grade; // a student's grade
double sumOfGrades; // a running total of the student grades
int numStudents; // a count of the students
int numPass; // a count of the number who pass
int numFail; // a count of the number who fail

Scanner scan = new Scanner(System.in);

System.out.println (" Grade Processing Program ");

// Initialize summing and counting variables
sumOfGrades = 0.0;
numStudents = 0;
numPass = 0;
numFail = 0;

// Read in the first grade
System.out.print ("Enter the first student's grade: ");
grade = scan.nextDouble();

// *** Step 1: Add the while loop here (checks if grade is >= 0.0)

{
// *** Step 2: Change this to use the += operator instead
sumOfGrades = sumOfGrades + grade;
// *** Step 2: Change this to use the ++ operator instead
numStudents = numStudents + 1;

if (grade < 60.0)
// *** Step 2: Change this to use the ++ operator instead
numFail = numFail + 1;
else
// *** Step 2: Change this to use the ++ operator instead
numPass = numPass + 1;

// Read the next grade
System.out.print ("Enter the next grade (a negative to quit): ");
grade = scan.nextDouble();
}

if (numStudents > 0)
{
System.out.println (" Grade Summary: ");
System.out.println ("Class Average: " + sumOfGrades/numStudents);
System.out.println ("Number of Passing Grades: " + numPass);
System.out.println ("Number of Failing Grades: " + numFail);
}
else
System.out.println ("No grades processed.");
}
}

Explanation / Answer

import java.util.Scanner;

public class Grades
{
//-------------------------------------------------------------------
// Reads in and processes grades until a negative number is entered.
//-------------------------------------------------------------------
public static void main (String[] args)
{
double grade; // a student's grade
double sumOfGrades; // a running total of the student grades
int numStudents; // a count of the students
int numPass; // a count of the number who pass
int numFail; // a count of the number who fail

Scanner scan = new Scanner(System.in);

System.out.println (" Grade Processing Program ");

// Initialize summing and counting variables
sumOfGrades = 0.0;
numStudents = 0;
numPass = 0;
numFail = 0;

// Read in the first grade
System.out.print ("Enter the first student's grade: ");
grade = scan.nextDouble();

// *** Step 1: Add the while loop here (checks if grade is >= 0.0)

while(ggrade>=0.0)

{
// *** Step 2: Change this to use the += operator instead
//sumOfGrades = sumOfGrades + grade;

sumOfGrades+ = grade;
// *** Step 2: Change this to use the ++ operator instead
//numStudents = numStudents + 1;
numStudents++;
if (grade < 60.0)
// *** Step 2: Change this to use the ++ operator instead
//numFail = numFail + 1;

numFail++;
else
// *** Step 2: Change this to use the ++ operator instead
//numPass = numPass + 1;

numPass++;

// Read the next grade
System.out.print ("Enter the next grade (a negative to quit): ");
grade = scan.nextDouble();
}

if (numStudents > 0)
{
System.out.println (" Grade Summary: ");
System.out.println ("Class Average: " + sumOfGrades/numStudents);
System.out.println ("Number of Passing Grades: " + numPass);
System.out.println ("Number of Failing Grades: " + numFail);
}
else
System.out.println ("No grades processed.");
}
}