Transcript ppt
Recitations Sep 8-9
conditionals:
if-else statement
boolean expressions
classes and instances
if statement
if (<boolean expression>)
<statement>
else
<statement>
Boolean expressions
evaluated
just like arithmetic expressions
evaluate to either TRUE or FALSE
exp1
true
true
false
false
exp2
true
false
true
false
exp1 || exp2
exp && exp2
!exp1
Examples
int count = 0;
int limit = 10;
if ( (count == 0) && (limit < 20) ) ...
if ( count == 0 && limit < 20 ) ...
if ( (limit > 20) || (count < 5) ) ...
if ( !(count == 12) ) ...
if ( (count == 1) && (x < y) ) ...
if ( (count < 10) || (x < y) ) ...
Examples
if ( !( ((count < 10) || (x < y)) && (count >= 0) ) ) ...
if ( ((limit/count) > 7) || (limit < 20) ) ...
if ( (limit < 20) || ((limit/count) > 7) ) ...
if ( ((limit/count) > 7) && (limit < 0) ) ...
if ( (limit < 0) || ((limit/count) > 7) ) ...
Operator precedence
unary
operators: +, -, ++, --, and !
*, /, %
+, <, >, <=, >=
==, !=
&&
||
Conditionals: Examples
int a = 5;
int b = -6;
if (a > b)
{c = 2; a = 4;}
if (a <= b)
{c = 2; a = 4;}
if (a <= b)
c = 2; a = 4;
int c = 15;
if (a > b)
c=2; a=4;
if (a > 5)
a=7;
else a = 6; b = 7;
if (a == 5)
a=7;
else a = 6; b = 7;
Class Coordinate
public class Coordinate {
public int x;
public int y;
// Set field x to p*p
public void setX(int p) {
x= p*p;
}
// 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 x*x + y*y;
}
}
“main” program
public class Example1
{
public static void main(String args[])
{
Coordinate c = new Coordinate();
c.setX(3);
c.setY(4);
}
}
New program: boxes
Box.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
Box.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);
}
}
CUCSDrawing Class
// Class CUCSDrawing: a simple graphics window.
public class CUCSDrawing extends Frame
{
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawOval(15,30,30,30);
g.drawRect(77,30,40,30);
g.setColor(Color.red);
g.drawString("circle",15,80);
g.drawString("rectangle",75,80);
}
}
“main” 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[])
{
CUCSDrawing d = new CUCSDrawing();
d.resize(200,150);
d.move(0,75);
d.setTitle("Drawing");
d.show();
d.toFront();
}
}
new 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;
// 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);
}