Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recitations Sep 8-9 u conditionals: if-else statement u boolean expressions u classes and instances.

Similar presentations


Presentation on theme: "Recitations Sep 8-9 u conditionals: if-else statement u boolean expressions u classes and instances."— Presentation transcript:

1 Recitations Sep 8-9 u conditionals: if-else statement u boolean expressions u classes and instances

2 if statement if ( ) else

3 Boolean expressions u evaluated just like arithmetic expressions u evaluate to either TRUE or FALSE exp1 exp2 exp1 || exp2 exp && exp2 !exp1 true true false false true false

4 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) )...

5 Examples if ( !( ((count = 0) ) )... if ( ((limit/count) > 7) || (limit < 20) )... if ( (limit 7) )... if ( ((limit/count) > 7) && (limit < 0) )... if ( (limit 7) )...

6 Operator precedence u unary operators: +, -, ++, --, and ! u *, /, % u +, - u, = u ==, != u && u ||

7 Conditionals: Examples int a = 5;int b = -6;int c = 15; if (a > b) {c = 2; a = 4;} if (a <= b) {c = 2; a = 4;} if (a <= b) c = 2; a = 4;

8 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;

9 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; } Class Coordinate

10 “main” program public class Example1 { public static void main(String args[]) { Coordinate c = new Coordinate(); c.setX(3); c.setY(4); }

11 New program: boxes

12 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

13 // 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); }

14 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); }

15 “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(); }

16 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;

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); }


Download ppt "Recitations Sep 8-9 u conditionals: if-else statement u boolean expressions u classes and instances."

Similar presentations


Ads by Google