Results 1 to 8 of 8
- 08-20-2010, 01:19 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 3
- Rep Power
- 0
incrementing elements in an array
Hello,
I am doing an exercise in which I am adding code to a salary survey. I need to define an if...else statement to increment the proper element of resultArray. The instructions go on to say to use the value stored in index as the location in resultArray to increment. Here is what I have so far the part in question is about 3/4 way down in green, any input would be greatly appreciated.
Java Code:import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.text.DecimalFormat; public class SalarySurvey extends JFrame { // JLabel and JTextField for entering total sales private JLabel enterSalesJLabel; private JTextField enterSalesJTextField; // JButton to calculate the total salary private JButton calculateJButton; // JLabel and JTextField for displaying the total salary private JLabel salaryJLabel; private JTextField salaryJTextField; // JButton for displaying the totals private JButton totalsJButton; // JLabel and JTextArea for displaying the survey results private JLabel resultJLabel; private JTextArea resultJTextArea; DecimalFormat dollars = new DecimalFormat( "$0.00" ); { // array to store number of employees int[] resultArray = {2,3,4,5,6,7,8,9,10}; resultArray = new int [ 11 ]; resultArray[2] = 200-299; resultArray[3] = 300-399; resultArray[4] = 400-499; resultArray[5] = 500-599; resultArray[6] = 600-699; resultArray[7] = 700-799; resultArray[8] = 800-899; resultArray[9] = 900-999; resultArray[10] = 1000; } // no-argument constructor public SalarySurvey() { createUserInterface(); } // create and position GUI components; register event handlers private void createUserInterface() { // get content pane for attaching GUI components Container contentPane = getContentPane(); // enable explicit positioning of GUI components contentPane.setLayout( null ); // set up enterSalesJLabel enterSalesJLabel = new JLabel(); enterSalesJLabel.setBounds( 20, 20, 80, 20 ); enterSalesJLabel.setText( "Enter sales:" ); contentPane.add( enterSalesJLabel ); // set up salesJTextField enterSalesJTextField = new JTextField(); enterSalesJTextField.setBounds( 120, 20, 70, 20 ); enterSalesJTextField.setHorizontalAlignment( JTextField.RIGHT ); contentPane.add( enterSalesJTextField ); // set up calculateJButton calculateJButton = new JButton(); calculateJButton.setBounds( 55, 60, 110, 20 ); calculateJButton.setText( "Calculate" ); contentPane.add( calculateJButton ); calculateJButton.addActionListener( new ActionListener() // anonymous inner class { // event handler called when calculateJButton is pressed public void actionPerformed( ActionEvent event ) { calculateJButtonActionPerformed( event ); } } // end anonymous inner class ); // end call to addActionListener // set up salaryJLabel salaryJLabel = new JLabel(); salaryJLabel.setBounds( 20, 100, 80, 20 ); salaryJLabel.setText( "Total salary:" ); contentPane.add( salaryJLabel ); // set up salaryJTextField salaryJTextField = new JTextField(); salaryJTextField.setBounds( 120, 100, 70, 20 ); salaryJTextField.setHorizontalAlignment( JTextField.CENTER ); salaryJTextField.setEditable( false ); contentPane.add( salaryJTextField ); // set up totalsJButton totalsJButton = new JButton(); totalsJButton.setBounds( 55, 140, 110, 20 ); totalsJButton.setText( "Show Totals" ); contentPane.add( totalsJButton ); totalsJButton.addActionListener( new ActionListener() // anonymous inner class { // event handler called when totalsJButton is pressed public void actionPerformed( ActionEvent event ) { totalsJButtonActionPerformed( event ); } } // end anonymous inner class ); // end call to addActionListener // set up resultJLabel resultJLabel = new JLabel(); resultJLabel.setBounds( 20, 180, 100, 20 ); resultJLabel.setText( "Survey results:" ); contentPane.add( resultJLabel ); // set up resultJTextArea resultJTextArea = new JTextArea(); resultJTextArea.setBounds( 20, 210, 170, 180 ); contentPane.add( resultJTextArea ); // set properties of application's window setTitle( "Salary Survey" ); // set title bar string setSize( 220, 450 ); // set window size setVisible( true ); // display window } // end method createUserInterface // calculate salary; increment counters private void calculateJButtonActionPerformed( ActionEvent event ) { int sales = Integer.parseInt( enterSalesJTextField.getText() ); double salary = 200 + .09 * sales; int index = ( int ) salary/100; // increments resultArray [COLOR="Lime"][B]This IS where I need help[/B][/COLOR] salaryJTextField.setText( dollars.format( salary ) ); } // end method calculateJButtonActionPerformed // display salary ranges and totals private void totalsJButtonActionPerformed( ActionEvent event ) { int lowerBound; int upperBound; resultJTextArea.setText( "Salary Range:\tTotal:\n" ); } // end method totalsJButtonActionPerformed // main method public static void main( String[] args ) { SalarySurvey application = new SalarySurvey(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main } // end class SalarySurveyLast edited by Fubarable; 08-20-2010 at 01:30 AM. Reason: Moderator Edit: Code tags added
-
Hello, and welcome to the forum. I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.
To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Best of luck, and again, welcome!Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
-
You also might want to post your actual instructions because at least for me, it's not clear what you're trying to do.
- 08-20-2010, 01:43 AM #4
Are you trying to cycle through the Array to look at the values?
or
Are you trying to add one more place holder to the array? (Dynamic Array)
or
neither:rolleyes: ~ Sno ~ :rolleyes:
'-~ B.S. Computer Science ~-'
- 08-20-2010, 01:56 AM #5
Member
- Join Date
- Aug 2010
- Posts
- 3
- Rep Power
- 0
Will do Fubarable and here are the instructions as well:
d) Storing the application's results. The line that reads double salary = 200 + .09 * sales; calculates the user's salary based on the sales input and stores this value in variable salary. You now need to increment the element resultArray that represents the proper salary range. Recall that you need to divide the user's salary by 100 ( i did this already).
Define an if...else statement to increment the proper element of resultArray. Use the value stored in index as the location in resultArray to increment. Keep in mind that for salaries in the final range, the value of the index may be more than 10.
I hope this clears it up, as I am a nubb so it's all new to me. Thanks for the rapid responses as well!
-
OK, I think I know what you're trying to do. First of all, the salaryRange array should hold nothing but 0's in it to start with, since no salary's have been added yet when the program begins. You're a little confused here with this code:
Java Code:// array to store number of employees int[] resultArray = {2, 3, 4, 5, 6, 7, 8, 9, 10}; // 1 resultArray = new int[11]; // 2 resultArray[2] = 200 - 299; // 3 resultArray[3] = 300 - 399; resultArray[4] = 400 - 499; resultArray[5] = 500 - 599; resultArray[6] = 600 - 699; resultArray[7] = 700 - 799; resultArray[8] = 800 - 899; resultArray[9] = 900 - 999; resultArray[10] = 1000;
As on line // 1 you declare and initialize the array filling it with numbers 2 through 10, when these will be your indices, not the array contents; again the contents should all be 0. Then on line // 2 you initialize the array (again!) filling it with default values (for an int array, the default value is 0 -- so this is OK), and then on line //3 and below, you fill the array elements with -99. You do realize of course that this:
fills resultArray[2] with the value 200 - 299 which is -99. And again all the array spots from index 2 through 10 will be filled with -99, which is not what you want to do. I understand that 200-299 represents the salary range that represents the index 2 element of the array, but this range should be used in your if blocks later on in your program, not in your array initialization block. The simplest thing to do in this section above is to declare and initialize the array with default (0 values) by replacing sections // 1, // 2, and //3 with simply:Java Code:resultArray[2] = 200 - 299;
Java Code:int resultArray[] == new int[11]; // that's all you have to do here!
Then later in your program, divide the salary by 100, and simply call the increment operater ++ on the resultArray element at the salary/100 index. That's it.
Java Code:int index = salary / 100; salaryArray[index]++;
- 08-20-2010, 02:17 AM #7
Member
- Join Date
- Aug 2010
- Posts
- 3
- Rep Power
- 0
Ok, I get it now, lol, I think I was doing to much too early! Thanks a bunch, I will let you know how it goes.
-
Similar Threads
-
sum of elements in array
By myst in forum New To JavaReplies: 7Last Post: 07-17-2010, 08:36 AM -
How array elements gets default value
By Veangat in forum New To JavaReplies: 1Last Post: 03-07-2010, 01:29 PM -
comparing elements in array
By garyscott101 in forum New To JavaReplies: 14Last Post: 12-10-2008, 03:01 PM -
comparing array elements
By Jeremy720 in forum New To JavaReplies: 2Last Post: 10-13-2008, 02:33 AM -
Help with array of elements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 05:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks