Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-08-2008, 09:30 AM
Member
 
Join Date: Jan 2008
Posts: 18
jimJohnson is on a distinguished road
new to arrays
I have writing a program with a few errors...One thing I am having trouble with is figuring out arrays....also I am trying to get the min and max value figured out using the math function and for loop but havin an awful time trying to figure this out...

my code is:

Code:
import java.io.*; public class Judges { public static void main(String[] args) throws IOException { //declaring variables BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); float average; double score = 0.0; boolean done = false; //Get input from user for scores and assign them to an array for (double i = 0; i <= 8; i++) { System.out.print("Enter score # 1:"); System.out.print("Enter score # 2:"); System.out.print("Enter score # 3:"); System.out.print("Enter score # 4:"); System.out.print("Enter score # 5:"); System.out.print("Enter score # 6:"); System.out.print("Enter score # 7:"); System.out.print("Enter score # 8:"); done = false; while(!done) { try { score = Double.parseDouble(dataIn.readLine()); if ((score < 0.0) && (score > 10.0)) throw new NumberFormatException(); //reasonable check else done = true; } catch(NumberFormatException e) { System.out.print("** Invalid Entry ** \nPlease re-enter score"); } } //end while } //end for //call a method to calculate the average average = averageScores(score); //Display grades and average System.out.println(""); System.out.println("The average grade is " + average); } //end main //method used to calculate the average public static double averageScores(double[] g) { double total = 0.0; double a = 0.0; for (double i = 0; i < g; i++) { total = total + g; a = total / g; System.out.println("The maxiumum score is " + max_score); System.out.println("The minimum score is " + min_score); System.out.println("The total score is " + total); return a; } } //end averageScores() } //end class
instructions are:

Arrays and For loops

In a diving competition, each contestant’s score is calculated by dropping the lowest and highest scores and then adding the remaining scores. Write a program (Judges.java) that allows the user to enter 8 judges’ scores into an array of doubles and then outputs the points earned by the contestant.

1. you can create either a command prompt console application or a console application using dialog boxes for this program
2. accept the user input as an array of doubles… accept 8 scores
3. only accept scores between 1.0 and 10.0 (include the endpoints)
4. make sure your user is entering valid scores only… if an invalid entry is made, prompt them with an error message and allow them to re-enter that score
5. in your prompt to the user (whether it is the original prompt or the prompt after an invalid entry was made), identify the number of the score they are entering (e.g., Enter score # 5
6. calculate the minimum and maximum scores using the functions from the Math class described in Chapter 3. (HINT: you can use these functions in a for loop to compare two numbers at a time to determine which is the max (or min))
7. calculate the total (the sum of the remaining 6 scores)
8. display the min, max, and total formatting each to two decimal places


Example:
Enter score # 1: 9.2
Enter score # 2: 9.3
Enter score # 3: 9.0
Enter score # 4: 9.9
Enter score # 5: 9.5
Enter score # 6: 9.5
Enter score # 7: 9.6
Enter score # 8: 9.8

The maximum score is 9.90
The minimum score is 9.00
The total score is 56.90


I am completely stuck and any help id really really appreciate
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-08-2008, 04:45 PM
Chris.Brown.SPE's Avatar
Member
 
Join Date: Apr 2008
Location: State College, PA
Posts: 50
Chris.Brown.SPE is on a distinguished road
Send a message via AIM to Chris.Brown.SPE
Code:
import java.io.*; public class Judges{ public static void main(String[] args) throws IOException { // declaring variables BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); int arraylength = 8; double average; double score[] = new double[arraylength]; boolean done = false; // Get input from user for scores and assign them to an array for (int i = 0; i < arraylength; i++) { System.out.print("Enter score # " + (i+1) + ":"); done = false; while (!done) { try { score[i] = Double.parseDouble(dataIn.readLine()); if ((score[i] < 0.0) && (score[i] > 10.0)) throw new NumberFormatException(); // reasonable check else done = true; } catch (NumberFormatException e) { System.out.print("** Invalid Entry ** \nPlease re-enter score"); } } // end while } // end for // call a method to calculate the average average = averageScores(score); // Display grades and average System.out.println(""); System.out.println("The average grade is " + average); } // end main // method used to calculate the average public static double averageScores(double[] g) { double max_score = g[0], min_score = g[0]; double total = 0.0; double a = 0.0; for (int i = 0; i < g.length; i++) { total = total + g[i]; if (g[i] < min_score) { min_score = g[i]; } else { if (g[i] > max_score) { max_score = g[i]; } } } a = total / g.length; System.out.println("The maxiumum score is " + max_score); System.out.println("The minimum score is " + min_score); System.out.println("The total score is " + total); return a; } // end averageScores() } // end class
Things I changed and why...

1) Use integers for your for loop counters unless you specifically need a double this allows you to use the counter as a current array location.

2) The way you had it placed all of your "Enter score x" each time through the loop. Just use one and make it dynamic.

3) I added a variable "arraylength" so you can change the size of the array with one number.

4) Arrays are declared by putting "[]" after the datatype. Then reference a specific array location by using ["desired array location"] but remember that arrays count starting from 0, so the first location is 0 and the last location is your length-1.

5) Pick one, doubles or floats. If you start mixing them you will get erros like when you returned the average as a double and tried to store it as a float.

6) I do not know of a way to get the min or max value of an unsorted array, so a quick addition of an if statement in your for loop finds them both.

7) Again, some things should stay outside your loop to make sure they arent executed each iteration like the System.out stuff and the calculation of your average.

8) array.length gives you the number of elements it contains.

Check out this website for more information on arrays.
https://java.sun.com/docs/books/jls/...rrays.doc.html
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Arrays bunbun New To Java 1 04-09-2008 04:24 AM
question about arrays broganm1 New To Java 3 02-13-2008 04:29 AM
2D-Arrays kbyrne New To Java 1 02-08-2008 12:08 AM
arrays help Warren New To Java 6 11-23-2007 09:23 PM
Problems with arrays Marcus New To Java 2 07-04-2007 10:10 AM


All times are GMT +3. The time now is 08:25 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org