Arraylist of averages of two arrays list
I have written the code below the third array is suppose to store the averages of test scores from the first two arrays that were entered, fourth array stores letter grade. My question is do I need to manually write out each value one by one if so how do I do that and if I don't what do I need to do. This is a project and I don't expect the answer but help in the right direction is very much appreciated.
Code jave:
import java.util.*;
public class TestArray
{
public static void main (String[] args)
{
int[] testOne = new int[4];
int[] testTwo = new int[4];
double[] average = new double[4];
char[] grade = {'A', 'B', 'C', 'F'}; // final letter grade
Scanner scan = new Scanner(System.in);
// Input scores for both test and all four students.
System.out.println("For test 1,");
for ( int i = 0; i < testOne.length; i++)
{
System.out.print("Enter score" + (i + 1) + ":");
testOne[i] = scan.nextInt();
}
System.out.println("For test 2,");
for ( int i = 0; i < testTwo.length; i++)
{
System.out.print("Enter score" + (i + 1) + ":");
testTwo[i] = scan.nextInt();
}
System.out.println("");
System.out.println("Test 1");
for(int i = 0; i < testOne.length; i++)
{
System.out.println(testOne[i]);
}
System.out.println("");
System.out.println("Test 2");
for(int i = 0; i < testTwo.length; i++)
{
System.out.println(testOne[i]);
}
}
}
Re: Arraylist of averages of two arrays list
Quote:
My question is do I need to manually write out each value one by one if so how do I do that and if I don't what do I need to do.
I guess you're asking about how to figure out the averages. No, you don't need to do this manually. If you did, it would look like:
Code :
average[0] = (testOne[0] + testTwo[0]) / 2;
average[1] = (testOne[1] + testTwo[1]) / 2;
average[2] = (testOne[2] + testTwo[2]) / 2;
average[3] = (testOne[3] + testTwo[3]) / 2;
But instead of all that use a for loop just as you do when you print the test scores. Instead of 0, 1, 2, 3 use the loop counter variable.
Re: Arraylist of averages of two arrays list
So in the loop would be written something like this:
Code java:
for(int i = 0; i < average.length; i++)
{
average[i] = (testOne[i] + testTwo[i]) / 2;
System.out.println(average[i]);
}
I am also trying to figure out the letter grade portion of the program should I create a new method with a if else to change each average to a letter grade or is there a way to do that with a loop? Below is what i've started but i realize one array is int and the other is string this is why i am stuck.
Code java:
if(average[i] >= 90)
average[i] = grade[0];
else if (average >= 80)
average[i] = grade[1];
else if (average >= 70)
average[i] = grade[2];
else if (average < 70)
average[i] = grade[3];
return grade;
also how would I line up the results to look like this:
Test 1 Test 2 Average Grade
------ ------ ------- -----
090 059 074.5 C
082 080 081.0 B
039 100 069.5 F
089 092 090.5 A
Re: Arraylist of averages of two arrays list
The loop looks good.
Quote:
I am also trying to figure out the letter grade portion of the program should I create a new method with a if else to change each average to a letter grade or is there a way to do that with a loop?
You can figure out the grade char as soon as you know the average. ie inside the loop you have just written.
A chain of if-else if-... is OK. But the logic of what you have written is a little weird. I *assume* the values of the grade array should be the grade chars assigned to each of the tests. In that case I don't really see where that initialisation way ack at the start came from:
Code :
char[] grade = {'A', 'B', 'C', 'F'}; // final letter grade
And you would say something like:
Code :
if(whatever) {
grade[i] = 'A';
} else if(somethingElse) {
grade[i] = 'B';
} // and so on
-----
System.out.printf() works well when trying to format numbers. Look up the API docs for that method and follow the links to find out about the format stringss. (There is also the NumberFormat class)
Re: Arraylist of averages of two arrays list
I see now setting the grades in the array before knowing the averages makes no sense so array should be empty, but should it be like this:
Code java:
for(int i = 0; i < average.length; i++)
{
average[i] = (testOne[i] + testTwo[i]) / 2;
for(int i = 0; i < grade.length; i++)
{
if (average[i] >= 90)
{
grade[i] = 'A';
}
else (average[i] >= 80)
{
grade[i] = 'B';
}
else (average[i] >= 70)
{
grade[i] = 'C';
}
else (average[i] < 70)
{
grade[i] = 'F';
}
}
}
System.out.println("Grades");
System.out.println(grade[i]);
here is the whole code re-written can you please look it over I am still looking at the API docs on format strings.
Code java:
import java.util.*;
public class TestArray
{
public static void main (String[] args)
{
int[] testOne = new int[4];
int[] testTwo = new int[4];
double[] average = new double[4];
char[] grade = new char[4]; // final letter grade
Scanner scan = new Scanner(System.in);
// Input scores for both test and all four students.
System.out.println("For test 1,");
for ( int i = 0; i < testOne.length; i++)
{
System.out.print("Enter score" + (i + 1) + ":");
testOne[i] = scan.nextInt();
}
System.out.println("For test 2,");
for ( int i = 0; i < testTwo.length; i++)
{
System.out.print("Enter score" + (i + 1) + ":");
testTwo[i] = scan.nextInt();
}
System.out.println("");
System.out.println("Test 1");
for(int i = 0; i < testOne.length; i++)
{
System.out.println(testOne[i]);
}
System.out.println("");
System.out.println("Test 2");
for(int i = 0; i < testTwo.length; i++)
{
System.out.println(testOne[i]);
}
System.out.println("Average");
for(int i = 0; i < average.length; i++)
{
average[i] = (testOne[i] + testTwo[i]) / 2;
for(int i = 0; i < grade.length; i++)
{
if (average[i] >= 90)
{
grade[i] = 'A';
}
else (average[i] >= 80)
{
grade[i] = 'B';
}
else (average[i] >= 70)
{
grade[i] <70)
{
grade[i] = 'F';
}
}
}
System.out.println("Grades");
System.out.println(grade[i]);
}
}
Re: Arraylist of averages of two arrays list
Quote:
Originally Posted by
Hazmat210
I see now setting the grades in the array before knowing the averages makes no sense so array should be empty, but should it be like this:
Code java:
for(int i = 0; i < average.length; i++)
{
average[i] = (testOne[i] + testTwo[i]) / 2;
for(int i = 0; i < grade.length; i++)
{
if (average[i] >= 90)
{
grade[i] = 'A';
}
else (average[i] >= 80)
{
grade[i] = 'B';
}
else (average[i] >= 70)
{
grade[i] = 'C';
}
else (average[i] < 70)
{
grade[i] = 'F';
}
}
}
System.out.println("Grades");
System.out.println(grade[i]);
Hello Hazmat210!
Some notifications. I think you don't need the inner for loop - you will be ok with one. You should use an if... else if... structure as pbrockway2 mentioned in his last post. It would also better to use intervals in your statements. For example:
Code java:
if (average[i] >= 80 && average[i] < 90)
Hope it helps.
Re: Arraylist of averages of two arrays list
So this below would be enough ? I just thought that a new loop would be necessary in order to set all four values in the grade array. Does this loop set all four values for grade array or is it replacing the value of grade[0] everytime is executes?
Code java:
System.out.println("Average");
for(int i = 0; i < average.length; i++)
{
average[i] = (testOne[i] + testTwo[i]) / 2;
if (average[i] >= 90 && average[i] <=100)
{
grade[i] = 'A';
}
else (average[i] >= 89 && average[i] <=80)
{
grade[i] = 'B';
}
else (average[i] >= 79 && average[i] <=70)
{
grade[i] = 'C';
}
else (average[i] < 70)
{
grade[i] = 'F';
}
}
}
System.out.println("Grades");
System.out.println(grade[i]);
}
Re: Arraylist of averages of two arrays list
Quote:
Originally Posted by
Hazmat210
So this below would be enough ? I just thought that a new loop would be necessary in order to set all four values in the grade array. Does this loop set all four values for grade array or is it replacing the value of grade[0] everytime is executes?
Code java:
System.out.println("Average");
for(int i = 0; i < average.length; i++)
{
average[i] = (testOne[i] + testTwo[i]) / 2;
if (average[i] >= 90 && average[i] <=100)
{
grade[i] = 'A';
}
else (average[i] >= 89 && average[i] <=80)
{
grade[i] = 'B';
}
else (average[i] >= 79 && average[i] <=70)
{
grade[i] = 'C';
}
else (average[i] < 70)
{
grade[i] = 'F';
}
}
}
System.out.println("Grades");
System.out.println(grade[i]);
}
It will set all four values of your grade array if you fix the errors. Have you try to compile it? I think it won't. You need to use an if.. else if... structure as i told you and post #4 describes.
Code java:
if (<condition 1>) {
/* Java statements (1) */
}
else if (<condition 2>) {
/* Java statements (2) */
}
else if (<condition n>) {
/* Java statments (n) */
}
[<else clause>]
Re: Arraylist of averages of two arrays list
Quote:
It will set all four values of your grade array if you fix the errors.
I agree. Don't be afraid of trying something and posting back with the new code (all of it) and the compiler messages if those messages are not clear. Someone is sure to be able to explain what they mean.
-----
printf() can be considered a second matter and you can safely leave it until you have the arrays all containing the values they should.
Re: Arraylist of averages of two arrays list
Ok so I got it all to work except for the formatting i've got some reading to do on formatting but input on how to make all nice and would be nice below is code so far everthing works as it should.
Code java:
import java.util.*;
public class TestArray
{
public static void main (String[] args)
{
int[] testOne = new int[4];
int[] testTwo = new int[4];
double[] average = new double[4];
char[] grade = new char[4];
Scanner scan = new Scanner(System.in);
// Input scores for both test and all four students.
System.out.println("For test 1,");
for ( int i = 0; i < testOne.length; i++)
{
System.out.print("Enter score " + (i + 1) + ":");
testOne[i] = scan.nextInt();
}
System.out.println("For test 2,");
for ( int i = 0; i < testTwo.length; i++)
{
System.out.print("Enter score " + (i + 1) + ":");
testTwo[i] = scan.nextInt();
}
System.out.println("");
System.out.println("Test 1");
for(int i = 0; i < testOne.length; i++)
{
System.out.println(testOne[i]);
}
System.out.println("");
System.out.println("Test 2");
for(int i = 0; i < testTwo.length; i++)
{
System.out.println(testOne[i]);
}
System.out.println("Average");
for(int i = 0; i < average.length; i++)
{
average[i] = (testOne[i] + testTwo[i]) / 2;
if (average[i] >= 90 && average[i] <= 100)
{
grade[i] = 'A';
}
else if (average[i] >= 80 && average[i] <= 89)
{
grade[i] = 'B';
}
else if (average[i] >= 70 && average[i] <= 79)
{
grade[i] = 'C';
}
else if(average[i] < 70)
{
grade[i] = 'F';
}
System.out.println(average[i]);
}
System.out.println("Grades");
for(int i = 0; i < grade.length; i++)
{
System.out.println(grade[i]);
}
}
}
Re: Arraylist of averages of two arrays list
Quote:
Originally Posted by
Hazmat210
ok tried this but i'm getting:
C:\Users\Gonzalez\Desktop\Java Chapter 5\TestArray.java:81: reached end of file while parsing
}
1 error
Tool completed with exit code 1
You have probably forgotten to close a curly brace.
Re: Arraylist of averages of two arrays list
Quote:
reached end of file while parsing
This message often indicates that you have missed a closing } brace somewhere.
Correct - make consistent - your indentation so that every } lines up with its corresponding opening { and you should be able to spot what's missing.