开发者

java grade report homework assignment

I am supposed to:

  1. create two constructors. a. query for student's names and three scores. b. take four arguments
  2. write a method calculateAvg that calculates and sets average
  3. write a method calculateGrade that calculates based on 90+=A, 80+=B, 70+=C, 60+=D, <60=F
  4. Write a method toString that displays a gradeReport with the a. name b. three scores on a single line c. average and letter grade
  5. when the first constructor is used ensure that the scores are within 0-100. if not prompt again and explain why.
  6. format the output to exactly two decimal places
  7. 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.nextLin开发者_StackOverflow社区e();

        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;
    }


}


You have elif statements commented out. I imagine if you uncommented them you'd get some compiler error. In Java, the elif should be written as else if.

Finally, your line

  String gradeReport = Double.toString(score1)\t, Double.toString(score2)\t,      Double.toString(score3); 

...what exactly did you intented by this? I think you kmight have meant:

String gradeReport = Double.toString(score1)+"\t, "+Double.toString(score2)+"\t,      "+Double.toString(score3); 

but it's not clear... If that is what you meant, the learning point here is that a string literal should be enclosed by double quotes, and the + operators is overloaded for strings to do string concatenation (appending one string to another).


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)

Class GradeReport{
//The values we get from the user
private string name
private int score1
private int score2
private int score3

//Things we are told we need to calculate at some point
private double average
private char grade

public GradeReport(){
// Get values from the user and validate them

// We will need to assign the values to the fields in some way 
// here...do we have somewhere else to do that already written?
}

public GradeReport(String name, int score1, int score2, int score3){
// We will probably want to assign these parameters to the fields here
}

public void calculateAverage(){
// You already have that pretty much, but you prob don't need to output it to the screen
}

public void calculateGrade(){
//Some sort of if...else logic is needed to work out the grade from the avg
}

public String toString(){
// We need to output our various bit of information here in a nicely formatted String
// I would recommend looking at String.Format() method in the Java API as a good starting point.

// At some point you will need to call calculateGrade and calculateAverage. It would 
// be useful to do that before you output to screen. It can either be done in here or 
// in your main class before you call toString()
}
}

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.


You also have logics errors in else if conditions. There should be: if average >= 90, else if average >= 80, else if average >= 70, else if average >= 60, else.

Replace public GradeReport() with public static void main(String[] args). Try to put all the code in one method - main. After the method will do what you expect it to do decompose (refactor) it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜