diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml new file mode 100644 index 0000000..c4c9543 --- /dev/null +++ b/.idea/codeStyleSettings.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/ch04/PrintDate.java b/ch04/PrintDate.java new file mode 100644 index 0000000..84273ce --- /dev/null +++ b/ch04/PrintDate.java @@ -0,0 +1,16 @@ +public class PrintDate { + public static void main (String[] args){ + printAmerican("Monday", "25", "July", "2016"); + printEuropean("Monday", "25", "July", "2016"); + } + + public static void printAmerican(String day, String date, String month, String year){ + System.out.print("American Time: "); + System.out.println(day + ", " + month + " " + date + ", " + year); + } + + public static void printEuropean(String day, String date, String month, String year){ + System.out.print("European Time: "); + System.out.println(day + ", " + date + " " + month + " " + year); + } +} diff --git a/ch05/BottlesOfBeer.java b/ch05/BottlesOfBeer.java new file mode 100644 index 0000000..81eeed8 --- /dev/null +++ b/ch05/BottlesOfBeer.java @@ -0,0 +1,52 @@ +import java.util.Scanner; + +public class BottlesOfBeer { + + public static void main(String[] args) { + int num; + Scanner in = new Scanner(System.in); + + System.out.println("How many verses of the song you would like to display?"); + System.out.print("Input the number (decimal): "); + num = in.nextInt(); + System.out.println("Here we go!"); + System.out.println(); + + printSong(num); + } + + public static void printSong(int salms) { + if (salms > 0) { + printVerse(salms); + salms -= 1; + // for proper last but one verse. 1 bottle, not 1 bottles + if (salms == 1){ + printOneBottleVerse(); + salms -= 1; + } + if (salms == 0) { + printLastVerse(); + } + printSong(salms); + } + } + + public static void printVerse(int x) { + System.out.println(x + " bottles of beer on the wall,"); + System.out.println(x + " bottles of beer,"); + System.out.println("ya' take one down, ya' pass it around,"); + } + + public static void printOneBottleVerse(){ + System.out.println("1 bottle of beer on the wall,"); + System.out.println("1 bottle of beer,"); + System.out.println("ya' take one down, ya' pass it around,"); + } + + public static void printLastVerse() { + System.out.println("No bottles of beer on the wall,"); + System.out.println("no bottles of beer,"); + System.out.println("ya' can't take one down, ya' can't pass it around,"); + System.out.println("cause there are no more bottles of beer on the wall!"); + } +} diff --git a/ch05/FermatTheorem.java b/ch05/FermatTheorem.java new file mode 100644 index 0000000..914e302 --- /dev/null +++ b/ch05/FermatTheorem.java @@ -0,0 +1,11 @@ + +public class FermatTheorem { + + public static void checkFermat(int x, int y, int z, int n) { + if (n > 2 || (Math.pow(x, n) + Math.pow(y, n) == Math.pow(z, n))){ + System.out.println("Holy smokes, Fermat was wrong! "); + }else{ + System.out.println("No. That doesn't work."); + } + } +} \ No newline at end of file diff --git a/ch05/GuessStarter.java b/ch05/GuessStarter.java new file mode 100644 index 0000000..a19c369 --- /dev/null +++ b/ch05/GuessStarter.java @@ -0,0 +1,48 @@ +import java.util.Random; +import java.util.Scanner; + +/** + * Starter code for the "Guess My Number" exercise. + */ +public class GuessStarter { + + public static void main(String[] args) { + + Scanner in = new Scanner(System.in); + Random random = new Random(); + int number = random.nextInt(100) + 1; + int guess; + + System.out.println("Can you guess what number I am thinking of?"); + System.out.println("I am thinking of a decimal number between 1 - 100, both included"); + System.out.print("Take a guess. Input your number: "); + guess = in.nextInt(); + System.out.println("So your guess is " + guess + ". Interesting!"); + guessLoop(guess, number); + } + + public static void decimalChecker(double x) { + if ((x % 1) != 0) { + System.out.println("This is not decimal. Please enter decimal number as your guess"); + } + } + + public static void guessChecker(int guess, int number) { + if (guess > number) { + System.out.print("Your guess was too high. \nTry Again :"); + } else { + System.out.print("Your guess was too low. \nTry Again: "); + } + } + + public static void guessLoop(int guess, int number) { + Scanner in = new Scanner(System.in); + if (guess != number) { + guessChecker(guess, number); + int newGuess = in.nextInt(); + guessLoop(newGuess, number); + } else { + System.out.println("Congrats. You got it right. I was indeed thinking of number " + number); + } + } +} \ No newline at end of file diff --git a/dump/UsefulMethods.java b/dump/UsefulMethods.java new file mode 100644 index 0000000..28d9dc5 --- /dev/null +++ b/dump/UsefulMethods.java @@ -0,0 +1,37 @@ +import java.util.Scanner; + +public class UsefulMethods { + + public static void main(String[] args) { + System.out.println(oddSum(8)); + + } + + public static boolean isDecimal(int x) { + return x % 2 == 0; + } + + /** + * Sums every odd integer starting from n to 1. Accepts only odd integer. Loops until user does so. + * + * @param n the integer where sum starts + * @return returns odd integers sum. + */ + + public static int oddSum(int n) { + Scanner in = new Scanner(System.in); + if (n % 2 != 0 && n != 1 && n > 0) { + int sum = n + (n - 2); + n -= 2; + int result = oddSum(n)+ sum; + return result; + + } else if (n % 2 == 0){ + System.out.println("Please enter odd decimal! :"); + int x = in.nextInt(); + oddSum(x); + return x; + } + return 0; + } +}