Page 1 of 1

## 8 Replies - 5879 Views - Last Post: 07 November 2011 - 05:41 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=254310&amp;s=aedc02c5171d8c01e87eab5f683a12f3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dedios09

Reputation: 0
• Posts: 13
• Joined: 21-October 11

Posted 04 November 2011 - 09:59 AM

```import java.util.Scanner;

/**
* Students in a course take four exams and earn a letter grade (A, A-, B+, B,
* B-, C+, C, C-, D+, D, D-, or F) for each of them. The course grade is
* determined by dropping the lowest grade and averaging the three remaining
* usual scheme A = 4.0, A- = 3.7, B+ = 3.3, . . ., D- = 0.7, F = 0. Then
* compute their average and convert it back to the closest letter grade. For
* example, an average of 3.51 would be an A-.
*
* For example, A- B+ C A
*
* For each input line, your output should be letterGrade where the
* letter grade is the grade earned in the course, as just described.
* For example, A
*
* The end of inputs will be indicated by a letterGrade1 field of -.
*
* @author smitra
*
*/

/**
* The program starts executing in the main method. It keeps calling processALine
* method until it returns false.
*
* @param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

}

/**
* Processes one line of input.
* @return true if the sentinel (the - character) was not encountered
*/
static boolean processALine(Scanner in)
return true;

return false; // this line is just a placeholder
}

/**
* Returns the smallest of four numbers.
*
* @param a   a number
* @param b   a number
* @param c   a number
* @param d   a number
* @return the smallest of a, b, c, and d
*/
public static double min(double a, double b, double c, double d)
{
// TODO: write code for this method

return 0; // this line is just a placeholder
}

/**
* Converts a number to the nearest letter grade using the
* usual scheme A = 4.0, A- = 3.7, B+ = 3.3, . . ., D- = 0.7, F = 0.
*
* For example, if x is the number grade, then we have:
* 2.5 ² x < 2.85: B-
* 2.85 ² x < 3.15: B
* 3.15 ² x < 3.5: B+
*
* Similarly, for other numbers.
*
* @param x a number between 0 and 4
* @return the nearest letter grade
*/
public static String numberToGrade(double x) {

if (n == "A") return "4.0";
if (n == "A-") return "3.7";
if (n == "B+") return "3.3";
if (n == "B") return "3.1";
if (n == "B-") return "2.8";
if (n == "C+") return "2.4 ";
if (n == "C") return "2.0 ";
if (n == "C-") return "1.7 ";
if (n == "D+") return "1.3 ";
if (n == "D") return "1.0 ";
if (n == "D-") return "0.7";
if (n == "F") return "0.0";

return ""; // this line is just a placeholder

}

/**
* Converts a letter grade to a number using the
* usual scheme A = 4.0, A- = 3.7, B+ = 3.3, . . ., D- = 0.7, F = 0.
*
* @param grade a letter grade (A+, A, A-, . . ., D-, F)
* @return the equivalent number grade
*/
{
if (n == "A") return "4.0";

return 0; // this line is just a placeholder

}

} // end of class Grades
```

Can someone help me out. Am i on the right track when it comes to the main method? Also going into the boolean method i'm not sure what i'm asked to do..any help would be appreciated

Is This A Good Question/Topic? 0

## Replies To: Calculating Letter Grades

### #2 blackcompe

• D.I.C Lover

Reputation: 1156
• Posts: 2,538
• Joined: 05-May 05

Posted 04 November 2011 - 10:47 AM

Firstly, you should be reading in the data as strings, not floating point integers. You have

```letterGrade1 =  in.nextDouble();

```

yet you said in the program requirements that an example input was

Quote

* For example, A- B+ C A

So you should be using Scanner.next.

In processALine, which should be called processLine, or something more meaningful, like isExitSymbol, your want to see if the string is a dash. It's formal parameter should be a String, not a Scanner. And as for the implementation (the code inside the block), using a conditional is correct, but you shouldn't use != on strings. That's not semantically correct. Use String.equals.

```public static String numberToGrade(double x) {

if (n == "A") return "4.0";

```

Semantically (and syntactically), it doesn't even make sense. Firstly, where has n been declared? Secondly, assuming that you meant

```if (x == "A") return "4.0";
```

your trying to compare x, which is a double, to a string.

```public static double numberToGrade(double x) {

if (x == 4.0) return "A";

```

### #3 dedios09

Reputation: 0
• Posts: 13
• Joined: 21-October 11

Posted 04 November 2011 - 11:11 AM

blackcompe, on 04 November 2011 - 10:47 AM, said:

Firstly, you should be reading in the data as strings, not floating point integers. You have

```letterGrade1 =  in.nextDouble();

```

yet you said in the program requirements that an example input was

Quote

* For example, A- B+ C A

So you should be using Scanner.next.

In processALine, which should be called processLine, or something more meaningful, like isExitSymbol, your want to see if the string is a dash. It's formal parameter should be a String, not a Scanner. And as for the implementation (the code inside the block), using a conditional is correct, but you shouldn't use != on strings. That's not semantically correct. Use String.equals.

```public static String numberToGrade(double x) {

if (n == "A") return "4.0";

```

Semantically (and syntactically), it doesn't even make sense. Firstly, where has n been declared? Secondly, assuming that you meant

```if (x == "A") return "4.0";
```

your trying to compare x, which is a double, to a string.

```public static double numberToGrade(double x) {

if (x == 4.0) return "A";

```

```public static double min(double a, double b, double c, double d)
{
if ((a < b && a < c) || (a < d));
return a;

if ((b < a && b < c) || (b < d));
return b;

if ((c < a && c < B)/> ||(c < d));
return c;

if ((d < a && d < b && d < c));
return d;

return 0; // this line is just a placeholder
}

/**
* Converts a number to the nearest letter grade using the
* usual scheme A = 4.0, A- = 3.7, B+ = 3.3, . . ., D- = 0.7, F = 0.
*
* For example, if x is the number grade, then we have:
* 2.5 ² x < 2.85: B-
* 2.85 ² x < 3.15: B
* 3.15 ² x < 3.5: B+
*
* Similarly, for other numbers.
*
* @param x a number between 0 and 4
* @return the nearest letter grade
*/
public static String numberToGrade(double x) {

if (x == 4.0) return "A";
if (x == 3.7) return "A-";
if (x == 3.3) return "B+";
if (x == 3.1) return "B";
if (x == 2.8) return "B-";
if (x == 2.4) return "C+";
if (x == 2.0) return "C ";
if (x == 1.7) return "C- ";
if (x == 1.3) return "D+ ";
if (x == 1.0) return "D ";
if (x == 0.7) return "D-";
if (x == 0.0) return "F";

return ""; // this line is just a placeholder

}

/**
* Converts a letter grade to a number using the
* usual scheme A = 4.0, A- = 3.7, B+ = 3.3, . . ., D- = 0.7, F = 0.
*
* @param grade a letter grade (A+, A, A-, . . ., D-, F)
* @return the equivalent number grade
*/
{
if (x == "A+") return 4.0;
if (n == "A-") return 3.7;
if (n == "B+") return 3.3;
if (n == "B") return 3.1;
if (n == "B-") return 2.8;
if (n == "C+") return 2.4;
if (n == "C") return 2.0;
if (n == "C-") return 1.7;
if (n == "D+") return 1.3;
if (n == "D") return 1.0;
if (n == "D-") return 0.7;
if (n == "F") return 0.0;

return 0; // this line is just a placeholder

}

} // end of class Grades
```

Okay i changed all the 'n' values to x and that seemed to work, also for the boolean method am i doing that correctly cause its asking to return the smallest of those 4.

### #4 blackcompe

• D.I.C Lover

Reputation: 1156
• Posts: 2,538
• Joined: 05-May 05

Posted 04 November 2011 - 12:32 PM

This about right. Those logical ORs need to be logical ANDs. And it should be less than or equal. If a, b, c, and d are the same none of your conditions are true.

``` if(a <= b && a <= c && a <= d)
```

This post has been edited by blackcompe: 04 November 2011 - 12:34 PM

### #5 Fuzzyness

• Comp Sci Student

Reputation: 669
• Posts: 2,438
• Joined: 06-March 09

Posted 04 November 2011 - 12:39 PM

You should also consider changing all of those if's to an else if statements.. dont want it to check 20 times if it is the first 1.

### #6 blackcompe

• D.I.C Lover

Reputation: 1156
• Posts: 2,538
• Joined: 05-May 05

Posted 04 November 2011 - 01:06 PM

A better way of calculating min would be:

```
public class Main {

public static void main( String[] args ) {
System.out.println( min( 23, 5345, -9, 29 ) );
}

static int min( int a, int b, int c, int d ) {
int e = min( a, b );
int f = min( c, d );
return min( e, f );
}

static int min( int a, int b ) {
return ( a < b ) ? a : b;
}
}

```

This post has been edited by blackcompe: 04 November 2011 - 01:07 PM

### #7 AMZDeCoder

Reputation: 25
• Posts: 102
• Joined: 04-November 11

Posted 04 November 2011 - 01:39 PM

Dear dedios09,

Following is the solution I have developed for your problem, it should work perfectly, for all the cases. Do try it out, also are you going to input the cases manually or automated ???

I have made use of maps, and look-up techniques and so the code is very efficient in execution. The code you have developed is not very efficient it seems and has a lot of unnecessary functions which i tried to avoid. Do let me know if you are satisfied with my solution.

```import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

/**
*
*/

/**
* @author ameya Software Consultant at http://aknsoftwaresolutions.110mb.com/
*
*
*/

private final static HashMap gradeMap = new HashMap();

private final static HashMap reverseGradeMap = new HashMap();

static {

}

/**
* @param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

// Convert them to numeric grades from the lookup table.
for (int i = 0; i < letterGradesArray.length; i++) {
}
// Sort in ascending order.
// Now just take the average of last 3 elements and show the course
// grade as output, that will eliminate the grade with the lowest score.
// Now get the letter grade corresponding to the course grade, round up

// Now check if there in the map else keep incrementing by 0.1
}
// Print the final course grade.
}
}

```

### #8 123ISU

Reputation: 0
• Posts: 1
• Joined: 07-November 11

Posted 07 November 2011 - 05:31 PM

This is a homework assignment for the Iowa State University Computer Science 207 class for Fall 2011.

This is a homework assignment for the Iowa State University Computer Science 207 class for Fall 2011.
FYI I just had to google "static boolean processALine(Scanner in){}" to find this.

### #9 dedios09

Reputation: 0
• Posts: 13
• Joined: 21-October 11

Posted 07 November 2011 - 05:41 PM

AMZDeCoder, on 04 November 2011 - 01:39 PM, said:

Dear dedios09,

Following is the solution I have developed for your problem, it should work perfectly, for all the cases. Do try it out, also are you going to input the cases manually or automated ???

I have made use of maps, and look-up techniques and so the code is very efficient in execution. The code you have developed is not very efficient it seems and has a lot of unnecessary functions which i tried to avoid. Do let me know if you are satisfied with my solution.

```import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

/**
*
*/

/**
* @author ameya Software Consultant at http://aknsoftwaresolutions.110mb.com/
*
*
*/

private final static HashMap gradeMap = new HashMap();

private final static HashMap reverseGradeMap = new HashMap();

static {

}

/**
* @param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

// Convert them to numeric grades from the lookup table.
for (int i = 0; i < letterGradesArray.length; i++) {
}
// Sort in ascending order.
// Now just take the average of last 3 elements and show the course
// grade as output, that will eliminate the grade with the lowest score.
// Now get the letter grade corresponding to the course grade, round up

// Now check if there in the map else keep incrementing by 0.1
}
// Print the final course grade.
}
}

```

this is actually wonderful code and i'm learning new things from it, but is there anyway to adjust it for the methods that i've listed. I just don't really know what my professor is asking but anything close to it would help me out.

nevermind i've got this.