I am supposed to:
- create two constructors. a. query for student’s names and three scores. b. take four arguments
- write a method calculateAvg that calculates and sets average
- write a method calculateGrade that calculates based on 90+=A, 80+=B, 70+=C, 60+=D, <60=F
- Write a method toString that displays a gradeReport with the a. name b. three scores on a single line c. average and letter grade
- when the first constructor is used ensure that the scores are within 0-100. if not prompt again and explain why.
- format the output to exactly two decimal places
- format the output so that the scores are separated by tabs.
I am not asking for this all to be done, but if you look at my code can you give me any leads on where I’m going wrong and what I might need to add?
import java.text.DecimalFormat;
import java.util.Scanner;
public class GradeReport
{
String name, name1, name2;
int score1, score2, score3;
double average;
char grade;
public GradeReport() //creates the first constructor
{
Scanner sc = new Scanner (System.in);
System.out.println ("Enter student's first name: ");
name1 = sc.nextLine();
System.out.println ("Enter the student's last name: ");
name2 = sc.nextLine();
System.out.println ("Enter first grade: ");
score1 = sc.nextInt();
System.out.println ("Enter second grade: ");
score2 = sc.nextInt();
System.out.println ("Enter third grade: ");
score3 = sc.nextInt();
}
public GradeReport (String name, int score1, int score2, int score3)
{
}
public void calculateAverage()
{
average = ((score1 + score2 + score3) / 3);
DecimalFormat fmt = new DecimalFormat ("0.###"); //to format average to 2 decimal places
}
public void calculateGrade()
{
if (average >= 90)
System.out.println("A");
else if (average >= 80)
System.out.println("B");
else if (average >= 70)
System.out.println("C");
else if (average >= 60)
System.out.println("D");
else
System.out.println("F");
}
public String toString()
{
//System.out.println (name1, name2);
String gradeReport = Double.toString(score1) + "\t," + Double.toString(score2)+ "\t," + Double.toString(score3);
//String gradeReport = Double.toString(average);
return gradeReport;
}
}
From your assignment you can work out the following about your code (Apologies for any syntactic errors, and please don’t jusge me for my lack of ability to write eligible Java after 3 years doing .net)
I think you are pretty close to the answer you need, you just need to combine all the pieces. Look at Formatting strings in various ways.