David Thornley 3d5f447011 completed | 6 yıl önce | |
---|---|---|
README.md | 6 yıl önce |
some simpler exercises for IFs and loops.
Yes, I am writing these on my phone, from I-70west in Ohio. No, I am not currently driving.
Some extra (easier) exercises.
Use them to practice writing both pseudo code and Java. Write each one in both.
Strive to feel good about the practice as you complete each one.
I suggest you write them into a notebook or onto paper. Once, you written them all, go thru them and type them into this file. You can use “atom” for that.
Then, if you’d like comments on your work, do a Pull Request and make the title of the PR “comments please”.
Write an IF statement that checks “player1.isAlive()” and if that’s false, calls “displayGameOver(player1)”
pseudo:
if(player1.isAlive() != true) { displayGameOver(player1); }
Write an IF statement that checks the “temperature(room)” and if that check is less than 70, calls “heatOn()” else calls “coolOn()”
pseudo:
Java:
if(temperature(room) < 70) { heatOn(room); } else { heatOff(room);}
Write an IF statement that checks “outsideTemp()” is less than 50 AND “insideTemp()” is less than 62, calls “startAFire(fireplace1)”
pseudo:
Java:
if(outsideTemp() < 50 && insideTemp() < 62) { startAFire(fireplace1); }
Write an IF statement that checks “fuelLevel” and if that check is less than 0.08, calls “refuel()”
pseudo:
Java:
if(fuelLevel < 0.08) { refuel(); }
For each loop you write, print “Hello” on each loop iteration.
pseudo:
Java:
for (int i = 0; i <=10; i++) { System.out.println("Hello"); }
Write a FOR loop that counts from 1 to 10.
pseudo:
Java:
for (int i = 1; i <= 10; i++) { System.out.println(i); }
Write a FOR loop that makes 10 iterations, start at 21
pseudo:
Java
for(int i = 21; i <= 30; i++) { System.out.println(i); }
Write a FOR loop that counts down from 100 to 0
pseudo:
Java:
for(int i = 100; i <= 0; i--) { System.out.println(i); }
Write a FOR loop from 0 to 32 by 2s
pseudo:
Java:
for(int i = 0; i >= 32; i +=2) {}
Write a FOR loop from 1 to less than 5001 by 11s.
pseudo:
Java:
for(int i = 1; i < 5001; i += 11) {}
Write a nested FOR loop(s), where one counts from 0 to less than 20 and the inner one counts from 0 to 4.
pseudo:
Java:
for(int i = 0; i < 20 ; i++){ for(int j = 0; j <= 4; j++){
} }
Write a FOR loop that counts from 5 to 105. Put an IF statement inside the loop that checks the loop index counter and if it’s greater than 51, prints “Hello Zipcode” instead on “hello”
pseudo:
Java:
for(int i = 5; i <= 105; i ++) { if(i > 51) {
System.out.println("Hello Zipcode");
} }
Write a WHILE loop that checks “gps.currentLocation()” and if that is not equal to “Home” then and it calls “driveSomeMore()”. After the loop is done, print “Honey, I’m Home!”
pseudo:
Java:
while( gps.currentLocation != "Home") { driveSomeMore(); }
System.out.println("Honey, I'm Home!");
First set “highestScore” to 0. Then set “currentScore” to “game.nextScore()”. Then write a WHILE loop that checks “currentScore” is greater than “highestScore” and if it is, sets “highestScore” to “currentScore” and then sets “currentScore” to “game.nextScore()”.
pseudo:
Java:
highestScore = 0; currentScore = game.nextScore();
while(highestScore > currentScore) {
currentScore = game.nextScore(); }
highestScore = currentScore; currentScore = game.nextScore();
Rewrite the previous WHILE loop as a REPEAT..UNTIL loop. Notice how the “currentScore” variable usage is different.
pseudo:
Java: do { if (currentScore < highestScore) {
currentScore = game.nextScore();
} else {
highestScore = currentScore
} } while (highestScore > currentScore);
Write a WHILE loop that checks “server.isRunning()” and if true calls “waitFor(5)” After the loop, write an IF and check “server.isRunning()” is false, and if so, call “sendEmergencyText(“Help!”, admin.iPhoneNumber)” and also calls “tryServerRestart()”
pseudo:
Java:
while (server.isRunning == true) { waitFor(5); }
if (server.isRunning != true) { sendEmergencyText("Help!", admin.iPhoneNumber); tryServerRestart(); }
Declare an “int” i. Set i to 7. Write a WHILE loop that checks “i” is less than 50, and if it is, add 7 to “i”
pseudo:
Java:
int i = 7; while(i < 50) { i += 7; }
Given an array voteTallies[], write a FOR loop that prints out each value in the array.
pseudo:
Java:
for( int i = 0; i <= voteTallies.length() - 1; i ++) { System.out.println(voteTallies[i]); }
Given an array voteTallies[], write a WHILE loop that prints out each value in the array. You should declare and use an index “idx” to keep track of where you are.
pseudo:
Java:
int idx = 0;
while(idx < (voteTallies.length() - 1)) { System.out.println(voteTallies[idx]); idx ++ ; }
Ponder this: can all FOR loops be rewritten as WHILE loops?
Ponder this: can all WHILE loops be rewritten as FOR loops?
Set “yardNeedsMowed” to true. Write WHILE loop that checks for “isSummer()”. inside the loop, write an IF that checks “yardNeedsMowed” and if true calls “yellAtJunior(chores.mowLawn())” After loop, call “sendJuniorToSchool(onTime)”
pseudo:
Java:
boolean yardNeedsMowed = true;
while (isSummer()) {
if (yardNeedsMowed == true) {
yellAtJunior(chores.mowLawn());
}
}
sendJuniorToSchool(onTime);