Page 1 of 1

## 8 Replies - 1111 Views - Last Post: 22 November 2009 - 08:32 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=140725&amp;s=240e9ece40ca84678b1438c0efbd22ff&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mistax

Reputation: 0
• Posts: 32
• Joined: 21-November 09

Posted 21 November 2009 - 08:56 PM

This code is my skeleton for a checkresult i use to see how well i've written the skeleton or if i have forgotten anything i've learned in the lesson.
The skeleton is about 60% correct and im trying to find an equation that will read the whole list of number that int score provides. I tried using an Array but a return type couldn't be done because it is a void.
I assume that using an Array to get the Total will also work to find the average in getting the Total / Number of Score used.
```public class Student
{
private String student;

/**
* name.
*
* @param name
*/
public Student(String name)
{
student = name;
}
/**
* get name.
*
* @return name.
*/
public String getName()
{
return student;
}
/**
*
*
* @param score
*/
{
for ( int i = 0; i  < score; i++)
{
}

}
/**
* total score.
*
* @return score.
*/
public int getTotalScore()
{
}
/**
* get average.
*
* @return average.
*/
public double getAverageScore()
{
}

}

```

This post has been edited by mistax: 21 November 2009 - 09:13 PM

Is This A Good Question/Topic? 0

### #2 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

Posted 21 November 2009 - 09:16 PM

```public void addQuiz(int score) // ONE SINGLE single
{
int[] grades = new int[score]; // MANY scores with the number of slots as score???
for ( int i = 0; i  < score; i++)
{
// so you cant add things to or from it.
}

}

```

If you make the array part of the class, and not the method, then inside the method, all you would have to do is append the score to the end of the array.

### #3 mistax

Reputation: 0
• Posts: 32
• Joined: 21-November 09

Posted 21 November 2009 - 09:49 PM

```/**
* Student.
*
* @author John Nguyen.
* @version November 17, 2009
*/
public class Student
{
private String student;
private int[] grades = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100 };

/**
* name.
*
* @param name
*/
public Student(String name)
{
student = name;
}
/**
* get name.
*
* @return name.
*/
public String getName()
{
return student;
}
/**
*
*
* @param score
*/
{

}
/**
* total score.
*
* @return score.
*/
public int getTotalScore()
{
}
/**
* get average.
*
* @return average.
*/
public double getAverageScore()
{
}

}

```

i added a 0-100 for the array but im not sure of which method to use for the void, as im not able to return a value.

### #4 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

Posted 21 November 2009 - 10:33 PM

The student object is used to hold all the grades the student makes. So, nobody knows all the grades from the start right? Cause right now, your addQuiz method does absolutely nothing.

Here is a code snippet illustrating what I mean.
```/**
* Student.
*
* @author John Nguyen.
* @version November 17, 2009
*/
public class Student
{
private String student;
private int numElements = 1;

/**
* name.
*
* @param name
*/
public Student(String name)
{
student = name;
}
/**
* get name.
*
* @return name.
*/
public String getName()
{
return student;
}
/**
*
*
* @param score
*/
{

numElements++

}
/**
* total score.
* Returns the sum of all the grades
* @return score.
*/
public int getTotalScore()
{
int sum = 0;
for (int i = 0; i < numElements; i++)
return sum;
}
/**
* get average.
*
* @return average.
*/
public double getAverageScore()
{
return getTotalScore() / numElements
}

}

```

From main() you would call this:
```int main()
{
int[] student1Scores = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100 };
Student s1 = new Student("Norman");
for (int i = 0; i < student1Scores.length; i++)
System.out.println("Sum: " + s1.getSum());
System.out.println("Average: " + s1.getAverage());
}

```

### #5 mistax

Reputation: 0
• Posts: 32
• Joined: 21-November 09

Posted 22 November 2009 - 03:33 AM

```public class Student
{
private String student;
private int numElements = 0;
private int percentage = 0;

/**
* name.
*
* @param name
*/
public Student(String name)
{
student = name;
}
/**
* get name.
*
* @return name.
*/
public String getName()
{
return student;
}
/**
*
*
* @param score
*/
{
percentage = score;
numElements++;
}
/**
* total score.
*
* @return score.
*/
public int getTotalScore()
{

return percentage;

}
/**
* get average.
*
* @return average.
*/
public double getAverageScore()
{
return getTotalScore();
}

}

```

okay after a bit of going over the coding and reading a bit more i understand most of the coding but im stuck on how to read the entire line of quizes.
X Is addQuiz working correctly? {22, 42} expected:<64> but was:<43>
by default getTotalScore() will return the last value always

and in this code
```  int sum = 0;
for (int i = 0; i < numElements; i++)
return sum;

```

i assume that the sum or the Grade will start 0, and when I is less than numElements which i think should be <= 0 because the count for numElements has to start at 0; i is suppose to read as 0 + the next number in line.
but when this is done i get that the getTotalScore() method is incorrect and if it does work the return value is 0.

### #6 mistax

Reputation: 0
• Posts: 32
• Joined: 21-November 09

Posted 22 November 2009 - 07:29 AM

after a bit more writing. When i run checkresult now i receive
6 java.lang.NullPointerException

here the code for Total Score
```   public int getTotalScore()
{
for (int i = 0; i < numElements; i++)

```

### #7 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

Posted 22 November 2009 - 07:42 AM

You and I both forgot to initialize teh grades array. Among other things, there are liitle things that can be done like checking to dee if there is enough room in the array before adding a new number.

Here is a *fixed* version of the code that I gave you last night.
```/**
* Student.
*
* @author John Nguyen.
* @version November 17, 2009
*/
public class Student
{
private String student;
private int[] grades = new int[100];
private int numElements = 0;

/**
* name.
*
* @param name
*/
public Student(String name)
{
student = name;
}
/**
* get name.
*
* @return name.
*/
public String getName()
{
return student;
}
/**
*
*
* @param score
*/
{
if (numElements < grades.length) { // Insures there will only be 100 scores..
numElements++;
}

}
/**
* total score.
* Returns the sum of all the grades
* @return score.
*/
public int getTotalScore()
{
int sum = 0;
for (int i = 0; i < numElements; i++)
return sum;
}
/**
* get average.
*
* @return average.
*/
public double getAverageScore()
{
return getTotalScore() / numElements;
}

}

```

And from teh main()
```public static void main(String[] args) {
int[] student1Scores = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102 };
Student s1 = new Student("Norman");
for (int i = 0; i < student1Scores.length; i++)
System.out.println("Sum: " + s1.getTotalScore());
System.out.println("Average: " + s1.getAverageScore());

}

```

Once again, sorry for screwing up, I was away fro my compiler last night.

This post has been edited by Dogstopper: 22 November 2009 - 07:53 AM

### #8 mistax

Reputation: 0
• Posts: 32
• Joined: 21-November 09

Posted 22 November 2009 - 08:09 AM

ah it was no problem dogstopper, I was able to look over the code and pick up some new stuff i missed from earlier chapter and it help the material stick in my head better so overall it wasn't a problem.

There is a problem with my double average setup since the answer keeps being rounding because i guess the numEle and TotalScore are both int. Anyway to get an exact value?

### #9 mistax

Reputation: 0
• Posts: 32
• Joined: 21-November 09