chapter 3 ch 1 – introduction to computers and java flow of control branching 1

42
Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

Upload: curtis-ralph-ray

Post on 05-Jan-2016

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

1

Chapter 3

Ch 1 – Introduction toComputers and Java

Flow of ControlBranching

Page 2: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

2

Chapter 3

3.1 The if-else Statement

3.2 The Type boolean

3.3 The switch Statement

3.4 Graphics Supplement

Page 3: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

3

3.1 The if-else Statement

Page 4: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

4

Flow Chart Deconstructed

booleanexpression

truestatements

falsestatement

Start

Stop

Terminator: Show start/stop

points

Process: Statements to

execute

Decision: Make a choice

Connector: Connect lines

FT

Page 5: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

5

Flow Chart Deconstructed<Taxes owed>

Start

income <= NO_TAX_AMOUNT

Stop

taxesOwed = income * TAX_RATE

F

taxesOwed = 0.0

T

Page 6: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

6

If else Deconstructed<Taxes owed>

...

if (income <= NO_TAX_AMOUNT) { taxesOwed = 0.0;}

else { taxesOwed = income * TAX_RATE;} //end if

...

boolean expression

true statements

false statements

Page 7: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

7

Application Deconstructed<TaxesOwed.java>

package taxesowed;

import java.util.Scanner;

public class TaxesOwed { public static final double NO_TAX_AMOUNT = 10000.0; public static final double TAX_RATE = 0.03; public static void main(String[] args) { double income; double taxesOwed; Scanner keyboard = new Scanner(System.in); System.out.print("Enter your income amount: $"); income = keyboard.nextDouble();

Page 8: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

8

Application Deconstructed<TaxesOwed.java>

if (income <= NO_TAX_AMOUNT) { taxesOwed = 0.0; } else { taxesOwed = income * TAX_RATE; }// end else System.out.println("You owe $" + taxesOwed + " on an income of $" + income); }// end main()}// end TaxesOwed

Page 9: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

9

Boolean expressions may usethe following relational operators

Operation Operator Example

equality == a == b

inequality != a != b

greater than > a > b

greater than or equal >= a >= b

less than < a < b

less than or equal <= a <= b

Page 10: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

10

... and the followinglogical operators

Operation Operator Example

And && (a < b) && (c > d)

Or || (a == b) || (c == d)

Not ! !fileIsOpen

Page 11: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

11

Application Deconstructed<StringEquality.java>

package stringequality;import java.util.Scanner;

public class StringEquality { public static void main(String[] args) { String str1; String str2; Scanner keyboard = new Scanner(System.in); System.out.print("Enter some text: "); str1 = keyboard.nextLine(); System.out.print("Enter more text: "); str2 = keyboard.nextLine();

Page 12: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

12

Application Deconstructed<StringEquality.java>

System.out.println(); System.out.println("str1 = \"" + str1 + "\" : str2 = \"" + str2 + "\""); System.out.println(); System.out.println("str1 == str2 ? " + (str1 == str2)); System.out.println("str1.equals(str2) ? " + str1.equals(str2));

System.out.println("str2.equals(str1) ? " + str2.equals(str1));

== asks: are they the same object?The others compare the text.

Page 13: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

13

Application Deconstructed<StringEquality.java>

str2 = "apples are red"; System.out.println(); System.out.println("str1 = \"" + str1 + "\" : str2 = \"" + str2 + "\""); System.out.println(); System.out.println("str1.equals(str2) ? " + str1.equals(str2)); System.out.println("str1.equalsIgnoreCase(str2) ? " + str1.equalsIgnoreCase(str2)); }// end main()}// end StringEquality

Page 14: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

14

Application Deconstructed<StringEquality.java>

Page 15: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

15

Application Deconstructed<MultiBranchIf.java>

package multibranchif;import java.util.Scanner;

public class MultiBranchIf { public static final int SUN = 1; public static final int MON = 2; public static final int TUE = 3; public static final int WED = 4; public static final int THU = 5; public static final int FRI = 6; public static final int SAT = 7; public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int weekDayNumber; String weekDayName = "";

constants for the days of the week

Page 16: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

16

Application Deconstructed<MultiBranchIf.java>

System.out.print("Enter the day of the week [1-Sunday...7-Saturday]: "); weekDayNumber = keyboard.nextInt(); if (weekDayNumber == SUN) { weekDayName = "Sunday"; }else if (weekDayNumber == MON) { weekDayName = "Monday"; }else if (weekDayNumber == TUE) { weekDayName = "Tuesday"; }else if (weekDayNumber == WED) { weekDayName = "Wednesday"; }else if (weekDayNumber == THU) { weekDayName = "Thursday"; }else if (weekDayNumber == FRI) { weekDayName = "Friday"; }else if (weekDayNumber == SAT) { weekDayName = "Saturday"; }// end if

This is just an if else, where each else contains an if else.

Page 17: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

17

Application Deconstructed<MultiBranchIf.java>

System.out.println("I see you chose " + weekDayName + "."); }// end main()}// end MultiBranchIf

Page 18: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

18

The conditional operator isvery handy

// The following if-else:if (minute == 60) { minute = 0;}else { minute += 1;}

// can be written with a conditional operator as:minute = (minute == 60)? 0 : minute + 1;

Page 19: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

19

Recap

Use flowcharts to express algorithms.

Use the if for decisions.

Use .equals or .equalsIgnoreCase with strings

Page 20: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

20

3.2The Type boolean

True or False?

Page 21: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

21

Boolean expressions evaluateto true or false

number > 0– True if number > 0, false otherwise

3 > 4– False

3– True, since not 0

Page 22: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

Boolean variables simplify

// Testing for leap year.if ( (year % 100 != 0) && (year % 4 == 0) || (year % 400 == 0) ) { // leap year}

// Testing for leap year.boolean isLeap = (year % 100 != 0) && (year % 4 == 0) || (year % 400 == 0);

if (isLeap) { // leap year}

The && has higher priority than ||

Page 23: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

Be mindful of the order of evaluation

Parentheses go first, then

Arithmetic, then

Relational, and then

Logical

Page 24: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

Brainteaser time!

int x = 3 * 5 + (4 – 5) / 2 + 6 % 4;

bool goAhead = 5 > 3 || 3 + 5 < 3 * 2;

bool isRaining = true;bool haveUmbrella = false;

bool gotWet = isRaining && !haveUmbrella;

Page 25: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

25

Booleans use true or falseboolean isOld = true;

System.out.println("isOld = " + isOld);// displays isOld = true

System.out.print("Answer true or false. This is fun: ";boolean isOfAge = keyboard.nextBoolean();

// IOAnswer true of false. This is fun: true

Page 26: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

26

Recap

Booleans hold either true or false.

(), arithmetic, relational, logical, in that order.

Boolean variables simplify code.

Page 27: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

27

3.3The switch statement

statementsTrue

False

case 1

statements

False

True

default

case n

Page 28: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

Switch Deconstructed

switch (expression) {case label1: statements;

[break];

case label2: statements;[break];

...default: statements;

[break];}

break is optional

default is optional

sdk 7: label canbe a string

Page 29: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

29

Switch Deconstructed<DayOfTheWeek>

// ... code omitted

switch (dayOfTheWeek) {case MON: case TUE: case WED: case THU: case FRI: System.out.println("Bah, still a work day!");

break;

case SAT: case SUN: System.out.println("Woo hoo, weekend!); break;

default: System.out.println("No comprendo!"); break;}

Page 30: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

Brainteaser time!

Draw the flow chart for the DayOfTheWeek switch

Page 31: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

31

Application Deconstructed<DayOfTheWeek.java>

package dayoftheweek;

import java.util.Scanner;

public class DayOfTheWeek { public static final int SUN = 1; public static final int MON = 2; public static final int TUE = 3; public static final int WED = 4; public static final int THU = 5; public static final int FRI = 6; public static final int SAT = 7; public static void main(String[] args) { int dayOfTheWeek; Scanner keyboard = new Scanner(System.in);

Page 32: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

32

Application Deconstructed<DayOfTheWeek.java>

System.out.print("Enter the day of the week [Sun = 1...Sat = 7]: "); dayOfTheWeek = keyboard.nextInt(); switch (dayOfTheWeek) { case MON: case TUE: case WED: case THU: case FRI: System.out.println("Bah, still a work day!"); break; case SAT: case SUN: System.out.println("Woo hoo, weekend!"); break; default: System.out.println("No comprendo!"); break; }// end switch }// end main()}// end DayOfTheWeek

Page 33: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

33

3.4Graphics

Supplement

Page 34: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

34

Applet Deconstructed< YellowSmileyFace.java >

package yellowsmileyface;

import javax.swing.JApplet;import java.awt.Color;import java.awt.Graphics;

public class YellowSmileyFace extends JApplet { public static final int FACE_DIAMETER = 200; public static final int X_FACE = 100; public static final int Y_FACE = 50; public static final int EYE_WIDTH = 10; public static final int EYE_HEIGHT = 20; public static final int X_RIGHT_EYE = 155; public static final int X_LEFT_EYE = 230; public static final int Y_RIGHT_EYE = 100; public static final int Y_LEFT_EYE = Y_RIGHT_EYE; public static final int NOSE_DIAMETER = 10; public static final int X_NOSE = 195; public static final int Y_NOSE = 135;

Page 35: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

35

Applet Deconstructed< YellowSmileyFace.java >

public static final int MOUTH_WIDTH = 100; public static final int MOUTH_HEIGHT = 50; public static final int X_MOUTH = 150; public static final int Y_MOUTH = 160; public static final int MOUTH_START_ANGLE = 180; public static final int MOUTH_EXTENT_ANGLE = 180;

@Override public void paint(Graphics canvas) { canvas.setColor(Color.YELLOW); canvas.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);

canvas.setColor(Color.BLACK); canvas.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);

Page 36: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

36

Applet Deconstructed< YellowSmileyFace.java >

canvas.setColor(Color.BLUE); canvas.fillOval(X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT); canvas.fillOval(X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT);

canvas.setColor(Color.RED); canvas.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT, MOUTH_START_ANGLE, MOUTH_EXTENT_ANGLE); }// end paint()}// end HappyFaceAppletConstants

canvas.setColor(Color.BLACK); canvas.fillOval(X_NOSE, Y_NOSE, NOSE_DIAMETER, NOSE_DIAMETER);

Page 37: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

37

Applet Deconstructed< YellowSmileyFace.java >

Page 38: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

38

Application Deconstructed< NameThatCountry.java >

package namethatcountry;

import javax.swing.JFrame;import java.awt.Color;import java.awt.Graphics;import javax.swing.JOptionPane;

public class NameThatCountry extends JFrame { public static final int FRAME_WIDTH = 600; public static final int FRAME_HEIGHT = 400; public static final int BAND_WIDTH = 120; public static final int BAND_HEIGHT = 250; public static final int X_FLAG = (FRAME_WIDTH - 3 * BAND_WIDTH) / 2; public static final int Y_FLAG = (FRAME_HEIGHT - BAND_HEIGHT) / 2; public static final int X_GREEN = X_FLAG; public static final int Y_GREEN = Y_FLAG;

Page 39: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

39

Application Deconstructed< NameThatCountry.java >

public static final int X_WHITE = X_GREEN + BAND_WIDTH + 1; public static final int Y_WHITE = Y_FLAG; public static final int X_ORANGE = X_WHITE + BAND_WIDTH + 1; public static final int Y_ORANGE = Y_FLAG; public static void main(String[] args) { NameThatCountry flagWindow = new NameThatCountry(); flagWindow.setVisible(true); String countryName = JOptionPane.showInputDialog("Can you name this country?"); if ( countryName.equalsIgnoreCase("Ireland") ) { JOptionPane.showMessageDialog(null, "Nice Job."); } else { JOptionPane.showMessageDialog(null, "Sorry, its Ireland"); }// end else System.exit(0); }// end main()

Page 40: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

40

Application Deconstructed< NameThatCountry.java >

public NameThatCountry() { setSize(FRAME_WIDTH, FRAME_HEIGHT); setTitle("Name that country"); }// end NameThatCountry() @Override public void paint(Graphics canvas) { canvas.setColor(Color.GREEN); canvas.fillRect(X_GREEN, Y_GREEN, BAND_WIDTH, BAND_HEIGHT); canvas.setColor(Color.WHITE); canvas.fillRect(X_WHITE, Y_WHITE, BAND_WIDTH, BAND_HEIGHT); canvas.setColor(Color.ORANGE); canvas.fillRect(X_ORANGE, Y_ORANGE, BAND_WIDTH, BAND_HEIGHT); }}// end NameThatCountry

Page 41: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

41

Application Deconstructed< NameThatCountry.java >

Page 42: Chapter 3 Ch 1 – Introduction to Computers and Java Flow of Control Branching 1

42

Application Deconstructed< NameThatCountry.java >