Class 1, Part 2 | Intro to Java 30-IT-396 |
Lab Solution
/*
* Names.java
*
* Created on August 7, 2003, 8:11 PM
*/
import javax.swing.*;
import java.util.ArrayList;
/**
*
*
* @author JonesB
*/
public class PetsNamesAL {
ArrayList names;
/** Creates a new instance of Names */
public PetsNamesAL() {
}
public void run() {
promptNames();
printNames();
}
public void promptNames() {
String numPets = JOptionPane.showInputDialog("How many pets do you have?");
int intNumPets = Integer.parseInt(numPets);
names = new ArrayList();
for (int i = 0; i < intNumPets; i++) {
String inputName = JOptionPane.showInputDialog("What is the name of pet " + (i + 1) + "?");
names.add(inputName);
}
}
public void printNames(){
for ( int j = 0; j < names.size(); j++) {
System.out.println("Pet name: " + names.get(j));
}
}
public static void main(String[] args) {
PetsNamesAL n = new PetsNamesAL();
n.run();
}
Created by: Brandan
Jones January 4, 2002