1 / 75

Tutorial 18 – Microwave Oven Application Building Your Own Classes and Objects

Tutorial 18 – Microwave Oven Application Building Your Own Classes and Objects.

pettya
Download Presentation

Tutorial 18 – Microwave Oven Application Building Your Own Classes and Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tutorial 18 – Microwave Oven ApplicationBuilding Your Own Classes and Objects Outline18.1 Test-Driving the Microwave Oven Application18.2 Designing the Microwave Oven Application18.3 Initializing Objects: Constructors18.4 Get and Set Methods18.5 Completing the Microwave Oven Application18.6 Controlling Access to Members18.7 The main Method18.8 Using the Debugger: The watch Command18.9 Wrap-Up

  2. Objectives • In this tutorial, you will learn to: • Declare your own classes. • Create and use objects of your own classes. • Control access to instance variables. • Use keyword private. • Declare your own get and set methods.

  3. 18.1 Test-Driving the Microwave Oven Application

  4. 18.1 Test-Driving the Microwave Oven Application (Cont.) • Running the completed application • Open a Command Prompt • Change to MircowaveOven directory • Type javaMircowaveOven

  5. Microwave’s glass window Numeric Keypad 18.1 Test-Driving the Microwave Oven Application (Cont.) Figure 18.1 Microwave Oven application GUI displayed when your application is executed.

  6. 18.1 Test-Driving the Microwave Oven Application (Cont.) Figure 18.2 Microwave Oven application accepts only four digits. • Enter test data into application

  7. 18.1 Test-Driving the Microwave Oven Application (Cont.) Figure 18.3 Microwave Oven application with invalid input. • Microwave doesn’t accept times greater than 60 minutes

  8. 18.1 Test-Driving the Microwave Oven Application (Cont.) Figure 18.4 Microwave Oven application after invalid input has been entered and the StartJButton clicked.

  9. Color yellow simulates microwave light 18.1 Test-Driving the Microwave Oven Application (Cont.) Figure 18.5 Microwave Oven application with valid time entered and inside light turned on (it’s now cooking).

  10. JTextFielddisplays Done! when cooking has finished Color returns to default color to simulate that cooking has finished 18.1 Test-Driving the Microwave Oven Application (Cont.) Figure 18.6 Microwave Oven application after the cooking time has elapsed.

  11. 18.2 Designing the Microwave Oven Application When the user clicks a numeric JButton Display the formatted time When the user clicks the Start JButton Display the formatted time Store the minutes and seconds Begin the countdown Turn the microwave light on

  12. 18.2 Designing the Microwave Oven Application (Cont.) When the timer ticks (once per second) Decrease the time by one second If the new time is not zero Display the new time Else Stop timer Display the text “Done!” Turn the microwave light off When the user clicks the Clear JButton Stop the countdown Display the text “Microwave Oven” Turn the microwave light off

  13. 18.2 Designing the Microwave Oven Application (Cont.)

  14. 18.2 Designing the Microwave Oven Application (Cont.)

  15. Empty class declaration. 18.2 Designing the Microwave Oven Application (Cont.) Figure 18.8 Empty class declaration. • Keyword class indicates that a class declaration follows • Members of a class • Methods or variables declared within the body of a class

  16. Instance variables store minute and second information 18.2 Designing the Microwave Oven Application (Cont.) Figure 18.9 CookingTime’s instance variables.

  17. 18.3 Initializing Objects: Constructors • Constructors • Used to initialize instance variables • Has the same name as the class that contains it • Similar to a method • Cannot return a value • Can take arguments

  18. Constructor 18.3 Initializing Objects: Constructors (Cont.) Figure 18.10 Declaring an empty constructor.

  19. Constructor arguments assigned to instance variables 18.3 Initializing Objects: Constructors (Cont.) Figure 18.11 Constructor initializing instance variables. • Values will be specified for the object in the client of the class.

  20. Declaring an instance of classCookingTime 18.3 Initializing Objects: Constructors (Cont.) Figure 18.12 Declaring an object of type CookingTime. • Java is an extensible language. • Can be extended with new classes.

  21. 18.4 Get and Set Methods • Access and modify instance variables • Get methods • Obtain the value of an instance variable • Also called accessors • Set methods • Set the value of an instance variable • Also called mutators

  22. 18.4 Get and Set Methods (Cont.) • Maintaining data in a consistent state • Set methods ensure that each instance variable contains a proper value • Instance variable minute should only contain values between 0 and 59

  23. Returning a value froma get method 18.4 Get and Set Methods (Cont.) Figure 18.13 getMinute declaration.

  24. Code used to validate data 18.4 Get and Set Methods (Cont.) Figure 18.14setMinute declaration. • setMinute accepts positive values less than 60.

  25. Code to return the second value 18.4 Get and Set Methods (Cont.) Figure 18.15 getSecond declaration.

  26. Method setSecond is similar to setMinute 18.4 Get and Set Methods (Cont.) Figure 18.16 setSecond declaration. • setSecond accepts positive values less than 60.

  27. Testing whether bothminuteand second are equal to 0 18.4 Get and Set Methods (Cont.) Figure 18.17 isDone declaration. • Returns true if microwave is done cooking, false otherwise

  28. Assign data by calling methods setMinute and setSecond 18.4 Get and Set Methods (Cont.) Figure 18.18 Constructor using set methods to initialize variables. • Calling set methods ensure that only valid data will be assigned to minute and second.

  29. 18.4 Get and Set Methods (Cont.) • The this reference • Refer to the current object of the class with the keyword this. • this is automatically added to the method call

  30. Decrementing second Decrementing minute and setting second to 59 18.4 Get and Set Methods (Cont.) Figure 18.19 Decrementing the time in the tick method. • Decrements the time by one second. • Save your source code file.

  31. Setting clockTimer’s delay 18.5 Completing the Microwave Oven Application Figure 18.20 Creating clockTimer with a delay of 1000 milliseconds. • Time is measured in milliseconds.

  32. Copy timeToDisplay into currentTime 18.5 Completing the Microwave Oven Application (Cont.) Figure 18.21 Declaring currentTime to hold timeToDisplay value. • Make the time output 4 characters long.

  33. Calling two different versions ofString method substring 18.5 Completing the Microwave Oven Application (Cont.) Figure 18.22 Extracting minutes and seconds from currentTime. • Format output as two digits, followed by a colon, followed by two digits.

  34. Start timer and turn“light” on to indicatemicrowave oven is cooking 18.5 Completing the Microwave Oven Application (Cont.) Figure 18.23 Starting the Microwave Oven countdown.

  35. Code that turns off microwave and resets variables 18.5 Completing the Microwave Oven Application (Cont.) Figure 18.24 Clearing the Microwave Oven input.

  36. Appending digit to instance variabletimeToDisplay 18.5 Completing the Microwave Oven Application (Cont.) Figure 18.25 Appending digit and formatting timeToDisplay. • Display the data as it is being input

  37. Decrements time during countdown 18.5 Completing the Microwave Oven Application (Cont.) Figure 18.26 Modifying the display during countdown.

  38. Stopping clockTimer 18.5 Completing the Microwave Oven Application (Cont.) Figure 18.27 Turning off the microwave when the timer runs out. • stop method turns off the clockTimer. • Reset background color to gray.

  39. 18.5 Completing the Microwave Oven Application (Cont.) Figure 18.28 Running the Microwave Oven application. • Save the changes to your code • Compile and run in the CommandPrompt

  40. Declaring instance variables of class CookingTime, private 18.6 Controlling Access to Members Figure 18.29 Declaring CookingTime’s instance variables as private. • private data is only accessible to members of the class.

  41. Declaring instance variables of class MicrowaveOven, private 18.6 Controlling Access to Members (Cont.) Figure 18.30 Declaring MicrowaveOven’s instance variables as private.

  42. Declaring method displayTime of class MicrowaveOven, private 18.6 Controlling Access to Members (Cont.) Figure 18.31 Declaring the displayTime method as private.

  43. 1 // Tutorial 18: MicrowaveOven.java 2 // Mimics the behavior of a microwave oven. 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.text.DecimalFormat; 6 import javax.swing.*; 7 import javax.swing.border.*; 8 9 publicclass MicrowaveOven extends JFrame 10 { 11 // JPanel for microwave window 12 private JPanel windowJPanel; 13 14 // JPanel for microwave controls 15 private JPanel controlJPanel; 16 17 // JTextField for cooking time 18 private JTextField displayJTextField; 19 20 // JButtons to set cooking time 21 private JButton oneJButton; 22 private JButton twoJButton; 23 private JButton threeJButton; 24 private JButton fourJButton; 25 private JButton fiveJButton; MicrowaveOven.java(1 of 22)

  44. 26 private JButton sixJButton; 27 private JButton sevenJButton; 28 private JButton eightJButton; 29 private JButton nineJButton; 30 private JButton zeroJButton; 31 32 // JButtons to start and clear timer 33 private JButton startJButton; 34 private JButton clearJButton; 35 36 // Timer to count down seconds 37 private Timer clockTimer; 38 39 // String for storing digits entered by user 40 private String timeToDisplay = ""; 41 42 // CookingTime instance for storing the current time 43 private CookingTime time = new CookingTime( 0, 0 ); 44 45 // DecimalFormat to format time output 46 private DecimalFormat timeFormat = new DecimalFormat( "00" ); Declaring instance variable timeToDisplayprivate Declaring instance variable cookingTimeprivate MicrowaveOven.java(2 of 22)

  45. 47 48 // no-argument constructor 49 public MicrowaveOven() 50 { 51 createUserInterface(); 52 } 53 54 // create and position GUI components; register event handlers 55 private void createUserInterface() 56 { 57 // get content pane for attaching GUI components 58 Container contentPane = getContentPane(); 59 60 // enable explicit positioning of GUI components 61 contentPane.setLayout( null ); 62 63 // set up windowJPanel 64 windowJPanel = new JPanel(); 65 windowJPanel.setBounds( 16, 16, 328, 205 ); 66 windowJPanel.setBorder( new LineBorder( Color.BLACK ) ); 67 contentPane.add( windowJPanel ); 68 MicrowaveOven.java(3 of 22)

  46. 69 // set up controlJPanel 70 controlJPanel = new JPanel(); 71 controlJPanel.setBounds( 368, 16, 149, 205 ); 72 controlJPanel.setBorder( new LineBorder( Color.BLACK ) ); 73 controlJPanel.setLayout( null ); 74 contentPane.add( controlJPanel ); 75 76 // set up displayJTextField 77 displayJTextField = new JTextField(); 78 displayJTextField.setBounds( 7, 5, 135, 42 ); 79 displayJTextField.setText( "Microwave Oven" ); 80 displayJTextField.setHorizontalAlignment( JTextField.CENTER ); 81 displayJTextField.setEditable( false ); 82 controlJPanel.add( displayJTextField ); 83 84 // set up oneJButton 85 oneJButton = new JButton(); 86 oneJButton.setBounds( 13, 59, 41, 24 ); 87 oneJButton.setText( "1" ); 88 controlJPanel.add( oneJButton ); 89 oneJButton.addActionListener( 90 MicrowaveOven.java(4 of 22)

  47. 91 new ActionListener() // anonymous inner class 92 { 93 // event handler called when oneJButton is pressed 94 public void actionPerformed( ActionEvent event ) 95 { 96 oneJButtonActionPerformed( event ); 97 } 98 99 } // end anonymous inner class 100 101 ); // end call to addActionListener 102 103 // set up twoJButton 104 twoJButton = new JButton(); 105 twoJButton.setBounds( 54, 59, 41, 24 ); 106 twoJButton.setText( "2" ); 107 controlJPanel.add( twoJButton ); 108 twoJButton.addActionListener( 109 110 new ActionListener() // anonymous inner class 111 { 112 // event handler called when twoJButton is pressed MicrowaveOven.java(5 of 22)

  48. 113 public void actionPerformed( ActionEvent event ) 114 { 115 twoJButtonActionPerformed( event ); 116 } 117 118 } // end anonymous inner class 119 120 ); // end call to addActionListener 121 122 // set up threeJButton 123 threeJButton = new JButton(); 124 threeJButton.setBounds( 95, 59, 41, 24 ); 125 threeJButton.setText( "3" ); 126 controlJPanel.add( threeJButton ); 127 threeJButton.addActionListener( 128 129 new ActionListener() // anonymous inner class 130 { 131 // event handler called when threeJButton is pressed 132 public void actionPerformed( ActionEvent event ) 133 { 134 threeJButtonActionPerformed( event ); 135 } 136 137 } // end anonymous inner class MicrowaveOven.java(6 of 22)

  49. 138 139 ); // end call to addActionListener 140 141 // set up fourJButton 142 fourJButton = new JButton(); 143 fourJButton.setBounds( 13, 83, 41, 24 ); 144 fourJButton.setText( "4" ); 145 controlJPanel.add( fourJButton ); 146 fourJButton.addActionListener( 147 148 new ActionListener() // anonymous inner class 149 { 150 // event handler called when fourJButton is pressed 151 public void actionPerformed( ActionEvent event ) 152 { 153 fourJButtonActionPerformed( event ); 154 } 155 156 } // end anonymous inner class 157 158 ); // end call to addActionListener 159 MicrowaveOven.java(7 of 22)

  50. 160 // set up fiveJButton 161 fiveJButton = new JButton(); 162 fiveJButton.setBounds( 54, 83, 41, 24 ); 163 fiveJButton.setText( "5" ); 164 controlJPanel.add( fiveJButton ); 165 fiveJButton.addActionListener( 166 167 new ActionListener() // anonymous inner class 168 { 169 // event handler called when fiveJButton is pressed 170 public void actionPerformed( ActionEvent event ) 171 { 172 fiveJButtonActionPerformed( event ); 173 } 174 175 } // end anonymous inner class 176 177 ); // end call to addActionListener 178 179 // set up sixJButton 180 sixJButton = new JButton(); 181 sixJButton.setBounds( 95, 83, 41, 24 ); 182 sixJButton.setText( "6" ); 183 controlJPanel.add( sixJButton ); 184 sixJButton.addActionListener( MicrowaveOven.java(8 of 22)

More Related