4 Replies - 1232 Views - Last Post: 25 June 2012 - 03:29 PM Rate Topic: -----

#1 thessler1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-June 12

Calculate GPA

Posted 25 June 2012 - 12:39 PM

I'm creating a program that asks the user to enter grades for each course of a given semester (A, B, C, D, F). Enter X to stop entering grades. The program should calculte the GPA for the semester. Assume that each course has the same number of credit hours. Use the NUMBERFORMAT class to format the GPA to 2 decimal places. Us the SCANNER class for input and the command window for output. Entering grades should be handled by one method and displaying the GPA by a different method. use a SWITCH statement to assign grade points for different grades.

I'm not looking for someone to do my homework, just to steer me in the right direction to correct my errors.


   import java.util.Scanner;
   public class CalculateGPA
{
   public void GetGrades ( ) 
{
   String grade;
   TextReader console = new TextReader ( );
   Double gradetotal = 0, count = 0;
   System.out.print ("\n\nPlease enter your grades below, using"
   + " upper and/or lower case. Non-grade characters will"
   + " be disregarded and you should enter 'x' or 'X' to"
   + " exit.");
   System.out.print ("\nList of grades: ");
   grade = console.readChar ( );
   while (grade !='x' || grade!='X') {
   if (grade == 'A' || grade == 'a') {
   gradetotal += 4;
   count++;
}
   if (grade == 'B' || grade == 'b') {
   gradetotal += 3;
   count++;
}
   if (grade == 'C' || grade == 'c') {
   gradetotal += 2;
   count++;
}
   if (grade == 'D' || grade == 'd') {
   gradetotal += 1;
   count++;
}
   if (grade == 'F' || grade == 'f') {
   gradetotal += 0;
   count++;
}
   if (grade == 'X' || grade == 'x')
   break;
}
}
   public double CalculateGPA ( )
{
   return (gradetotal/(double)count);
}
}



[ERRORS:
----jGRASP exec: javac CalculateGPA.java

CalculateGPA.java:13: error: cannot find symbol
TextReader console = new TextReader ( );
^
symbol: class TextReader
location: class CalculateGPA
CalculateGPA.java:13: error: cannot find symbol
TextReader console = new TextReader ( );
^
symbol: class TextReader
location: class CalculateGPA
CalculateGPA.java:14: error: incompatible types
Double gradetotal = 0, count = 0;
^
required: Double
found: int
CalculateGPA.java:14: error: incompatible types
Double gradetotal = 0, count = 0;
^
required: Double
found: int
CalculateGPA.java:21: error: incomparable types: String and char
while (grade !='x' || grade!='X') {
^
CalculateGPA.java:21: error: incomparable types: String and char
while (grade !='x' || grade!='X') {
^
CalculateGPA.java:22: error: incomparable types: String and char
if (grade == 'A' || grade == 'a') {
^
CalculateGPA.java:22: error: incomparable types: String and char
if (grade == 'A' || grade == 'a') {
^
CalculateGPA.java:26: error: incomparable types: String and char
if (grade == 'B' || grade == 'b') {
^
CalculateGPA.java:26: error: incomparable types: String and char
if (grade == 'B' || grade == 'b') {
^
CalculateGPA.java:30: error: incomparable types: String and char
if (grade == 'C' || grade == 'c') {
^
CalculateGPA.java:30: error: incomparable types: String and char
if (grade == 'C' || grade == 'c') {
^
CalculateGPA.java:34: error: incomparable types: String and char
if (grade == 'D' || grade == 'd') {
^
CalculateGPA.java:34: error: incomparable types: String and char
if (grade == 'D' || grade == 'd') {
^
CalculateGPA.java:38: error: incomparable types: String and char
if (grade == 'F' || grade == 'f') {
^
CalculateGPA.java:38: error: incomparable types: String and char
if (grade == 'F' || grade == 'f') {
^
CalculateGPA.java:42: error: incomparable types: String and char
if (grade == 'X' || grade == 'x')
^
CalculateGPA.java:42: error: incomparable types: String and char
if (grade == 'X' || grade == 'x')
^
CalculateGPA.java:48: error: cannot find symbol
return (gradetotal/(double)count);
^
symbol: variable gradetotal
location: class CalculateGPA
CalculateGPA.java:48: error: cannot find symbol
return (gradetotal/(double)count);
^
symbol: variable count
location: class CalculateGPA
20 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.[

This post has been edited by jon.kiparsky: 25 June 2012 - 12:41 PM
Reason for edit:: fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Calculate GPA

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,105
  • Joined: 19-March 11

Re: Calculate GPA

Posted 25 June 2012 - 12:49 PM

Those errors are pretty straightforward. What is a TextReader? It's not a standard class - is it something your course uses?

This one is telling you exactly what the problem is: grade is a String and 'x' is a char. Single quotes make a char, double quotes make a String.

CalculateGPA.java:21: error: incomparable types: String and char
while (grade !='x' || grade!='X') {


If you want to compare two Strings, you'll want to use the .equals method, or a convenient variation on it:
while ( ! grade.equalsIgnoreCase("x")) {



This one is a little more subtle, though the fix is the easiest of the bunch:

Double gradetotal = 0, count = 0;




"Double", with a capital D, is the name of the Double wrapper class, which you'll learn about pretty soon. It's different from "double" which is the primitive class representing double-precision floating point numbers. Change the initial 'D' to lowercase and it should be fine.
Was This Post Helpful? 0
  • +
  • -

#3 thessler1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-June 12

Re: Calculate GPA

Posted 25 June 2012 - 01:11 PM

The TextReader is for the class. My teacher likes things coded wierd. I've fixed most of the errors but am still getting 2 I don't understand.



   import java.util.Scanner;
   public class CalculateGPA1
{
   public void GetGrades ( ) 
{
   String grade;
   TextReader console = new TextReader ( );
   double gradetotal = 0, count = 0;
   System.out.print ("\n\nPlease enter your grades below, using"
   + " upper and/or lower case. Non-grade characters will"
   + " be disregarded and you should enter 'x' or 'X' to"
   + " exit.");
   System.out.print ("\nList of grades: ");
   grade = console.readChar ( );
   while (grade !='x' || grade!='X') 
   if (grade == 'A' || grade == 'a')
   gradetotal += 4;
   count++;

   if (grade == 'B' || grade == 'b') 
   gradetotal += 3;
   count++;

   if (grade == 'C' || grade == 'c') 
   gradetotal += 2;
   count++;

   if (grade == 'D' || grade == 'd') 
   gradetotal += 1;
   count++;

   if (grade == 'F' || grade == 'f') 
   gradetotal += 0;
   count++;

   if (grade == 'X' || grade == 'x')
   break;
}
}
   public double CalculateGPA ( )
{
   return (gradetotal/(double)count);
}



ERRORS:
CalculateGPA1.java:46: error: class, interface, or enum expected
public double CalculateGPA ( )
^
CalculateGPA1.java:49: error: class, interface, or enum expected
}
^
2 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

This post has been edited by jon.kiparsky: 25 June 2012 - 01:21 PM

Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,105
  • Joined: 19-March 11

Re: Calculate GPA

Posted 25 June 2012 - 01:19 PM

Okay, what's going on there is that your calculateGPA method is outside of the scope of the class.

The curly braces define a scope: everything belonging to the class has to be within that class's braces.

public class CalculateGPA1
{
  public void GetGrades ( ) 
  {
  String grade;
  TextReader console = new TextReader ( );
  double gradetotal = 0, count = 0;
... 
  if (grade == 'X' || grade == 'x')
  break;
  } 
}  // end of the class

//now we're not in a class definition anymore... 
 
public double CalculateGPA ( )  // so we can't define a method here
{
return (gradetotal/(double)count);
}


The solution is simple, just move one of those last curly braces before the calculateGPA method to the end of the file. You'll want to start indenting your code in the standard manner, it'll help you spot things like that.

Also, you'll notice the [code] button in the editor control panel. If you select your code as a block of text and hit that button, it'll put the code tags around it for you.
Was This Post Helpful? 0
  • +
  • -

#5 thessler1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-June 12

Re: Calculate GPA

Posted 25 June 2012 - 03:29 PM

I'm sorry, I guess I didn't understand what you were saying cause I still have 1 error.


 import java.util.Scanner;
   public class CalculateGPA
{
   public void GetGrades ( ) 
{
   String grade;
   TextReader console = new TextReader ( );
   double gradetotal = 0, count = 0;
   System.out.print ("\n\nPlease enter your grades below, using"
   + " upper and/or lower case. Non-grade characters will"
   + " be disregarded and you should enter 'x' or 'X' to"
   + " exit.");
   System.out.print ("\nList of grades: ");
   grade = console.readChar ( );
   while (grade !='x' || grade!='X') 
   if (grade == 'A' || grade == 'a') 
   gradetotal += 4;
   count++;

   if (grade == 'B' || grade == 'b') 
   gradetotal += 3;
   count++;

   if (grade == 'C' || grade == 'c') 
   gradetotal += 2;
   count++;

   if (grade == 'D' || grade == 'd') 
   gradetotal += 1;
   count++;

   if (grade == 'F' || grade == 'f') 
   gradetotal += 0;
   count++;

   if (grade == 'X' || grade == 'x')
   break;
   }

} //end main

   public double CalculateGPA ( )
	
// end class


ERROR:
CalculateGPA.java:48: error: class, interface, or enum expected
public double CalculateGPA ( )
^
1 error
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1