2/5/00 SEM107 © Kamin & Reddy Review -1
Class 19 - ReviewThis 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.)
2/5/00 SEM107 © Kamin & Reddy Review -2
Applications vs. applets
Applications: stand-alone programs, usually invoked from command line.
2/5/00 SEM107 © Kamin & Reddy Review -3
Strings
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...
2/5/00 SEM107 © Kamin & Reddy Review -4
Review
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.
2/5/00 SEM107 © Kamin & Reddy Review -5
Summary
Classes introduce types. Values with these types are called objects.
Create objects using new (exception: String’s)
Classes have Class methods: classname . methodname (arg’s) Class variables: classname . variablename
2/5/00 SEM107 © Kamin & Reddy Review -6
Summary (cont.)
Objects have: Instance methods: expression . methodname (arg’s) 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.
2/5/00 SEM107 © Kamin & Reddy Review -7
Methods
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
2/5/00 SEM107 © Kamin & Reddy Review -8
Conditional 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.
2/5/00 SEM107 © Kamin & Reddy Review -9
Conditional 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
2/5/00 SEM107 © Kamin & Reddy Review -10
Recursive methods
As of now, we lack the ability to do repetitive actions. Can solve them using recursive methods:
A recursive method is onethat calls itself.
A recursive method is onethat calls itself.
2/5/00 SEM107 © Kamin & Reddy Review -11
Recursive 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)
}
2/5/00 SEM107 © Kamin & Reddy Review -12
Thinking 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.
Don’t forget base cases - small values that can be calculated directly.
2/5/00 SEM107 © Kamin & Reddy Review -13
Lists
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 >= 0)
Elements can be added and removed only at the beginning (x0).
2/5/00 SEM107 © Kamin & Reddy Review -14
Recursion 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
2/5/00 SEM107 © Kamin & Reddy Review -15
Lines
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.
2/5/00 SEM107 © Kamin & Reddy Review -16
Line 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
2/5/00 SEM107 © Kamin & Reddy Review -17
Drawing lines at an angle
To draw a line of length m at an angle theta from the x-axis:
my = m sin
x = m cos
(But remember, this is upside-down...)
2/5/00 SEM107 © Kamin & Reddy Review -18
Drawing “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(radius*Math.cos(theta)), (int)Math.round(radius*Math.sin(theta))), star1(order-1, radius, theta+angleIncr, angleIncr));}
(Detail: Math.cos and Math.sin take arguments in radians, instead of degrees.)
2/5/00 SEM107 © Kamin & Reddy Review -19
Rotation
Rotation is more complicated. Consider rotating one point around the origin by angle :
1. Calculate m and m = x2+y2
= tan-1(y/x)2. = + 3. (x’,y’) = point of length m, at angle
(x,y)
(x’,y’)
2/5/00 SEM107 © Kamin & Reddy Review -20
Rotation (cont.)
We’ll rotate shapes (i.e. LineList’s) about theorigin 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)); }
2/5/00 SEM107 © Kamin & Reddy Review -21
HTML
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.
2/5/00 SEM107 © Kamin & Reddy Review -22
The APPLET tag
Place all class files in a subdirectory, say appletcode.
<APPLET CODE=“appletname.class” CODEBASE=“appletcode” HEIGHT = 400 WIDTH = 300></APPLET>
mandatory
stuff in here will bedisplayed if browser isnot Java-equipped
2/5/00 SEM107 © Kamin & Reddy Review -23
Writing 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 ...}
Usuallypresent
2/5/00 SEM107 © Kamin & Reddy Review -24
init 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 applet’s window Called when applet is first displayed, and
whenever it is re-displayed. The Graphics objects is used to do the
drawing
2/5/00 SEM107 © Kamin & Reddy Review -25
Drawing 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
2/5/00 SEM107 © Kamin & Reddy Review -26
Example: drawing a LineListWhen 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); }}
2/5/00 SEM107 © Kamin & Reddy Review -27
ColorThe 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);
2/5/00 SEM107 © Kamin & Reddy Review -28
Instance variables
Variables declared inside a method (the kind we’ve 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.)
2/5/00 SEM107 © Kamin & Reddy Review -29
Components
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);
2/5/00 SEM107 © Kamin & Reddy Review -30
Placing 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
2/5/00 SEM107 © Kamin & Reddy Review -31
Example: 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); }}
2/5/00 SEM107 © Kamin & Reddy Review -32
Input in applets
Users enter text in TextField componentsReading the value in a TextField involves
four changes to what we’ve seen:
import java.awt.*;import java.applet.*;import java.awt.event.*;
public class appletname extends Applet implements ActionListener {
1
2
2/5/00 SEM107 © Kamin & Reddy Review -33
Input 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
Top Related