Week 11 Lecture

Download Report

Transcript Week 11 Lecture

COIT 11222 – Visual Programming
• Lecture:
Week 11
References:
• Java Programming - Complete
Concepts and Techniques,
3rd Edition, Shelly Cashman et al.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 1
Topics For This Week
•
•
•
•
•
•
•
Instance and Driver Classes
Public Vs Private
Arrays and Classes
Java Hints and Tips
Applets with a main
Instance classes with a main
Other Useful Functions
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 2
Topics For This Week (cont)
•
•
•
•
•
Useful Functions - Strings
Useful Functions - TextField
Useful Functions - TextArea
Useful Functions - Window Width and Height
Next Week = Week 12, Practice Exam, and
Exam Hints and Tips
• Week 13 + 14 (Revision and Exams)
• End of Course + Your Feedback
• Topics for the Future
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 3
Instance and Driver Classes
• Here is a UML Class Diagram for our
instance and driver classes (from last week's
lecture, after being extended according to last
week's tutorial questions):
W10_01_Bank__App
-My_Account: W10_01_Bank_Account
+W10_01_Bank__App ()
+actionPerformed (ActionEvent): void
+main (String): void
We even had some
Polymorphic methods
COIT 11222 - Visual Programming
W10_01_Bank_Account
-Acct_Name: String
-Acct_Balance: double
+W10_01_Bank_Account ()
+Get_Acct_Name (): String
+Get_Acct_Balance (): double
+Set_Acct_Name (String): void
+Deposit_Money (String): void
+Deposit_Money (double): void
+Withdraw_Money (String): void
+Withdraw_Money (double): void
Author(s): Mike O’Malley
Slide: 4
Public Vs Private
• We will now revisit public and private data to
highlight data security again (we began
discussing this way back in Week 3 and 4).
• We could make the class data in our instance
class public instead of private as follows:
public class W10_01_Bank_Account
{
public String Acct_Name;
public double Acct_Balance;
// Methods not shown (they are same as before).
} // W10_01_Bank_Account
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 5
Public Vs Private (cont)
• Now, because the class data is public, every other
class that uses this class has full access to its data.
• For example, in the actionPerformed method in our
Driver Class, we could set the account balance:
My_Account.Acct_Balance = 1000000000.0;
• Or, we could set the balance to a huge negative
number:
My_Account.Acct_Balance = -1000000000.0;
• Or, we could increase the balance:
My_Account.Acct_Balance
= My_Account.Acct_Balance + 9999.0;
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 6
Public Vs Private (cont)
• Or, our code could increase the balance only
for some account holders:
if (My_Account.Acct_Name.compareTo ("Mike") == 0)
{
My_Account.Acct_Balance
= My_Account.Acct_Balance + 999.0;
}
• Or, we could set the Account Name to
whatever we like, for example:
My_Account.Acct_Name = "12345"; // Illegal name ??
• Or:
My_Account.Acct_Name = "";
COIT 11222 - Visual Programming
// A blank account name !!
Author(s): Mike O’Malley
Slide: 7
Public Vs Private (cont)
• This is a disastrous and very serious
situation !!!
• All of our data security is gone.
• All of the validation rules enforced by Bank
Account class methods have been bypassed
because the class data is public.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 8
Public Vs Private (cont)
• Exercise:
– How long would a bank be in business if they
developed their classes with public data ?
– How long until it was bankrupt or the law suits
started flooding in ?
• As strongly recommended in Week 4's
lecture material:
– Declare all class data as private unless there is
*very* good reason for it to be public.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 9
Public Vs Private (cont)
• OK, so you should now understand the
security about class data.
• But, what about class methods ?
• Imagine an Apply_Interest () method that
should only be invoked once a month (on a
certain day), but the method is public.
• The Driver Class could do this:
for (int k = 0; k < 1000; k++)
My_Account.Apply_Interest ();
• effectively giving the account 1000 months
worth of interest in the blink of an eye.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 10
Public Vs Private (cont)
• As you can see, method security can be just as
important as data security.
• As strongly recommended in Week 4's lecture
material:
– Declare all of the User Defined Methods you create
as private unless there is *very* good reason for
them to be public.
• In addition, if you have special methods, for
example, that are only supposed to be run once at a
certain time, day, or by a certain user, from a certain
location, etc, then the method should verify that
these details are 100% correct before proceeding.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 11
Arrays and Classes
• Last week we looked at a Bank Account class
which could deal with a single account.
• If we wanted to deal with 100's (or more) of
Bank Accounts, then it would be logical to
store the data in an array.
• But, where should we put the array ?
• If other applications also needed an array of
Bank Accounts, then we could put the array
in our Instance Class.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 12
Arrays and Classes (cont)
• If only one application needs an array of Bank
Accounts, then we could put the array in
either of our classes: the Driver Class or the
Instance Class.
• If we put the array in our Bank Account
(instance) class, and an application only
needs one account, then that is fine - just
create an array of size 1.
• If many accounts are needed, simply add
more to the array.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 13
Arrays and Classes (cont)
• We could also follow a third strategy:
– Create another class to sit between the other two classes
and this contains the array.
• This is the best option all around, because all of the
data and functionality is placed into correct logical
groupings:
– The Bank Account data and functionality is in the Bank
Account class.
– The array functionality (add, delete, sort, etc) is in the
Array Class.
– The user interface creation / setup is in the Driver Class,
which calls methods in our other classes as required.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 14
Arrays and Classes (cont)
• Also, if we build the array class properly, then it
should be very easy to make it work with any type of
data:
–
–
–
–
–
Animals
Students
Insurance Claims
Products for Sale
etc
• So, the code should be highly re-usable for any
data type.
• This is a huge advantage !!
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 15
Arrays and Classes (cont)
• Whatever we do, we are going to need to
change our existing methods (or method
calls) to work with the array (or array class),
and add more methods (and buttons).
• For example:
– Add Account: to add an account to the array.
– Delete Account: to delete an account from the
array.
– Sort Accounts: to sort accounts by name or
balance.
– Save to / Load from file / database.
– etc
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 16
Arrays and Classes (cont)
• Here is a UML Class Diagram of the kind of
setup we might end up with:
W10_01_Bank__App
-My_Bank: Array_Class
+W10_01_Bank__App ()
+actionPerformed (ActionEvent): void
+main (String): void
Array_Class
-My_Array: W10_01_Bank_Account
-Num_Items: integer
+Add_Item (…)
+Delete_Item (…)
+Sort_Array (…)
etc
COIT 11222 - Visual Programming
W10_01_Bank_Account
-Acct_Name: String
-Acct_Balance: double
+W10_01_Bank_Account ()
+Get_Acct_Name (): String
+Get_Acct_Balance (): double
+Set_Acct_Name (String): void
+Deposit_Money (String): void
+Deposit_Money (double): void
+Withdraw_Money (String): void
+Withdraw_Money (double): void
Author(s): Mike O’Malley
Slide: 17
Arrays and Classes (cont)
• Note: We could also use this design strategy
instead of parallel arrays for array-driven
applications like Assignment 2.
• Sadly, that's as far as we go on this topic in
this course.
• Further exploration of this topic will be left to
future studies / follow on courses.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 18
Java Hints and Tips
• In this week’s remaining slides, we cover
some useful hints and tips that will help you
make your Java programs even better.
• Recommendation: Keep these in mind for
future use.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 19
Applets with a main
• If you attempt to run an Applet as a Java
Application, you will get a "Exception in thread
"main" java.lang.NoSuchMethodError: main" error
message.
• For example:
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 20
Applets with a main (cont)
• We can improve this so that the user gets a
more meaningful and friendly message.
• We do this by adding a main () method to our
Applet.
• The main () method will not be executed
when the Applet is run in a Browser.
• It will only be executed if someone attempts
to run the Applet as a Java application.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 21
Applets with a main (cont)
• If we have this main () in our Applet:
public static void main (String[] args)
{
System.out.println ("This is an Applet" +
" - please run it in a WEB Browser.");
} // public static void main (String[] args)
• then we will see this user friendly message if
we try and run it as a Java Application:
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 22
Instance classes with a main
• Similarly, Instance Classes are often not
meant to be run on their own.
• Rather, they are meant to be used by Driver
Classes that provide the user interface, etc.
• If we attempt to run an Instance Class as a
Java Application, we get the same error as
before:
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 23
Instance classes with a main (cont)
• We can improve this so that the user gets a
more meaningful and friendly message.
• We do this by adding a main () method to our
Instance Class.
• The main () method will not be executed
when the Instance Class is used by a Driver
Class.
• It will only be executed if someone attempts
to run the Instance Class as a Java
application.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 24
Instance classes with a main (cont)
• If we have something like this main () in our
Instance Class:
public static void main (String[] args)
{
System.out.println ("This is an Instance " +
"Class - it cannot be run on its own.");
System.out.println ("This class is only " +
"meant to be used by a Driver Class.");
} // public static void main (String[] args)
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 25
Instance classes with a main (cont)
• We will see this user friendly message if we
try and run it as a Java Application:
• Some users may not understand exactly what
this means, but it is still a lot better than an
ugly Java error message.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 26
Other Useful Functions
• So far in this course, to keep things as simple
as possible, we have covered just a few of
the 1,000’s of methods that Java provides.
• Always keep on the lookout for new and
useful methods.
• We will look at a few more useful functions on
the next slides …
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 27
Useful Functions - Strings
• When we import the following package:
import java.lang.String;
• we get access to a whole range of useful
String functions, such as:
– charAt(int)
• Returns the character at the specified index.
– compareTo(String)
• Compares this string with another string. (Returns an
integer depending on how the strings compare).
– compareToIgnoreCase(String)
• Compares this string with another string, case is
ignored.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 28
Useful Functions - Strings (cont)
– equals(String)
• Compares this string with another string. (Returns true
or false depending of whether the strings are equal).
– equalsIgnoreCase(String)
• Compares this string with another string, case is
ignored. (Returns true or false – see above).
– indexOf(int)
• Returns the index within this string of the first
occurrence of the specified character.
– length()
• Returns the length of this string.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 29
Useful Functions - Strings (cont)
– replace(char, char)
• Returns a new string resulting from replacing all
occurrences of oldChar in this string with newChar.
– substring(int, int)
• Returns a new string that is a substring of this string.
– toLowerCase()
• Converts this String to lowercase.
– toUpperCase()
• Converts this string to uppercase.
– trim()
• Removes white space from both ends of this string.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 30
Useful Functions - TextField
• As well as the functions we have already
looked at, there are many other TextField
functions that are useful, such as:
– setEchoChar(char)
• Sets the echo character for this text field.
• Example:
Emp_ID_TextField.setEchoChar('*');
• An echo character is useful for text fields
where user input should not be echoed to the
screen, as in the case of a text field for
entering a password.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 31
Useful Functions - TextArea
• As well as the functions we have already
looked at, there are many other TextArea
functions that are useful, such as:
– append(String)
• Appends the given text to the text area's current text.
– getRows()
• Gets the number of rows of height of a text area.
– insert(String, int)
• Inserts the specified text at the specified position in
this text area.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 32
Useful Functions - TextArea (cont)
– replaceRange(String, int, int)
• Replaces text between the indicated start and end
positions with the specified replacement text.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 33
Useful Functions - Window Width
and Height
• Two very useful methods, which work with Applets
and Windowed Applications are:
– getHeight ()
• Return the height of the window.
– getWidth ()
– Return the width of the window.
• Example:
Output_Label.setText ("Width = " + getWidth() +
", Height = " + getHeight());
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 34
Useful Functions - Window Width
and Height (cont)
• These functions can be very useful.
• For example, you can display the values returned
by these methods in a label (or in a Swing Dialog,
etc) when a button is clicked.
• For an Applet, you could do this when you get the
Window size just how you want it, so that you know
what width and height parameters to put in an
Applet’s HTML file.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 35
Useful Functions – beep
• When we import the following package:
import java.awt.Toolkit;
• We can make our program emit a beep sound:
Toolkit.getDefaultToolkit().beep();
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 36
Other Useful Functions Conclusion
• There are many 1,000’s of others !!
• It is a good idea to keep a list of these handy
functions, and add to your list when you find
new ones that are useful.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 37
Next Week = Week 12, Practice
Exam, and Exam Hints and Tips
• In next week's lecture timeslot, we will work
through a Practice Exam.
• Teaching staff will *NOT* be providing
solutions to any past or practice exam
questions.
• The emphasis will be on students
participating and suggesting what should be
written next.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 38
Next Week = Week 12, Practice
Exam, and Exam Hints and Tips(cont)
• Instructions to Teaching Staff:
– Please make sure you emphasise the importance
of reading the question. This is where way too
many students make mistakes !!
– Teach them to underline keywords.
– Tell them "Only answer what the question is
asking."
– If students are stuck, and can't answer a
question, then suggest something that may
sound right (at first glance) but is wrong and ask
the students to analyse and correct this.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 39
Next Week = Week 12, Practice
Exam, and Exam Hints and Tips(cont)
• Instructions to Internal Students (and/or those who have
/ attend the Week 12 lecture):
– Read the Exam Hints and Tips on the course web
page.
– Make sure you have made a very good attempt at the
Practice Exam before you attend next week's lecture.
– There will be NO free rides. Answers will not be
given out by teaching staff, and those who don't do
the work simply wont learn.
– To get the most out of this session, it is vitally
important that you have made very good attempts at
answering all questions before attending the lecture.
– Check your answers by writing little Java programs
and compiling and running these.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 40
Next Week = Week 12, Practice
Exam, and Exam Hints and Tips(cont)
• Instructions to External Students (and any others who do not
have / attend the Week 12 lecture):
– Read the Exam Hints and Tips on the course web page.
– Work through the Practice Exam during Week 12.
– Check your answers by writing little Java programs and
compiling and running these.
– If you have any questions or aren't sure of anything, then
please email the Course Mailing List.
– Yes, students can discuss Practice Exam questions on
the Course Mailing List - this is not assessment, but
practice for assessment.
– Make sure you do this well before the exam.
– Do NOT leave it until the Exam Period to start revision, or
attempt the Practice Exam, or seek the help you need.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 41
Week 13 + 14 (Revision and Exams)
• After Week 12, there are no lectures or tutes.
• If you have any questions, then :
– Email or visit your campus lecturer / tutor or the
Course Coordinator,
– Email the Course Mailing List,
– Read the Exam Hints and Tips on the course
web page.
– Do NOT leave it until the Exam Period to start
revision, or attempt the Practice Exam, or seek
the help you need.
– Revision should be Revision – not covering new
material for the first time.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 42
End of Course + Your Feedback
• That's everything we cover in this course.
• We hope you enjoyed the course and found
the course material useful.
• If you have any constructive feedback /
comments (good or bad) about the course or
its materials, please let us know via any or all
of these methods:
– Course Survey / Teaching Survey
– Course Barometer (on Course WEB Page)
– Email to Course Coordinator
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 43
Topics for the Future
• This term, we've only just barely scratched the
surface of the basics of Java.
• Lots of very interesting topics lie ahead for those
who proceed with Java programming, such as:
– Advanced GUI Application Applets (program can be both
an Application and an Applet)
– Graphics, sound, animation, games, etc
– File I/O
– Database / SQL
– Web Server applications
– Networking
• Note: Only some of these topics may be covered in
CQ University's courses.
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 44
Summary of Topics Covered
•
•
•
•
•
•
•
Instance and Driver Classes
Public Vs Private
Arrays and Classes
Java Hints and Tips
Applets with a main
Instance classes with a main
Other Useful Functions
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 45
Summary of Topics Covered (cont)
•
•
•
•
•
Useful Functions - Strings
Useful Functions – TextField
Useful Functions – TextArea
Useful Functions - Window Width and Height
Next Week = Week 12, Practice Exam, and
Exam Hints and Tips
• Week 13 + 14 (Revision and Exams)
• End of Course + Your Feedback
• Topics for the Future
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 46
End of Lecture
Quack, Quack !!
Hope you enjoyed
the course !!!
COIT 11222 - Visual Programming
Author(s): Mike O’Malley
Slide: 47