If I Were a Rich Man: Making Decisions with Code

12.01.2018 |

Episode #5 of the course An introduction to Java programming by Alex Theedom

 

Welcome to the fifth of ten lessons about the Java programming language. I hope you are still excited from the last lesson. Let’s get coding. Today, you are going to learn how to make decisions with code.

Step 1: Preparation

• Go to the online IDE, codiva.io, sign in, and click the New Project button.

• Call today’s project, Lesson 5.

• Create a class file called, WhatIf.

• Delete the line, System.out.println(“Hello Codiva”);.

Now you are ready to start coding.

 

Step 2: Start Coding

You will use the if(condition){ … code … } statement to write code that makes a decision about which piece of code to execute based on a condition being true or false. So, what kind of decision does a program need to make? Well, here are a few examples:

• If age is more than 13, then you can join the website.

• If score is more than 80, then you pass the test.

• If temperature is less than 60 degrees Fahrenheit, then switch on the heater.

• If account balance less than 0, then reject payment.

• If gasoline level equals four gallons, then stop filling tank.

• If password is correct, give access to the site.

All of these if statements can be converted into Java code and used in a program. You are going to implement the last example. The code you write will ask for a password and test if that password is correct using an if statement.

To start, you need to write code that asks for a password. Remember back to the second lesson in which you wrote code that asked a question and then stored that question. You need to write very similar code this time that uses the Scanner class.

• Go back to Lesson 2 for a quick refresher. The code you write should look something like this:

import java.util.Scanner;
class WhatIf {
   public static void main(String[] args) {

     Scanner scanner = new Scanner(System.in);
     System.out.print(“Enter your password: “);
     String password = scanner.next();

   }
}

Notice that the text entered by the user is stored in the String variable called password.

• Now you need to write code that checks the text entered with the password. The if statement has the form: if(condition) { … code … }, and you want to test if the text entered is equal to password. You will use the equals() method to compare the value stored in the password variable against the actual password secret101. The code you write looks like this: if(password.equals(“secret101”)){ … code … }.

If the password variable contains the text, secret101, then the code between the braces is run. If not, the code between the braces is not run.

Your code should look like this:

import java.util.Scanner;
class WhatIf {
   public static void main(String[] args) {

     Scanner scanner = new Scanner(System.in);
     System.out.print(“Enter your password: “);
     String password = scanner.next();

     if(password.equals(“secret101”)){
     }

   }
}

• At the moment, the if statement doesn’t have any code to run, so let’s add a simple welcome message that prints to the console. Something like this: System.out.println(“Welcome to the secret site”);.

 

Step 3: Check and Run Your Program

Here is the code in full:

import java.util.Scanner;
class WhatIf {
   public static void main(String[] args) {

     Scanner scanner = new Scanner(System.in);
     System.out.print(“Enter your password: “);
     String password = scanner.next();

     if(password.equals(“secret101”)){
       System.out.println(“Welcome to the secret site”);
     }

   }
}

Run your program by clicking the Run button. Enter the password, secret101, and if all went well, you will see a welcome message printed to the console.

 

Step 4: Extend Your WhatIf Program

What happens if you don’t enter the correct password? It would be good to run code that also shows a message. This is possible by adding an else clause to the if statement. It looks like this: if(condition){ … code if true … } else { … code if false … }. Now you can add code that prints a message informing the user that the password was wrong.

 

Step 5: Check and Run Your Program

Here is the code in full:

import java.util.Scanner;
class WhatIf {
   public static void main(String[] args) {

     Scanner scanner = new Scanner(System.in);
     System.out.print(“Enter your password: “);
     String password = scanner.next();

     if(password.equals(“secret101”)){
       System.out.println(“Welcome to the secret site”);
     } else {
       System.out.println(“Sorry, you can’t come in”);
     }

   }
}

Run your program. Enter an incorrect password to see the alternative message.

Congratulations, you have successfully completed your fifth Java program!

In Lesson 6, you will learn how to write code that generates a random number. So, see you then.

 

Recommended book

Starting Out with Java: From Control Structures through Objects by Tony Gaddis

 

Share with friends