Expressions, Statements, and Control Structures Announcements - - PowerPoint PPT Presentation

expressions statements and control structures
SMART_READER_LITE
LIVE PREVIEW

Expressions, Statements, and Control Structures Announcements - - PowerPoint PPT Presentation

Expressions, Statements, and Control Structures Announcements Assignment 2 out, due next Wednesday, February 1. Explore the Java concepts we've covered and will be covering. Unleash your creative potential! YEAH Hours Y our E


slide-1
SLIDE 1

Expressions, Statements, and Control Structures

slide-2
SLIDE 2

Announcements

  • Assignment 2 out, due next Wednesday,

February 1.

  • Explore the Java concepts we've covered and

will be covering.

  • Unleash your creative potential!
slide-3
SLIDE 3

YEAH Hours

  • Your Early Assignment Help Hours.
  • Review session going over major points
  • f the assignment.
  • Tonight at 7:00PM in Braun Auditorium.
  • Should be available on SCPD tomorrow.
slide-4
SLIDE 4

Highlights from Emails

slide-5
SLIDE 5

CS is not lame, Too many essays are lame, Prove I'm not just fuzz. I play Temple Run, And like to watch the sky and, Waste time with haikus.

slide-6
SLIDE 6

Sending Messages

  • To call a method on an object stored in a

variable, use the syntax

  • bject.method(parameters)
  • For example:

label.setFont("Comic Sans-32"); label.setColor(Color.ORANGE);

slide-7
SLIDE 7

Operations on the GObject Class

  • bject.setColor(color)

Sets the color of the object to the specified color constant.

  • bject.setLocation(x, y)

Changes the location of the object to the point (x, y).

  • bject.move(dx, dy)

Moves the object on the screen by adding dx and dy to its current coordinates.

The following operations apply to all GObjects:

Standard color names defined in the java.awt package:

Color.BLACK Color.DARK_GRAY Color.GRAY Color.LIGHT_GRAY Color.WHITE Color.RED Color.YELLOW Color.GREEN Color.CYAN Color.BLUE Color.MAGENTA Color.ORANGE Color.PINK

Graphic courtesy of Eric Roberts

slide-8
SLIDE 8

Drawing Geometrical Objects

Graphic courtesy of Eric Roberts

slide-9
SLIDE 9

Drawing Geometrical Objects

Constructors

new GRect( x, y, width, height)

Creates a rectangle whose upper left corner is at (x, y) of the specified size

Graphic courtesy of Eric Roberts

+x +y Graphics Program (x, y) (x + width, y + height)

slide-10
SLIDE 10

Drawing Geometrical Objects

Constructors

new GRect( x, y, width, height)

Creates a rectangle whose upper left corner is at (x, y) of the specified size

new GOval( x, y, width, height)

Creates an oval that fits inside the rectangle with the same dimensions.

Graphic courtesy of Eric Roberts

Graphics Program +x (x, y) (x + width, y + height) +y

slide-11
SLIDE 11

Drawing Geometrical Objects

Constructors

new GRect( x, y, width, height)

Creates a rectangle whose upper left corner is at (x, y) of the specified size

new GOval( x, y, width, height)

Creates an oval that fits inside the rectangle with the same dimensions.

new GLine( x0, y0, x1, y1)

Creates a line extending from (x0, y0) to (x1, y1).

Graphic courtesy of Eric Roberts

Graphics Program +x (x0, y0) (x1, y1) +y

slide-12
SLIDE 12

Drawing Geometrical Objects

Constructors

new GRect( x, y, width, height)

Creates a rectangle whose upper left corner is at (x, y) of the specified size

new GOval( x, y, width, height)

Creates an oval that fits inside the rectangle with the same dimensions.

Methods shared by the GRect and GOval classes

  • bject.setFilled( fill)

If fill is true, fills in the interior of the object; if false, shows only the

  • utline.
  • bject.setFillColor( color)

Sets the color used to fill the interior, which can be different from the border.

new GLine( x0, y0, x1, y1)

Creates a line extending from (x0, y0) to (x1, y1).

Graphic courtesy of Eric Roberts

slide-13
SLIDE 13

The Collage Model

slide-14
SLIDE 14

The Collage Model

slide-15
SLIDE 15

Constants

  • Not all variables actually vary.
  • A constant is a name for a value that

never changes.

  • Syntax (defined outside of any method):

private static final type name = value;

  • By convention, constants are named in

UPPER_CASE_WITH_UNDERSCORES to differentiate them from variables.

slide-16
SLIDE 16

Magic Numbers

  • A magic number is a number written in

a piece of code whose meaning cannot easily be deduced from context.

double weight = 9.8 * (m - 14);

  • Constants make it easier to read code:

double weight = GRAVITY * (m - TARE_MASS);

  • Avoid magic numbers in your code by

using constants.

slide-17
SLIDE 17

Expressions

slide-18
SLIDE 18

class Add2Integers extends ConsoleProgram { public void run() { println("This program adds two numbers."); int n1 = readInt("Enter n1: "); int n2 = readInt("Enter n2: "); int total = n1 + n2; println("The total is " + total + "."); } } n1 n2 total 17 25 42

slide-19
SLIDE 19

class Add2Integers extends ConsoleProgram { public void run() { println("This program adds two numbers."); int n1 = readInt("Enter n1: "); int n2 = readInt("Enter n2: "); int total = n1 + n2; println("The total is " + total + "."); } } n1 n2 total 17 25 42

slide-20
SLIDE 20

Expressions

  • Variables and other values can be used in

expressions.

  • Some familiar mathematical operators:
  • + (addition)
  • – (subtraction)
  • * (multiplication)
  • / (division)
slide-21
SLIDE 21

Fun with Division

slide-22
SLIDE 22

Size of the Graphics Window

Methods provided by GraphicsProgram class

getWidth()

Returns the width of the graphics window.

getHeight()

Returns the height of the graphics window.

Based on slides by Eric Roberts

Note: receiver of these calls is the GraphicsProgram itself, so we don’t need to specify a separate object as receiver.

slide-23
SLIDE 23

Centering an Object

Graphics Program getWidth(); W getWidth() / 2.0; W / 2.0

x = (getWidth() / 2.0) – (W / 2.0); x = (getWidth() - W) / 2.0;

slide-24
SLIDE 24

The Remainder Operator

  • The special operator % computes the

remainder of one value divided by another.

  • For example:
  • 15 % 3 = 0
  • 14 % 8 = 6
  • 21 % 2 = 1
  • 14 % 17 = 14
slide-25
SLIDE 25

Operator Precedence

  • Java's mathematical operators have the

following precedence:

  • ()

(highest)

  • * / %
  • + -

(lowest)

  • Operators of equal precedence are

evaluated left-to-right.

slide-26
SLIDE 26

A Useful Shorthand

  • Commonly, programs contain code like

this:

x = x + 1; y = y * 137; z = z / 14; w = w – 3;

  • The statement

variable = variable op value; can be rewritten as variable op= value;

slide-27
SLIDE 27

A Useful Shorthand

  • Commonly, programs contain code like

this:

x = x + 1; y = y * 137; z = z / 14; w = w – 3;

  • The statement

variable = variable op value; can be rewritten as variable op= value;

slide-28
SLIDE 28

A Useful Shorthand

  • Commonly, programs contain code like

this:

x += 1; y *= 137; z /= 14; w -= 3;

  • The statement

variable = variable op value; can be rewritten as variable op= value;

slide-29
SLIDE 29

Another Useful Shorthand

  • In the special case of writing

variable = variable + 1; we can instead write variable ++;

  • In the special case of writing

variable = variable - 1; we can instead write variable --;

slide-30
SLIDE 30

Based on slides by Mehran Sahami

Boolean Expressions

  • A boolean expression is a test for a

condition (it is either true or false).

  • Value comparisons:

== “equals” (note: not single =) != “not equals”(cannot say <>) > “greater than” < “less than” >= “greater than or equal to” <= “less than or equal to”

slide-31
SLIDE 31

Logical Operators

  • We can apply logical operators to boolean

values to produce new values.

  • Logical NOT: !p
  • !p is true if p is false; !p is false if p is true.
  • Logical AND: p && q
  • p && q is true when both p and q are true.
  • Logical OR: p || q
  • p || q is true when p is true, q is true, or both p and

q are true.

  • Order of precedence given above.
slide-32
SLIDE 32

Short-Circuit Evaluation

  • Cute observations:
  • true || p is always true.
  • false && p is always false.
  • The logical operators short-circuit: if the

answer is known from the left operand, the right side is not computed.

  • Example: The code

boolean b = (x == 0) || ((y / x) < 20) will never divide by zero.

Based on slides by Mehran Sahami

slide-33
SLIDE 33

Control Statements Revisited

slide-34
SLIDE 34

Control Structures in Karel for if while

slide-35
SLIDE 35

Control Structures in Karel for if while

slide-36
SLIDE 36

for (int i = 0; i < 3; i++) { … }

This is called the initialization statement and is performed before the loop starts. This is called the loop condition or termination

  • condition. The loop will

check whether this statement is true before each execution. This is called the step

  • r increment and is

performed at the end

  • f each loop iteration.
slide-37
SLIDE 37

Nyan nyan nyan nyan, nyan nyan nyan nyan nyan, nyan, nyan nyan nyan …

slide-38
SLIDE 38

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

slide-39
SLIDE 39

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

int i

slide-40
SLIDE 40

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

int i

slide-41
SLIDE 41

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

int i

slide-42
SLIDE 42

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

int i

Nyan!

slide-43
SLIDE 43

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

int i

Nyan!

slide-44
SLIDE 44

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

1

int i

Nyan!

slide-45
SLIDE 45

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

1

int i

Nyan!

slide-46
SLIDE 46

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

1

int i

Nyan!

slide-47
SLIDE 47

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

1

int i

Nyan! Nyan!

slide-48
SLIDE 48

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

1

int i

Nyan! Nyan!

slide-49
SLIDE 49

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

2

int i

Nyan! Nyan!

slide-50
SLIDE 50

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

2

int i

Nyan! Nyan!

slide-51
SLIDE 51

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

2

int i

Nyan! Nyan!

slide-52
SLIDE 52

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

2

int i

Nyan! Nyan! Nyan!

slide-53
SLIDE 53

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

2

int i

Nyan! Nyan! Nyan!

slide-54
SLIDE 54

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

3

int i

Nyan! Nyan! Nyan!

slide-55
SLIDE 55

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

3

int i

Nyan! Nyan! Nyan!

slide-56
SLIDE 56

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

3

int i

Nyan! Nyan! Nyan!

slide-57
SLIDE 57

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

3

int i

Nyan! Nyan! Nyan! Nyan! Nyan!

slide-58
SLIDE 58

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

3

int i

Nyan! Nyan! Nyan! Nyan!

slide-59
SLIDE 59

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

4

int i

Nyan! Nyan! Nyan! Nyan!

slide-60
SLIDE 60

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

4

int i

Nyan! Nyan! Nyan! Nyan!

slide-61
SLIDE 61

for (int i = 0; i < 4; i++) { println("Nyan!"); }

Console Program

4

int i

Nyan! Nyan! Nyan! Nyan!