ISQS 2341 Lab 8

ISQS 2341 Business Computer Programming

Lab 8 Programming:

This program is designed to provide a form to survey computer usage. After completing the form, the user should press the Submit button. A dialog box should show the user what was input. Create a Reset button so that the user can clear the form. The survey form may look similar to following:

Directions:

1. Run JBuilder and set-up your project as Lab8XXXX (Last your 4-digits of your student ID) 2. You need to create two classes; MyApp and Survey classes.

3. In the MyApp class, define the main( ) method 4. In the main( ) method, you need to first create an instance of the Survey class as follows;

1 ISQS 2341 Lab 8

Survey App = new Survey( );

5. In the Survey class, you need to have the following class fields:

 Text fields for first name, last name, and e-mail  Label fields for first name, last name, and e-mail  A combo box with a list of computer stores as follows public String choices[] = {"CompUSA", "Best Buy", "Office Depot", "online", "other"};  Radio buttons to select what is important in a computer purchase  Buttons to submit or to reset

public String choices[] = {"CompUSA", "Best Buy", "Office Depot", "online", "other"}; JLabel String1, FName, LName, EMail, String2, empty, String3, String4; JTextField jtfFName, jtfLName, jtfEMail; JComboBox jcbStores; JRadioButton jrbBrand, jrbPrice, jrbSupport; JButton jbnSubmit, jbnReset;

6. Create the interface( ) method, which is called in the default constructor. The interface( ) method is void method.

public Survey() { interface();}

Public void interface() { }

7. The container for the survey form uses the GridLayout Manager class with seven rows and one column. In addition, the form has four different sub containers using JPanel. Each panel used an appropriate layout manager class.

JPanel p1 = new JPanel(); p1.setLayout(new GridLayout (3,2)); FName = new JLabel("First Name: "); LName = new JLabel("Last Name: "); EMail = new JLabel("E-Mail: "); jtfFName = new JTextField (); jtfLName = new JTextField (); jtfEMail = new JTextField (); p1.add(FName); p1.add(jtfFName); p1.add(LName); p1.add(jtfLName); p1.add(EMail); p1.add(jtfEMail); empty = new JLabel();

2 ISQS 2341 Lab 8

c.add(p1);

8. For a JComboBox, you can add String choices[] as a list of the combo box.

String2 = new JLabel("Where do yo unormally purchase your computer?"); c.add(String2);

JPanel p2= new JPanel(); jcbStores = new JComboBox (choices); jcbStores.setMaximumRowCount(3); p2.add(jcbStores); c.add(p2);

9. The radio buttons for identifying important factor in buying computer need to be grouped. In other words, the system allows users to select only one item at a time.

10.For two buttons, those buttons need to be registered.

11.In survey class, create inner class named ButtonHandler

JPanel p5 = new JPanel(); p5.setLayout(new GridLayout (1,2)); jbnSubmit = new JButton ("Submit"); jbnReset = new JButton("Reset"); ButtonHandler h2 = new ButtonHandler(); jbnSubmit.addActionListener(h2); jbnReset.addActionListener(h2); p5.add(jbnSubmit); p5.add(jbnReset); c.add(p5);

12.In the handler class, you need to get users’ first, last, and e-mail address from the text fields. In addition, you need to figure out where the user purchased computer and information about the important factors for purchasing computers from the components when the user clicks “submit” button. The output needs to be displayed on showMessage dialog box.

3 ISQS 2341 Lab 8

if (e.getSource()==jbnSubmit) { String output; int select; select = jcbStores.getSelectedIndex();

output = "Thank you for participating in our survey. You entered\n" + jtfFName.getText() + "\n" + jtfLName.getText() +"\n" + jtfEMail.getText() +

4 ISQS 2341 Lab 8

"\nLocation where you purchase your computer/accessories:" + choices[select];

//identify user's selection from JRadioButton output += ("\nYour most important purchase decision is: "); if (jrbBrand.isSelected() == true) output += "brand"; else if (jrbPrice.isSelected() == true) output += "price"; else if (jrbSupport.isSelected() == true) output += "support";

JOptionPane.showMessageDialog(null, output);

}

13.When a user clicks on “Reset” button, the fields need to be cleared.

if (e.getSource()==jbnReset) { jtfFName.setText (" "); jtfLName.setText (" "); jtfEMail.setText (" "); jrbBrand.setSelected(false); jrbPrice.setSelected(false); jrbSupport.setSelected(false);

}

5