Download Unit3

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
Agenda For Feb 11
1. PowerPoint Presentation (Introduction to Java Methods)
2. Finish Unit 2 exercises on page 13 (due by the end of the
class today).
3. Assignment 2 (Also to be handed in by the end of Friday).
4. If you finish all the above read Unit 3 carefully and try
•
Section 3.1 Mastery Questions 1, 2, & 3 (page 18)
•
Section 3.2 Mastery Question (page 19)
•
Unit 3 Exercises 1-3 (page 20)
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?
100 of them?
Java Methods
A Different approach to this problem is to create a
method that draws one polygon. We can then use
that method to draw as many polygons as we want.
This will cut down the number of instructions
we’d have to write.
Java Methods
General structure of a Java method:
public void method_name ()
{
// instruction 1
// instruction 2
// instruction 3
// …
}
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
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