catie welsh february 2, 2011 1. program 1 due today by 11:59pm today program 2 assigned today lab...

23
Catie Welsh February 2, 2011 1

Upload: vincent-white

Post on 21-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

Catie WelshFebruary 2, 2011

1

Page 2: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

Program 1 Due Today by 11:59pm today

Program 2 Assigned Today

Lab 2 Due Friday by 1:00pm

2

Page 3: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

3

Page 4: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

4

Formatting decimals

Review Worksheet

If/Else statements

Boolean Expressions

Page 5: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

import java.text.*;

DecimalFormat df = new DecimalFormat("0.00");

df.format(myVariable);

Example code on class website

5

Page 6: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

public class MyProgram{

public static void main(String[] args) {

String myString = “This is a string”; int len = myString.length(); System.out.print(“the length is “ + len); String shortString = myString.substring(10);

} }

6

Page 7: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

Double myDouble = (1 / 2) * 5.0;

int i = 1 / 2;

0.5

O

7

Page 8: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

Suppose that mary is an object of class Person, and suppose that increaseAge is a method of class Person that uses one argument, an integer. Write the invocation of the method increaseAge for the object mary using the argument 5.

mary.increaseAge(5);

Person mary = new Person;

8

Page 9: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

9

Page 10: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

Check time;

if (time < 7am){

take bus;}

else //time >= 7am{

take subway;}

Reach school;

10

Page 11: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

import java.util.*;

public class FlowChart { public static void main(String[] args) { System.out.println("Give me an integer:"); Scanner keyboard = new Scanner(System.in); int inputInt = keyboard.nextInt(); if( inputInt > 5) { System.out.println("Big number"); } else { System.out.println("Small number"); } } }

11

Start

Prompt User for int

Is user input greater than 5?

Print: “small number”

Print: “bignumber”

YESNO

Page 12: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

== Equal to

!= Not equal to

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

12

Page 13: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

True of False Example expressions

◦ 5 == 3;◦ Variable <= 6;◦ myInt != temp;

if (boolean expression){ statements }

13

Page 14: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

What if you need multiple expressions to be true

(expression) && (expression) && …◦ Expressions go in ( )

Will only be true if ALL statements are true

14

Page 15: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

What if you need ONE expression to be true out of many expressions

(expression) || (expression) || …◦ Expressions go in ( )

Will be true if ONE expression is true

15

Page 16: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

FIGURE 3.7 The Effect of the Boolean Operators && (and), || (or), and ! (not) on Boolean values

Page 17: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

var1 = var2 (assignment statement)◦ Error!!!!!!!

var1 == var2 (boolean expression)

Do NOT use == to compare Strings◦ string1 == string2 //BAD

◦ string1.equals(string2); //GOOD

17

Page 18: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

SyntaxString.equals(Other_String)String.equalsIgnoreCase(Other_String)

Page 19: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

You can use just an if statement

if (boolean expression){ (statements) }the rest of your code

19

Page 20: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

if (boolean expression){

if (boolean expression) { stuff goes here }else { more stuff }

}else

20

Page 21: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

Start

Prompt User for int

What is the integer?

Print: “hello” Print: “how may I help you”

inputInt > 1inputInt == 0

Print: “how are you”

inputInt == 1

21

Page 22: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

import java.util.*;

public class FlowChart { public static void main(String[] args) { System.out.println("Give me an integer:"); Scanner keyboard = new Scanner(System.in); int inputInt = keyboard.nextInt(); if ( inputInt == 0) System.out.println(”hello");

else if ( inputInt == 1)System.out.println(”how are you");

else System.out.println(”how may I help you");

} }

22

Page 23: Catie Welsh February 2, 2011 1.  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2

Recitation

Bring Laptop

Bring Book

23