CS111 Lab 3a:

Building OOP Applications

Exercise I: Build a Quiz Application


Build a QuizApp that allows the user to practice a subject by taking a quiz containing a series of basic fill-in-the blank questions as many times as they like. Each time the quiz is taken, the questions should be randomly shuffled to avoid the memorization of answers in a particular order. Once the user has read and answered a question, they receive a point if a response is correct otherwise they are given the proper answer.

Quiz applications are data driven. They rely on questions and answers to questions as primary game elements. Normally quiz data, such as the questions and answers, should be stored as external text files. In this application you will be asked to populate our quiz directly from your code file.


Goal 1: Build your app using the following Dependency and Aggregation Structure.




Goal 2: Use the following UML diagram (Unified Modeling Language) as your guide in building each class.




Question.java





QuizEngine.java





MyMain.java






Exercise 2: Build the Sales Application shown below.

  1. Construct a SalesItem class that stores a sales item being purchased. A SalesItem object can only be instantiated with explicit data values.
  2. Construct a SalesSlip class that records and computes the sales total of a list of SalesItems.
  3. The Sales Application uses a Graphical User Interface. The MyMain.java code, shown at the bottom of this page, can be used.

Classes needed: MyMain.java, SalesSlip.java, and SalesItem.java.





    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;

    public class MyMain {

      // SPECIFY THE APPLICATION ELEMENTS: UI AND OBJECTS
      private static JFrame frame;
      private static JTextField itemTextField;
      private static JTextField costTextField;
      private static JTextField quantityTextField;
      private static JButton addItemBtn;
      private static JLabel totalSaleslbl;
      private static JTextArea salesTextArea;

      // OBJECT REPRESENTING A SALES SLIP
      private static SalesSlip salesSlip;

      // ********************************************************************************
      public static void main(String[] args) {

        // TASK 1: CREATE A APPLICATION WINDOW
        createGraphicWindow();

        // TASK 2: CREATE A SALESSLIP
        salesSlip = new SalesSlip();

        // TASK 3: ADD BUTTON LISTENER EVENT FOR ADDING SALES ITEMS
        addListenerEvent();

      }

      // ********************************************************************************
      public static void addListenerEvent() {
        addItemBtn.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            updateSalesSlip(); // HANDLES THE EVENT
          }
        });
      }

      // ********************************************************************************
      // EVENT HANDLER: CREATES A SALES ITEM AND UPDATES THE SALES SLIP
      public static void updateSalesSlip() {

        // TASK 1: COLLECT INPUT INFORMATION
        String item = itemTextField.getText();
        if (costTextField.getText().length() > 0 && quantityTextField.getText().length() > 0) {
          double price = Double.parseDouble(costTextField.getText());
          int quantity = Integer.parseInt(quantityTextField.getText());

          // TASK 2: ADD INPUT INFORMATION TO THE SALESSLIP AND DISPLAY
          salesTextArea.setText(salesTextArea.getText() + salesSlip.addSalesItem(item, price, quantity));

          // TASK 3: UPDATE THE SALESSLIP WITH THE NEW TOTAL
          totalSaleslbl.setText("Sales Total: $" + String.format("%.2f", salesSlip.getTotalSales()));

          // TASK 4: CLEAR THE INPUT TEXTFIELDS
          itemTextField.setText("");
          costTextField.setText("");
          quantityTextField.setText("");
        }

      }

      // ********************************************************************************
      // CREATES THE WINDOW FOR USERS TO INTERACT WITH
      private static void createGraphicWindow() {
        frame = new JFrame();
        frame.setBounds(20, 20, 450, 700);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblTitle = new JLabel("Sales Application");
        lblTitle.setBounds(159, 6, 163, 16);
        frame.getContentPane().add(lblTitle);

        JLabel lbl1 = new JLabel("Item Name:");
        lbl1.setBounds(6, 69, 129, 16);
        frame.getContentPane().add(lbl1);
        itemTextField = new JTextField();
        itemTextField.setBounds(142, 64, 273, 26);
        frame.getContentPane().add(itemTextField);
        itemTextField.setColumns(10);

        JLabel lbl2 = new JLabel("Cost per Item : $");
        lbl2.setBounds(6, 102, 129, 16);
        frame.getContentPane().add(lbl2);
        costTextField = new JTextField();
        costTextField.setColumns(10);
        costTextField.setBounds(142, 97, 273, 26);
        frame.getContentPane().add(costTextField);

        JLabel lbl3 = new JLabel("Quantity : $");
        lbl3.setBounds(6, 133, 129, 16);
        frame.getContentPane().add(lbl3);
        quantityTextField = new JTextField();
        quantityTextField.setColumns(10);
        quantityTextField.setBounds(142, 128, 273, 26);
        frame.getContentPane().add(quantityTextField);

        addItemBtn = new JButton("Add Item to the Sales List");
        addItemBtn.setBounds(142, 164, 273, 29);
        frame.getContentPane().add(addItemBtn);

        totalSaleslbl = new JLabel("Total Sales: $0.00");
        totalSaleslbl.setBounds(6, 226, 336, 16);
        frame.getContentPane().add(totalSaleslbl);

        salesTextArea = new JTextArea("");
        salesTextArea.setBounds(35, 250, 400, 400);
        salesTextArea.setText("");
        frame.getContentPane().add(salesTextArea);

        frame.setVisible(true);

      }

    }