Download Feb11

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java 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.
2.
3.
4.
5.
Make the dough
Make the pizza sauce
Put sauce on the pizza
Put toppings on the pizza
Cook the pizza
Java Methods
So when we say “make a pizza” we mean do all those
instructions. Lucky for us we don’t 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!
Java 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.
Java 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 this
public void paint (Graphics screen)
{
}
Java 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
…
*/
}
Java 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);
The 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
• Graphics
• paint
• init
• Color
Java Applet Template (so we can create Applets)
Java Applet Variable (lets us draw to the screen)
Java Applet method
Java Applet method
Variable
No need to take notes here.
Java Download Websites
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
JDK 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.