Download presentation
Presentation is loading. Please wait.
Published byEvan Singleton Modified over 9 years ago
1
2/5/00SEM107 © Kamin & Reddy Review -1 Class 19 - Review r This lecture contains a selection of slides from previous lectures, giving the “high points” of the material we have covered. r 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
2/5/00SEM107 © Kamin & Reddy Review -2 Applications vs. applets r Applications: stand-alone programs, usually invoked from command line.
3
2/5/00SEM107 © 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() r... lots more...
4
2/5/00SEM107 © 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.
5
2/5/00SEM107 © Kamin & Reddy Review -5 Summary r Classes introduce types. Values with these types are called objects. Create objects using new (exception: String ’s) r Classes have Class methods: classname. methodname (arg’s) Class variables: classname. variablename
6
2/5/00SEM107 © Kamin & Reddy Review -6 Summary (cont.) r Objects have: m 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.
7
2/5/00SEM107 © Kamin & Reddy Review -7 Methods r Method = named collection of statements Can be class method ( static ) or instance method m Has return type Method call is statement if return type is void Method call is expression if return type is not void
8
2/5/00SEM107 © 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.
9
2/5/00SEM107 © Kamin & Reddy Review -9 Conditional expressions r A conditional expression has the form: condition ? expression1 : expression2 r It evaluates the condition and then either evaluates expression1 or expression2, depending whether condition is true or false r Note: the type of expression1 and expression2 must be the same; that type is the type of the conditional expression
10
2/5/00SEM107 © 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 one that calls itself. A recursive method is one that calls itself.
11
2/5/00SEM107 © 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) }
12
2/5/00SEM107 © Kamin & Reddy Review -12 Thinking recursively Key ideas: m Know exactly what the method is supposed to do. m Assume method works for smaller values. Then just figure out how to use it to compute method for larger values. m Don’t forget base cases - small values that can be calculated directly.
13
2/5/00SEM107 © 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: x 0, x 1,..., x n-1 (n >= 0) Elements can be added and removed only at the beginning (x 0 ).
14
2/5/00SEM107 © Kamin & Reddy Review -14 Recursion on lists Writing recursive methods on lists follows same principle as for integers: m To compute f(L), assume f(L’) can be calculated for lists L’ smaller than L, and use f(L’) to calculate f(L). m Some lists are small enough for f to be calculated directly
15
2/5/00SEM107 © 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.
16
2/5/00SEM107 © 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 m nil
17
2/5/00SEM107 © Kamin & Reddy Review -17 Drawing 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...)
18
2/5/00SEM107 © 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.)
19
2/5/00SEM107 © Kamin & Reddy Review -19 Rotation Rotation is more complicated. Consider rotating one point around the origin by angle : 1. Calculate m and m = x 2 +y 2 = tan -1 (y/x) 2. = + 3. (x’,y’) = point of length m, at angle (x,y) (x’,y’)
20
2/5/00SEM107 © Kamin & Reddy Review -20 Rotation (cont.) We’ll rotate shapes (i.e. LineList’s) 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)); }
21
2/5/00SEM107 © Kamin & Reddy Review -21 HTML r Web pages are text documents with special formatting commands, called tags. r The formatting language is called HTML. r Can view raw HTML by selecting “view source” in browser.
22
2/5/00SEM107 © 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> mandatory stuff in here will be displayed if browser is not Java-equipped
23
2/5/00SEM107 © 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... } Usually present
24
2/5/00SEM107 © 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 r Called when applet is first displayed, and whenever it is re-displayed. The Graphics objects is used to do the drawing
25
2/5/00SEM107 © 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
26
2/5/00SEM107 © Kamin & Reddy Review -26 Example: drawing a LineList r 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. r 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); }
27
2/5/00SEM107 © Kamin & Reddy Review -27 Color 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: m Color col1 = new Color(100,220,155); m Color darkred = new Color(100,50,50);
28
2/5/00SEM107 © Kamin & Reddy Review -28 Instance variables r 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.)
29
2/5/00SEM107 © Kamin & Reddy Review -29 Components Some sample components: Label: Place text in applet (alternative to drawString ): new Label(“A label”); m Button: Place button, possibly with label: new Button(); new Button(“Press me”); m TextField: Create field of given width to input text: new TextField(10);
30
2/5/00SEM107 © 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 m Initialize variable. Use add method to place it in applet. paint does not have to draw components - they are automatically drawn
31
2/5/00SEM107 © 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); }
32
2/5/00SEM107 © Kamin & Reddy Review -32 Input in applets r Users enter text in TextField components r Reading 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
33
2/5/00SEM107 © 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.