Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
Lab Time
-
Make a Java class with arrays and loops.
-
First, use JOptionPane.showInputDialog("prompt text"); to ask the user
how many pets he or she has.
-
Next, declare an array of that size.
-
Then, loop through that array, using JOptionPane.showInputDialog to ask
the user for his or her pets' names.
-
In other words, prompt once for each array element. Assign a name
to each individual element.
-
Finally, print them out, using a for loop.
-
Notes:
-
The array should be a String array.
-
You can do this all in one method if that makes it easier.
-
Don't forget to have a public static void main(String args[])
method that will be called from the command line and will instantiate
an
object of your class.
-
import javax.swing.*; at the very top of the program. This will
allow
you to use the JOptionPane class in your program.
-
The prompt to ask the number of pets should be outside and before
your first loop. This will tell you how many times to loop, so
you
should know this ahead of time!
-
The prompt and array assignment should look something like this:
petNames[i] = JOptionPane.showInputDialog("Enter the name for
pet
" + (i + 1) + ":");
-
This should be in the body of a for loop, and i should be the counter
variable.
-
Use another for loop to print out the names. Thus, you should
have
two for loops total.
-
Look at previous examples, or ask me for help!
Stuck? See the solution here.
Created by: Brandan
Jones January 4, 2002