Cornelius Eanes's Programming Portal
Assignments (Strings)
[32] Making Decisions with If Statements

Goal: Learn about how to create If statements.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Making Decisions with If Statements
/// File Name: AgeMessages.java
/// Date Finished: Oct 08, 2015

import java.util.Scanner;

public class AgeMessages {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int age;

        System.out.println("How old are you?");
        age = input.nextInt();

        if (age < 13) {
            System.out.println("You are too young to create a Facebook account.");
        }

        if (age < 16) {
            System.out.println("You are too young to get a driver's license.");
        }

        if (age < 21) {
            System.out.println("You are too young to drink alcohol.");
        }

        if (age < 35) {
            System.out.println("You are too young to run for president of the USA.");
        }

    }

}

Output

[33] What If?

Goal: Figure out what If statements do.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: What If?
/// File Name: WhatIf.java
/// Date Finished: Oct 08, 2015

public class WhatIf {

    public static void main(String[] args) {

        int people = 20;
        int cats = people;
        int dogs = 15;

        if (people < cats) {
            System.out.println("There are too many cats!");
        }
        if (people > cats) {
            System.out.println("There are a normal amount of cats.");
        }

        if (people < dogs) {
            System.out.println("There are too many dogs!");
        }
        if (people > dogs) {
            System.out.println("There are a normal amount of dogs.");
        }

        dogs += 5;

        if (people >= dogs) {
            System.out.println("People are greater than or equal to dogs.");
        }
        if (people <= dogs) {
            System.out.println("People are less than or equal to dogs.");
        }

        if (people == dogs) {
            System.out.println("People are dogs.");
        }

        // The If Statement runs the code inside of the braces if the passed
        // conditional evaluates to true.

        // The braces determines what code is ran when the conditional does evaluate
        // to true. The ending brace signifies the end of the code block.

    }

}

Output

[34] How Old are You?

Goal: Create a little program using If statements.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: How Old are You?
/// File Name: HowOldAreYou.java
/// Date Finished: Oct 08, 2015

import java.util.Scanner;

public class HowOldAreYou {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String name;
        int age;

        System.out.print("Hello, what's your name? ");
        name = input.nextLine();

        System.out.print("OK, " + name + ", how old are you? ");
        age = input.nextInt();

        if (age < 16) {
            System.out.println("You can't drive.");
        }
        if (age < 18) {
            System.out.println("You can't vote.");
        }
        if (age < 25) {
            System.out.println("You can't rent a car.");
        }
        if (age >= 25) {
            System.out.println("You can do anything that's legal.");
        }

    }

}

Output

[35] Else and If

Goal: Learn about the Else-If and Else statements.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Else and If
/// File Name: ElseAndIf.java
/// Date Finished: Oct 08, 2015

public class ElseAndIf {

    public static void main(String[] args) {

        int people = 30;
        int cars = 40;
        int buses = 15;

        if (cars > people) {
            System.out.println("We should take the cars.");
        } else if (cars < people) {
            System.out.println("We should not take the cars.");
        } else {
            System.out.println("We can't decide.");
        }


        if (buses > cars) {
            System.out.println("That's too many buses.");
        }
        if (buses < cars) {
            System.out.println("Maybe we could take the buses.");
        } else {
            System.out.println("We still can't decide.");
        }


        if (people > buses) {
            System.out.println("All right, let's just take the buses.");
        } else {
            System.out.println("Fine, let's stay home then.");
        }

        // Else-If statements will be checked if the preceding If/Else-If statement
        // evaluates to false.

        // If none of the If nor Else-If Statement conditionals evaluates to true,
        // then whatever's in the Else code block (if there is one) is ran.

        // Removing an 'else' from an Else-If statement will guarantee that it will
        // be checked. As mentioned earlier, Else-If statements only get checked when
        // the preceding statements evaluate to false.

    }

}

Output

[36] Weekday Name

Goal: Print out what day of the week it is based on the given input.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Weekday Name
/// File Name: WeekdayName.java
/// Date Finished: Oct 14, 2015

import java.util.GregorianCalendar;

public class WeekdayName {

    public static String getWeekdayName(int weekday) {

        String result;
        if (weekday == 1) {
            result = "Sunday";
        } else if (weekday == 2) {
            result = "Monday";
        } else if (weekday == 3) {
            result = "Tuesday";
        } else if (weekday == 4) {
            result = "Wednesday";
        } else if (weekday == 5) {
            result = "Thursday";
        } else if (weekday == 6) {
            result = "Friday";
        } else if (weekday == 7) {
            result = "Saturday";
        } else {
            result = "error";
        }
        return result;
    }

    public static void main( String[] args ) {

        System.out.println("Weekday 1: " + getWeekdayName(1));
        System.out.println("Weekday 2: " + getWeekdayName(2));
        System.out.println("Weekday 3: " + getWeekdayName(3));
        System.out.println("Weekday 4: " + getWeekdayName(4));
        System.out.println("Weekday 5: " + getWeekdayName(5));
        System.out.println("Weekday 6: " + getWeekdayName(6));
        System.out.println("Weekday 7: " + getWeekdayName(7));
        System.out.println("Weekday 0: " + getWeekdayName(0));
        System.out.println();
        System.out.println("Weekday 43: " + getWeekdayName(43));
        System.out.println("Weekday 17: " + getWeekdayName(17));
        System.out.println("Weekday -1: " + getWeekdayName(-1));

        GregorianCalendar cal = new GregorianCalendar();
        int dow = cal.get(GregorianCalendar.DAY_OF_WEEK);

        System.out.println("\nToday is a " + getWeekdayName(dow) + "!");
    }

}

Output

[37] How Old are You, Specifically?

Goal: Condense the output from Assignment 34 into a single line.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: How Old are You, Specifically?
/// File Name: HowOld2.java
/// Date Finished: Oct 14, 2015

import java.util.Scanner;

public class HowOld2 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String name;
        int age;

        System.out.print("Hello, what's your name again? ");
        name = input.nextLine();

        System.out.print("Okay, " + name + ", how old are you? ");
        age = input.nextInt();

        if (age < 16) {
            System.out.println("You can't drive.");
        } else if (age < 18) {
            System.out.println("You can drive, but not vote.");
        } else if (age < 24) {
            System.out.println("You can vote, but not rent a car.");
        } else {
            System.out.println("You can do pretty much anything.");
        }

    }

}

Output

[38] Space Boxing

Goal: Calculate your weight on a specified planet.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Space Boxing
/// File Name: SpaceBoxing.java
/// Date Finished: Oct 14, 2015

import java.util.Scanner;

public class SpaceBoxing {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        double weight;
        int planetId;

        System.out.print("Please enter your current Earth weight: ");
        weight = input.nextDouble();

        System.out.println("I have information for the following planets:");
        System.out.println("\t1. Venus   2. Mars    3. Jupiter");
        System.out.println("\t4. Saturn  5. Uranus  6. Neptune");
        System.out.println();
        System.out.print("Which planet are you visiting? ");
        planetId = input.nextInt();

        if (planetId == 1) {
            weight *= 0.78;
        } else if (planetId == 2) {
            weight *= 0.39;
        } else if (planetId == 3) {
            weight *= 2.65;
        } else if (planetId == 4) {
            weight *= 1.17;
        } else if (planetId == 5) {
            weight *= 1.05;
        } else if (planetId == 6) {
            weight *= 1.23;
        } else {
            System.out.println("ERROR: Invalid planet ID: " + planetId);
            weight = 0;
        }

        System.out.println("Your weight would be " + weight + " [lbs] on that planet.");

    }

}

Output

[39] A Little Quiz

Goal: Quiz and score the user based on their answers.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: A Little Quiz
/// File Name: ALittleQuiz.java
/// Date Finished: Oct 14, 2015

import java.util.Scanner;

public class ALittleQuiz {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int choice1, choice2, choice3, numCorrect = 0;

        System.out.println("Q1) What do you do to properly compare two strings together?");
        System.out.println("\t1) Use the reference equals (==) operator.");
        System.out.println("\t2) Use the .equals(String) method from one string.");
        System.out.print("\n> ");

        choice1 = input.nextInt();
        System.out.println();
        if (choice1 == 2) {
            System.out.println("That's correct!");
            numCorrect++;
        } else {
            System.out.println("Sorry, that's incorrect!");
        }
        System.out.println();

        System.out.println("Q2) How many terms can a U.S. president run for, and for how long?");
        System.out.println("\t1) 4 terms / 2 years");
        System.out.println("\t2) 2 terms / 6 years");
        System.out.println("\t3) 2 terms / 4 years");
        System.out.print("\n> ");

        choice2 = input.nextInt();
        System.out.println();
        if (choice2 == 3) {
            System.out.println("That's correct!");
            numCorrect++;
        } else {
            System.out.println("Sorry, that's incorrect!");
        }
        System.out.println();

        System.out.println("Q3) What is the most common type of market in the United States today?");
        System.out.println("\t1) Differentiated Oligopoly");
        System.out.println("\t2) Pure Oligopoly");
        System.out.println("\t3) Differentiated Monopolistic");
        System.out.println("\t4) Pure Monopolistic");
        System.out.println("\t5) Monopoly");
        System.out.print("\n> ");

        choice3 = input.nextInt();
        System.out.println();
        if (choice3 == 3) {
            System.out.println("That's correct!");
            numCorrect++;
        } else {
            System.out.println("Sorry, that's incorrect!");
        }
        System.out.println();

        System.out.println("Looks like you got " + numCorrect + " / 3 correct.");
        System.out.println("Thanks for playing!");

    }

}

Output

[40] Modulus Animation

Goal: Create a little animation using modulus arithmetic and If statements.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Modulus Animation
/// File Name: ModulusAnimation.java
/// Date Finished: Oct 14, 2015

public class ModulusAnimation {

    public static void main(String[] args) throws Exception {

        int repeats = 5, framesPerSecond = 5, framesPerCycle = 14;

        for (int i = 0; i < framesPerCycle * repeats + 1; i++) {
            if (i % framesPerCycle == 0) {
                System.out.print(" ..ooOoo..     \r");
            } else if (i % framesPerCycle == 1) {
                System.out.print("  ..ooOoo..    \r");
            } else if (i % framesPerCycle == 2) {
                System.out.print("   ..ooOoo..   \r");
            } else if (i % framesPerCycle == 3) {
                System.out.print("    ..ooOoo..  \r");
            } else if (i % framesPerCycle == 4) {
                System.out.print("     ..ooOoo.. \r");
            } else if (i % framesPerCycle == 5) {
                System.out.print(" .    ..ooOoo. \r");
            } else if (i % framesPerCycle == 7) {
                System.out.print(" ..    ..ooOoo \r");
            } else if (i % framesPerCycle == 8) {
                System.out.print(" o..    ..ooOo \r");
            } else if (i % framesPerCycle == 9) {
                System.out.print(" oo..    ..ooO \r");
            } else if (i % framesPerCycle == 10) {
                System.out.print(" Ooo..    ..oo \r");
            } else if (i % framesPerCycle == 11) {
                System.out.print(" oOoo..    ..o \r");
            } else if (i % framesPerCycle == 12) {
                System.out.print(" ooOoo..    .. \r");
            } else if (i % framesPerCycle == 13) {
                System.out.print(" .ooOoo..    . \r");
            }
            Thread.sleep(1000 / framesPerSecond);

    }

}

Output

Of course, this is just a still image. I might replace it with a GIF.