Title: COIT 11222 - Visual ProgrammingAuthor(s): Mike O
1COIT 11222 Visual Programming
- Lecture Week 5
- References
- Java Programming - Complete
- Concepts and Techniques,
- 3rd Edition, Shelly Cashman et al.
2Topics For This Week
- Review and Revision
- Review of Java Applets.
- Validation Strategies A Deeper Look
- Exception Handling A Deeper Look
- Unary and Assignment Operators
- Loops - for and while
- Tracing Program Code and Pseudo Code
- Batch Programs Vs Event Driven Programs
- Arguments Vs Parameters
- Assignment 1 100
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 2
3Review of all Java Applets
- We will now review what we have learnt so far
about - Java Applets
- In addition, some probing questions will be
asked, and these should hopefully help improve
your understanding of everything covered so far.
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 3
4GUI Applet - General Form
- public class My_Applet extends Applet
implements .. -
- // Declare variables
- // Construct GUI Components
- public void init()
-
- // Add GUI components, Setup / Add
ActionListner, Load Logo(s) -
- public void actionPerformed (ActionEvent e)
-
- // Act on button click. e.g. Get inputs,
process, display output. -
- public void paint (Graphics g)
-
- // Draw Logo on screen
-
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 4
5GUI Applet (cont )
- import java.applet. // For Applet support
- import java.awt. // For GUI components
- import java.awt.event. // For event handling
- public class W03_Wages_Calc__Applet extends
Applet implements ActionListener -
- // Declare variables
- Image Logo
- String Emp_ID
- int Hours, Rate
- double Gross_Pay
- // Construct GUI Components
- Label Window_Title_Label new Label ("Gross Pay
Calculator") - Label Emp_ID_Label new Label ("Enter Employee
ID") - Label Hours_Label new Label ("Enter Hours
Worked") - Label Rate_Label new Label ("Enter Hourly
Rate") - Label Output_Label new Label ("Please click
the 'Calculate' button !!") - TextField Emp_ID_TextField new TextField
(20)
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 5
6GUI Applet (cont )
- public void init()
-
- setForeground (Color.blue)
- add (Window_Title_Label)
- add (Emp_ID_Label)
- add (Emp_ID_TextField)
- add (Hours_Label)
- add (Hours_TextField)
- add (Rate_Label)
- add (Rate_TextField)
- add (Calc_Button)
- add (Output_Label)
- Calc_Button.addActionListener (this)
- Logo getImage (getDocumentBase(),
"cqulogo.gif") -
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 6
7GUI Applet (cont )
- public void actionPerformed (ActionEvent e)
-
- // Get Text Input and Convert to Integers if
required. - Emp_ID Emp_ID_TextField.getText()
- Hours Integer.parseInt(Hours_TextField.getTe
xt()) - Rate Integer.parseInt(Rate_TextField.getTex
t()) - // Calculations
- Gross_Pay Hours Rate
- Output_Label.setText ("Gross Pay "
Gross_Pay) -
- public void paint (Graphics g)
-
- g.drawImage (Logo, 120, 160, this)
-
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 7
8GUI Applet (cont )
- In the prior Applet example
- Why is the logo drawn on screen in the "paint"
method ? - Answer Only the paint method has access to the
Graphics canvas for the Applet's window. - Could we draw the logo in the "init" method or
the "actionPerformed" method ? - Answer No, only the paint method has access to
the Applet's graphics canvas.
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 8
9GUI Applet (cont )
- In the prior Applet example
- Could we do without the "init" method ? Could we
place all the code they contain in the
"actionPerformed" method ? - Answer No and No.
- If there was no "init" method, no GUI components
would be created or added to the user interface. - actionPerformed runs when a button is clicked.
With no "init" method, how could the user click a
button that does NOT exist and has NOT been
assigned a Listener ?
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 9
10GUI Applet (cont )
- In the prior Applet example
- Could we add and use Swing Dialogs in the Applet
to display information and error messages, prompt
user for input, etc ? - Answer Yes, see tutorial questions.
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 10
11GUI Applet (cont )
- In the prior Applet example
- Can the "paint" method be invoked multiple times
? e.g. Is it invoked when another program's
window covers your Applet's window and then focus
returns to your Applet ? - Answer Yes and Yes - see tutorial questions.
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 11
12Unary Operators
- and -- are operators that increment or
decrement an integer by 1 they are called Unary
Operators. - Example, each of these lines of code increment
the value of k by 1 - k
- k
- Each of these lines of code decrement the value
of k by 1 - k--
- --k
13Unary Operators (cont)
- Note Well
- When the operation is applied depends on whether
the Unary Operator is before the variable, a
pre-increment or pre-decrement, or after the
variable, a post-increment or post-decrement. - If the operator is before the variable, then the
operator is applied when it is encountered, and
the updated value used immediately. - If the operator is after the variable, then the
current value of the variable is used, and the
operator is then applied. - Java works from left to right applying these
operators and substituting the latest value of
each variable as it goes ! - Keep the above in mind ! It is very easy to
make errors in complex calculations.
14Unary Operators (cont)
- Exercise what would be output by the following
code - int k 5
- System.out.println (k)
- System.out.println (k)
- Solution below.
- Answer We would see the original value of k, a
5, for the first println, and the
post-incremented value of k, a 6, for the second
println.
15Unary Operators (cont)
- Exercise what would be output by the following
code - int k 5
- System.out.println (k)
- System.out.println (--k)
- System.out.println (k--)
- System.out.println (k)
- Solution below.
- Answer We would see the pre-incremented value of
k, a 6, for the first println, the
pre-decremented value, a 5, for the second
println, the current value, a 5, for the third
println, and the post-decremented value, a 4, for
the final println.
16Unary Operators (cont)
- Exercise what values would j and m have after
the following code - int m 5
- int j ((m 2) / (--m - 2)) - --m
- Remember the values are updated and substituted
as they are encountered from left to right. - Solution below.
- Answer After the first m, 5 is substituted
because we have a post-increment, and m is then
immediately updated to 6. Then, for the first
--m, the pre-decrement operator is applied on the
latest value of m, so 6-1 5 is substituted.
For the final --m, the operator is applied and
the value 5-1 4 is substituted. So, the
equation translates to this - j ((5 2) / (5 - 2)) - 4 10 / 3 - 4
3 - 4 -1
17Unary Operators (cont)
- Exercise what values would j and m have after
the following code - int m 5
- int j (--m (((m 2)) / (--m - 2)))
- Answer j8, m4. Students to verify this now -
fill in the ___ blanks below. - j (--m (((m 2)) / (--m - 2)))
- (___ (((___ 2)) / (___ - 2)))
- (___ (___ / ___ ))
- ___ ___ _____
- Final value of m _____
18Unary Operators (cont)
- Recommendations
- Never write code like this !!
- It is way too easy to make mistakes and create a
mess of code that is difficult to debug, test,
and understand. - A much better way to write this code and make it
easy to debug, test, and understand is to create
additional variables and then use these in the
calculation(s). - For example, if we need various incremented and
decremented values of m, we could create - int m_Plus_1 m 1
- int m_Minus_1 m - 1
- int m_Minus_2 m - 2
- // etc ...
- and then use these when needed in calculations.
19Assignment Operators
- As well as the standard Assignment Operator ()
there are other Assignment Operators , -, ,
/, and . - Original Code Using an Assignment Operator
- k k 10 k 10
- k k - 10 k - 10
- k k 10 k 10
- k k / 10 k / 10
- k k 10 k 10 // Modulo Arithmetic
20Loops
- Loops are blocks of code that are repeated
multiple times until some sort of condition is
satisfied. - We cover two types of Java loops
- for
- while
- for loops are normally counter controlled loops -
the code within them is executed a specific
number of times. Assignment and unary operators
are often used to update the counter. - while loops are conditional loops - the code
within them is executed until a specific
condition (or group of conditions) is satisfied.
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 20
21Loops (cont)
- The code in the body of the loop - between the
braces - is executed for every iteration of
the loop. - If there are no braces , then the body of loop
includes the code upto and including the next
semi-colon.
22for Loops
- A for loop consists of
- Three Java commands separated by semi-colons
inside round brackets - The initialiser is executed before anything else.
- The condition is checked before the body of the
loop is executed. - If the condition is satisfied, the body of the
loop is executed - that is, an iteration of the
loop occurs. - If the condition is not satisfied, the loop is
exited and program execution continues at the
first line of code after the loop. - The update is executed after every iteration of
the loop. - Followed by the body of the loop.
23for Loops (cont)
- For example
- for (int k 0 k lt 10 k)
-
- System.out.println (k)
-
- Declares an integer k and initialises it to 0.
- Checks if k is less than 10, and if so, executes
the code within the body of the loop, which is
just a single println statement in this simple
example. - After all code within the body of the loop is
executed, the value of k is incremented by 1 via
the k command. - Steps 2 and 3 are repeated until k is no longer
less than 10.
24for Loops (cont)
- Recall what happens if there are no braces.
- For example
- for (int k 0 k lt 10 k)
- System.out.println (k) // In the loop.
- System.out.println (k) // NOT in the loop!
25for Loops (cont)
- Exercise What code is contained in the body of
this loop ? - for (int k 0 k lt 10 k)
- System.out.println (k)
- System.out.println ("hi")
- Solution below.
- Answer There is a semi-colon at the end of the
for statement, so no lines of code and none of
the println commands are included in the loop.
26for Loops (cont)
- Exercise what output would the following code
produce - for (int k 0 k lt 10 k)
-
- System.out.println (k)
- k 11
-
- Solution below.
- Answer the first value of k, 0, would be output,
and then k is set to 11, so the loop would then
exit.
27for Loops (cont)
- Exercise what output would the following code
produce - for (int k 5 k lt 14 k k 3)
-
- System.out.println (k)
-
- Solution below.
- Answer the first value of k, 5, would be output,
and then k would be incremented by 3, the
condition checked, the loop body executed, and so
on until k is not lt 14. - The values 5, 8, and 11 would be output and then
the loop would exit.
28for Loops (cont)
- Exercise what output would the following code
produce - for (int k 11 k lt 10 k k 3)
-
- System.out.println (k)
-
- System.out.println ("Hi")
- Solution below.
- Answer k is initialised to 11, so the loop
condition is not satisfied, so the loop exits and
"Hi" is output.
29for Loops (cont)
- Exercise which of the following loops produce
the same results - for (int k 1 k lt 10 k)
- for (int k 1 k lt 10 k)
- for (int k 1 k lt 10 k k 1)
- for (int k 1 k lt 10 k 1)
- Solution below.
- Answer all loops produce exactly the same
results.
30while Loops
- while loops are conditional loops - the code
within them is executed until a specific
condition (or group of conditions) is satisfied. - while loops can do everything a for loop can do.
31while Loops (cont)
- For example, these for and while loops both loop
the same number of times - for (int k 0 k lt 10 k)
-
- // Body of loop here ...
-
- int k 0
- while (k lt 10)
-
- // Body of loop here ...
- k
-
32while Loops (cont)
- Exercise The prior slides for and while loops
are not quite identical though why ? - Solutions below
- Answer the integer k only exists for the life of
the for loop because it is declared in the for
loop. - However, for the while loop the integer k is
declared outside of the loop, so it exists and
can be used after the end of the while loop.
33while Loops (cont)
- Exercise How could both of these loops and the
lives of associated variables be made identical ? - Solutions below
- Answer simply declare the integer k before the
for loop - int k 0
- for (k 0 k lt 10 k)
-
- // Body of loop here ...
34while Loops (cont)
- With while loops, you can specify multiple
conditions, for example - int Num_Retries 0
- boolean Data_Valid false
- while ((Data_Valid false) (Num_Retries lt
10)) -
- // Body of loop here ...
- Num_Retries
-
35Tracing Program Code and Pseudo Code
- To be sure that your algorithm or strategy for
solving a problem is correct and complete, you
need to be able to trace through your pseudo code
and program code and keep track of the values of
all relevant variables at key points in the code. - This is an essential skill for all analysts,
designers, and programmers. - If you dont do this, then you may waste time
developing a program which is wrong, or you may
develop a program that produces wrong results and
you cannot determine why. - Tracing through pseudo code and program code to
verify the processing is essential to checking
everything is correct, checking that everything
is understood, checking that there are no gaps or
no missing steps, and that the desired and
correct results are being produced.
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 35
36Tracing Program Code and Pseudo Code (cont)
- Exercise What would be output by this simple
pseudo code (further explanations on next slide)? - number 10
- OUTPUT number ", "
- WHILE number lt 25
- if (Remainder (number, 3) 0)
- number number 2
- else if (Remainder (number, 4) 0)
- number number 5
- else
- number number 1
- OUTPUT number ", "
- END WHILE
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 36
37Tracing Program Code and Pseudo Code (cont)
- Further explanations
- Assume the program is a console application and
that OUTPUT displays output to the console
screen. - In the above pseudo code, Remainder is a function
that returns the remainder of the division of the
two parameters passed to it. - For example Remainder (20, 6) would return 2,
because 20 / 6 3 with 2 remainder. - Note Remainder is exactly the same as Javas
operator - the modulo arithmetic operator (see
Week 2s lecture slides).
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 37
38Tracing Program Code and Pseudo Code (cont)
- The best way to trace through even the most
complex pseudo code (or program code) is to use a
multi-columned table to keep track of the values
of relevant variables at particular points during
the processing. - e.g. at the start and end of loops, before and
after if tests, etc. - Example of the table that could be used for this
exercise on the next screen.
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 38
39Tracing Program Code and Pseudo Code (cont)
Intera-tion Value of "number" at top of loop Num 3 Num 4 Value of "number" at end of loop Output So Far
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 39
40Tracing Program Code and Pseudo Code (cont)
Intera-tion Value of "number" at top of loop Num 3 Num 4 Value of "number" at end of loop Output So Far
Before 10,
1 10 1 2 11 10, 11,
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 40
41Tracing Program Code and Pseudo Code (cont)
Intera-tion Value of "number" at top of loop Num 3 Num 4 Value of "number" at end of loop Output So Far
Before 10,
1 10 1 2 11 10, 11,
2 11 2 3 12 10, 11, 12,
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 41
42Tracing Program Code and Pseudo Code (cont)
Intera-tion Value of "number" at top of loop Num 3 Num 4 Value of "number" at end of loop Output So Far
Before 10,
1 10 1 2 11 10, 11,
2 11 2 3 12 10, 11, 12,
3 12 0 n/a 14 10, 11, 12, 14,
4 14 2 2 15 10, 11, 12, 14, 15,
5 15 0 n/a 17 10, 11, 12, 14, 15, 17,
6 17 2 1 18 10, 11, 12, 14, 15, 17, 18,
7 18 0 n/a 20 10, 11, 12, 14, 15, 17, 18, 20,
8 20 2 0 25 10, 11, 12, 14, 15, 17, 18, 20, 25,
Num 25, so loop exits. Num 25, so loop exits. Num 25, so loop exits. Num 25, so loop exits. Num 25, so loop exits. Num 25, so loop exits.
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 42
43Tracing Program Code and Pseudo Code (cont)
- So, the output would be
- 10, 11, 12, 14, 15, 17, 18, 20, 25,
- And there would be a total of 8 iterations of
loop. That is, the code in the body of the loop
would be executed 8 times.
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 43
44Batch Programming
- In Batch Programming, the processing flow is
determined by the programmer. - All of the Console Applications we develop in
this course are batch programs. - When they are executed, Batch Programs step
through the exact sequence of steps that the
programmer coded. - With a Batch Program, if the user enters invalid
input, there are really only 2 options - Display an error and exit.
- Display an error and ask the user to try again.
That is, keep looping asking for input until
valid input is entered.
45Event Driven Programming
- Event Driven Programs sit there doing nothing
until the user clicks on a button, a menu option,
etc and then they leap into action and do their
processing, such as validating inputs, performing
calculations, generating output, etc. - Most GUI programs are Event Driven Programs.
- All GUI Programs in this course are Event Driven
Programs. - That is all of the Java Applets and GUI
Applications in this course are Event Driven
Programs.
46Event Driven Programming (cont)
- For example, in an Event Driven Program, the user
could click the Save button, or they could
click the Calculate menu option, or they could
click on a check box and each of these actions
could initiate different processing and provide
different results. - The Event Driven Program is responding to events
and dealing with them as the events happen.
47Event Driven Programming (cont)
- With an Event Driven Program, if the user enters
invalid input, there is really only 1 option - Display an error message and STOP.
- That is, the user can then fix their input data
and click the button again, or they may decide to
click on something else or do something else. - With Event Driven Programs, there is no forcing
the user down a single road. e.g. they dont
loop until valid data is entered.
48Event Driven Programming (cont)
- That is the beauty and simplicity of event driven
programs - They do nothing until the user clicks something.
- If user input is required, then they check the
input and either continue if all is OK, or
display an error message and stop if an error is
found. - This is completely different to what happens in a
Batch Program.
49Validation Strategies A Deeper Look
- We will now review what we have already covered
with Data Validation and explore things more
deeply. - When validating a lot of input data, it is easy
to end up with a mess of nested ifs. - We will now look at simple techniques to help us
perform the validation of any number of inputs
and avoid having too many nested if statements or
ending up with a mess of code.
50Validation Strategies (cont)
- Remember (from Batch and Event Driven Programming
slides earlier) - If an invalid input is encountered by an Applet
or a GUI Application, then we want to display an
error message and then STOP. - Note STOP does NOT mean exit the program, it
means wait for the user to do something. e.g.
wait for them to fix the input and click the
button again. - If an invalid input is encountered by a Console
Application, then we want to display an error
message and then ask the user to input the data
again.
51Validation Strategies Batch Programs
- // Validation Strategy Batch Programs
- // Author Mike O'Malley
- // Note This is all pseudo code ! This is NOT
Java code. - // If converted to Java, the resulting code could
be running, for example, - // in the main method for a Console Application.
- // The user cannot proceed until valid inputs are
entered at each stage !! - boolean Valid_Input false // Assume First
Input is invalid. - while (Valid_Input false) // Loop until FIRST
input is valid - Get the FIRST input item.
- Check the input is valid - data type check,
range check, or whatever is needed. - if data is not valid
- display an error
- else
- Valid_Input true
- end if
- end loop
52Validation Strategies Batch Programs (cont)
- Valid_Input false // Assume Second Input is
invalid. - while (Valid_Input false) // Loop until
SECOND input is valid - Get the SECOND input item.
- Check the input is valid - data type check,
range check, or whatever is needed. - if data is not valid
- display an error
- else
- Valid_Input true
- end if
- end loop
- etc for all other inputs
- if (Valid_Input true) // Are all inputs valid
?? - Do whatever calculations and processing are
needed. - Display any results.
- etc.
53Validation Strategies Batch Programs (cont)
- As you can see, the program will only proceed to
the next step in the processing once the current
input has been validated. - Keep this strategy in mind for all Console
Applications.
54Validation Strategies Event Driven Programs
- // Validation Strategy Event Driven Programs by
Mike O'Malley - // Note This is all pseudo code ! This is NOT
Java code. - // If converted to Java, the resulting code could
be running, for example, - // in an Applet's actionPerformed method,
responding to mouse clicks on a button. - boolean Valid_Input true // Assume inputs are
valid and try to disprove this. - if (Valid_Input true)
- Get the FIRST input item.
- Check the input is valid - data type check,
range check, or whatever is needed. - if data is not valid
- display an error
- Valid_Input false
- end if
- end if
- if (Valid_Input true) // Are all inputs
valid so far ?? - Get the SECOND input item.
- Check the input is valid - data type check,
range check, or whatever is needed.
55Validation Strategies Event Driven Programs
(cont)
- etc for all other inputs
- if (Valid_Input true) // Are all inputs valid
?? - Do whatever calculations and processing are
needed. - Display any results.
- etc.
- end if
- // end of method.
56Validation Strategies Event Driven Programs
(cont)
- As you can see, once any inputs are invalid, no
other if tests are entered so none of the
processing they contain is done. - The method being executed (such as
actionPerformed) reaches the end, and the program
sits there waiting for the user to do something
else, such as fix inputs and click the button
again. - Keep this strategy in mind for all Applets and
GUI Applications.
57Exception Handling A deeper Look
- We will now review what we have already covered
for Exception Handling and explore things more
deeply. - Anything that could cause your program to crash
or produce ugly error messages (that the user
should not be exposed to) should be placed in a
try ... catch block.
58Exception Handling A deeper Look (cont)
- For example, code for
- Converting strings to floats, ints, etc
- Performing calculations (where there could be
division by zero, etc), - Or any other situation where the code could cause
the program to crash or display ugly errors to
the user. - should go in try ... catch blocks.
- Also, if we were writing more advanced programs,
reading and writing files, connecting to and
querying databases, etc then all of this code
would go inside try ... catch blocks.
59Exception Handling A deeper Look (cont)
- However, if you put all of the conversions in a
single try ... catch block, then you end up
with "generic error messages which are not
ideal. - For example
- try
-
- Weight Double.parseDouble (weightField.getText
()) - Height Double.parseInt (heightField.getText()
) - // etc for other data items
-
- catch (NumberFormatException e)
-
- // Generate a nasty "generic" error message !
- outputLabel1.setText("You must enter a valid
amount.")
60Exception Handling A deeper Look (cont)
- Splitting this into separate try ... catch ...
blocks would be slightly better - try
-
- Weight Double.parseDouble (weightField.getText
()) -
- catch (NumberFormatException e)
-
- outputLabel1.setText("Please enter a valid
Weight.") -
- try
-
- Height Double.parseInt (heightField.getText()
) -
- catch (NumberFormatException e)
-
- outputLabel1.setText("Please enter a valid
Height.")
61Exception Handling A deeper Look (cont)
- However, the error messages are still very
uninformative, and there is not range checking. - A better solution is to also do the range
checking inside try block and manually throw an
exception (to execute all of the code in the
catch block) if needed. - For example, lets assume the Weight must between
10 and 200 see below.
62Exception Handling A deeper Look (cont)
- try
-
- Weight Double.parseDouble (weightField.getText
()) - if ((Weight lt 10) (Weight gt 200))
- throw new NumberFormatException()
-
- catch (NumberFormatException e)
-
- outputLabel1.setText("You must enter Weight in
KG between 10 and 200.")
63Exception Handling A deeper Look (cont)
- So, the error message will appear if they enter 1
or 270 or abcd and so on. - We could (and should !) improve this code further
by using Global Constants to hold the Min and Max
values for each input value as we have done in
prior lectures. - See last weeks lecture material.
64Exception Handling A deeper Look (cont)
- One problem with the above code is that you end
up with a try catch blocks for each input
item. - If you have a dozen inputs, then you might need a
dozen (or so) try catch blocks. - So, a better way is to create a User Defined
Method for each type of input data and call this
for each input of that type. - For example, for double checking, the method
could be called something like Get_Valid_Double.
65Exception Handling A deeper Look (cont)
- We would need to pass such a method the data to
allow it to do its job, which might include - Min value allowed
- Max value allowed
- The name we want to be displayed in prompts,
error messages, etc (if required). - And anything else that might be required.
- We could call this method like this
- Weight Get_Valid_Double ("Weight (Kg)", 10.0,
200.0) - Height Get_Valid_Double ("Height (m)", 1.0,
2.2)
66Exception Handling A deeper Look (cont)
- Your Get_Valid_Double method needs to be able to
signal to a calling method when invalid input was
entered, so why not return a value less than the
minimum allowed ? - So, for example, you might do this
- Weight Get_Valid_Double ("Weight (Kg)", 10.0,
200.0) - if (Weight lt 10.0)
-
- // Oops, the input wasn't valid ...
-
67Exception Handling A deeper Look (cont)
- What might the implementation of this method look
like ? Heres one possibility (no Java code
supplied just comments and hints) - private double Get_Valid_Double (String
Data_Name, - double Min_Value, double
Max Value) -
- // Get the data (e.g. from the user)
- // Check it isnt blank
- // Type check it. e.g. convert input string
to a double. - // Range check the data against Min and Max
values. - // Hint A try ... catch ... block is ideal
for this !! - // If input data is invalid, display an
informative - // error message and then return a value which
is less - // than the Minimum allowed to signal to the
calling - // program that an error occurred.
68Exception Handling A deeper Look (cont)
- If you want to pass the string data that was
entered by the user to the method, how could you
do this ? Heres one possibility - First, we add another String parameter
- private double Get_Valid_Double (String
Data_Name, - String Input_String,
- double Min_Value, double
Max Value) - And then we need to pass the extra string in each
function call, for example - Weight Get_Valid_Double ("Weight (Kg)",
- Weight_TextField.getText (), 10.0,
200.0) - See this weeks tutorials !!
69Arguments Vs Parameters
- Arguments are variables and values that are
provided in the calling method and are used
within a method call. Examples - My_Method (33, "Fred", 3.2)
- My_Method (In_Age, In_Name, In_BSL)
- GST_Val Calculate_GST (125.0)
- GST_Val Calculate_GST (Total_Excl_GST)
- Red text above indicates the Arguments.
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 69
70Arguments Vs Parameters (cont)
- Parameters are variables defined in the methods
header that receive the Argument values when the
method is called. - private void My_Method (int Age, String Name,
double BSL) -
- // Body of method
-
- private double Calculate_GST (double Tot_Excl)
-
- // Body of method
-
- Red text above indicates the Parameters.
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 70
71Assignment 1 100
- You can now complete 100 of Assignment 1.
- Before you attempt to finish Assignment 1, make
sure you - Thoroughly read, review, and explore all topics
discussed in the lecture material for Weeks 1-5. - Do all of the tutorial questions for Weeks 1-5.
- Follow the Development Hints and Tips in the
assignment specification (Course Profile). - Students who tried to avoid these activities in
the past have found that the Assignment took them
far longer to finish !! e.g. 70 hours instead of
5 hours.
COIT 11222 - Visual Programming Author(s) Mike
OMalley Slide 71
72Summary of Topics Covered
- Review and Revision
- Review of Java Applets.
- Validation Strategies A Deeper Look
- Exception Handling A Deeper Look
- Unary and Assignment Operators
- Loops - for and while
- Tracing Program Code and Pseudo Code
- Batch Programs Vs Event Driven Programs
- Arguments Vs Parameters
- Assignment 1 100
73End of Lecture