Laboratory 14 Lesson 14-2 Exercise 1 Exercise 2 Exercise 3 Exercise 4 Exercise 5 Exercise 6 Lesson 14-3 Exercise 1 Exercise 2 Exercise 3 Lesson 14-4 Exercise 1 Exercise 2 Exercise 3 Solutions Lesson 14-2 Exercise 1: The following is an example of the class heading and shell for the applet OddEven. import java.awt.*; // Supplies layout manager import java.awt.event.*; // Supplies event classes import javax.swing.*; // Supplies class JApplet and GUI components public class OddEvenApplet extends JApplet implements ActionListener { // Declaring fields /* TO BE FILLED IN: Exercise 3 */ // Event handler method public void actionPerformed(ActionEvent event) { /* TO BE FILLED IN: Exercise 2 */ } public void init() { /* TO BE FILLED IN: Exercise 4 */ } } Exercise 2: The following is an example of the actionPerformed method. // Event handler method public void actionPerformed(ActionEvent event) { int number = 0; try { number = Integer.parseInt(inputField.getText()); if ( Math.abs(number) < 100 || Math.abs(number) > 999 ) throw new NumberFormatException(); } catch( NumberFormatException e ) { JOptionPane.showMessageDialog( this, "Please enter a three digit integer.", "Error: Wrong input!", JOptionPane.ERROR_MESSAGE ); return; } if ((number / 2) * 2 == number) outLabelUnits.setText("Unit's position is even."); else outLabelUnits.setText("Unit's position is odd."); if ((((number / 10) / 2) * 2) == (number / 10)) outLabelTens.setText("Ten's position is even."); else outLabelTens.setText("Ten's position is odd."); if ((((number / 100) / 2) * 2) == (number / 100)) outLabelHundreds.setText("Hundred's position is even."); else outLabelHundreds.setText("Hundred's position is odd."); } Exercise 3: The following are the variables that are needed for this applet. // Declaring fields private JTextField inputField; private JButton button; private JLabel outLabelUnits, outLabelTens, outLabelHundreds; Exercise 4: The following is an example of the init method. public void init() { // Set layout for GUI components setLayout(new GridLayout(6,0)); // Instantiate GUI components JLabel label; label = new JLabel("Enter a three digit number and click enter button"); outLabelUnits = new JLabel("Units"); outLabelTens = new JLabel("Tens"); outLabelHundreds = new JLabel("Hundreds"); button = new JButton("enter"); button.addActionListener(this); inputField = new JTextField("Value here"); // Add window compontents add(label); add(inputField); add(button); add(outLabelHundreds); add(outLabelTens); add(outLabelUnits); } Exercise 5: The following is an example of the complete applet. import java.awt.*; // Supplies layout manager import java.awt.event.*; // Supplies event classes import javax.swing.*; // Supplies class JApplet and GUI components public class OddEvenApplet extends JApplet implements ActionListener { // Declaring fields private JTextField inputField; private JButton button; private JLabel outLabelUnits, outLabelTens, outLabelHundreds; // Event handler method public void actionPerformed(ActionEvent event) { int number = 0; try { number = Integer.parseInt(inputField.getText()); if ( Math.abs(number) < 100 || Math.abs(number) > 999 ) throw new NumberFormatException(); } catch( NumberFormatException e ) { JOptionPane.showMessageDialog( this, "Please enter a three digit integer.", "Error: Wrong input!", JOptionPane.ERROR_MESSAGE ); return; } if ((number / 2) * 2 == number) outLabelUnits.setText("Unit's position is even."); else outLabelUnits.setText("Unit's position is odd."); if ((((number / 10) / 2) * 2) == (number / 10)) outLabelTens.setText("Ten's position is even."); else outLabelTens.setText("Ten's position is odd."); if ((((number / 100) / 2) * 2) == (number / 100)) outLabelHundreds.setText("Hundred's position is even."); else outLabelHundreds.setText("Hundred's position is odd."); } public void init() { // Set layout for GUI components setLayout(new GridLayout(6,0)); // Instantiate GUI components JLabel label; label = new JLabel("Enter a three digit number and click enter button"); outLabelUnits = new JLabel("Units"); outLabelTens = new JLabel("Tens"); outLabelHundreds = new JLabel("Hundreds"); button = new JButton("enter"); button.addActionListener(this); inputField = new JTextField("Value here"); // Add window compontents add(label); add(inputField); add(button); add(outLabelHundreds); add(outLabelTens); add(outLabelUnits); } } The following HTML code runs the IntegerRead applet. <applet code="OddEvenApplet.class" width=400 height=200></applet> Exercise 6: The OddEven applet inputs a three digit integer, and determines whether each digit is odd/even. Test the applet with a sufficient number of (positive/negative) integer inputs to cover all possible combinations of odd/even. For example: Three Digit Input Integer Hundreds Tens Units Even Even Even Odd Odd Odd Even Odd Odd Even Odd Odd Even Even Odd Even Even Odd Also, test the applet with some invalid inputs, such as integers with less/more than three digits, floating point numbers, and nonnumeric characters. The following are the results. Input 444 632 815 -928 -714 931 0 23 -1001 1.2 -3.45 Enter nothing; just press Enter. Expected Results Hundred’s position is even. Ten’s position is even. Unit’s position is even. Hundred’s position is even. Ten’s position is odd. Unit’s position is even. Hundred’s position is even. Ten’s position is odd. Unit’s position is odd. Hundred’s position is odd. Ten’s position is even. Unit’s position is even. Hundred’s position is odd. Ten’s position is odd. Unit’s position is even. Hundred’s position is odd. Ten’s position is odd. Unit’s position is odd. Error: Wrong input! Please enter a three digit integer. Error: Wrong input! Please enter a three digit integer. Error: Wrong input! Please enter a three digit integer. Error: Wrong input! Please enter a three digit integer. Error: Wrong input! Please enter a three digit integer. Error: Wrong input! Please enter a three digit integer. Actual Results As expected. As expected. As expected. As expected. As expected. As expected. As expected. As expected. As expected. As expected. As expected. As expected. XY Error: Wrong input! Please enter a three digit integer. As expected. Lesson 14-3 Exercise 1: The following is an example of the applet. import java.awt.*; import java.awt.event.*; import javax.swing.*; // Supplies layout manager // Supplies event classes // Supplies class Applet public class IntegerRead extends JApplet implements ActionListener { // Declaring fields private JTextField inputField; private JButton button; private JLabel outLabel; // Event handler method public void actionPerformed(ActionEvent event) { String input = inputField.getText(); try { int num = Integer.parseInt( input ); if ( num < 0 ) input = Integer.toString( Math.abs( num ) ); } catch( NumberFormatException e ) { JOptionPane.showMessageDialog( this, "Please enter a valid integer.", "Error: Wrong input!", JOptionPane.ERROR_MESSAGE ); return; } if (input.length()/2 * 2 == input.length()) outLabel.setText("The integer has an even number of digits"); else outLabel.setText("The integer has an odd number of digits"); } public void init() { setLayout(new GridLayout(4,0)); JLabel label = new JLabel("Enter an integer and click enter button"); outLabel = new JLabel("odd or even?"); button = new JButton("enter"); button.addActionListener(this); inputField = new JTextField("Value here"); // Add window compontents add(label); add(inputField); add(button); add(outLabel); } } The test cases include: A valid integer with an odd number of digits: 123 A valid integer with an even number of digits: -4567 An invalid number: 123.45 An invalid number: qwer5 An empty string (just clicking Enter) Exercise 2: The following is an example of the applet. import java.awt.*; import java.awt.event.*; import javax.swing.*; // Supplies layout manager // Supplies event classes // Supplies class Applet public class StringReverse extends JApplet implements ActionListener { // Declaring fields private JTextField inputField; private JButton button; private JLabel outLabel; // Event handler method public void actionPerformed(ActionEvent event) { String input; String reverseInput; input = inputField.getText(); // Input String must have at least one (visible) character try { // Remove leading and trailing spaces String temp = input.trim(); // If String length is 0, throw an exception if ( temp.length() < 1 ) throw new Exception(); } catch( Exception e ) { JOptionPane.showMessageDialog( this, "Input String must have at least one (visible) character.", "Error: Wrong input!", JOptionPane.ERROR_MESSAGE ); return; } char[] inputChar = input.toCharArray(); char[] inputCharReverse = new char [inputChar.length]; // Reverse String for (int position = inputChar.length-1; position >= 0; position--) inputCharReverse[inputChar.length-1-position] = inputChar[position]; reverseInput = String.valueOf(inputCharReverse); outLabel.setText("Reversed String is : " + reverseInput); inputField.setText(""); } public void init() { setLayout(new GridLayout(4,0)); JLabel label = new JLabel("Enter a string and click enter button"); outLabel = new JLabel("Reverse order string is"); button = new JButton("Enter"); button.addActionListener(this); inputField = new JTextField("String here"); // Add window compontents add(label); add(inputField); add(button); add(outLabel); } } Exercise 3: The following is an example of the applet. import java.awt.*; import java.awt.event.*; import javax.swing.*; // Supplies layout manager // Supplies event classes // Supplies class Applet public class DateFormat extends JApplet implements ActionListener { // Declaring fields private JTextField inputDay; private JTextField inputMonth; private JTextField inputYear; private JButton button; private JLabel outLabelUS, outLabelEng; // Event handler method public void actionPerformed(ActionEvent event) { String day = inputDay.getText(); String month = inputMonth.getText(); String year = inputYear.getText(); outLabelUS.setText("US: " + month + " " + day + " " + year); outLabelEng.setText("English: " + day + " " + month + " " + year); inputDay.setText(""); inputMonth.setText(""); inputYear.setText(""); } public void init() { setLayout(new GridLayout(5,2)); JLabel label, labelDay, labelMonth, labelYear; label = new JLabel(""); labelDay = new JLabel("Enter the day"); labelMonth = new JLabel("Enter the month"); labelYear = new JLabel("Enter a year and click enter button"); outLabelUS = new JLabel("US date format"); outLabelEng = new JLabel("English date format"); button = new JButton("enter"); button.addActionListener(this); inputDay = new JTextField("Day here"); inputMonth = new JTextField("Month here"); inputYear = new JTextField("Year here"); // Add window compontents add(label); add(button); add(labelDay); add(inputDay); add(labelMonth); add(inputMonth); add(labelYear); add(inputYear); add(outLabelUS); add(outLabelEng); } } Lesson 14-4 Exercise 1: The following code runs the IntegerRead applet. <applet code="IntegerRead.class" width=400 height=200></applet> Exercise 2: The following code runs the StringReverse applet. <applet code="StringReverse.class" width=400 height=200></applet> Exercise 3: The following code runs the DateFormat applet. <applet code="DateFormat.class" width=400 height=200></applet>