yeah 2: simple java! - stanford universityweb.stanford.edu › class › ... › assignments ›...
TRANSCRIPT
![Page 1: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/1.jpg)
YEAH 2: Simple Java!
Avery WangJared Bitz7/6/2018
![Page 2: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/2.jpg)
What are YEAH Hours?
➢ “Your Early Assignment Help”
➢ Only for some assignments
➢ Review + Tips for an assignment
➢ Lectures are recorded, slides are posted on website.
2
![Page 3: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/3.jpg)
Bye Karel!
3
![Page 4: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/4.jpg)
Variables
4
int
double
char
boolean
int date = 7;
double height = 5.8;
char letter = ‘A’;
boolean lovesCS106A = true;
integer
real values
letters
true/false
(From Lecture 4)
![Page 5: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/5.jpg)
Variables
5
CONSTANTdouble
something
sumnumDays
i(From Lecture 4)
Good vs. Bad names
![Page 6: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/6.jpg)
Variables
6
CONSTANTdouble
something
sumnumDays
i(unless it is a loop counter)
(From Lecture 4)
Good vs. Bad names
![Page 7: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/7.jpg)
Constants
7
private static final double CIRCLE_RADIUS = 5.5;
Variables whose value doesn’t change.
type name value
(From Lecture 5)
![Page 8: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/8.jpg)
Arithmetic Operators
8
+
-
*
/
%
(From Lecture 5)
Evaluates as you’d expect.
Careful when dividing ints – truncates decimals!
“mod” operator
![Page 9: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/9.jpg)
Arithmetic Operators
9
(From Lecture 5)
a % b What’s the remainder when you divide a by b?
17 % 2 evaluates to 1
52 % 2 evaluates to 0
100 % 3 evaluates to 1
![Page 10: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/10.jpg)
Logical Operators
10
!p
p && q
p || q
NOT
AND
OR
evaluates to true if p is false.
evaluates to true if both p and q are true
evaluates to true if either p or q is true
(From Lecture 5)
![Page 11: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/11.jpg)
Relational Operators
11
a == b
a != b
a > b
a >= b
a < b
a <= b
(From Lecture 5)
evaluates to true if a is equal to b.
evaluates to true if a is not equal to b.
evaluates to true or false as you’d expect.
![Page 12: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/12.jpg)
Relational Operators
12
(From Lecture 5)
a == b
checks if a is equal to b.
if(a == b){
println(“equal!”);
}
a = b
assigns a to the value of b.
int b = 3;
int a = 2;
a = b; // now a is 3
![Page 13: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/13.jpg)
Control Flow
13
for (init; test; step){
statements
}
(From Lecture 5)
init
while (test) {
statements
}
We know how many times to iterate.
We don’t know how many times to iterate.
![Page 14: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/14.jpg)
Control Flow
14
while (true) {
// get input
if (input == SENTINEL){
break;
}
// rest of body
}
(From Lecture 5)
// get input – fencepost
while (input != SENTINEL){
// rest of body
// get input
}
![Page 15: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/15.jpg)
Scope
15
(From Lecture 6)
public void run(){
for (int i = 0; i < 3; i++){
if (i == 0){
int j = 0;
j++;
}
i--;
}
}
A variable’s lifetime• starts at initialization• until end of code block
Scope of i
Scope of j
![Page 16: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/16.jpg)
Forbidden Java Features(For Assignment 2)
16
• parameters• return• Strings• instance variables (more on this later)• concepts from Chapter 5 and beyond
![Page 17: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/17.jpg)
Practice: FizzBuzz
17
▪ Write a program that prints all of the numbers in a range, separated by spaces▪ For multiples of three print "Fizz" instead of the number▪ For the multiples of five print "Buzz". ▪ For numbers which are multiples of both three and five print "FizzBuzz". ▪ Get the upper limit from the user▪ For a limit of 100, the output would be:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 BuzzFizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 FizzBuzz 41 Fizz 43 44 FizzBuzz46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 5859 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 7677 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 BuzzFizz 97 98 Fizz Buzz
![Page 18: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/18.jpg)
18
public void run() {
}
![Page 19: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/19.jpg)
19
public void run() {
int limit = readInt(“Limit? “);
}
![Page 20: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/20.jpg)
20
public void run() {
int limit = readInt(“Limit? “);
for (int i = 1; i <= limit; i++){
}
}
![Page 21: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/21.jpg)
21
public void run() {
int limit = readInt(“Limit? “);
for (int i = 1; i <= limit; i++){
if (i % 3 == 0 && i % 5 == 0){
print(“FizzBuzz “);
}
}
}
![Page 22: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/22.jpg)
22
public void run() {
int limit = readInt(“Limit? “);
for (int i = 1; i <= limit; i++){
if (i % 3 == 0 && i % 5 == 0){
print(“FizzBuzz “);
} else if (i % 3 == 0){
print(“Fizz “);
}
}
}
![Page 23: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/23.jpg)
23
public void run() {
int limit = readInt(“Limit? “);
for (int i = 1; i <= limit; i++){
if (i % 3 == 0 && i % 5 == 0){
print(“FizzBuzz “);
} else if (i % 3 == 0){
print(“Fizz “);
} else if (i % 5 == 0){
print(“Buzz “);
}
}
}
![Page 24: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/24.jpg)
24
public void run() {
int limit = readInt(“Limit? “);
for (int i = 1; i <= limit; i++){
if (i % 3 == 0 && i % 5 == 0){
print(“FizzBuzz “);
} else if (i % 3 == 0){
print(“Fizz “);
} else if (i % 5 == 0){
print(“Buzz “);
} else {
print(i + “ “);
}
}
}
![Page 25: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/25.jpg)
Assignment 2:Intro to Java!Due Date: Wed, Jul. 11, 2018 at 11 AM.
![Page 26: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/26.jpg)
Assignment 2▪ Consists of 4 console programs
▪ Applies concepts from lectures 4-6 (up to Tuesday’s lecture) and section 2.
▪ Done individually.
26
![Page 27: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/27.jpg)
1. Quadratic Formula
𝒙 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂
![Page 28: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/28.jpg)
Quadratic Formula
𝒙 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂
𝑎(assume nonzero)
𝑏 𝑐
readInt(prompt)
println(message)
Root(s)(do not round!)
![Page 29: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/29.jpg)
Discriminant𝜟 = 𝒃𝟐 − 𝟒𝒂𝒄
𝜟 > 𝟎 𝜟 = 𝟎 𝜟 < 𝟎
![Page 30: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/30.jpg)
Discriminant𝜟 = 𝒃𝟐 − 𝟒𝒂𝒄
𝜟 > 𝟎
Two real roots
𝜟 = 𝟎
One root
𝜟 < 𝟎
No real roots
![Page 31: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/31.jpg)
Quadratic Formula
➢ Assume 𝑎 ≠ 0.
➢ Assume 𝑎, 𝑏, and 𝑐 are integers.
➢ Do not round your answer(s).
31
double y = Math.sqrt(x);
Useful Concepts
▪ Conditionals
▪ Math with intand double.
▪ Reading input.
![Page 32: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/32.jpg)
2. Weather
Accuweather forecast for CA 94305
![Page 33: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/33.jpg)
WeatherPrompt until SENTINEL.
Print the following:• Highest temperature• Lowest temperature• Average temperature• Cold days (50 degrees or less)
![Page 34: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/34.jpg)
Weather
SENTINEL has value −1(value you should set as default).
![Page 35: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/35.jpg)
Weather
SENTINEL has value −42(one of many values you should test).
![Page 36: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/36.jpg)
Weather
SENTINEL has value −1
Highest, lowest, and average temperature are equal.
If only one temperature:
![Page 37: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/37.jpg)
Weather
SENTINEL has value −1
Print error message.
If no temperatures:
![Page 38: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/38.jpg)
Weather
➢ SENTINEL must be a constant.
➢ Assume inputs are integers.
➢ Do not round your answer(s).
➢ Output should match exactly.
38
Useful Concepts
▪ Fencepost.
▪ Scope.
▪ Sentinel loops.
![Page 39: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/39.jpg)
3. Hailstone Sequence
![Page 40: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/40.jpg)
Pick some positive integer and call it 𝒏.Do the following until 𝒏 is equal to 𝟏:• If 𝒏 is odd, multiply it by three and add one.• If 𝒏 is even, divide it by two.
Hailstone Sequence
17
![Page 41: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/41.jpg)
Pick some positive integer and call it 𝒏.Do the following until 𝒏 is equal to 𝟏:• If 𝒏 is odd, multiply it by three and add one.• If 𝒏 is even, divide it by two.
Hailstone Sequence
17 52make 3𝑛 + 1
![Page 42: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/42.jpg)
Pick some positive integer and call it 𝒏.Do the following until 𝒏 is equal to 𝟏:• If 𝒏 is odd, multiply it by three and add one.• If 𝒏 is even, divide it by two.
Hailstone Sequence
17 52make 3𝑛 + 1
26take half
![Page 43: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/43.jpg)
Pick some positive integer and call it 𝒏.Do the following until 𝒏 is equal to 𝟏:• If 𝒏 is odd, multiply it by three and add one.• If 𝒏 is even, divide it by two.
Hailstone Sequence
17 52 26 13 40 20
10 5 16 8 4 2
1
make 3𝑛 + 1 make 3𝑛 + 1
make 3𝑛 + 1
take half take half take half take half
take halftake halftake halftake halftake half
![Page 44: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/44.jpg)
Pick some positive integer and call it 𝒏.Do the following until 𝒏 is equal to 𝟏:• If 𝒏 is odd, multiply it by three and add one.• If 𝒏 is even, divide it by two.
Hailstone Sequence
17 52 26 13 40 20
10 5 16 8 4 2
1
make 3𝑛 + 1
It took 12 steps to reach 1.
make 3𝑛 + 1
make 3𝑛 + 1
take half take half take half take half
take halftake halftake halftake halftake half
![Page 45: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/45.jpg)
Hailstone Sequence
Must have a method to output a single Hailstone sequence.
![Page 46: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/46.jpg)
Hailstone Sequence
➢ Assume input is an integer.
➢ Output should match exactly(including all spaces on the console).
➢ Ask the user whether to play directly inside the while loop:
46
while (readBoolean(“Run again?”, “y”, “n”)) {
Useful Concepts
▪ Fencepost.
▪ Scope & loops.
▪ Binary Operators.
![Page 47: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/47.jpg)
4. Rocket
![Page 48: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/48.jpg)
Rocket
➢ Program is non-interactive.
➢ SIZE must be a constant.
➢ Assume SIZE is 2 or greater.
➢ Must use a nested for loop.
![Page 49: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/49.jpg)
SIZE has value 5(value you should set as default).
Rocket
![Page 50: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/50.jpg)
SIZE has value 3(one of many values you should test).
Rocket
![Page 51: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/51.jpg)
Rocket
➢ Decompose each part of the rocket.➢ No println() inside run()
➢ Output should match exactly
➢ Helpful Tips:➢ Make a table.➢ Solve the default size (5) before
using constant.
51
Useful Concepts
▪ Nested for loop.
▪ Constants.
▪ Decomposition.
![Page 52: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/52.jpg)
52
Example from Tuesday
![Page 53: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/53.jpg)
Note about Instance Variables
public class Example {
private static final int SIZE = 5; // constant
private int num = 0; // instance variable – bad!
public void run() {
int sum = 0; // local variable
}
}
For this assignment, don’t use non-constant variables declared outside of methods to get avoid having to deal with scope issues!
![Page 54: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/54.jpg)
Output Comparison Tool
Output should match exactly.
![Page 55: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/55.jpg)
Other Advice
➢ Read spec very carefully about requirements.
➢ Use constant, but no instance variables.
➢ Read the Assignment 2 style guide.
➢ Fix a bug, before moving on.
➢ Make sure output matches exactly (Output Comparison Tool).
➢ Test your programs extensively.
➢ Visit the LaIR if you get stuck.
➢ Incorporate feedback from Assignment 1!55
![Page 56: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/56.jpg)
Questions?56
![Page 57: YEAH 2: Simple Java! - Stanford Universityweb.stanford.edu › class › ... › assignments › Simple-Java... · Practice: FizzBuzz 17 Write a program that prints all of the numbers](https://reader033.vdocuments.net/reader033/viewer/2022060418/5f157c858ba5ad79670cfb55/html5/thumbnails/57.jpg)
Have fun!
57