Created
September 15, 2008 17:18
-
-
Save ELLIOTTCABLE/10894 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.lang.Math; | |
public class Exercises { | |
static Scanner keyboard = new Scanner(System.in); | |
public static void main(String[] args) { | |
System.out.println("I wasn't sure which excercises I was supposed to do, " | |
+ "so I did all the actual programming ones."); | |
// Each of these lines will announce an excercise and then run it | |
announceExcercise(6); runExcerciseSix(); | |
announceExcercise(7); runExcerciseSeven(); | |
announceExcercise(8); runExcerciseEight(); | |
announceExcercise(9); runExcerciseNine(); | |
announceExcercise(10); runExcerciseTen(); | |
announceExcercise(16); runExcerciseSixteen(); | |
announceExcercise(17); runExcerciseSeventeen(); | |
} | |
/** | |
* Prints an annoucement that a certain-numbered excercise is about to be | |
* run. | |
* TODO: Figure out how to metaprogrammatically call an excercise's method | |
* as well. | |
*/ | |
public static void announceExcercise(int excercise) { | |
System.out.println("Enter any key to execute exercise " + excercise + "..."); // \n | |
keyboard.next(); // We discard this value, as the purpose of this line is to wait for the user | |
System.out.println(""); // \n | |
System.out.println("Excercise " + excercise + ":"); | |
} | |
// --- -- --- * --- -- --- // | |
/** | |
* Executes and prints the answer to excercise 6. | |
*/ | |
public static void runExcerciseSix() { | |
int age; | |
age = 20; | |
System.out.println("My age is"); | |
System.out.println(19); | |
System.out.println("You just saw:"); | |
System.out.println("My age is\\n19"); | |
} | |
/** | |
* Executes excercise 7. | |
*/ | |
public static void runExcerciseSeven() { | |
System.out.println("1"); | |
System.out.println("2"); | |
System.out.println("3"); | |
} | |
/** | |
* Executes excercise 8. | |
*/ | |
public static void runExcerciseEight() { | |
System.out.println("Please enter your age:"); | |
int age; | |
age = keyboard.nextInt(); | |
// Let's run some validation on the user input, just to be safe... | |
if((age > 0) && (age < 120)) { | |
System.out.println("You are apparently " + age + " years old."); | |
} else { | |
System.out.println("Don't be silly!"); | |
} | |
} | |
/** | |
* Executes excercise 9. | |
*/ | |
public static void runExcerciseNine() { | |
// TODO: Figure out how to get the current date from the system | |
// First we need to know what year it is... | |
System.out.println("Please enter the current year: (yyyy)"); | |
int currentYear = keyboard.nextInt(); | |
// Some validation on the currentYear user entry | |
if(currentYear < 2008) { | |
// Since this was written in 2008, I know it's now or later. | |
System.out.println("Hah. Nice try."); | |
return; | |
} | |
// Then their year of birth | |
System.out.println("Please enter your year of birth: (yyyy)"); | |
int yearBorn = keyboard.nextInt(); | |
// Some validation on the yearBorn user entry | |
if(yearBorn > currentYear) { | |
System.out.println("What are you, from the future?"); | |
return; | |
} | |
int age = currentYear - yearBorn; | |
// More validation, now on the combination of the two user inputs so far. | |
if(age < 120) { | |
System.out.println("You are apparently " + age + " years old."); | |
} else { | |
System.out.println("Don't be silly!"); | |
return; | |
} | |
System.out.println("Finally, enter your age at the year you're looking for:"); | |
int target = keyboard.nextInt(); | |
int result = yearBorn + target; | |
if(age < target) { | |
System.out.println("Okay! According to my calculations, you will be " | |
+ target + " years old in... " + result + "!"); | |
} else if(age > target) { | |
System.out.println("Okay! According to my calculations, you were " | |
+ target + " years old in... " + result + "!"); | |
} else { | |
System.out.println("You are *currently* " + target + " years old!"); | |
} | |
} | |
/** | |
* Executes excercise 10. | |
*/ | |
public static void runExcerciseTen() { | |
System.out.println("Please enter one integer, then hit return, and enter another:"); | |
int firstInt = keyboard.nextInt(); | |
int secondInt = keyboard.nextInt(); | |
// We're looking for a 'distance' on the number line, aka an absolute value | |
int diff = Math.abs(secondInt - firstInt) + 1; | |
System.out.println("There are " + diff + " integers between " | |
+ firstInt + " and " + secondInt); | |
} | |
/** | |
* Executes excercise 16. | |
*/ | |
public static void runExcerciseSixteen() { | |
System.out.println("Please enter one integer, then hit return, and enter another:"); | |
int firstInt = keyboard.nextInt(); | |
int secondInt = keyboard.nextInt(); | |
// We're looking for a 'distance' on the number line, aka an absolute value | |
int diff = Math.abs(secondInt - firstInt) + 1; | |
System.out.println("There are " + diff + " integers between " | |
+ firstInt + " and " + secondInt); | |
} | |
/** | |
* Executes excercise 17. | |
* TODO: Figure out how to acquire an array of abitrary size from user input. | |
*/ | |
public static void runExcerciseSeventeen() { | |
int[] arr = {5, 80, 12, 19, 56}; | |
int largest = 0; | |
// This, while wordy, should be a little faster than the traditional | |
// for(){} loop used to iterate arrays. Saves you checking the bounds. | |
try { | |
for(int i = 0 ;; i++) { | |
// TODO: Profile to figure out if it's more efficient to... | |
// 1. Initialize a needless variable, but only have to call arr[i] | |
// once per iteration, even when the block *is* executed! | |
// 2. Call arr[i] both places, thus having to call it twice when the | |
// block is actually executed | |
int val = arr[i]; | |
if(largest < val) { largest = val; } | |
} | |
} catch(ArrayIndexOutOfBoundsException aioobe) {} | |
System.out.println("The largest is " + largest + "."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment