Copy this fragment to the run() method of an exercise in IntelliJ.
Try to run it.
int age = 10; if (age < 10) { SaxionApp.printLine("too young"); } SaxionApp.printLine("The end");
Change the code so the text "too young" only appears when the age is less than 18.
int age = 10;
if (age < 18) { // the number in the condition should change
SaxionApp.printLine("too young");
}
SaxionApp.printLine("The end");
Change it that the text appears when age is less than or equal to 18. Test it with age 10, age 18 and age 20.
int age = 10; // change this to 18 or 20 to test the program with other values
if (age <= 18) { // instead of "smaller than", <, it is now "smaller than or equal to", <=
SaxionApp.printLine("too young");
}
Make sure to print "too old" when age is more than 80. Test it with age 10, 80 and 85
int age = 10; // change this to 80 or 85 to test the program with other values
if (age > 80) { // the condition is change to "more than", >, and the number is also changed.
SaxionApp.printLine("too old"); // this line should also have been changed
}
SaxionApp.printLine("The end");
Make a new program. Use a variable (int) lengthInCm to store a person's length. Set it to 160 and make sure to check this length and print "Person is tall" when the length is larger than 190. Test it with length 160 and 200.
int lengthInCm = 160; // change to 200 to test the program with other values
if (lengthInCm > 190) {
SaxionApp.printLine("Person is tall");
}
SaxionApp.printLine("The end");
We have seen that if the condition is false, the body of the if-statement is not executed. If that is the case, we can use the else
statement to offer an alternative part of the program that will be executed if the first condition is not true.
Example:
if (someVariable == 1) { // do things } else { // do other things }
Change the program you created in the previous level, to print "Person is tiny" when the if-statement becomes false. Test the program with different values.
int lengthInCm = 160; // change to 200 to test the program with other values
if (lengthInCm > 190) {
SaxionApp.printLine("Person is tall");
} else { // add this else-statement
SaxionApp.printLine("Person is tiny");
}
SaxionApp.printLine("The end");
Now change the program so you ask the user to input their size with readInt()
.
SaxionApp.printLine("What is your length in cm?");
int lengthInCm = SaxionApp.readInt();
if (lengthInCm > 190) {
SaxionApp.printLine("Person is tall");
} else { // add this else-statement
SaxionApp.printLine("Person is tiny");
}
SaxionApp.printLine("The end");
It is also possible to add more conditions to the if-else statement, using else if
. Example:
if (someVariable == 1) { // do something } else if (someVariable == 2) { // do something else } else { // give up }
Change your program so it prints "Person has an average length" if the person is not taller than 190, but is taller than 170. Add an else if
statement for this, look at the example above.
SaxionApp.printLine("What is your length in cm?");
int lengthInCm = SaxionApp.readInt();
if (lengthInCm > 190) {
SaxionApp.printLine("Person is tall");
} else if (lengthInCm > 170) { // add this else if statement
SaxionApp.printLine("Person has an average length");
} else {
SaxionApp.printLine("Person is tiny");
}
SaxionApp.printLine("The end");
Create a new program that asks the user for a number between 1 and 5. After that, use if
and multiple else if
s to select an inspirational quote to show to the user, based on the number they chose. If they chose a different number, show an insulting quote (using else
). Add a small pause to make it look like the program is "thinking" about which quote to show. Example:
You need inspiration! Choose a number: > 5 Finding a personalized quote for you ... Trying is the first step toward failure.
SaxionApp.print("You need inspiration! Please select a number: ");
int quote = SaxionApp.readInt();
SaxionApp.printLine("Finding a personalized quote for you... ");
SaxionApp.sleep(2);
if (quote == 1) {
SaxionApp.printLine("Everything happens for a reason. Sometimes the reason is you're stupid and make bad decisions.");
} else if (quote == 2) {
SaxionApp.printLine("Success is just failure that hasn't happened yet.");
} else if (quote == 3) {
SaxionApp.printLine("Eagles may soar, but weasels don't get sucked into jet engines.");
} else if (quote == 4) {
SaxionApp.printLine("Never underestimate the power of stupid people in large groups.");
} else if (quote == 5) {
SaxionApp.printLine("Trying is the first step toward failure.");
} else {
// wrong number was chosen
SaxionApp.printLine("I'm busy right now, can I ignore you another time?");
}
When the user selects the wrong number, we immediately proceed to insult them. But maybe it was a mistake, and we should give them a second chance. Change the program: instead of printing the insult, ask the user to try again. If the number is too small, show the insult. If it is too large, also show the insult. Otherwise show one of the quotes.
SaxionApp.print("You need inspiration! Please select a number: ");
int quote = SaxionApp.readInt();
SaxionApp.printLine("Finding a personalized quote for you... ");
SaxionApp.sleep(2);
if (quote == 1) {
SaxionApp.printLine("Everything happens for a reason. Sometimes the reason is you're stupid and make bad decisions.");
} else if (quote == 2) {
SaxionApp.printLine("Success is just failure that hasn't happened yet.");
} else if (quote == 3) {
SaxionApp.printLine("Eagles may soar, but weasels don't get sucked into jet engines.");
} else if (quote == 4) {
SaxionApp.printLine("Never underestimate the power of stupid people in large groups.");
} else if (quote == 5) {
SaxionApp.printLine("Trying is the first step toward failure.");
} else {
// wrong number was chosen
// let user try again here:
SaxionApp.printLine("The number was incorrect. Please try again:");
quote = SaxionApp.readInt();
if (quote < 1) {
// too small
SaxionApp.printLine("I'm busy right now, can I ignore you another time?");
} else if (quote > 5) {
// too large
SaxionApp.printLine("I'm busy right now, can I ignore you another time?");
} else {
SaxionApp.printLine("Light travels faster than sound. This is why some people appear bright until you hear them speak.");
}
}
Comparing strings is done in a different way than numbers. For comparing string, use .equals("text to compare")
and .equalsIgnoreCase("tExT to COMpaRe")
. The last one ignores capitalization when comparing.
Create a new program that asks the user a question: "what is the capital of the Netherlands?". Check the answer with .equalsIgnoreCase
, and show a message whether the user was correct or not using if
and else
.
SaxionApp.printLine("What is the capital of the Netherlands?");
String answer = SaxionApp.readString();
if (answer.equalsIgnoreCase("amsterdam")) {
SaxionApp.printLine("Correct");
} else {
SaxionApp.printLine("Incorrect");
}
What happens if the user answers aMSterDAm?
Try the same but first change the program so .equals()
is used instead of .equalsIgnoreCase()
. What happens now?
Sometimes we only want one character from the user, for example "y" or "n" on a question that requires a yes or no answer. Or is you want one of the characters WASD for steering in a game. A single character can be stored in the char
type. You can ask a single character input using the .readChar()
method in the SaxionApp.
Confusingly, if you're using the char type, you actually should compare with ==. For example:
char input = SaxionApp.readChar(); if (input == 'w') { // go north } else if (input == 'a') { // go west (life is peaceful there) } // etc
Create a new program that asks the user to press a key to specificy what we should draw on screen: a line (l), a circle (c) or a rectangle (r). In an if-statement, make sure the correct shape is drawn. If the user selects the wrong character, draw this text in a LARGE FONT on the screen: "\uD83D\uDCA9"
SaxionApp.print("What to draw? (c)ircle, (l)ine or (r)ectangle > ");
char answer = SaxionApp.readChar();
if (answer == 'c') {
SaxionApp.drawCircle(300, 300, 200);
} else if (answer == 'l') {
SaxionApp.drawLine(10, 10, 300, 300);
} else if (answer == 'r') {
SaxionApp.drawRectangle(10, 10, 400, 400);
} else {
// draw the unicode character "Pile of Poo" on screen
SaxionApp.drawText("\uD83D\uDCA9", 150, 150, 200);
}
When we are using many if
and else if
statements, it can often be replaced by the switch
statement. Example:
char input = SaxionApp.readChar(); switch (input) { case 'w': // go north break; case 'a': // go west break; // etc default: // do something when none of the case // statements matches the input break; }
Change your program so it uses a switch statement instead of the if else if
list.
SaxionApp.print("What to draw? (c)ircle, (l)ine or (r)ectangle > ");
char answer = SaxionApp.readChar();
switch (answer) {
case 'c':
SaxionApp.drawCircle(300, 300, 200);
break;
case 'l':
SaxionApp.drawLine(10, 10, 300, 300);
break;
case 'r':
SaxionApp.drawRectangle(10, 10, 400, 400);
break;
default:
// draw the unicode character "Pile of Poo" on screen
SaxionApp.drawText("\uD83D\uDCA9", 150, 150, 200);
break;
}
Change your program and remove the "break" statements. Now test the program and press "c". What happens? Can you explain why that is?