This shows you the differences between the selected revision and the current version of the page.
| courses:cs102-200903:labs:lab7 2009/11/06 12:07 | courses:cs102-200903:labs:lab7 2009/11/06 12:33 current | ||
|---|---|---|---|
| Line 58: | Line 58: | ||
| - Now that you have the input number stored in the local variable ''number'', output the result to the user using the JOptionPane class as follows: \\ ''JOptionPane.showMessageDialog(null,"The number you typed is " + number);'' \\ \\ The first argument to the ''showMessageDialog'' method is ''null'' because the method gives you the option of placing the dialog box inside some other window, which would be given in place of null. For now, null as the first argument to ''showMessageDialog'' is fine. \\ \\ Notice when you type "''JOptionPane.''" a window pops up with all the possible methods you can invoke on a JOptionPane. So instead of typing ''showMessageDialog'', you can simply arrow down and choose the proper method from the given list. Yes, NetBeans does make your life easier! \\ \\ | - Now that you have the input number stored in the local variable ''number'', output the result to the user using the JOptionPane class as follows: \\ ''JOptionPane.showMessageDialog(null,"The number you typed is " + number);'' \\ \\ The first argument to the ''showMessageDialog'' method is ''null'' because the method gives you the option of placing the dialog box inside some other window, which would be given in place of null. For now, null as the first argument to ''showMessageDialog'' is fine. \\ \\ Notice when you type "''JOptionPane.''" a window pops up with all the possible methods you can invoke on a JOptionPane. So instead of typing ''showMessageDialog'', you can simply arrow down and choose the proper method from the given list. Yes, NetBeans does make your life easier! \\ \\ | ||
| - When all syntax errors are fixed, run your program. Pretty simple, but I hope you can see the possibilities. \\ \\ | - When all syntax errors are fixed, run your program. Pretty simple, but I hope you can see the possibilities. \\ \\ | ||
| + | |||
| Line 81: | Line 82: | ||
| In this part, you will write a program that reads the number of numbers to be entered from the command line and then reads in that number of integers from the user in a loop, finding the maximum of those integers as they are read, and outputs the maximum value at the end. | In this part, you will write a program that reads the number of numbers to be entered from the command line and then reads in that number of integers from the user in a loop, finding the maximum of those integers as they are read, and outputs the maximum value at the end. | ||
| - | - Create a new project called ''FindMaxProject'' with Main class ''FindMax''. In this project, you will have the user input a sequence of numbers. \\ \\ To determine how many numbers the user should enter, you will use an extra argument taken from the command line by using the syntax shown below: \\ \\ ''int total = Integer.parseInt(args[0]);'' \\ \\ The args[0] part of this statement expects to use the first argument entered on the command line after the command-line statement **java FindMax**. But, remember that NetBeans is running this command internally. To add an extra argument if you were running a java program at the command line (which you are not), you would use a command like **java FindMax 5**, where the 5 is read, as a String, into the array of Strings called ''args'' - the parameter to the main method. To accomplish the entry of a command line argument in NetBeans, right-click on the project name, choose //Properties//, highlight //Run//, type 5 in the input box to the right of the word //Arguments//, and click OK. \\ \\ | + | - Create a new project called ''FindMaxProject'' with Main class ''FindMax''. You will need to use the same import statement as you did for the program you wrote in Part II. In this program, you will have the user input a sequence of numbers. \\ \\ To determine how many numbers the user should enter, you will use an extra argument taken from the command line by using the syntax shown below: \\ \\ ''int total = Integer.parseInt(args[0]);'' \\ \\ The args[0] part of this statement expects to use the first argument entered on the command line after the command-line statement **java FindMax**. However, NetBeans is running this command internally so you never have to type **java FindMax**. To add an extra argument if you were running a java program at the command line (which you are not), you would use a command like **java FindMax 5**, where the 5 is read, as a String, into the array of Strings called ''args'' - the parameter to the main method. To accomplish the entry of a command line argument in NetBeans, right-click on the project name, choose //Properties//, highlight //Run//, type 5 in the input box to the right of the word //Arguments//, and click OK. \\ \\ |
| - Below the line where you set the value of ''total'', declare a local variable called ''max'' to hold the maximum number entered. You should initialize ''max'' to 0. \\ \\ | - Below the line where you set the value of ''total'', declare a local variable called ''max'' to hold the maximum number entered. You should initialize ''max'' to 0. \\ \\ | ||
| - Below the line where you declared ''max'', write a //for// loop that iterates from 0 to total-1 (or from 1 to total, either range will work). \\ \\ On each iteration of the loop, you should use the ''JOptionPane showInputDialog'' method to prompt for the next number, just like you did in Part II. You can simply prompt the user to enter a number, or you can get fancy and prompt the user as follows: "There are 5 more numbers to enter. Please enter a number", where the number 5 is replaced in the next iteration by 4, in the next by 3, and so on. \\ \\ Store the number just entered in a variable that is //declared inside the for loop//. \\ \\ Remember that your goal is to to make sure that, on each iteration of the for loop, that max holds the greatest number entered up to that point. During the first iteration of the loop, set ''max'' to be equal to the number just read from the keyboard. Otherwise, compare the number just entered to the current ''max'', and, if the number just entered is greater than ''max'', set ''max'' to that number and keep iterating. Otherwise, if the number just entered is not greater than ''max'', do nothing to the value of ''max'' and keep iterating. \\ \\ After 5 numbers have been entered, the loop should end and you should output ''max'' in a ''JOptionPane'' outside the body of the for loop along with some String explaining what the output represents. \\ \\ | - Below the line where you declared ''max'', write a //for// loop that iterates from 0 to total-1 (or from 1 to total, either range will work). \\ \\ On each iteration of the loop, you should use the ''JOptionPane showInputDialog'' method to prompt for the next number, just like you did in Part II. You can simply prompt the user to enter a number, or you can get fancy and prompt the user as follows: "There are 5 more numbers to enter. Please enter a number", where the number 5 is replaced in the next iteration by 4, in the next by 3, and so on. \\ \\ Store the number just entered in a variable that is //declared inside the for loop//. \\ \\ Remember that your goal is to to make sure that, on each iteration of the for loop, that max holds the greatest number entered up to that point. During the first iteration of the loop, set ''max'' to be equal to the number just read from the keyboard. Otherwise, compare the number just entered to the current ''max'', and, if the number just entered is greater than ''max'', set ''max'' to that number and keep iterating. Otherwise, if the number just entered is not greater than ''max'', do nothing to the value of ''max'' and keep iterating. \\ \\ After 5 numbers have been entered, the loop should end and you should output ''max'' in a ''JOptionPane'' outside the body of the for loop along with some String explaining what the output represents. \\ \\ | ||