-2

EDIT:

I think I copied slightly wrong code!

public class GradeCalculator {

 public static void calculateGrade(int mark) {
  if (mark >= 70) System.out.println("grade = A");
  if (mark >= 60) System.out.println("grade = B");
  if (mark >= 50) System.out.println("grade = C");
  if (mark >= 40) System.out.println("grade = D");
  if (mark <  40) System.out.println("grade = F");
 }

 public static void main(String[] args) {

 }
}

I'm only just beginning to try Java (or any programming. I'm working my way through practice questions. I have this code and need to write statements in the main method to test it.

How can I do that?

Thanks for any help / pointers in the right direction.

Mark

mark e
  • 1
  • 1
  • Your code doesn't compile, mate. – Konstantin Yovkov Jan 15 '15 at 08:36
  • If you have a bug in your code, the first thing you should do is use your debugger to work out why your program is doing what it does. I also suggest you use the reformatter in your IDE. BTW You can turn the code above into a 1 liner. – Peter Lawrey Jan 15 '15 at 08:38

4 Answers4

1

You need to call the method from main. I highly recommend you to go through the very basic Java tutorial, you're missing a very basic concepts.

Once you did that, you need to have else. In your case, if the first if is satisfied, the following ifs will be.

Think about it, if (mark >= 70) then for sure if (mark >= x) for any x <= 70.

Maroun
  • 94,125
  • 30
  • 188
  • 241
1

Well, to begin this farce, let's edit your code into something that compiles and works in a sensical way:

public class GradeCalculator {

    public static void claculateGrade (int mark) {
        if (mark >= 70) System.out.println("grade = A");
        else if (mark >= 60) System.out.println("grade = B");
        else if (mark >= 50) System.out.println("grade = C");
        else if (mark >= 50) System.out.println("grade = D");
        else if (mark >= 40) System.out.println("grade = E");    
    }

    public static void main (String[] args)  {

    }

}

Now, to print a grade some guy would get with some marks, you can call your "claculateGrade" method like this:

public static void main (String[] args)  {
    claculateGrade(55);
}

This should print "grade = C" to the console. The full code:

public class GradeCalculator {

    public static void claculateGrade (int mark) {
        if (mark >= 70) System.out.println("grade = A");
        else if (mark >= 60) System.out.println("grade = B");
        else if (mark >= 50) System.out.println("grade = C");
        else if (mark >= 50) System.out.println("grade = D");
        else if (mark >= 40) System.out.println("grade = E");    
    }

    public static void main (String[] args)  {
        claculateGrade(55);
    }

}

Now, one could further improve this. Let's start by making the method return the grade:

public static char getGrade (int mark) {
    if (mark >= 70) return 'A';
    else if (mark >= 60) return 'B';
    else if (mark >= 50) return 'C';
    else if (mark >= 50) return 'D';
    else if (mark >= 40) return 'E';

    /* if below 40, return "fail" */
    return 'F';
}

This would change your code into:

public class GradeCalculator {

    public static char getGrade (int mark) {
        if (mark >= 70) return 'A';
        else if (mark >= 60) return 'B';
        else if (mark >= 50) return 'C';
        else if (mark >= 50) return 'D';
        else if (mark >= 40) return 'E';

        /* if below 40, return "fail" */
        return 'F';
    }

    public static void main (String[] args)  {
        System.out.println("Grade: " + getGrade(55));
    }

}
Olavi Mustanoja
  • 2,045
  • 2
  • 23
  • 34
  • So the issue was the else if (once I'd posted the code I meant to in my original question!) Thanks for the replies. – mark e Jan 15 '15 at 08:49
0

You calculate grade should return the grade, not print it. If you want to print it the caller can print the grade e.g.

public static char calculateGrade(int mark) {
    return "UUUUEDCBAAAA".charAt(mark/10);
}

System.out.println("Grade = " + calculateGrade(65));

The way the code works is that it takes advantage of the fact each region is a multiple of 10. i.e. when you divide by 10 you get

100 -> 10
90 - 99 -> 9
80 - 89 -> 8
70 - 79 -> 7
60 - 69 -> 6
50 - 59 -> 5
40 - 49 -> 4
30 - 39 -> 3
20 - 29 -> 2
10 - 19 -> 1
0 - 9 -> 0

So now all the numbers are turning into regions of 0 to 10. However we want grades as a letter so you get

100 -> A
90 - 99 -> A
80 - 89 -> A
70 - 79 -> A
60 - 69 -> B
50 - 59 -> C
40 - 49 -> D
30 - 39 -> E
20 - 29 -> U
10 - 19 -> U
0 - 9 -> U
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • It's hard to see what the code is doing – Olavi Mustanoja Jan 15 '15 at 08:47
  • @OlaviMustanoja I have updated my answer. Was the that dividing by 10 was unclear or that looking up a letter in a String is unclear? – Peter Lawrey Jan 15 '15 at 08:52
  • 1
    Sorry my comment was a bit vague. I meant that for an obvious beginner that piece of code might be a bit overwhelming. Now that you've explained it it's quite ok. – Olavi Mustanoja Jan 15 '15 at 08:54
  • I don't think I saw the answer before it was edited. Now it is, all replies make sense (for an obvious beginner :) ) Thank you – mark e Jan 15 '15 at 09:11
  • @OlaviMustanoja I would hope that a beginner could work out what it does, though I would not expect a beginner to imagine writing the code that way. ;) – Peter Lawrey Jan 15 '15 at 09:24
0

To make your code work you need to call your method as all your coding is done in that part ie to print on the basis of condition .

public class GradeCalculator { // This is your class

     public static void calculateGrade(int mark) { 
  //This is the method containing conditions
    on the basis of these conditions the corroesponding
   sentences will be  printed
      if (mark >= 70) System.out.println("grade = A");
      if (mark >= 60) System.out.println("grade = B");
      if (mark >= 50) System.out.println("grade = C");
      if (mark >= 40) System.out.println("grade = D");
      if (mark <  40) System.out.println("grade = F");
     }
    //now u need to call the method from your main method.. main method is the entry point of your program so it will start from main then check your method call and do as per the code written there
     public static void main(String[] args) {
    // to call your method just right your method name and enter marks

    calculateGrade(50); // you can enter any integer 
     }
    }
kirti
  • 4,499
  • 4
  • 31
  • 60