The Artima Developer Community
Sponsored Link

Java Answers Forum
Java Beginner needs HELP!

2 replies on 1 page. Most recent reply: Jun 29, 2002 1:13 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Mark Griffith

Posts: 1
Nickname: rrclimber
Registered: Jun, 2002

Java Beginner needs HELP! Posted: Jun 26, 2002 10:39 AM
Reply to this message Reply
Advertisement
I'm taking a class in java and I need some help with a program. I have a two dimensional array one element holds names and the other element holds grades. I have to: write an application that calculates the average for each student, stores that average in an array called averages and prints the name of the student followed by that average. This program has to be portable so all work needs to be done by methods.
I have no clue how to do this and since I'm taking the course online, I have no help. someone help me please!

I was given the following skeleton code:

public class Test {

public void printAverages(String [ ] theNames, double [ ] theAverages) {

// use System.out.print to print one name followed by the average, one line per student.

................................

}

public double average (int [] x) {

........... ....

}

public static void main ( String [] args ) {

Test t = new Test();

int grades[ ][ ] = { { 80, 72, 93} , { 92, 76, 90} , { 40, 20, 80} } ;

String names [ ] = { ?John X?, ?Mary Y?, ?Lesley Z?};

double averages[ ] = new double [ grades.length];

// calculate the average for each student i and assign it to averages

..............................................


t.printAverages(names, averages);

} // end of main
} // end of class Test


Karl Laird

Posts: 2
Nickname: iiq374
Registered: Jun, 2002

Re: Java Beginner needs HELP! Posted: Jun 26, 2002 8:09 PM
Reply to this message Reply
Start with the smaller parts first and the rest will follow (divide and conquer).

Figure out how to calculate the average of an array (hint for loop), you can then make that one of your methods.

Then use that method to iterate over the top level array (hint another for loop) (or while loop)

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Java Beginner needs HELP! Posted: Jun 29, 2002 1:13 PM
Reply to this message Reply

public class Test {

public void printAverages(String [ ] theNames, double [ ] theAverages) {

// use System.out.print to print one name followed by the average, one line per student.

for (int i = 0; i < theNames.length; i ++){
System.out.println(theNames[i] + "\t" + String.valueOf(theAverages[i]));
}
}

public double average (int [] x) {

double sum = 0;
for (int i = 0; i < x.length; i++){
sum = sum + (double) x[i];
}
return sum/(double)x.length;

}

public static void main ( String [] args ) {

Test t = new Test();

int grades[ ][ ] = { { 80, 72, 93} , { 92, 76, 90} , { 40, 20, 80} } ;

String names [ ] = { "John X", "Mary Y", "Lesley Z"};

double averages[ ] = new double [names.length];

// calculate the average for each student i and assign it to averages

for (int i = 0;i<names.length;i++){
int[] studentGrades = new int[3];
for (int j = 0; j < 3;j++){
studentGrades[j] = grades[i][j];
}
averages[i] = t.average(studentGrades);
}

t.printAverages(names, averages);

} // end of main
} // end of class Test

Flat View: This topic has 2 replies on 1 page
Topic: WebSphere - Java Runtime? Previous Topic   Next Topic Topic: animation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use