wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Programming help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Programming help" Watch "Programming help" New topic
Author

Programming help

Stephen Norris
Ranch Hand

Joined: Oct 27, 2003
Posts: 44
I am trying to write a program with an array. The program is suppose to ask the user to enter test scores with a max of 50. These test scores are suppose to make up the array. The test scores have to be between 0 and 100. I have to add them up and calculate the average and sort the scores using BubbleSort. Then everything has to in to a JTextArea. I am afraid that I am in over my head and would appreciate any help that you could offer.
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Hi Stephen
Let's break this up into separate 'pieces':
- Get the scores from the user (validating as you go)
- Calculate the average
- Sort using bubble sort
- Display in the JTextArea
Have a think about how to do the first part (getting user input). What are your ideas on this so far?
Hopefully nobody will post you a complete coded solution - the purpose of this exercise is for you to learn something and improve your programming skills. But DON'T PANIC!
Help is at hand when you get stuck, but I think people want to see evidence that you are trying to get through this yourself (i.e. that you have made some attempt to do this on your own).
See what you can come up with for the first part and post here when you get stuck.
Michael


"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Stephen Norris
Ranch Hand

Joined: Oct 27, 2003
Posts: 44
import java.awt.*;
import javax.swing.*;
public class Assignment3 {
public static void main (String args[])
{
int array[];
array = new int [50];
do { String scores = JOptionPane.showInputDialog("Enter test scores.");
int s = Integer.parseInt(scores);
int counter = 0;
array[counter] = s;
counter++;
} while (array[counter] < array.length);
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Okay, one problem I can see from your code is that you will be putting each new value into the same element of the array (at index 0), because you have this code:

Inside the loop. You need to declare the 'counter' variable outside the loop and initialise it to 0, so take the first line of the segment above out of the loop (put it immediately before the loop).
This part is also a problem:

Think about the intent of your loop - I think you want to do this action enough times to fill the array, so compare the counter to the size of the array. I think what you want here is:

However, it seems like you would want to allow the user to input a maximum of 50, but a minimum of some smaller number (e.g. 1), so provide some way for the user to input something that indicates they have finished inputting scores. Or, you could ask the user how many scores they have at the start of the program and create an array of that size.
Also, remember that your instructions specify that scores be between 0 and 100 - you need to check this for each value that you collect from the dialog box.
One last thing - when you are posting code here, make sure to enclose it with tags, to make it more readable.
Hope this helps
Michael
Stephen Norris
Ranch Hand

Joined: Oct 27, 2003
Posts: 44
This is what I have so far. I do not understand the concept of making a method call that will use the array from this code and transfer it somewhere so that I can BubbleSort. HELP!!

import java.awt.*;
import javax.swing.*;
public class Assignment3 {
public static void main (String args[])
{
int array[];
array = new int [50];
int counter = 0;
int response = 1;
int average;

do { String scores = JOptionPane.showInputDialog("Enter test scores" + response + ".");
int s = Integer.parseInt(scores);
if (s >= 0 && s <= 100){
array[counter] = s;
counter++;
response++;}
else
JOptionPane.showMessageDialog(null, "Invalid test score", "Invalid Result",
JOptionPane.INFORMATION_MESSAGE );
} while (counter < array.length);
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

do { String scores = JOptionPane.showInputDialog("Enter test scores" + response + ".");

Please break this into "do {" and the rest on a new line.
Please use *code* tags.
I do not understand the concept of making a method call that will use the array from this code and transfer it somewhere so that I can BubbleSort.

Do you know how to pass a variable into a method?


Please ignore post, I have no idea what I am talking about.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Not sure what you are using for a book, here are some array examples from the Sun site: Array Examples
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Stephen
A couple more observations. Firstly, this is an improvement over what you had before, so you are making progress. However, you have a variable called 'response' - what is this for? You don't seem to be using it.
So, now you have your array of valid numbers, as passed in by the user. So. the first part is looking good. If I remember correctly, the next thing you need to do is to work out the average, yes? This is something you can do relatively easily. Use another variable of type int, declare it outside the loop and initialise it to 0. Call it total, and add each valid number the user inputs to it, so

Then, when the loop is complete and you have all your numbers, divide the total by the number of scores (hint - you will need to use a floating point number for the result, and cast the 2 ints representing total and number of scores to floats). Hey presto, you now have the average. Part 2 is done.
As far as the method call - you will need to declare it as being static (because you will be calling it from main, which is static, and static methods cannot call non-static methods). This is actually fine in this case, since a sorting method that operates on an int array is a good case for a static method anyway. So, you will need to write a method with the following signature:

Call this method from your main, with the array you populated from user input. For help on bubble sort, try here. Search on google for bubble sort if you want further explanations and code snippets.
Hopefully you now feel a lot closer to completing your task.
Michael
P.S. 's' is not a good name for an int variable - s is usually used for String arguments to methods or short-lived local variables. Perhaps give that variable a more descriptive name, e.g. 'nextScore' or something.
Stephen Norris
Ranch Hand

Joined: Oct 27, 2003
Posts: 44
Michael,
The response variable is used in the display of the scores entered. It starts the count of "Enter test score 1" and so on until the loop is done running. With the way I have the array set up, will this only allow for 50 numbers, or if they only enter 25, will the other 25 be the value of 0?
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

The loop from the code you posted will loop for the entire array. When testing this code out use a loop size of something like 5. Who wants to enter 50 scores just to see if you code works?
And yes. If you don't assign a value to an element in the array it will contain zero.
Write a little bit of code and get it working, run it. Than move on from there.
Stephen Norris
Ranch Hand

Joined: Oct 27, 2003
Posts: 44
My assignment says that I have to have a max of 50 elements in the array. I was just wondering if there was a way to set up the array so that it only contains the amount of scores that are entered, whether it be 50 or 1.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

The array is only as large as you make it. So looking at your code I see you declared it to have "50" elements. If you want an array with only 25 elements, than you make an array with those number of elements.
You can also not assign a value to every element in the array, in which case those elements will have a value of zero.
Stephen Norris
Ranch Hand

Joined: Oct 27, 2003
Posts: 44
Is there a way that I can set up the array as empty, so that whatever number of scores I enter will determine the number of elements in the array?
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Ha!
Welcome to Java. No, there isn't any way of doing that (using arrays). Unless you want to collect the scores in one large array, than make a second array the exact size and transfer the data.
Just make an array which is large enough, than have a variable which keeps track of the number of elements which are "filled".
Stephen Norris
Ranch Hand

Joined: Oct 27, 2003
Posts: 44
I hate to ask this, but could you give an example of this so that I have something to work off of? If you could, that would be very helpful.
Thanks
Stephen Norris
Ranch Hand

Joined: Oct 27, 2003
Posts: 44
Can you do a Method call in an application, or just in running an Applet?
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Can you do a Method call in an application, or just in running an Applet?

You can do that with an application.
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168

Is there a way that I can set up the array as empty, so that whatever number of scores I enter will determine the number of elements in the array?

In situations like this you would use an ArrayList, which you can think of as a kind of dynamic array. When you are done adding to it, you can then convert it to an array using the toArray method. The thing to bear in mind is that you can only add Objects to it, so you can't use it with ints (you could wrap the ints in Integer objects, but maybe its not worth the effort for your particular needs).
Have you considered asking the user to specify how many scores they have, then setting the array up with that size? If you stick with just using an array of size 50, I would make sure you initialise all the elements to -1, or something other than the default (which is 0). Either that, or create another array when you know how many scores the user did enter and map the contents of the active portion of the first array into this new one. So, you set up an array of size 50, user enters 5 scores then indicates that they are done, so you create an array of size 5 and map the first 5 elements of the original array into this one.
I am thinking ahead to the operations you will have to carry out later, which your instructions say must use an int[] (averaging & sorting).
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

I hate to ask this, but could you give an example of this so that I have something to work off of? If you could, that would be very helpful.


When you get out of the loop the variable "NumberOfScores" will contain the number of scores (the number of elements in the array which have a value).
Stephen Norris
Ranch Hand

Joined: Oct 27, 2003
Posts: 44
Thank you very much for the help.
Stephen Norris
Ranch Hand

Joined: Oct 27, 2003
Posts: 44
Can you tell me how this is going to work because it seems like to me that the array would still fill up with all 50 of the entries.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Stephen
What you could do, to incorporate most of the suggestions above, and use Williams snippet would be something like this:

The array will only contain only as many valid entrys as are entered and will break out when they enter 999. Another option, but it might be too much for this app, would be to include a cancel button on the JOptionPane and when they click that button, it means they are done.
Also, dont forget that parseInt can throw an exception, so you'll need to put it in a try-catch block to catch it in case they enter a value that can't be converted to an int.
hope that helps


Dave
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Dave is much nicer, he actually gives you code. I was just giving vague suggestions. Thanks Dave.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Yea, but it was fast, and on the fly so there is no guarantee that it'll compile
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Programming help
 
Similar Threads
Cannot throw exception
Help me with BubbleSort
array instantiation
What is he saying?
Question on Multidimensional Array