tags:

views:

109

answers:

3

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


}
+1  A: 

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).

FrustratedWithFormsDesigner
I used the \t to force the output to be spaced a tabs width apart in between each score. Do I need to enclose the \t in double quotes?
Josh
@Josh: Yes. `\t` is an escape character and is a part of the string. For a single character (or a single escape character), I think you can also also write `'\t' + ", "+Double.toString(score2)...`, but I don't remember for sure, I don't think I use that notation very often, but those who know better will correct me if need be. I just put everything into double-quotes to keep it simpler. ;)
FrustratedWithFormsDesigner
I apologize for not posting the errors from the console. I uncommented everything and ran it. Here is the output from the error console
Josh
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax error on token "public", new expected The constructor GradeReport(String) is undefined Syntax error on token(s), misplaced construct(s) sc cannot be resolved sc cannot be resolved sc cannot be resolved sc cannot be resolvedsc cannot be resolvedSyntax error on token "}", delete this tokenSyntax error on token "void", @ expectedSyntax error on token "{", invalid TypeSyntax error on token "}", delete this tokenSyntax error on token "void", @ expectedSyntax error, insert "enum Identifier" to complete
Josh
EnumHeaderName Syntax error, insert "EnumBody" to complete BlockStatement Syntax error on token(s), misplaced construct(s) Syntax error on token "String", @ expected Syntax error on token "" ,"", delete this token Syntax error on token "" ,"", delete this token at GradeReport.main(GradeReport.java:13)
Josh
@Josh: Try my suggestions and recompile. Also, the compiler errors will be *much* easier to read if you *edit* your original question, and enclose the errors in a `<pre>...</pre>` block.
FrustratedWithFormsDesigner
sorry. I wasn't sure how to put code tags in a comment...
Josh
@Josh: You can't do it in comments, but you *can* do it in the original question.
FrustratedWithFormsDesigner
A: 

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.

bancer
A: 

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.

chillysapien
Thanks a lot chillysapien. This is really helpful. I will update with progress.
Josh
So I updated my code to what it currently looks like. Are you recommending that I should use the String.Format() method instead of the .toString() method? I believe that that is what I would have to do, right? Then I will call the two other methods and then print all of that stuff.
Josh
Josh: I was suggesting that within your toString method you could look at using the String.Format to build up your return String. Also look at using the StringBuilder class to build up multi-line strings. It depends on how you want to format your output. There are loads of ways of formatting strings for output but those might be a couple of places to start looking. What you are currently doing is perfectly fine its just a matter of personal preference :)
chillysapien