Look at both TriangleMethodDemo.java with its html page
Could you define another method for making triangles just giving three points? Discuss
Look at the code for lab 4:
ComputeFigure.java and trace it. Methods
(these notes are not in the text)
We call this invoking or calling the method.
g.drawstring("Hello World", 50, 50); is invoking the
method drawString on the instance g of the class
Graphics
Look at both TriangleMethodDemo.java with its html page
and consider these issues:
formal and actual
parameters
difference between method Name and method Signature
difference between overload and overridden
Local Variables
Sometimes it is easier (and/or more clear what is happening) to define new variables inside of a method return and results
To get results out of a method (the text calls these queries as compared to commands) we need the return
Notice that when a call to a method returns something, you
cannot just call the method with
areaRectangle(30,40);
Why not?
Building on methods
We have a handy drawTriangle method. Use it to build a house.
and its html page
Methods return to who they were invoked from. So, trace the program.
Here is the code numbered so we can follow the
line numbers for the trace
We now have the understanding to provide a basic template that all classes fit. One last thing though:
Java programs that do not run on a browser (i.e., not Applets) need a main method to get them started. The main method
The book focuses on these (applications), but I will continue to show both.
imports ...
public class ClassName extends super {
// Declare and possibly set values for variables
Instance and Class Variable declarations
private int instanceVariable;
public static int classVariable;
// Define Constructors (if none - looks at supers. No return type)
public ClassName (formal parameters) {
code
}
// Define methods with no returned values
public void methodName (formal parameters) {
code
}
// Define methods with return values
public returnType methodName (formal parameters) {
code
return ... // returnType of value returned must match method signature
}
}
Text examples: code and exercises Ch 3,
exercises Ch4