Help Please.

Student Class / Total Grade/Average

Page 1 of 1

8 Replies - 1094 Views - Last Post: 22 November 2009 - 08:32 AM Rate Topic: -----

#1 mistax   User is offline

  • New D.I.C Head

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

Help Please.

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;
	private int grade;

	/**
	 * name.
	 * 
	 * @param name
	 */
	public Student(String name)
	{
		student = name;
	}
	/**
	 * get name.
	 * 
	 * @return name.
	 */
	public String getName()
	{
		return student;
	}
	/**
	 * 
	 * add quiz.
	 * 
	 * @param score
	 */
	public void addQuiz(int score)
	{
		int[] grades = new int[score];
		int sumOfGrades = 0;
		for ( int i = 0; i  < score; i++)
		{
		 sumOfGrades += grades[i];   
		}
		grade = sumOfGrades;
		
		
	}
	/**
	 * total score.
	 * 
	 * @return score.
	 */
	public int getTotalScore()
	{
		return grade;
	}
	/**
	 * get average.
	 * 
	 * @return average.
	 */
	public double getAverageScore()
	{
		return grade / 4;
	}

}


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


Is This A Good Question/Topic? 0
  • +

Replies To: Help Please.

#2 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

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

Re: Help Please.

Posted 21 November 2009 - 09:16 PM

If you made a global ArrayList instead of local arrays... and your addQuiz method is all wrong. Let me demonstrate:
public void addQuiz(int score) // ONE SINGLE single
	{
		int[] grades = new int[score]; // MANY scores with the number of slots as score???
		int sumOfGrades = 0;
		for ( int i = 0; i  < score; i++)
		{
		 sumOfGrades += grades[i];  // grades at the moment is an empty array,
								   // so you cant add things to or from it.
		}
		grade = sumOfGrades;
	   
	   
	}




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.
Was This Post Helpful? 0
  • +
  • -

#3 mistax   User is offline

  • New D.I.C Head

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

Re: Help Please.

Posted 21 November 2009 - 09:49 PM

/**
 * Student.
 * 
 * @author John Nguyen.
 * @version November 17, 2009
 */
public class Student
{
	private String student;
	private int grade;
	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;
	}
	/**
	 * 
	 * add quiz.
	 * 
	 * @param score
	 */
	public void addQuiz(int score)
	{
	   int[] grades = new int[100];
	   int sumOfGrades = 0;
	   

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

}


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.
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

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

Re: Help Please.

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[] grades;
	private int numElements = 1;

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

	   grades[numElements] = score; // Add the score to the Student's grades!  SO simple?
	   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++)
			sum += grades[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++)
		s1.addQuiz(student1Scores[i]);
	System.out.println("Sum: " + s1.getSum());
	System.out.println("Average: " + s1.getAverage());
}


Was This Post Helpful? 0
  • +
  • -

#5 mistax   User is offline

  • New D.I.C Head

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

Re: Help Please.

Posted 22 November 2009 - 03:33 AM

public class Student
{
	private String student;
	private int[] grades;
	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;
	}
	/**
	 * 
	 * add quiz.
	 * 
	 * @param score
	 */
	public void addQuiz(int 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++)
			sum += grades[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.
Was This Post Helpful? 0
  • +
  • -

#6 mistax   User is offline

  • New D.I.C Head

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

Re: Help Please.

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()
	{
		int totalGrades = 0;
		for (int i = 0; i < numElements; i++)
			totalGrades += grades[0];
		return totalGrades;


Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

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

Re: Help Please.

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;
	}
	/**
	 *
	 * add quiz.
	 *
	 * @param score
	 */
	public void addQuiz(int score)
	{
		if (numElements < grades.length) { // Insures there will only be 100 scores..
			grades[numElements] = score; // Add the score to the Student's grades!  SO simple?
			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++)
			sum += grades[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++)
			s1.addQuiz(student1Scores[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

Was This Post Helpful? 0
  • +
  • -

#8 mistax   User is offline

  • New D.I.C Head

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

Re: Help Please.

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?
Was This Post Helpful? 0
  • +
  • -

#9 mistax   User is offline

  • New D.I.C Head

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

Re: Help Please.

Posted 22 November 2009 - 08:32 AM

nvm typecasting double on the result fixed it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1