Methods - top of Musings page

By: Nicholas Duchon

(Since this is a rather long response, I will number the paragraphs.)

(1) Let me start with a short description of the difference between classes and methods, then I will do 5 problems from the methods worksheet associated with Lewis and Loftus, 1 and 23-26.

(2) Classes are the large structures of Java (larger ones are packages and jar files, but we will leave those for later). You have already created classes and have noticed that in simple situations a class is exactly a file. In particular, the name of the (one) public class in a file must be exactly the name of the file - including getting the capitalization correct. By Java coding conventions, class names should start with the first letter capitalized, and the rest of each word in lower case. If more than one word is involved, such as MyPerfectCatClass, the first letter of each word is capitalized.

(3) In a simple case, each class extends exactly one other class, and contains fields (also called instance and static variables) which hold the data of that class, and methods (called functions and/or subroutines in other languages). In this sense, variables hold information (data) and methods are instructions about what to do with the data available.

(4) To get a quick overview, page 4 of G&T shows the syntax of a class declaration, and page 11 shows the syntax of a method declaration. It would be asking a lot for you to understand all of what those diagrams are saying at this point, but I suggest that as we go through the course, you all look at these diagrams occasionally to compare what the text and programs are saying to the diagrams. As you get more used to the diagrams and the information they have, I think you will find the diagrams one of the best and quickest references while you are writing code.

(5) I mentioned that every class must extend some other class - well ... almost. There is only one top-level class, the class Object. All other classes, either directly or indirectly, extend this class. If no "extends" statement follows a class name, then the default understood by the compiler is that the class being declared "extends Object". This will be elaborated in great detail later in this class and every other Java class you will take.

(6) In Java, every method (function/subroutine) lives inside some class and belongs to that class. Java has no "free" methods (read functions or subroutines), such as C++, C, Fortran, Ada, and almost every other programming language you may have heard of. This is part of why java seems particularly complex when one is just starting out.

(7) In a simple case, a method is defined as on G&T page 11:

  1. Start with a modifier - public, private, static, final, and abstract are the most common ones.
  2. The return type, for example, "int" would be a method that returns an int data type; "void" means that this method returns no value at all. If this is a constructor, no type declaration is allowed, since the return type is automatically the class, and the returned object is "this".
  3. The name of the method - by convention, the first letter is lower case, and only the first letter of any additional embedded words are capitalized, so: myFirstMethod, thisIsTheMeaningOfIs, dinosaursWalkedTheEarth would all be conventional method names.
  4. A parameter list. This list may be empty, but ()'s are required. The individual parameters are identified in two ways: their type, and their local names (names that are used inside the method body). The type can be any primitive data type (such as int, float, double, char, etc.), or any previously defined class (such as Object, Applet, String, etc.).
  5. A throws clause - we will discuss this later.
  6. The body of the method - these are the lines of code, each ending with a semicolon, all encased in curly braces, {}'s. If this method returns a value, as declared in part 2 above, the method must end with a statement such as "return i;", assuming that i is an int and the method declaration says the method is of int type.

(8) Just one other point before we proceed to the example: how is a method called? The Java run-time environment (RTE) determines which method to call by considering three things: the class of the object being used, the name of the method, and the type of parameters used in the method call. This first is a little obscure at the moment, but perhaps you have noticed in the programming examples the use of a class along with a method. Take, for example, the program on page 160 (Lewis & Loftus), where Math.random () is called. The referent class is "Math", the method is called "random", and in this case, the method is the no-parameter version. If no class is evident, the default is for the RTE to look in the current class (and its parent classes). Notice that there may be more than one method with the same name in a class, they are distinguished by their parameter lists (also called, together with their class and name, their signature). Also, the same method name can be used in other classes.

(9) This covers the basic theory, let's try to apply it:

1. Write a method called powersOfTwo that prints the first 10 powers of 2 (starting with 2). The method takes no parameters and doesn't return anything.


void powersOfTwo () {
int i; // loop counter
int power = 2; // initialize power to starting value
for (i=1; i<=10; i++) {
System.out.println ("2^" + i + " = " + power);
power *= 2; // multiply current power by 2
} // end for 10 powers of two
} // end method powersOfTwo

Note that to actually do anything with this method, it must be embedded in some class.

(10) For the following problems, we will assume that all of the methods are in the same class, so that they can reference each other without any explicit class indication. Note that the method defined in 23 is the most general, and all of the other problems are setting some default parameters, then calling the method defined in 23.

23. Write a method called drawCircle that draws a circle based on the method's parameters: a Graphics object through which to draw the circle, two integer values that define the (x, y) coordinate of the center of the circle, another integer that represents the circle's radius, and a Color object that defines the circle's color. The method does not return anything.

void drawCircle (Graphics g, int x, int y, int radius, Color clr) {
int top, left; // the top and left side of the bounding box of the circle
top = y - radius;
left = x - radius;
g.setColor (clr);
g.drawOval (left, top, 2 * radius, 2 * radius); // this is the AWT predefined method
} // end method drawCircle (Graphics, int, int, int, Color)

24. Overload the drawCircle method of the previous exercise such that if the Color parameter is not provided, the circle's color will default to black.

void drawCircle (Graphics g, int x, int y, int radius) {
drawCircle (g, x, y, radius,;
} // end method drawCircle (Graphics, int, int, int)

25. Overload the drawCircle method again such that if the radius is not provided, a random radius in the range 10 to 100 will be used.

void drawCircle (Graphics g, int x, int y, Color clr) {
int rad;
rad = Math.random () * 90 + 10; // random number between 10 and 100
drawCircle (g, x, y, rad, clr);
} // end method drawCircle (Graphics, int, int, Color)

26. Overload the drawCircle method yet again such that if both the color and radius of the circle are not provided, the color will default to red and the radius will default to 40.

void drawCircle (Graphics g, int x, int y) {
drawCircle (g, x, y, 40,;
} // end method drawCircle (Graphics, int, int)

(11) Notice that the methods in 24, 25, 26 call the method defined in 23. Pay particular attention to the parameter lists of each of these methods. Note also that these methods do not return a value, therefore there need be no "return" statement.