Void methods

Chapter: Void methods

In Alice you met methods and functions. Alice methods correspond to void methods in Java. They are "for effect" only: they cause something to happen (in Alice, some movement perhaps; in Java, the printing of a message perhaps) and do not return any value for the caller to use. Alice functions correspond to non-void methods in Java. Their primary purpose is to compute some value that will be returned to be used by the caller. non-void methods in Java need a return statement.

Study the program in Cross.java. You will notice:

Be sure to create a project workspace for lab9.


Exercise 1

Adapt the cross-drawing program above so that it draws a face. The user should be presented with a JFrame like this:

and on providing a value and clicking the button should see a better face than:

Hint: Look at the documentation for the Graphics class. Look for a method that can draw a circle (remember that a circle is a special case of an ellipse). The arguments you supply are the coordinates of the TOP LEFT corner of the oval's bounding rectangle, and the width and height of the oval (the width and height will be equal for a circle, of course).

Name your class Ex1. This will require you to change all occurrences of Cross to Ex1 and to save the file as Ex1. Ask your lab instructor if you are not sure how to do this.



rhyspj@gwu.edu