Goal: Print something related to whatever the user types in.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: 2 Questions
/// File Name: TwoQuestions.java
/// Date Finished: Oct 15, 2015
import java.util.Scanner;
public class TwoQuestions {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String choice1, choice2, result;
System.out.println("TWO QUESTIONS!\nThink of an object, and I'll try to guess it.\n");
System.out.print("Question 1: Is it an animal, vegetable, or mineral?\n> ");
choice1 = input.nextLine();
System.out.print("\nQuestion 2: Is it bigger than a breadbox?\n> ");
choice2 = input.nextLine();
if (choice1.equalsIgnoreCase("animal")) {
if (choice2.equalsIgnoreCase("yes")) {
result = "giraffe";
} else {
result = "mouse";
}
} else if (choice1.equalsIgnoreCase("vegetable")) {
if (choice2.equalsIgnoreCase("yes")) {
result = "watermelon";
} else {
result = "carrot";
}
} else {
if (choice2.equalsIgnoreCase("yes")) {
result = "smart car";
} else {
result = "paperclip";
}
}
System.out.println("\nYou were probably thinking of a " + result + ".");
}
}
Goal: Create a little choose-your-own-adventure game.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Choose Your Own Adventure
/// File Name: ChooseYourAdventure.java
/// Date Finished: Oct 15, 2015
import java.util.Scanner;
public class ChooseYourAdventure {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] choices = new String[3]; // doing this the fancy way
System.out.print("You find yourself in the middle of a long road. You don't recall how you got here or where you are. Do you \"walk\" or \"wait\"?\n> ");
choices[0] = input.nextLine();
if (choices[0].equalsIgnoreCase("wait")) {
System.out.print("A pickup truck appears to be driving towards you. Do you \"hitchhike\" or \"ignore\"?\n> ");
choices[1] = input.nextLine();
if (choices[1].equalsIgnoreCase("hitchhike")) {
System.out.print("You hold your hand out, sticking your thumb out. The car stops, and a strange-looking man is driving. Do you get in? \"Yes\" or \"no\"?\n> ");
choices[2] = input.nextLine();
if (choices[2].equalsIgnoreCase("yes")) {
System.out.println("You were wrong to judge the man. He happily takes you back to civilization and you now have a new friend.\n\nGOOD END");
} else {
System.out.println("The man scoffs at you for wasting his time. No other cars come by for 3 days. You die of dehydration.\n\nBAD END");
}
} else {
System.out.print("You ignore the pickup truck driving by. He whizzes past you. You find a water bottle near you. It's capped off, but half-empty. Do you drink it? \"Yes\" or \"no\"?\n> ");
choices[2] = input.nextLine();
if (choices[2].equalsIgnoreCase("yes")) {
System.out.println("The water happened to contain cholera. You slowly die of a violent coughing fit.\n\nBAD END");
} else {
System.out.println("Too bad, because three days later, you don't find any other water sources. You die of dehydration.\n\nBAD END");
}
}
} else {
System.out.print("You start walking along the road. You happen to encounter a bear besides the road. Do you \"run\" or \"intimidate\" the bear?\n> ");
choices[1] = input.nextLine();
if (choices[1].equalsIgnoreCase("run")) {
System.out.print("You run into the nearby woods to escape from the bear. The bear slowly starts to catch up. Do you \"throw\" your backpack at it or \"continue\" running?\n> ");
choices[2] = input.nextLine();
if (choices[2].equalsIgnoreCase("throw")) {
System.out.println("Throwing your backpack at the bear caused it to stumble and fall. However, you didn't notice the cliff up ahead. You fall down, breaking your neck.\n\nBAD END");
} else {
System.out.println("It's no use. The bear catches up to you and has a tasty meal out of you.\n\nBAD END");
}
} else {
System.out.print("The bear appears confused. Seems like you made the right decision. Do you slowly \"back away\" or \"continue\" intimidating the bear?\n> ");
choices[2] = input.nextLine();
if (choices[2].equalsIgnoreCase("back away")) {
System.out.println("While backing away from the bear, the bear runs away from you. However, you don't notice the oncoming pickup truck slamming into you.\n\nBAD END");
} else {
System.out.println("The bear notices that you aren't so mighty after all. Before you can react, he makes a tasty meal out of you.\n\nBAD END");
}
}
}
System.out.println("\nThank you for playing!");
}
}
Goal: Instead of Else-If statements, use compound boolean expressions.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Age Messages 3
/// File Name: AgeMessages3.java
/// Date Finished: Oct 16, 2015
import java.util.Scanner;
public class AgeMessages3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name;
int age;
System.out.print("So, what is your name? ");
name = input.nextLine();
System.out.print("All right, " + name + ", how old are you? ");
age = input.nextInt();
if (age < 16) {
System.out.println("You can't drive.");
}
if (age >= 16 && age < 18) {
System.out.println("You can drive, but not vote.");
}
if (age >= 18 && age < 24) {
System.out.println("You can vote, but not rent a car.");
}
if (age >= 24) {
System.out.println("You can do pretty much anything.");
}
}
}
Goal: Instead of Else-If statements, use compound boolean expressions.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Two More Questions
/// File Name: TwoMoreQuestions.java
/// Date Finished: Oct 16, 2015
import java.util.Scanner;
public class TwoMoreQuestions {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String choice1, choice2, result = "";
System.out.print("Does it stay \"inside\", \"outside\", or \"both\"? ");
choice1 = input.nextLine();
System.out.print("Is it a living thing? \"Yes\" or \"no\"? ");
choice2 = input.nextLine();
if (choice1.equalsIgnoreCase("inside") && choice2.equalsIgnoreCase("yes")) {
result = "houseplant";
}
if (choice1.equalsIgnoreCase("inside") && choice2.equalsIgnoreCase("no")) {
result = "shower curtain";
}
if (choice1.equalsIgnoreCase("outside") && choice2.equalsIgnoreCase("yes")) {
result = "bison";
}
if (choice1.equalsIgnoreCase("outside") && choice2.equalsIgnoreCase("no")) {
result = "billboard";
}
if (choice1.equalsIgnoreCase("both") && choice2.equalsIgnoreCase("yes")) {
result = "dog";
}
if (choice1.equalsIgnoreCase("both") && choice2.equalsIgnoreCase("no")) {
result = "cell phone";
}
System.out.println("\nYou were probably thinking of a " + result + ".");
}
}
Goal: Determine the BMI value and category of the user.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: BMI Categories
/// File Name: BMICategories.java
/// Date Finished: Oct 16, 2015
import java.util.Scanner;
public class BMICategories {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double height, weight, bmiValue;
String category;
System.out.print("Your height in meters: ");
height = input.nextDouble();
System.out.print("Your weight in kilograms: ");
weight = input.nextDouble();
bmiValue = weight / Math.pow(height, 2.0d);
System.out.println("Your BMI is " + bmiValue);
if (bmiValue <= 18.5) {
category = "underweight";
} else if (bmiValue <= 24.9) {
category = "normal weight";
} else if (bmiValue <= 29.9) {
category = "overweight";
} else {
category = "obese";
}
System.out.println("BMI Category: " + category);
}
}
Goal: Decide how to properly call someone.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Gender Game
/// File Name: GenderGame.java
/// Date Finished: Oct 16, 2015
import java.util.Scanner;
public class GenderGame {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String gender, firstName, lastName, isMarried, result;
int age;
System.out.print("What is your gender (M/F)? ");
gender = input.nextLine();
System.out.print("What is your first name? ");
firstName = input.nextLine();
System.out.print("What is your last name? ");
lastName = input.nextLine();
System.out.print("Are you married (Y/N)? ");
isMarried = input.nextLine();
System.out.print("How old are you? ");
age = input.nextInt();
if (age > 20) {
if (gender.equalsIgnoreCase("m")) {
result = "Mr. " + lastName;
} else {
if (isMarried.equalsIgnoreCase("y")) {
result = "Mrs. " + lastName;
} else {
result = "Ms. " + lastName;
}
}
} else {
result = firstName + " " + lastName;
}
System.out.println("\nThen I shall call you " + result + ".");
}
}
Goal: Learn what the compareTo(String) method does.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: compareTo() Challenge
/// File Name: CompareToChallenge.java
/// Date Finished: Oct 16, 2015
public class CompareToChallenge {
public static void main(String[] args) {
String[] strings = new String[] {
"apple", "bear",
"keyboard", "mouse",
"theater", "theatre",
"computer", "monitor",
"book", "teacup",
"transport", "translate",
"motherboard", "keyboard",
"centre", "center",
"kangaroo", "coffee",
"duly", "dull",
"apple", "apple",
"jump", "jump"
};
for (int i = 0; i < strings.length; i += 2) {
System.out.println("Comparing \"" + strings[i] + "\" with \"" + strings[i + 1] + "\" produces " + strings[i].compareTo(strings[i + 1]) + ".");
}
}
}
Goal: Determine how long someone has to wait in a alphabetical line.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Alphabetical Order
/// File Name: AlphabeticalOrder.java
/// Date Finished: Oct 19, 2015
import java.util.Scanner;
public class AlphabeticalOrder {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name;
System.out.print("What's your last name? ");
name = input.nextLine();
if (name.compareTo("Carswell") < 0) {
System.out.println("You don't have to wait long.");
} else if (name.compareTo("Jones") < 0) {
System.out.println("That's not bad.");
} else if (name.compareTo("Smith") < 0) {
System.out.println("Looks like a bit of a wait.");
} else if (name.compareTo("Young") < 0) {
System.out.println("It's gonna be a while");
} else {
System.out.println("You're not going anywhere for a while.");
}
}
}
Goal: Make a simple number-guessing game.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: The Worst Number Guessing Game Ever
/// File Name: GuessingGame.java
/// Date Finished: Oct 19, 2015
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int correct = 6, guess;
System.out.print("I'm thinking of a number from 0 - 10. What is it? ");
guess = input.nextInt();
if (guess == correct) {
System.out.println("Correct! The answer was " + correct + "!");
} else {
System.out.println("Wrong! The correct answer was " + correct + "!");
}
}
}