Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Conditionals and Loops - Lab Exercise - Computer Science 1 | CPSC 1301, Lab Reports of Computer Science

Material Type: Lab; Class: Computer Science 1; Subject: Computer Science; University: Columbus State University; Term: Unknown 1989;

Typology: Lab Reports

Pre 2010

Uploaded on 08/04/2009

koofers-user-0mj
koofers-user-0mj 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 5: Conditionals and Loops (3)
Lab Exercises
Topics Lab Exercises
Determining Event Sources Vote Counter, Revisited
Dialog Boxes Modifying EvenOdd.java
A Pay Check Program
Checkboxes & Radio Buttons Adding Buttons to StyleOptions.java
Chapter 5: Conditionals and Loops 1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Conditionals and Loops - Lab Exercise - Computer Science 1 | CPSC 1301 and more Lab Reports Computer Science in PDF only on Docsity!

Chapter 5: Conditionals and Loops (3)

Lab Exercises

Topics Lab Exercises

Determining Event Sources Vote Counter, Revisited Dialog Boxes Modifying EvenOdd.java A Pay Check Program Checkboxes & Radio Buttons Adding Buttons to StyleOptions.java

Vote Counter, Revisited

Chapter 4 had a lab exercise that created a GUI with two buttons representing two candidates (Joe and Sam) in an election or popularity contest. The program computed the number of times each button was pressed. In that exercise two different listeners were used, one for each button. This exercise is a slight modification. Only one listener will be used and its ActionPerformed method will determine which button was pressed. The files VoteCounter.java and VoteCounterPanel.java contain slight revisions to the skeleton programs used in the Chapter 4 exercise. Save them to your directory and do the following:

  1. Add variables for Sam - a vote counter, a button, and a label.
  2. Add the button and label for Sam to the panel; add the listener to the button.
  3. Modify the ActionPerformed method of the VoteButtonListener class to determine which button was pressed and update the correct counter. (See the LeftRight example or the Quote example for how to determine the source of an event.)
  4. Test your program.
  5. Now modify the program to add a message indicating who is winning. To do this you need to instantiate a new label, add it to the panel, and add an if statement in actionPerformed that determines who is winning (also test for ties) and sets the text of the label with the appropriate message. //********************************************************* // VoteCounter.java // // Demonstrates a graphical user interface and event // listeners to tally votes for two candidates, Joe and Sam. //********************************************************* import javax.swing.JFrame; public class VoteCounter { //---------------------------------------------- // Creates the main program frame. //---------------------------------------------- public static void main(String[] args) { JFrame frame = new JFrame("Vote Counter"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new VoteCounterPanel()); frame.pack(); frame.setVisible(true); } }

Modifying EvenOdd.java

File EvenOdd.java contains the dialog box example in Listing 5.21 the text.

  1. Compile and run the program to see how it works.
  2. Write a similar class named SquareRoots (you may modify EvenOdd) that computes and displays the square root of the integer entered. //******************************************************************** // EvenOdd.java Author: Lewis/Loftus // // Demonstrates the use of the JOptionPane class. //******************************************************************** import javax.swing.JOptionPane; class EvenOdd { //----------------------------------------------------------------- // Determines if the value input by the user is even or odd. // Uses multiple dialog boxes for user interaction. //----------------------------------------------------------------- public static void main (String[] args) { String numStr, result; int num, again; do { numStr = JOptionPane.showInputDialog ("Enter an integer: "); num = Integer.parseInt(numStr); result = "That number is " + ((num%2 == 0)? "even" : "odd"); JOptionPane.showMessageDialog (null, result); again = JOptionPane.showConfirmDialog (null, "Do Another?"); } while (again == JOptionPane.YES_OPTION); } }

A Pay Check Program

Write a class PayCheck that uses dialog boxes to compute the total gross pay of an hourly wage worker. The program should use input dialog boxes to get the number of hours worked and the hourly pay rate from the user. The program should use a message dialog to display the total gross pay. The pay calculation should assume the worker earns time and a half for overtime (for hours over 40).

// StyleOptionsPanel.java Author: Lewis/Loftus // // Demonstrates the use of check boxes. //******************************************************************** import javax.swing.; import java.awt.; import java.awt.event.*; public class StyleOptionsPanel extends JPanel { private int fontSize = 36; private int style = Font.PLAIN; private JLabel saying; private JCheckBox bold, italic; //----------------------------------------------------------------- // Sets up a panel with a label and some check boxes that // control the style of the label's font. //----------------------------------------------------------------- public StyleOptionsPanel() { saying = new JLabel ("Say it with style!"); saying.setFont (new Font ("Helvetica", style, fontSize)); bold = new JCheckBox ("Bold"); bold.setBackground (Color.cyan); italic = new JCheckBox ("Italic"); italic.setBackground (Color.cyan); StyleListener listener = new StyleListener(); bold.addItemListener (listener); italic.addItemListener (listener); add (saying); add (bold); add (italic); setBackground (Color.cyan); setPreferredSize (new Dimension(300, 100)); } //***************************************************************** // Represents the listener for both check boxes. //***************************************************************** private class StyleListener implements ItemListener { //-------------------------------------------------------------- // Updates the style of the label font style. //-------------------------------------------------------------- public void itemStateChanged (ItemEvent event) { style = Font.PLAIN; if (bold.isSelected()) style = Font.BOLD; if (italic.isSelected()) style += Font.ITALIC;

saying.setFont (new Font ("Helvetica", style, fontSize)); } } }