Title: Java Methods
1Java Methods
- Methods contain a group of instructions that
together perform a single task. For example if I
want to perform the task of making a pizza, I
would have to do the following - 1. Make the dough
- 2. Make the pizza sauce
- 3. Put sauce on the pizza
- 4. Put toppings on the pizza
- 5. Cook the pizza
2Java Methods
- So when we say make a pizza we mean do all
those instructions. Lucky for us we dont talk
using such detail because conversations would
take for ever!
If it takes one instruction to draw a single line
how many instructions does it take to draw one of
these polygons? Answer 6. How about 100
polygons? Answer 600. A Lot!
3Java Methods
- A Different approach to this polygon drawing
problem is to create a method that draws an
entire polygon. We can then use that method to
draw as many polygons as we want. Methods cut
down the number of instructions we have to write.
They also make our programs a lot easier to read
if we place a set of instructions inside a
method, the method name will indicate what all
these instructions actually do. Being able to
group instructions in this manner also makes our
program easier to debug.
4Java Methods
- // Comment General structure of a Java
method
public void method_name () // instruction
1 // instruction 2 // instruction 3
// // we can use our method inside the
paint like thispublic void paint (Graphics
screen)
5Java Methods
Our method for drawing a polygon would look like
this. public void drawPolygon ()
/ instruction for drawing line
1 instruction for drawing line
2 instruction for drawing line 3
/
6Java Methods
- Methods contain a group of related
instructions for the computer. For the time being
we will be using methods that already exist in
Java. Later in the course we will be creating
our own methods from scratch. Some example of
already defined Java methods are
- drawString(Anything I want goes here, x, y)
- drawRect(width, height, x, y)
- setColor(Color.blue)
7The import Statement
- Many useful Java Applet methods and variables
are located in pre-made packages. If a programmer
wants to use one of these methods or variables he
or she must first import the package that it
belongs to. Otherwise the compiler will not know
where that special word came from. Import
statements are used to import these useful
packages. Examples of some Java special words we
will be using are
- Applet Java Applet Template (so we can create
Applets) - Graphics Java Applet Variable (lets us draw to
the screen) - paint Java Applet method
- init Java Applet method
- Color Variable
8Java Download Websites
No need to take notes here.
Java Developers Kit (Compiler)
http//java.sun.com/j2se/1.4.2/download.html
JCreator A Java Text Editor (For Windows)
Be sure to select JCreator LE version
http//www.jcreator.com/Download.htm
Instructions on installing JDK and JCreator
http//www.cs.nyu.edu/courses/fall03/V22.0101-002/
instruc.html
9JDK Java Development Kit
- The JDK is a free compiler for Java offered
by Sun Microsystems. Project Builder is the
visual interface for the compiler. In other words
Project builder is a program that allows us to
use the JDK in an easy (user-friendly) fashion.
Project Builder provides us with, windows,
buttons, command icons, text-editor etc.
Otherwise we would have to use the command line
to compile our Java source code. The command to
compile Java source files using just the JDK
(with no Project Builder) would be
javac mySourceFile.java
Then to run your Applet you would type
appletviewer MyHtmlFile.html
Or you could simply open your HTML file with a
browser.