Week 14 · 14.3 · Thu, Nov 5 · Do Now
Work every question in your notebook. Do it on your Chromebook with your ID.
Where it starts
Which line runs first?
public class Main { public static void intro() { System.out.println("welcome"); } public static void extra() { System.out.println("bonus"); } public static void main(String[] args) { System.out.println("play"); } }- line 1
- line 5
- line 13
- line 15
Predict
What does this print? The first line printed is first.
int y = 4; y = y + 6; y = y * 2; System.out.println(y); y = y - 5; System.out.println(y);
- 20, then 15
- 15, then 15
- 14, then 9
- 20, then 20
Error analysis
A student says this program prints Lunch and then Class. It prints only Class. What mistake did the student make?
public class Main { public static void bell() { System.out.println("Lunch"); } public static void main(String[] args) { System.out.println("Class"); } }- The student ran bell, but main never calls bell.
- The student started at line 10 instead of line 1.
- The student forgot that println prints on its own line.
- The student ran the statements from the bottom up.
Order matters
A program stores 3 in m. Which order of the next two statements makes it print 10?
- m = m * 3; then m = m + 1;
- m = m + 1; then m = m * 3;
- either order prints 10
- m = m * 3; only
Output
Write exactly what this program prints, one line for each print.
public class Main { public static void main(String[] args) { System.out.println("up"); turn(); System.out.println("down"); } public static void turn() { System.out.println("around"); } }Turn and Talk, Part 1: What went wrong?
Turn and Talk · Part 1 · talk with your partner first.
- “I think the mistake is in line ___ because ___.”
- “I agree / disagree, because ___.”
A student wrote down what the screen shows. Talk with your partner: what went wrong?
public class Greet { public static void main(String[] args) { System.out.print("Hi"); System.out.println("there"); System.out.println("Sam"); } }The student's screen Line Shown 1 Hi 2 there 3 Sam - print does not end the line, so Hi and there share line 1
- println never ends the line
- Sam should come first
- The quotation marks show on the screen
Turn and Talk, Part 2: Is it right now?
Turn and Talk · Part 2 · talk with your partner first.
- “I think it is right now, because ___.”
- “I think it is still wrong at line ___, because ___.”
The student fixed line 1. Is it right now?
public class Greet { public static void main(String[] args) { System.out.print("Hi"); System.out.println("there"); System.out.println("Sam"); } }The student's new screen Line Shown 1 Hi there 2 Sam - Still wrong: there is no space, so line 1 is Hithere
- Right: line 1 is Hi there
- Still wrong: Sam belongs on line 1 too
- Still wrong: there are 3 lines