TechArena Community How to calculate substitution

#1
25-04-2009
 Member Join Date: Nov 2008 Posts: 1,221
How to calculate substitution

I created a class student, with 2 results for their exams. The first is Terminal marks and second is Exam marks. I created a method called average.

Here is the code for this method:

Code:
```/ **
* Calculates average of a list of integers
* /
public double average (List <integer> list)
{
double sum = 0.0;
for (int i = 0; i <list.size () i = i +1)
{
sum = sum + list.get (i);
}
return sum/4;
}```
This method works great, when I type in display pane:

Integer[] numberArray = {75, 59, 0, 70};
Student.average (Arrays.asList (numberArray));

51 is the result which is correct.

My problem is to create the following method. I have to calculate the average of Terminal over Exam marks divided by 2. For example 51 + 64 / 2, which gives me the result of substitution of 57%. Here is my code:

Code:
```/ **
* Calculates and sets the substitutionScore of the receiver
* /
public void calcSubstitutionScore ()
{
for (int aScore:scores)
{
this. results. put (aScore, new HashSet<String> ( ) ) ;
}
for (String average: students. keySet ())
{
for (String graded: this. results. get (average))
{
}
}
}```
#2
25-04-2009
 Member Join Date: Feb 2008 Posts: 194
Re: How to calculate substitution

Why does not it function? What is the error message?
#3
25-04-2009
 Member Join Date: Nov 2008 Posts: 1,221
Re: How to calculate substitution

It gives me a lot of syntax errors. For example for the line:

It makes me: not a statement.

For the line:

for (int aScore : scores)

It makes me: cannot find symbol - variable score

I have in a class called Student: private int substitutionScore; Having 2 methods:

Code:
```/ **
* Sets the substitutionScore of the receiver to aScore
* /
private void setSubstitutionScore (int aScore)
{
this. substitutionScore = aScore;
}

/ **
* Returns the substitutionScore of the receiver
* /
public int getSubstitutionScore ()
{
return this. substitutionScore;
}```
#4
25-04-2009
 Member Join Date: Feb 2008 Posts: 194
Re: How to calculate substitution

In Java the use of the terms subject to different rules of visibility. It is not enough that you sent in your head, and so the Java will work.

As a very simple example:

Code:
```{// Block
{// Another block
int quantity; // I declare my variable here

quantity = 45 // good: the variable quantity is declared in the same block
Quantity = 45 // syntax error here: quantity is not EQUAL to Quantity in Java
}
quantity = 45 // syntax error here: quantity is not declared in this block.
}```

 Tags: