• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Whats wrong with my code

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello, can some one help me out here. This code does not work, i know its got problems but im stuck. Please tell me what im doing wrong and how to fix it.

 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

You know the routine! What errors do you get?

I'm not going to copy, paste and compile your code, nor am I going to look through every line carefully, but if you tell us what error you get, we will hopefully be able to glance at the code and point out the error.

So, compile error or runtime error?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That's the first problem: you seem to have some errant brackets. Don't you mean to close that code chunk rather than open another? Also, yout getName() method is void, yet you imply a return type when you say this:



Start with fixing those, and take it from there. The compiler will give you clues.
[ April 05, 2005: Message edited by: Ben Poole ]
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello John Swain,
I try to help you to explain what's wrong with your code:
1. you can't declare some other methods in a method.
2. you can't parse parameter(s) when you call a method, if the method doesn't provide it, e.g: in your getName() method, it doesn't provide parameter to be parsed, but when you called, you parsed a parameter, getName(name);
3. local variable can't be used to global, e.g in your main method, there are many global variables, and it just can be used in main method, if you want to use your local variable to another method, you can pass it in method's parameter(look at my code in getScore or getLetterGrade method). But don't forget with pass by value and pass by parameter in java.
4. look at your code : keyboard.readline(). It suppose to be keyboard.readLine(). Don't forget, java is case sensitive.
5. you forget to close the braches, e.g in if (( finalScore >= 70) && ( < 80 ))
6. if (( finalScore >= 70) && ( < 80 )), it suppose to be if (( finalScore >= 70) && ( finalScore < 80 )) and others.
7. this is not a technical issue, but in method getLetterGrade(), i always found that the final score is less than 60 if the input range between 0 - 100 .
glad to help..
cmiiw..

here's the fixed code:
import java.io.*;
public class NewGradesProgram{

private static BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException{
//Section: Variables
String name;
int quiz1 = 0;
int quiz2 = 0;
double tMidTerm, tFinal, tQuiz;
int midTermExam = 0;
int finalExam = 0;
double finalScore = 0;
char grade;
name = getName();
quiz1 = getFirstQuiz();
quiz2 = getSecondQuiz();
midTermExam = getMidTermExam();
finalExam = getFinalExam();
finalScore = getScore(quiz1, quiz2, midTermExam, finalExam);
grade = getLetterGrade(finalScore);
System.out.println( "Student name: " + name );
System.out.println( "First quiz score: " + quiz1 );
System.out.println( "Second quiz score: " + quiz2 );
System.out.println( "Midterm Exam score: " + midTermExam );
System.out.println( "Final Exam score: " + finalExam );
System.out.println( "Final Score: " + finalScore );
System.out.println( "Your final grade is: " + grade );

} //End Method: main
//Method: get grades
private static String getName() throws IOException
{
System.out.println("Students calculated final scores");
System.out.println("and letter grades. ");
System.out.println("Please enter your name. ");
String name = keyboard.readLine();
return name;
}

//Method: get First Quiz
private static int getFirstQuiz() throws IOException
{
System.out.println("Please enter your first quiz grade. ");
int quiz1 = Integer.parseInt(keyboard.readLine());
return quiz1;
}
//Method: get Second Quiz
private static int getSecondQuiz() throws IOException
{
System.out.println("Please enter your second quiz grade. ");
int quiz2 = Integer.parseInt(keyboard.readLine());
return quiz2;
}
//Method: get Mid Term Exam
private static int getMidTermExam() throws IOException
{
System.out.println("Please enter your midterm exam grade. ");
int midTermExam = Integer.parseInt(keyboard.readLine());
return midTermExam;
}
//Method: get Final Exam
private static int getFinalExam() throws IOException
{
System.out.println("Please enter your finalexam grade. ");
int finalExam = Integer.parseInt(keyboard.readLine());
return finalExam;
}
//Method: Calculate Score
private static double getScore(int quiz1, int quiz2, int midTermExam, int finalExam)
{
double tQuiz = (((quiz1 + quiz2)/20)*.25);
double tMidTerm = ((midTermExam/100)*.25);
double tFinal = ((finalExam/100)*.50);
double finalScore = tQuiz + tMidTerm + tFinal;
return finalScore;

}
//Method: get letter grade
private static char getLetterGrade(double finalScore){
char grade = 'E';//assumption:if grade < 60 then grade E
if ( finalScore >= 90 )
{
grade = 'A';
}
else if (( finalScore >= 80) && ( finalScore < 90 ))
{
grade = 'B';
}
else if (( finalScore >= 70) && ( finalScore < 80 ))
{
grade = 'C';
}
else if (( finalScore >= 60) && ( finalScore < 70 ))
{
grade = 'D';
}
return grade;
}
}//End Class: gradesProgram
 
John Swain
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Swain:
hello, can some one help me out here. This code does not work, i know its got problems but im stuck. Please tell me what im doing wrong and how to fix it.



Well, you could sit down with your Java book and a compiler and really work on the mechanics of the language. Get so you really understand how methods and their arguments work, how braces format and structure your code, how variables and types work... that sort of thing. Tackle small programs before taking on big ones. Force yourself to never write a single line of code that you couldn't explain to someone.

Or, you could just keep posting until someone eventually does your homework for you. That works too.

- Jeff
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Or, you could just keep posting until someone eventually does your homework for you. That works too."

Apparently that works pretty well, judging from Ricky's reply. Of course I haven't actually looked at or run Ricky's code so who knows.
 
reply
    Bookmark Topic Watch Topic
  • New Topic