Average Test scores using arrays and methods

how do you sum arrays? And issues with the main method

Page 1 of 1

4 Replies - 11676 Views - Last Post: 18 November 2009 - 01:30 PM Rate Topic: -----

#1 aeemma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-November 09

Average Test scores using arrays and methods

Post icon  Posted 18 November 2009 - 12:40 PM

The purpose is to calculate a student's test score average in the class and sum of the total score after the scores have been adjusted. Need to create a program that reads in 12 test scores between 0 and 10 (use an array). Also need to adjust these scores by 12 values (do this by creating a method). After the test scores have been adjusted the 2 lowest scores need to be removed before calculating the student's average test score and total score (a method must be used for this part).

This is the code that I have so far but I am having issues with the main method, and how to sum the arrays..... please help thank you.

// Main Method
public static void main(String args[]){

AssignValues(); // The Assign Values method is called.
totalQuizScore =
System.out.println(totalQuizscore+" is the student's quiz score.");

}


// Method for assigning the values
public static void AssignValues(){
for(int i=0; i<number_of_quizscores; i++){
//Prompt user to enter the test scores up to 12
System.out.println("Enter a test score between 0 and 10: ");
quizscores_array[i] = myScanner.nextInt();
}
for(int j=0; j<adjustment_values; j++){
//Prompt user to enter the adjustment scores up to 12
System.out.println("Enter a test score between 0 and 10: ");
adjustmentvalues_array[j] = myScanner.nextInt();
}

}
// Method for calculating the adjusted quiz scores
public static int newAdjustedQuizScores() {

// make an new empty list
ArrayList<Integer> newAdjustedScores = new ArrayList<Integer>();

// Add the arrays of the quiz scores and adjustment values
int newAdjustedScores = quizscores_array[i]+ adjustmentvalues_array[j];

// make a new array after adjusting the scores
int set[] = new int[newAdjustedScores.size()];

for (int k = 0; k < newAdjustedScores.size(); k++) {
set[k] = newAdjustedScores.get(k);
}
return set;
}

public static int removeTwoLowest() {
int firstLow = -1;
int secondLow = -1;
int count = 0;

// check which student has the first low score
if (newAdjustedScores < firstLow) {
secondLow = firstLow;
firstLow = newAdjustedScores;

}
else if (newAdjustedScores < secondLow) {
secondLow = newAdjustedScores;
}
count++;
}

}
}

Is This A Good Question/Topic? 0
  • +

Replies To: Average Test scores using arrays and methods

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Average Test scores using arrays and methods

Posted 18 November 2009 - 12:50 PM

For future reference: :code: Thank you.

// Main Method
public static void main(String args[]){

AssignValues(); // The Assign Values method is called.
totalQuizScore =
System.out.println(totalQuizscore+" is the student's quiz score.");



You call AssignValues, which is is ok. The next line you have an invalid steament.
totalquizScore = 0;


Would be far more viable of a solution.
On the next line, you print out a variable but because none of the other methods have been called (like newAdjustedQuizScores() and removeTwoLowest() are never used), then there is no valid number for the output to output

This post has been edited by Dogstopper: 18 November 2009 - 12:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 aeemma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-November 09

Re: Average Test scores using arrays and methods

Posted 18 November 2009 - 01:07 PM

View PostDogstopper, on 18 Nov, 2009 - 11:50 AM, said:

For future reference: :code: Thank you.

// Main Method
public static void main(String args[]){

AssignValues(); // The Assign Values method is called.
totalQuizScore =
System.out.println(totalQuizscore+" is the student's quiz score.");



You call AssignValues, which is is ok. The next line you have an invalid steament.
totalquizScore = 0;


Would be far more viable of a solution.
On the next line, you print out a variable but because none of the other methods have been called (like newAdjustedQuizScores() and removeTwoLowest() are never used), then there is no valid number for the output to output


 import java.util.Scanner;
	class ComputeTotalQuizScore{
   	
   	// Declaring Variables
	  static int number_of_quizscores = 12;
		static int adjustment_values = 12;
	  static int[] quizscores_array = new int[number_of_quizscores];
		static int[] adjustmentvalues_array = new int[adjustment_values];	
	  static int newAdjustedScores = 0;
	  static Scanner myScanner = new Scanner(System.in);
 		
		 // Main Method
	   public static void main(String args[]){
	  
		 AssignValues(); // The Assign Values method is called.
			newAdjustedQuizScores(); // method is called
			removeTwoLowest(); // method is called
		 totalQuizScore = 0;
		 System.out.println(totalQuizscore+" is the student's quiz score.");
		  
	  }


		 // Method for assigning the values
	   public static void AssignValues(){ 
		 for(int i=0; i<number_of_quizscores; i++){
		 	//Prompt user to enter the test scores up to 12
			System.out.println("Enter a test score between 0 and 10: ");
			quizscores_array[i] = myScanner.nextInt();
		 }
			for(int j=0; j<adjustment_values; j++){
		 	//Prompt user to enter the adjustment scores up to 12
			System.out.println("Enter a test score between 0 and 10: ");
			adjustmentvalues_array[j] = myScanner.nextInt();
			}
					
		}	
			// Method for calculating the adjusted quiz scores		
		  public static int newAdjustedQuizScores() {
	  
	 			 // make an new empty list
		 	ArrayList<Integer> newAdjustedScores = new ArrayList<Integer>(); 
	  
	 		 // Add the arrays of the quiz scores and adjustment values
		 	int newAdjustedScores = quizscores_array[i]+ adjustmentvalues_array[j]; 
	  
		  // make a new array after adjusting the scores
		 int scores[] = new int[newAdjustedScores.size()]; 
			
			for (int k = 0; k < newAdjustedScores.size(); k++) {
			sscores[k] = newAdjustedScores.get(k);
		 }
		 return scores;
	  }

 		public static int removeTwoLowest() {
			int firstLow = -1;
			int secondLow = -1;
		 int count = 0;

		// check which student has the first low score
		if (newAdjustedScores < firstLow) {
			secondLow = firstLow;
			firstLow = newAdjustedScores;
			
			}
			else if (newAdjustedScores < secondLow) {
				secondLow = newAdjustedScores;
				}
			count++;
			}

		}






View Postaeemma, on 18 Nov, 2009 - 12:04 PM, said:

View PostDogstopper, on 18 Nov, 2009 - 11:50 AM, said:

For future reference: :code: Thank you.

// Main Method
public static void main(String args[]){

AssignValues(); // The Assign Values method is called.
totalQuizScore =
System.out.println(totalQuizscore+" is the student's quiz score.");



You call AssignValues, which is is ok. The next line you have an invalid steament.
totalquizScore = 0;


Would be far more viable of a solution.
On the next line, you print out a variable but because none of the other methods have been called (like newAdjustedQuizScores() and removeTwoLowest() are never used), then there is no valid number for the output to output


 import java.util.Scanner;
	class ComputeTotalQuizScore{
   	
   	// Declaring Variables
	  static int number_of_quizscores = 12;
		static int adjustment_values = 12;
	  static int[] quizscores_array = new int[number_of_quizscores];
		static int[] adjustmentvalues_array = new int[adjustment_values];	
	  static int newAdjustedScores = 0;
	  static Scanner myScanner = new Scanner(System.in);
 		
		 // Main Method
	   public static void main(String args[]){
	  
		 AssignValues(); // The Assign Values method is called.
			newAdjustedQuizScores(); // method is called
			removeTwoLowest(); // method is called
		 totalQuizScore = 0;
		 System.out.println(totalQuizscore+" is the student's quiz score.");
		  
	  }


		 // Method for assigning the values
	   public static void AssignValues(){ 
		 for(int i=0; i<number_of_quizscores; i++){
		 	//Prompt user to enter the test scores up to 12
			System.out.println("Enter a test score between 0 and 10: ");
			quizscores_array[i] = myScanner.nextInt();
		 }
			for(int j=0; j<adjustment_values; j++){
		 	//Prompt user to enter the adjustment scores up to 12
			System.out.println("Enter a test score between 0 and 10: ");
			adjustmentvalues_array[j] = myScanner.nextInt();
			}
					
		}	
			// Method for calculating the adjusted quiz scores		
		  public static int newAdjustedQuizScores() {
	  
	 			 // make an new empty list
		 	ArrayList<Integer> newAdjustedScores = new ArrayList<Integer>(); 
	  
	 		 // Add the arrays of the quiz scores and adjustment values
		 	int newAdjustedScores = quizscores_array[i]+ adjustmentvalues_array[j]; 
	  
		  // make a new array after adjusting the scores
		 int scores[] = new int[newAdjustedScores.size()]; 
			
			for (int k = 0; k < newAdjustedScores.size(); k++) {
			sscores[k] = newAdjustedScores.get(k);
		 }
		 return scores;
	  }

 		public static int removeTwoLowest() {
			int firstLow = -1;
			int secondLow = -1;
		 int count = 0;

		// check which student has the first low score
		if (newAdjustedScores < firstLow) {
			secondLow = firstLow;
			firstLow = newAdjustedScores;
			
			}
			else if (newAdjustedScores < secondLow) {
				secondLow = newAdjustedScores;
				}
			count++;
			}

		}



I made the suggestions you made but I am still getting errors and don't see where the problem is. Please point out where if you can. Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Average Test scores using arrays and methods

Posted 18 November 2009 - 01:16 PM

Look at the compiler errors.
#1 error:
The compiler tells you that totalQuizScore has not been defined. It needs to defined as an int. Although with your code, I don't see the purpose of it, as it will always print 0.

#2 error:
ArrayList is undefined...add this to the beginning of your program.
import java.util.ArrayList;
 


#3 error:
This line that you wrote:
int newAdjustedScores = quizscores_array[i]+ adjustmentvalues_array[j];



The i and j that you are attempting to reference are no longer there, after the end of the AssignValues() method, they were destroyed.

# 4:
sscores[k] = newAdjustedScores.get(k);


||
V
scores[k] = newAdjustedScores.get(k);



# 5:
public static int newAdjustedQuizScores() returns an int, you are attempting to return an array of ints.

# 6:
public static int removeTwoLowest() { : you have no return statement. Either make it return something or declare it void, not int.

that is just the extent of compiler errors. Fix those and then truly think about the way to make it work.
Was This Post Helpful? 0
  • +
  • -

#5 aeemma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-November 09

Re: Average Test scores using arrays and methods

Posted 18 November 2009 - 01:30 PM

View PostDogstopper, on 18 Nov, 2009 - 12:16 PM, said:

Look at the compiler errors.
#1 error:
The compiler tells you that totalQuizScore has not been defined. It needs to defined as an int. Although with your code, I don't see the purpose of it, as it will always print 0.

#2 error:
ArrayList is undefined...add this to the beginning of your program.
import java.util.ArrayList;
 


#3 error:
This line that you wrote:
int newAdjustedScores = quizscores_array[i]+ adjustmentvalues_array[j];



The i and j that you are attempting to reference are no longer there, after the end of the AssignValues() method, they were destroyed.

# 4:
sscores[k] = newAdjustedScores.get(k);


||
V
scores[k] = newAdjustedScores.get(k);



# 5:
public static int newAdjustedQuizScores() returns an int, you are attempting to return an array of ints.

# 6:
public static int removeTwoLowest() { : you have no return statement. Either make it return something or declare it void, not int.

that is just the extent of compiler errors. Fix those and then truly think about the way to make it work.

Ok I will keep trying to figure it out thank you for your input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1