Title: Recitations Sep 8-9
1Recitations Sep 8-9
- conditionals if-else statement
- boolean expressions
- classes and instances
2if statement
if (ltboolean expressiongt) ltstatementgt else
ltstatementgt
3Boolean expressions
- evaluated just like arithmetic expressions
- evaluate to either TRUE or FALSE
exp1 exp2 exp1 exp2 exp exp2
!exp1 true true true false false true
false false
4Examples
int count 0 int limit 10 if ( (count
0) (limit lt 20) ) ... if ( count 0
limit lt 20 ) ... if ( (limit gt 20) (count lt
5) ) ... if ( !(count 12) ) ... if ( (count
1) (x lt y) ) ... if ( (count lt 10) (x lt
y) ) ...
5Examples
if ( !( ((count lt 10) (x lt y)) (count gt 0)
) ) ... if ( ((limit/count) gt 7) (limit lt 20)
) ... if ( (limit lt 20) ((limit/count) gt 7) )
... if ( ((limit/count) gt 7) (limit lt 0) )
... if ( (limit lt 0) ((limit/count) gt 7) )
...
6Operator precedence
- unary operators , -, , --, and !
- , /,
- , -
- lt, gt, lt, gt
- , !
-
-
7Conditionals Examples
int a 5 int b -6 int c 15 if (a gt b)
c 2 a 4 if (a lt b) c 2 a
4 if (a lt b) c 2 a 4
8if (a gt b) c2 a4 if (a gt 5) a7 else
a 6 b 7 if (a 5) a7 else a 6 b
7
9Class Coordinate
public class Coordinate public int x public
int y // Set field x to pp public void
setX(int p) x pp // Set field
y to q public void setY(int q) y q
// Return the sum of the squares of the
fields public int sumSquares() return xx
yy
10main program
public class Example1 public static void
main(String args) Coordinate c new
Coordinate() c.setX(3) c.setY(4)
11New program boxes
12Box.java
import java.awt. // An instance of class Box
is a rectangle. public class Box public int
xUpperLeft // The box has an upper left corner
at public int yUpperLeft // position
(xUpperLeft, yUpperLeft). public int width //
width of box public int height // height of
box public Color color // color of box
13Box.java
// Draw the box in designated color using
Graphics g public void drawBox (Graphics
g) // save original color in variable
oldColor Color oldColor g.getColor () //
set new color and draw the rectangle g.setColor
(color) g.fillRect (xUpperLeft, yUpperLeft,
width, height) // restore original
color g.setColor (oldColor)
14CUCSDrawing Class
// Class CUCSDrawing a simple graphics
window. public class CUCSDrawing extends
Frame public void paint(Graphics
g) g.setColor(Color.black) g.drawOval(15,3
0,30,30) g.drawRect(77,30,40,30) g.setColor(
Color.red) g.drawString("circle",15,80) g.dr
awString("rectangle",75,80)
15main program
// Main program for simple Graphics Applications.
The program creates the // drawing window and
sets its size and title. The actual drawing is
done // by the paint method in class Drawing.
The drawing window is moved down // the screen
slightly so it won't overlap the System.in window
if used. public class CUCSGraphicsApplication
public static void main(String args) CUCSDr
awing d new CUCSDrawing() d.resize(200,150)
d.move(0,75) d.setTitle("Drawing") d.show(
) d.toFront()
16new paint method
// Draw 2 Boxes and print their
locations public void paint(Graphics
g) Box b1 // first box Box b2 //
second box // Create the first box and
initialize its fields. b1 new
Box() b1.xUpperLeft 50 b1.yUpperLeft
70 b1.width 60 b1.height 20
17 // Create the second box and initialize its
fields. b2 new Box() b2.xUpperLeft
150 b2.yUpperLeft 150 b2.width
20 b2.height 30 // Draw the
boxes b1.drawBox(g) b2.drawBox(g)