import java.util.Scanner;

public class Checker 
{
    public static void main(String[] args) 
    {
        int number;  
	
        // Create a Scanner object for keyboard input.  
        Scanner keyboard = new Scanner(System.in);  
             
        // Get a number from the user.  
        System.out.print("Enter a number in the range of 1 through 100: ");  
        number = keyboard.nextInt();  

        while ((number > 100 || number < 1))
        {  
           System.out.print("Invalid input. Enter a number in the range " +  
                            "of 1 through 100: ");  
           number = keyboard.nextInt();  
        } 
    }
}
public class LoopConversion 
{
    public static void main(String[] args) 
    {
        int count = 0;
        //convert to for loop
        for(int i = 0;i < 5; i++){
            System.out.println("count is " + ++count);
        }
    }
}

LoopConversion.main(null);
count is 1
count is 2
count is 3
count is 4
count is 5
for(int i = 0;i < 6;i++){
    System.out.println(i);
}

int j = 0;
while(j<6){
    System.out.println(j);
    j++;
}
public void recursionTry(int num){
    System.out.println(num);
    num++;
    if (num < 6) {
        recursionTry(num);
    }
}

recursionTry(0);
0
1
2
3
4
5
0
1
2
3
4
5
0
1
2
3
4
5

Homework - Part 1

Choose one or do both

  1. Write a program where the user inputs their monthly budget. The loop should then ask the user to input each of their monthly expenses. These expenses should be kept in a running total. The final output should display if the user is over or under their budget for the month, and by how much.
  2. Write a program where a random number is generated. Then the user tries to guess the number. If they guess too high display something to let them know, and same for if they guess a number that is too low. The loop must iterate until the number is guessed correctly.
import java.util.*;
import java.lang.Math;

public class GuesserGame {
    private int secret;
    private boolean game = false;

    public void guess(){
        secret = (int) (Math.random() * 500);
        int num = 0;
        int guesscount = 0;

        while (!game){
            System.out.println("Guess a number 1-500: ");

            // Scanner for int
            Scanner sc = new Scanner(System.in);
            num = sc.nextInt();
            System.out.print("" + num + " - ");
            // increment guesscount and check if guess is correct
            guesscount++;
            if (num == secret) {
                System.out.println("Congratulations! You got it right in " + guesscount + " guesses !");
                game = true;
            }
            else if (num > secret) {
                System.out.println("Your guess is too high!");
            }
            else if (num < secret) {
                System.out.println("Your guess is too low!");
            }
        }
    }

    public void guess(int num){
        
        
        
    }
}

GuesserGame a = new GuesserGame();
a.guess();
Guess a number 1-1000: 
500 - Your guess is too high!
Guess a number 1-1000: 
250 - Your guess is too high!
Guess a number 1-1000: 
125 - Your guess is too high!
Guess a number 1-1000: 
65 - Your guess is too low!
Guess a number 1-1000: 
80 - Your guess is too low!
Guess a number 1-1000: 
100 - Your guess is too low!
Guess a number 1-1000: 
120 - Your guess is too high!
Guess a number 1-1000: 
110 - Your guess is too high!
Guess a number 1-1000: 
105 - Your guess is too low!
Guess a number 1-1000: 
107 - Your guess is too high!
Guess a number 1-1000: 
106 - Congratulations! You got it right in 11 guesses !

Vocab found in this unit

  • Truth tables - can be used to determine the output of one or more boolean expressions. Start by writing a combination of one or two values for each condition, then writing the output that would happen when those two booleans are combined through what operator they have.
  • Compound boolean expressions - Involve the use of two or more booleans to satisfy a condition in code. In this case, we are using boolean expressions in the Checker class where we use (number > 100 || number < 1) to determine if the number is out of range, which needs one of two conditions to be met, hence the OR operator.
  • De Morgan's Law - States that the conditions met can be written in a different way and still give the same output. For example NOT(A or B) = NOT(A) and NOT(B). This is useful when trying to simplify boolean expressions to be more legible for yourself and for others.