Download presentation
Presentation is loading. Please wait.
1
1 Name & Gender Example At a certain University the registry require a small application to help those on the front desk work out the gender with the name. The student enters their family name and gender The application then gives the name and gender
2
2 Possible Design Gender & Name read read name Label TextField Male/female radio button submit accept data Button calculatedisplay display name+gender Label TextField clear clear all text Button GUI elements female male enter name submit clear
3
3 MethodGUI calculate WorkOut
4
4 How does the MethodGUI use the auxiliary class WorkOut? First attempt would be to set up a ‘calculating’ class with a method to work out the name and gender It would be sent the values for the 2 inputs and return the name and gender It would not hold any data of its own and would really only be a glorified function holding the calculating method only
5
5 MethodGUI calculate Workout String name, boolean genderStatus String who
6
6 Code for the GUI ( 3 parts) setting up all the GUI features. declaring their types and constructing new objects with the correct parameters Constructing all GUI elements
7
7 setting up the JFrame adding all the GUI elements to the content pane registering the buttons with the listeners adding all GUI elements Code for the GUI (part 2)
8
8 Code for the GUI (part 3) resetting all the text fields to empty strings when the clear button is selected takes the information from the GUI and uses it to call the method of the auxiliary class places the return value back on the frame Setting up the events triggered by the GUI elements
9
The Auxiliary Class 9 Why is this static?
10
10 Design for the Calculator part The calculator will receive 2 pieces of data –The name (name) –the gender status (gStatus) It will return the name & gender (name, gStatus)(n, g) MethodGUI (who) (return)
11
11 Alternative Solution Although this first attempt works, it is not good Java style and does not really make use of the object oriented power of the language Leaving the JFrame GUI construction as it is, the changes to this new solution involve the creation of an object of the Workout class which holds its own data so can be given values for its instance variables When the call to use the auxiliary class is made the values needed are passed by the call and assigned to the instance variables either using ‘set’ methods of the auxiliary class or directly
12
12 MethodGUI2 and Workout2 created an object called genderWorker of the type Workout2 i.e. an instance of the class Workout2 using the parameters we have got from the GUI to construct it We then use a method of that object to find the gender & name The changed code in the GUI is :
13
13 data members of the class (instance variables) Workout2 name : String gStatus : boolean +Workout2(named:String,status:boolean) +calcWho() : String The value of named is assigned from the constructor to the instance variable name This value of name can then be used by the calcWho method directly in the object without being passed explicitly the class constructor needing 2 parameters
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.