Only declare method public if intended to be used by clients (external objects)
Notice can refer to instance variables within methods without passing as parameters (global to class). Can also refer to instance variables as components of "this". E.g., this.top
Keyword "this" refers to object executing method
Notice HourlyEmployee and ExemptEmployee declared as subclasses using "extends".
then create via myEmployee = new Employee(...);
where Employee(...) is a constructor for Employee class.
Constructor must have same name as class
Constructors can be overloaded so can handle different collections of parameters.
Note distinction between "static" and "dynamic" type of object.
If variable declared with a static type of, say "Employee", but want to instantiate as "HourlyEmployee", then write:
myEmployee = new HourlyEmployee(...);In general can always use object of a subclass in place of object of superclass.
Equality test (==) tests if identical references,
I.e., if fst, snd : Employee, and execute: fst = snd;
then change to fst.Name will change snd.Name.
Typically create a new object using a class constructor or use clone method inherited from Object (see pg 64 of text).
Object also has equals method.
Unfortunately clone always returns Object, which is superclass of all other classes.
Must downcast in order to actually use it & might throw exception.
Talk about in more detail later when discuss exceptions.
Can assign an object of subclass to variable of superclass (but not vice-versa).
Can also pass an object of subclass as parameter where expect object of superclass.
E.g.
public void DoSomething(Employee thisEmployee,... ); begin : System.out.println(thisEmployee.GetName() + " makes " + thisEmployee.GetWkPay()); : end;If pass in a parameter of class Employee, then print out 0.
If pass in a parameter of class HourlyEmployee, then print out 40 * HourlySalary.
If pass in a parameter of class ExemptEmployee, then print out yearlySalary / 52.
Uses dynamic method-lookup of message to find corresponding method of object.
E.g., if emp is type Employee, hEmp is type HourlyEmployee, then can write:
emp = hemp; hEmp = emp ; // Illegal assignment - won't compile! hEmp = (HourlyEmployee) emp; // example of downcast // Ok if emp is really HourlyEmployee, raises exception o'wise if (emp instanceof HourlyEmployee) then // Safer alternative - check before casting! hemp = (HourlyEmployee)emp; emp.setHourlyPay(6.25f) // illegal even if emp is HourlyEmployee ((HourlyEmployee)emp).setHourlyPay(6.25f) // OK since did cast
Applets are executable from within web browser. Live on web page.
Can draw on applets - we'll use classes from graphics library and can add predefined GUI (graphic user interface) components to applet.
Calling paint problematic since usually don't have Graphics object in hand. Instead call repaint(); which invokes "update" method (passing it a Graphics object).
The update method erases any drawing done previously and then calls paint with the Graphics object.
Java code for library can be found in CS136 folder on Cider Press. Documentation is available from the CS136 web page.
Interface Renderable provides common interfaces for all classes in the library.
Abstract class RenderAbsClass gives part of the implementation that is common to all classes.
Look at SimpleGraphicsApp in which all painting is done in paint() method.
Note never call paint directly yet it is invoked when program starts up and when screen needs refreshing.
Constructor is:
public Button(String label);when executed creates a button with "label" printed on it. Generally large enough to display label.
Responds to variety of messages. One of most useful is
public void setBackground(Color buttonColor);which changes color of button.
Color is predefined class with many constants representing colors:
Add a button to an applet by writing:
add(startButton);This will normally result in button being added to applet in first available space starting from top left and working across from left to right (and then trying next row, etc.)
Button generates an event when user clicks on it.
Method "action" is executed whenever there is an event (and no other routine executing)
public boolean action(Event evt, Object obj);Event has a number of fields which can be used to identify the kind of event. One of simplest things to do is to identify the object involved with the action by examining the "target" field.
E.g., if Button startButton = new Button("start");
then can write
if (evt.target = startButton) doStart();See program buttons for simple example.
Look at program bouncing ball to see use of button and graphics objects from library.