I hit a bump when I'm doing my Java code. I feel like I somehow got the concept messed up, like I not sure for this:
void setScore(float[] sco)
{
sco = score;
}
public void setScore(float sco, int id)
{
sco[id] = score;
}
The error message corresponds to "sco[ID] = score; "
The type of the expression must be an array type but it resolved to float
I'm confused what I should put in the bracket, the book asks me to put "float[] score" instead of "float[] sco", but it doesn't work, so I edited a bit after several trials. This part of coding generally describes the method of overloading that stores the score for 5 subjects.
And this is my whole coding:
public class Score {
float math, english, physics, chemistry, biology;
float sum, average;
float[] score;
int id;
void setMath(float Math) {
math = Math;
}
void setEnglish(float English) {
english = English;
}
void setPhysics(float Physics) {
physics = Physics;
}
void setChemistry(float Chemistry) {
chemistry = Chemistry;
}
void setBiology(float Biology) {
biology = Biology;
}
void setSum(float Sum) {
sum = math + english + physics + chemistry + biology;
}
void setAverage(float Average) {
average = sum / 5;
}
float getMath() {
return math;
}
float getEnglish() {
return english;
}
float getPhysics() {
return physics;
}
float getChemistry() {
return chemistry;
}
float getBiology() {
return biology;
}
float getSum() {
return sum;
}
float getAverage() {
return average;
}
public void setScore(float[] sco)
{
sco = score;
}
public void setScore(float sco, int id)
{
sco[id] = score;
}
}
Now my problem solved! Since I just changed like this:
public void score()
{
}
public void setScore(float[] score)
{
sco = score;
}
Can anyone tell me why now the problem resolved? Will really appreciate a lot!
You are assigning a value of the class variable
score
to the parametersco
when you should do the opposite. In other words:Your code says:
But what you should do is:
In both functions you need to switch the order of
score
andsco
so thatscore
gets the value ofsco
.As far as your error, in
setScore(float sco, int id)
you defined parametersco
as afloat
but you are trying to access it as an array (by sayingsco[Id] = score
). That is why you are getting your error message.Like I said, you can fix this by switching the order again:
Into:
EDIT:
As far as this part:
Since you wanted to know how to use the same names for parameters and class variables, as @Smutje mentioned, you should use the keyword
this
.That way, there is no ambiguity with which
score
you are using:this.score
belongs to your class calledScore
and can be used in any function inside the class. It is visible to everything inside the class.score
is a function parameter local to a functionsetScore()
and can only be used insidesetScore()
. It is only visible inside the function.Therefore, including everything mentioned, you should make the following changes:
Change:
To:
Check again how you set your score:
What is
sco
? What you want to assign to what?I guess you mixed up the variable names:
because
score
is the field which you want to populate andsco
is the parameter which you want to populatescore
with. The code above does not change any contents ofScore
, so try to swap it toit also would have helped if you have started using
this
to mark instance fields explicitly.