Goal: Return the square root of a number, only if it's positive or zero.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Safe Square Root
/// File Name: SafeSquareRoot.java
/// Date Finished: Jan 13, 2016
import java.util.Scanner;
public class SafeSquareRoot {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double num;
System.out.println("SQUARE ROOT!");
System.out.print("Enter a number: ");
// combines initialization and comparing
while ((num = input.nextDouble()) < 0) {
System.out.println("Sorry, you can't take the square root of a negative number!");
System.out.print("Enter a number: ");
}
System.out.println(Math.sqrt(num));
}
}
Goal: Check if three given member lengths can make up a right triangle.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Right Triangle Checker
/// File Name: RightTriangleChecker.java
/// Date Finished: Jan 13, 2016
import java.util.Scanner;
public class RightTriangleChecker {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a, b, c;
System.out.println("Enter three integers:");
System.out.print("Side 1: ");
a = input.nextInt();
do {
System.out.print("Side 2: ");
b = input.nextInt();
if (b < a) {
System.out.println(b + " is smaller than " + a + ". Try again.");
}
} while (b < a);
do {
System.out.print("Side 3: ");
c = input.nextInt();
if (c < b) {
System.out.println(c + " is smaller than " + b + ". Try again.");
}
} while (c < b);
System.out.println("Your three sides are " + a + ", " + b + ", and " + c + ".");
if (a * a + b * b == c * c) {
System.out.println("These sides do make a right triangle.");
} else {
System.out.println("These sides to NOT make a right triangle.");
}
}
}
Goal: Check the validity of the Collatz sequence.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Collatz Sequence
/// File Name: CollatzSequence.java
/// Date Finished: Jan 13, 2016
import java.util.Scanner;
public class CollatzSequence {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n, steps = 0;
do {
System.out.print("Starting number: ");
n = input.nextInt();
if (n < 0) {
System.out.println("Must be a natural number (greater than / equal to 0).");
}
} while (n < 0);
while (n != 1) {
if (n % 2 == 0) {
n = n / 2;
} else {
n = n * 3 + 1;
}
// Makes the printed output prettier, pads the number with 6 spaces
System.out.print(String.format("%6s", n));
steps++;
if (steps % 8 == 0) {
System.out.println();
}
}
}
}
Goal: Create a Choose-Your-Own-Adventure game, now with loops.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Short Adventure 2: With a Loop
/// File Name: LoopingAdventure.java
/// Date Finished: Feb 09, 2016
import java.util.Scanner;
public class LoopingAdventure {
public static void main(String[] agrs) {
Scanner input = new Scanner(System.in);
int room = 1, choice = -1;
boolean noticeFound = false, teacherFound = false;
System.out.println("NOTE: Any input that doesn't follow the prompt will default to the last option.");
System.out.println("You've been told to give an important notice to a teacher, Mr. Anderson. However, you don't what room he's in.");
while (room != 0) {
if (room == 1) {
System.out.println("You're looking down the hallway outside your classroom, where the second years are.");
if (!noticeFound && choice != -1) {
System.out.println("You see something on the ground. It's the notice! You must have dropped it.");
noticeFound = true;
}
System.out.println("Do you go (1) upstairs, (2) downstairs, or (3) to the next door class?");
choice = input.nextInt();
if (choice == 1) {
room = 2;
} else if (choice == 2) {
room = 3;
} else {
room = 4;
}
} else if (room == 2) {
System.out.println("You're looking down the hallway of the third years. Do you go (1) back downstairs, or (2) into one of the nearby classrooms?");
choice = input.nextInt();
if (choice == 1) {
room = 1;
} else {
room = 5;
}
} else if (room == 3) {
System.out.println("You're looking down the hallway of the first years. Do you go (1) back upstairs, or (2) into a nearby classroom?");
choice = input.nextInt();
if (choice == 1) {
room = 1;
} else {
room = 6;
}
} else if (room == 4) {
System.out.println("You open the door, and find all the students writing something down, the teacher at the podium.");
System.out.println("Do you (1) excuse yourself and go back into the hallway, or (2) ask the teacher where Mr. Anderson is?");
choice = input.nextInt();
if (choice == 1) {
room = 1;
} else {
room = 7;
}
} else if (room == 5) {
System.out.println("This classroom is empty. Reserved for the drama club after school. You step back into the hallway.");
room = 2;
} else if (room == 6) {
System.out.println("You look into one of the classrooms. There's a male teacher on what looks like his prep period.");
System.out.println("Do you (1) excuse yourself and step back outside, or (2) ask where Mr. Anderson is?");
choice = input.nextInt();
if (choice == 1) {
room = 3;
} else {
room = 8;
}
} else if (room == 7) {
System.out.println("As it turns out, the students were taking an important test, and the teacher was already having a bad day.");
System.out.println("You're promptly sent to the office for causing a disruption.");
System.out.println("BAD END");
room = 0;
} else if (room == 8) {
if (!teacherFound) {
System.out.print("As it turns out, this is Mr. Anderson! ");
teacherFound = true;
}
if (!noticeFound) {
System.out.println("You reach to grab the notice, but your fingers go right through your pocket.");
System.out.println("You excuse yourself and step back into the hallway, now needing to find the notice.");
room = 3;
} else {
System.out.println("You hand Mr. Anderson the notice. You return back to your homeroom.");
System.out.println("SUCCESS END");
room = 0;
}
}
}
System.out.println("\nThanks for playing!");
}
}