Java Help
I am doing this project in my CS class and have been working on fixing the same problem in my code. I've looked all over for some kind of solid guidelines, i just cant find anything basic enough that it applies. I'm not asking for an answer, just a little helpful advice. i keep getting "illegal start of expression" code as follows=
public class Grades {
// Fields
int grade1, grade2, grade3;
public Grades() {
public void setGrade1(int grade1) {
this.grade1 = grade1;
}
public void setGrade2(int grade2) {
this.grade2 = grade2;
}
public void setGrade3(int grade3) {
this.grade3 = grade3;
}
double gradeAverage = (grade1 + grade2 + grade3 / 3);
public double gradeAverage() {
return double gradeAverage;
}
;}}
[828 byte] By [
Soggy616a] at [2007-10-3 5:21:41]

public class Caller
{
// Fields
int grade1, grade2, grade3;
public Caller()
{
//not impl
}
public void setGrade1(int grade1)
{
this.grade1 = grade1;
}
public void setGrade2(int grade2)
{
this.grade2 = grade2;
}
public void setGrade3(int grade3)
{
this.grade3 = grade3;
}
public double gradeAverage()
{
return ((grade1 + grade2 + grade3) / 3);
}
}
You have a bunch of methods inside your constructor.
Your constructor starts here:
public Grades() {
You need to close that brace.
Then the following two lines should be swapped:
double gradeAverage = (grade1 + grade2 + grade3 / 3);
public double gradeAverage() {
Then remove the extra brace near the end.
You'll then need to account for the fact that you are doing integer division and you need double division.
> return ((grade1 + grade2 + grade3) / 3);This isnt calculating the average. Java follows the Order of Operations.
Norweed, why do you insist on giving full code, instead of guidance toward the answer? You aren't helping anyone.
> Norweed, why do you insist on giving full code,
> instead of guidance toward the answer? You
> aren't helping anyone.
Because that's what I'm going to do. Besides, he already had the code. I was just finxing some syntax. He can look at the differences.
If that doesn't work he can read a book or get an IDE.
> > return ((grade1 + grade2 + grade3) /
> 3);
>
> This isnt calculating the average. Java follows the
> Order of Operations.
ummmyeah. I know this. Id the parens weren't there then it would calcualte grade3 / 3then add grade 2 and 1 to it. Parens need to be there order of operations screws it up.
also, yeah, put a d after the 3
> > return ((grade1 + grade2 + grade3) /
> 3);
>
> This isnt calculating the average. Java follows the
> Order of Operations.
I really don't know what you're saying here. care to explain. What's an order of operation :nilly:
> > Norweed, why do you insist on giving full code,
> > instead of guidance toward the answer? You
> > aren't helping anyone.
>
> Because that's what I'm going to do.
Hey! Now look here one minute. If you want to "help" the dregs by posting full code answers go right ahead but when the OP says
"I'm not asking for an answer, just a little helpful advice"
Then I think you should at least have respect for them. I mean being mad at wanerja, and me, and whoever else is one thing but please don't take that out on people who actually want to learn. That's not fair to them.
OK, well I agree with that and if I could edit my post I would, but I can't.however, deep down I think if he had a choice between code and advice he'd ask for both. People that post here just have to add that legal boiler plate so they don't get ignored.
> I really don't know what you're saying here. care to> explain. What's an order of operation :nilly:I wasnt paying attention. Yours is correct, but the OP's isnt.
silly that that was all it was. thank you. but i still have the compiler telling it needs".class" what exactly does that mean?
> silly that that was all it was. thank you. but i> still have the compiler telling it needs".class" what> exactly does that mean?I think you have some garbage formatting stuff at the end of your source file still.Post all your code and we'll see.
public class Grades {
// Fields
int grade1, grade2, grade3;
public Grades() {}
public void setGrade1(int grade1) {
this.grade1 = grade1;
}
public void setGrade2(int grade2) {
this.grade2 = grade2;
}
public void setGrade3(int grade3) {
this.grade3 = grade3;
}
public double gradeAverage() {
double gradeAverage = (grade1 + grade2 + grade3 / 3);
return double gradeAverage;
}
}
Look at the code that was already given to you. Specifically the last few lines.
There is a syntax error in here
public double gradeAverage() {
double gradeAverage = (grade1 + grade2 + grade3 / 3);
return double gradeAverage;
}
Specifically the line "return double gradeAverage. You don't need the double in there because you already said what gradeAverage is (and it doesn't go on a return statement anyway).
And the way hes calculating the average is incorrect.
> And the way hes calculating the average is incorrect.I think alot of students could find that bug very helpful. :D
SoggyDoes your assignment state that you need the three setGradeX methods? If not I would pass the three grades as parameters to your constructor and set them there.