Question:
Java homework help please!!!!?
Rawri
2013-07-26 21:10:55 UTC
My code has to use Method. I am still learning how to use method so somebody please help me fix this code by being very detailed.

Here is homework prompt:
Write a program that asks the user to enter 5 test scores. Your program should then display a letter grade for each test score, the average test score and the overall letter grade for the average score. You can NOT use a for loop or an array (we haven’t even covered that) in this program – the point is to learn how to define methods and call them several times if necessary.

Your program is required to have and use the following methods: (These methods CAN NOT be void methods)

• calcAverage – This method must accept 5 test scores as arguments and must return the average of the 5 scores (NOT A VOID METHOD).

• determineGrade – This method must accept a test score as an argument and must return a letter grade for the score it accepted as an argument (NOT A VOID METHOD).

Here’s the grading scale:

Score Letter Grade
90-100 A
80-89 B
70-79 C
60-69 D
Below 60 F



Here is what I have:
import java.util.Scanner;


public class TestGrade
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);

int testA;
int testB;
int testC;
int testD;
int testF;
double calcAverage;
double determineGrade;



System.out.println("Please enter the first score between 0 and 100: ");
determineGrade1 = keyboard.nextInt();
System.out.println("Please enter the second score between 0 and 100: ");
determineGrade2 = keyboard.nextInt();
System.out.println("Please enter the third score between 0 and 100: ");
determineGrade3 = keyboard.nextInt();
System.out.println("Please enter the fourth score between 0 and 100: ");
determineGrade4 = keyboard.nextInt();
System.out.println("Please enter the fifth score between 0 and 100: ");
determineGrade5 = keyboard.nextInt();
keyboard.nextLine();

double average = calcAverage(testA, testB, testC, testD, testF);

System.out.println("The Average grade is ");
determineGrade(average);
}

public static double calAverage(int testA, int testB, int testC, int testD, int testF);
{
double average = (testA + testB + testC + testD + testF) /5;
return average;
}
public static double determineGrade(double average)
{
if (average>90);
{
System.out.println("You recieved an A");
}
else if (double average>=80);
{
System.out.println("You recieved a B");
}
else if (average>=70);
{
System.out.println("You recieved a C");
}
else if (average>=60);
{
System.out.println("You recieved a D");
}
else if (average<60);
{
System.out.println("You recieved an F");
}
return 0;


System.out.println("The first letter grade is: F");
System.out.println("The second letter grade is: D");
System.out.println("The third letter grade is: C");
Sysytem.out.println("The fourth letter grade is: B");
System.out.println("The fifth letter grade is: A");
System.out.println("\n");



System.out.println("The average test score is:");
System.out.println("The average grade is:");









}
}
Four answers:
Jamie
2013-07-26 21:26:03 UTC
I think you are off to a nice start with this program, however, there are a few things that you may want to look over, in order to ensure that your program works properly (and follows proper Java conventions).



One thing that I noticed was that you instantiated, or created, an object of "Scanner" class, called "keyboard", and called the "nextInt()" method of the object five times, storing the user-entered data in multiple variables (determineGrade1, determineGrade2, etc.), in order to collect grades.



However, I noticed that you didn't actually declare the variables in your program, so you will probably end up getting some errors when you attempt to compile your program.



This can be fixed by declaring the variables, like in the example below:



int determineGrade1, determineGrade2, determineGrade2; //etc.



(You can also declare the variables on multiple lines, should you desired to do it that way, instead.)



Another thing that you may want to consider would be to change the primitive datatype of the variables that are supposed to store the user-entered grades to "double" instead of "int", as some users may attempt to enter a decimal value for a grade, rather than an integer, which could potentially cause some issues if a user types in a decimal value.



Also, it seems as though you created variables called "TestA", "TestB", "TestC", "TestD", and "TestF", which are used as parameters in the "calcAverage()" method.



However, it doesn't appear as though these variables are being assigned values in the code, so the "calcAverage()" method will be receiving "null" variables, which would cause it to not calculate the user's average grade.



Instead, you may want to try using the "determineGrade1", "determineGrade2", etc., as parameters of the "calcAverage()" method, as they would be storing user-entered data, in the form of an integer, and could be used to calculate the user's average grade.



Best of luck with your program and I hope I helped you!



Edit: If you also follow Timothy's advice, you should be good to go! Like he said, there weren't any "serious" errors in your program and the ones that were present were easy to fix!



Also, if you're looking for a website to practice writing Java (and Python) code, I would suggest that you check out CodingBat.com, which offers a variety of free exercises on various topics, and you can also check your solutions right on the website!



http://CodingBat.com



CodingBat also allows users to sign up for a free account, enabling them to keep track of their progress with the practice examples, etc.
?
2013-07-27 00:24:20 UTC
Your homework missing something in which have been mentioned, and plus the following...



1. Does not cover score 90 in letter grade;

2. Missing the the overall letter grade for average;

3. Request to define methods and call them SEVERAL times.

4. The program fails to compile.



Good luck, you have a well start keeping on.



import java.util.InputMismatchException;

import java.util.Scanner;



public class TestGrade {

public static void main(String[] args) {

int scoreForTestA = input("first");

int scoreForTestB = input("second");

int scoreForTestC = input("third");

int scoreForTestD = input("fourth");

int scoreForTestE = input("fifth");



report(scoreForTestA, scoreForTestB, scoreForTestC, scoreForTestD,

scoreForTestE);



System.exit(0);

}



private static int input(String subject) {

boolean done = false;

int score = 0;

do {

Scanner keyboard = new Scanner(System.in);

System.out.format("Please enter the %s score between 0 and 100:",

subject);

try {

score = keyboard.nextInt();

if (score >= 0 && score <= 100) {

done = true;

} else {

System.err

.println("Error: input must be between 0 and 100.");

}

} catch (InputMismatchException ex) {

System.err.println("Error: invalid input entered.");

}



} while (!done);

return score;

}



private static void report(int scoreForTestA, int scoreForTestB,

int scoreForTestC, int scoreForTestD, int scoreForTestE) {

reportItem("first", scoreForTestA);

reportItem("second", scoreForTestB);

reportItem("thirth", scoreForTestC);

reportItem("fourth", scoreForTestD);

reportItem("fifth", scoreForTestE);



double average = calcAverage(scoreForTestA, scoreForTestB,

scoreForTestC, scoreForTestD, scoreForTestE);

System.out.format("The average test score is %.2f as grade %s.%n",

average, determineGrade(average));

}



private static void reportItem(String subject, int score) {

System.out.format("The %s test score is %d as grade %s.%n", subject,

score, determineGrade(score));

}



private static double calcAverage(int scoreForTestA, int scoreForTestB,

int scoreForTestC, int scoreForTestD, int scoreForTestE) {

// assume 5 subjects only

return (scoreForTestA + scoreForTestB + scoreForTestC + scoreForTestD + scoreForTestE) / 5d;

}



private static String determineGrade(double score) {

String grade = "F";

if (score >= 90 && score <= 100) {

grade = "A";

} else if (score >= 80) {

grade = "B";

} else if (score >= 70) {

grade = "C";

} else if (score >= 60) {

grade = "D";

} else {

grade = "F";

}

return grade;

}

}
2013-07-26 21:24:48 UTC
Hi there,



You have done very well with only a handful of small mistakes easily fixed.



Below is your program with the repairs done:



import java.util.Scanner;

public class TestGrade {

  // Declare global variables main and methods can use

  static double average;

  static char grade;

  public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);

    int testA, testB, testC, testD, testF;



    System.out.println("Please enter the first score between 0 and 100: ");

    testA = keyboard.nextInt();



    System.out.println("Please enter the second score between 0 and 100: ");

    testB = keyboard.nextInt();



    System.out.println("Please enter the third score between 0 and 100: ");

    testC = keyboard.nextInt();



    System.out.println("Please enter the fourth score between 0 and 100: ");

    testD = keyboard.nextInt();



    System.out.println("Please enter the fifth score between 0 and 100: ");

    testF = keyboard.nextInt();



    System.out.println("The average of your five tests was: " +

      calcAverage(testA, testB, testC, testD, testF)

    );



    System.out.println(

      "For TestA you scored: " + testA + " giving you Grade: "

          + determineGrade(testA)

      + "\nFor TestB you scored: " + testB + " giving you Grade: "

          + determineGrade(testB)

      + "\nFor TestC you scored: " + testC + " giving you Grade: "

          + determineGrade(testC)

      + "\nFor TestD you scored: " + testD + " giving you Grade: "

          + determineGrade(testD)

      + "\nFor TestF you scored: " + testF + " giving you Grade: "

          + determineGrade(testF)

      + "\nBased on your average: " + average + " Your final Grade is: "

          + determineGrade((int)average)

    );

  }

  // Method takes 5 integers and calculates average

  public static double calcAverage(int a, int b, int c, int d, int f) {

    average = (a + b + c + d + f) / 5;

    return average;

  }

  // Method takes one integer and calculates grade

  public static char determineGrade(int score) {

    if (score > 90) {

      grade = 'A';

    }

    else if (score >= 80) {

      grade = 'B';

    }

    else if (score >= 70) {

      grade = 'C';

    }

    else if (score >= 60) {

      grade = 'D';

    }

    else {

      grade = 'F';

    }

    return grade;

  }

}



You'll notice in main method I had to cast an integer on the last printout to the variable average.



This is because average was declared a double, but the method it is being passed to, to calculate the grade is requiring an integer as its parameter.



thus (int)average [the casting of type int to a double variable]



so determineGrade(int score) can accept average to calculate final overall grade.
2016-03-09 02:32:42 UTC
I wonder how long it'll take you to realise that although getting someone else to do your homework is an ideal short-term solution, it doesn't actually teach you anything about Java. Presumably the purpose of your course is to teach you Java, so you're not exactly helping yourself by cheating, are you?...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...