Title: Class 19 Review
1Class 19 - Review
- This lecture contains a selection of slides from
previous lectures, giving the high points of
the material we have covered. - This selection is intended merely as an outline
of the topics. All material covered in lecture
may be included in the exam. (Exception you
will not be asked to draw a Hilbert curve.)
2Applications vs. applets
- Applications stand-alone programs, usually
invoked from command line.
3Strings
- Strings have many instance methods provided in
the Java API, e.g. - int length ()
- int indexOf (String)
- String substring(int, int)
- String toUpperCase()
- ... lots more...
4Review
- Class methods call using class name and method
name, e.g. Math.sin(...)In documentation, begins
with word static. - Instance methods call using value and method
name, e.g. String s
s.substring(0,4)In documentation, does not begin
with word static.
5Summary
- Classes introduce types. Values with these types
are called objects. - Create objects using new (exception Strings)
- Classes have
- Class methods classname . methodname (args)
- Class variables classname . variablename
6Summary (cont.)
- Objects have
- Instance methods expression . methodname
(args) - Instance variables expression . variablename
- Variables declared inside main (or other methods)
are called local variables. These are different
from class variables and instance variables, and
are referred to simply by their names.
7Methods
- Method named collection of statements
- Can be class method (static) or instance method
- Has return type
- Method call is statement if return type is void
- Method call is expression if return type is not
void
8Conditional statements
- A conditional statement has the form if (
condition ) statement1 else statement2 - It executes statement1 if the condition is true,
statement2 if the condition is false.
9Conditional expressions
- A conditional expression has the form
- condition ? expression1 expression2
- It evaluates the condition and then either
evaluates expression1 or expression2, depending
whether condition is true or false - Note the type of expression1 and expression2
must be the same that type is the type of the
conditional expression
10Recursive methods
- As of now, we lack the ability to do repetitive
actions. Can solve them using recursive methods
A recursive method is one that calls itself.
11Recursive methods in Java
- Recursive method f with argument x has the form
- static typename f (typename x)
- if (x is simple enough)
- solve directly
- else
- use f(value smaller than x) to
calculate f(x) -
12Thinking recursively
- Key ideas
- Know exactly what the method is supposed to do.
- Assume method works for smaller values. Then
just figure out how to use it to compute method
for larger values. - Dont forget base cases - small values that can
be calculated directly.
13Lists
- Lists are a simple data structure in which data
are stored in a row. - We will talk first about lists of integers
- x0, x1, ..., xn-1 (n gt 0)
- Elements can be added and removed only at the
beginning (x0).
14Recursion on lists
- Writing recursive methods on lists follows same
principle as for integers - To compute f(L), assume f(L) can be calculated
for lists L smaller than L, and use f(L) to
calculate f(L). - Some lists are small enough for f to be
calculated directly
15Lines
- We will provide a class called Line, with several
operations - Construct a line from (x0,y0) to (x1,y1) by
new Line(x0,y0,x1,y1) - Give line L, find coordinates of starting and
ending points using instance methods x0(), y0(),
x1(), y1(). - Lines can be printed by System.out.println.
16Line lists
- The class LL has the same operations as IL, but
contains operations on lists of Line objects - LineList cons (Line ln, LineList L) - construct
list containing i at front - nil
17Drawing lines at an angle
- To draw a line of length m at an angle theta from
the x-axis
m
y m sin ?
?
x m cos ?
(But remember, this is upside-down...)
18Drawing stars (cont.)
static LineList star1 (int order, int radius,
double theta, double
angleIncr) if (order 0) return
LL.nil else return LL.cons( new
Line(0, 0, (int)Math.round(radiusMa
th.cos(theta)), (int)Math.round(radi
usMath.sin(theta))), star1(order-1, radius,
thetaangleIncr, angleIncr))
(Detail Math.cos and Math.sin take arguments in
radians, instead of degrees.)
19Rotation
- Rotation is more complicated. Consider rotating
one point around the origin by angle ?
(x,y)
1. Calculate m and ? m ?x2y2 ?
tan-1(y/x) 2. ? ? ? 3. (x,y) point of
length m, at angle ?
?
?
?
(x,y)
20Rotation (cont.)
Well rotate shapes (i.e. LineLists) about
the origin by rotating each line
static LineList rotateShapeAboutOrigin
(LineList L, double theta) if
(LL.empty(L)) return LL.nil else
return LL.cons(rotateLine(LL.hd(L), theta),
rotateShapeAboutOrigin(LL.tl(L), theta))
21HTML
- Web pages are text documents with special
formatting commands, called tags. - The formatting language is called HTML.
- Can view raw HTML by selecting view source in
browser.
22The APPLET tag
- Place all class files in a subdirectory, say
appletcode.
ltAPPLET CODEappletname.class
CODEBASEappletcode HEIGHT 400
WIDTH 300gt lt/APPLETgt
stuff in here will be displayed if browser is not
Java-equipped
mandatory
23Writing applets
- The general form of an applet is
import java.applet. import java.awt. public
class appletname extends Applet public void
init () ... public void paint
(Graphics g) ... ... other methods
...
Usually present
24init and paint methods
- Most applets have these two methods (at least)
- init initializes instance variables (see below).
- paint performs graphics operations to draw shapes
in the applets window - Called when applet is first displayed, and
whenever it is re-displayed. - The Graphics objects is used to do the drawing
25Drawing operations
- The Graphics class defines numerous instance
methods - drawLine(int x0, int y0, int x1, int y1) -
draw a line from (x0,y0) to (x1,y1) - drawRect(int x0, int y0, int width, int height)
- draw a width x height box, with upper left
corner at (x0,y0) - fillRect(int x0, int y0, int width, int height)
- same as drawRect, but filled
26Example drawing a LineList
- When the paint method is called with a Graphics
object, it can call other methods. The Graphics
object can be used to draw in the applet window. - Draw a LineList in a Graphics object
static void drawLineList (LineList L, Graphics g)
if (!LL.empty(L)) Line ln LL.hd(L)
g.drawLine(ln.x0(), ln.y0(), ln.x1(),
ln.y1()) drawLineList(LL.tl(L), g)
27Color
- The Color class (in the java.awt package) has
values that denote different colors. - Symbolic constants Color.black, Color.red, etc.,
are predefined. - Arbitrary colors can be created by giving RGB
values in the range 0-255 when constructing a
Color value - Color col1 new Color(100,220,155)
- Color darkred new Color(100,50,50)
28Instance variables
- Variables declared inside a method (the kind
weve used so far) are either parameters or local
variables. - Variables declared inside a class but outside of
any method are either class variables or instance
variables. (They are class variables if they are
declared with the static keyword we will
consider only instance variables for now.)
29Components
- Some sample components
- Label Place text in applet (alternative to
drawString) - new Label(A label)
- Button Place button, possibly with label
- new Button()
- new Button(Press me)
- TextField Create field of given width to input
text - new TextField(10)
30Placing GUI components in an applet
- Normally, declare an instance variable of the
component type (Button, Checkbox, TextField,
etc.) - In init
- Initialize variable.
- Use add method to place it in applet.
- paint does not have to draw components - they are
automatically drawn
31Example A component-filled applet
public class BusyApplet extends Applet
Label L new Label(A label) Button b
new Button() Button bl new Button(Press
me) TextField t new TextField(10)
CheckBox cb new Checkbox() public void init
() add(L) add(b) add(bl) add(t)
add(cb)
32Input in applets
- Users enter text in TextField components
- Reading the value in a TextField involves four
changes to what weve seen
import java.awt. import java.applet. import
java.awt.event. public class appletname
extends Applet implements
ActionListener
1
2
33Input in applets (cont.)
TextField t new TextField(4) public void init
() ... add(t) t.addActionListener(this)
public void actionPerformed (ActionEvent e)
... t.getText() ... repaint()
3
4