Help starting program, need done by Nov 4
Write a program that reads in a set of test scores, computes their average, and finds out how many scores are above the average. Use a sentinel value of -1 to indicate the end of the set of scores. Assume that the number of test scores is less or equal to 30. Your solution should include two methods in addition to the main method. One to compute the average and one to print all scores above the average. (This is an expanded version of problem 6.1 on page 222 of the book.)
Re: Help starting program, need done by Nov 4
Re: Help starting program, need done by Nov 4
:)]This is what i have im lost on how to keep track of the testscores entered. I also need help creating a method for the average and for printing those scores above average!!!!!!!!:)]
Code :
import java.util.Scanner;
public class ComputeAverage{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
Sysetm.out.println("Enter test scores, enter in -1 when you are done: ");
int testScores = input.nextInt();
int total = 0;
while (testScores != -1){
total += testScores;
System.out.println("Enter test scores, enter in -1 when you are done: ");
testScores= input.nextInt();
}
int N = 0; // number input values
double sum = 0.0; // sum of input values
while (!StdIn.isEmpty()) {
testScores = StdIn.readDouble();
N = N + 1;
sum = sum + testScores;
}
public static void getAverage(int testScores){
if (testScore <= 100){
double average = sum / N;
return;
}
}
public static void printAboveAverage(int testScores){
if (testScores >= average){
Re: Help starting program, need done by Nov 4
What i really need is to create an array to store the test scores but im very new to java and unfamiliar with arrays..
Re: Help starting program, need done by Nov 4
problem statement doesn't state that testscores are integers.
Re: Help starting program, need done by Nov 4
A simple guide to arrays:
Arrays are declared like this:
type is any type you want, and var_name is any variable name you want (just like any other variables).
To create an array of ints:
Code :
some_array = new int[length];
where length is the length of the array you want. NOTE: Once you create an array, it's length cannot be changed! If you need an array of a different size (usually larger), you must create a new array and move stuff from the old array to the new array.
How to access the elements of an array:
Array's are accessed by using the var_name and the index. The index is which element inside that array you want to get. In Java, array indices run from 0 to one less than the length.
this gives you the element stored at index in some_array.
Re: Help starting program, need done by Nov 4
here is what i have revised, im not sor wheather the for loop or the while loop is better to use
the for loop seems to save test scores to array where as while loop just has that sentinal value yo stop the loop.
Code :
import java.util.Scanner;
public class ComputeAverage{
public static void main(String [] args){
* *
* *Scanner input = new Scanner(System.in);
**// Determine number of test scores to be computed
* *System.out.println("Enter number of test scores, less than 30: ");
* *int n = input.nextInt();
* *// Create array to store test scores
* *int [] scores = new int [n]
* *// Enter test scores
* * * *System.out.println("Enter test scores, enter in -1 when you are done: ");
* * **int testScores = input.nextInt();
* *// Create sentinal value when done with inputing scores
* *int total = 0;
* *while (testScores != -1){
* * * *total += testScores;
* * * *System.out.println("Enter test scores, enter in -1 when you are done: ");
* * * *
* * * *testScores = input.nextInt();
* *}
* *for (int i = 0; i < scores.length; i++){
* * * *System.out.println("Enter a score: ");
* * * *scores[i] = input.nextInt();
* *
* * * *if (testScores == -1) * *break;
* *} * *
* *
* *// Method to compute average
**public static void getAverage(int testScores){
* * * *if (n < 30){
* * * * * *int average = scores[i]/n;
********return;
* * **
* *}
* *// Method to print scores above the Average
* *public static void printAboveAverage(int testScores){
* * * *if (testScores >= average){
* * * *return;
* *} * *
}
}
Re: Help starting program, need done by Nov 4