+ All Categories
Home > Documents > Catie Welsh February 14, 2011 1. Program 2 Due Tonight by 11:59pm Program 3 Assigned 2.

Catie Welsh February 14, 2011 1. Program 2 Due Tonight by 11:59pm Program 3 Assigned 2.

Date post: 19-Jan-2018
Category:
Upload: gilbert-edwards
View: 215 times
Download: 0 times
Share this document with a friend
Description:
3
18
Catie Welsh February 14, 2011 1
Transcript
Page 1: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

Catie WelshFebruary 14, 2011

1

Page 2: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

Program 2 Due Tonight by 11:59pm

Program 3 Assigned

2

Page 3: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

3

Page 4: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

int oddSum = 0;int evenSum = 0;for (int i = 1; i <= 6; i++){ if (i % 2 == 0) { evenSum = evenSum + i; } else { oddSum = oddSum + i; }}

4

Page 5: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

int oddSum = 0; int evenSum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) evenSum = evenSum + i;} else {oddSum = oddSum + i;}}

5

Page 6: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

int oddSum = 0;int evenSum = 0;for (int i = 1; i <= 6; i++){ if (i % 2 == 0) evenSum = evenSum + i; } else { oddSum = oddSum + i; }}

6

Page 7: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

Indentation◦ Makes code easier to read◦ Helps with finding syntax and logic errors◦ Indent code that goes between { and }

Be consistent!

7

Page 8: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

Brackets are required when your if/else or loop contains > 1 line of code!

Brackets are highly recommended when your if/else or loop contains 1 line of code

Please use brackets around all your if/else or loop statements from now on!

8

Page 9: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

Variables declared in outer scopes are visible to code inside inner scopes

public static void main(String[] args) { int total = 15; int n = 5; if (n <= 10) { total = total + n; } System.out.println(total); }

outer inner

9

Page 10: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

Variables declared in inner scopes are NOT visible to outer code

public static void main(String[] args) { int n = 5; if (n <= 10) { int total = 15 + n; } System.out.println(total); // ERROR!!! }

outerinner

10

Page 11: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

if (inputString.equals(“”)) canvas.setColor(Color.BLACK);

else if (inputString.equals(“BLUE”)) canvas.setColor(Color.BLUE);else if (inputString.equals(“GREEN”)) canvas.setColor(Color.GREEN);else if (inputString.equals(“RED”)) canvas.setColor(Color.RED);else canvas.setColor(Color.WHITE);

11

Page 12: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

if (inputString.equals(“MOUTH”)){ mouthStartAngle = 0;}else{}

12

Also not needed when you are setting a variable to the same value it already has: ( mouthStartAngle = 180; )

Page 13: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

if (inputString.equals(“MOUTH”)){ mouthStartAngle = 0;}if (inputString.equals(“EYES”)){ eyeColor = JOptionPane.showInputDialog(“Enter an eye color.”);

}

13

Compiler tests both statements even if 1st one is true

Page 14: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

if (inputString.equals(“MOUTH”)){ mouthStartAngle = 0;}else if (inputString.equals(“EYES”)){ eyeColor = JOptionPane.showInputDialog(“Enter an eye color.”);

}

14

Compiler only tests else if statement if the 1st if statement is false

Page 15: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

if (inputString.equals(“NOSE”)){ String input = JOptionPane.showInputDialog(“Enter an integer.”); int inputInt = Integer.parseInt(input);

if(inputInt > 1 && inputInt < 4) { noseDiameter = inputInt * 10;

noseDiameter = inputInt * noseDiameter; }

}

15

// noseDiameter = inputInt * 10;

Page 16: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

Practice String Parsing

Practice loops - code on web (Loops.java)

Worksheet ◦ hand in for class participation◦ Answers posted on website tonight

16

Page 17: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

Recitation

Lab 4

17

Page 18: Catie Welsh February 14, 2011 1.  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.

Read Section 5.1

Classes◦ You will need them to complete Program 3

18


Recommended