From 92a6ea7c81f950142894a904ae38d057d8cd9b2f Mon Sep 17 00:00:00 2001 From: Inkotasti Date: Wed, 27 Jul 2016 23:12:06 +0300 Subject: [PATCH 1/2] PrintDate --- .idea/codeStyleSettings.xml | 9 +++++++ ch04/PrintDate.java | 16 ++++++++++++ ch05/BottlesOfBeer.java | 52 +++++++++++++++++++++++++++++++++++++ ch05/FermatTheorem.java | 11 ++++++++ ch05/GuessStarter.java | 48 ++++++++++++++++++++++++++++++++++ dump/UsefulMethods.java | 35 +++++++++++++++++++++++++ 6 files changed, 171 insertions(+) create mode 100644 .idea/codeStyleSettings.xml create mode 100644 ch04/PrintDate.java create mode 100644 ch05/BottlesOfBeer.java create mode 100644 ch05/FermatTheorem.java create mode 100644 ch05/GuessStarter.java create mode 100644 dump/UsefulMethods.java 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..a7a95ba --- /dev/null +++ b/dump/UsefulMethods.java @@ -0,0 +1,35 @@ +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; + } + + /** + * @param n + * @return Returns every odd integer sum from n to 1. + */ + + 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; + } +} From 670a0d9071e4196800c9fd8418e1e82dc9bf540c Mon Sep 17 00:00:00 2001 From: Inkotasti Date: Wed, 27 Jul 2016 23:26:43 +0300 Subject: [PATCH 2/2] Some documentation for testing --- dump/UsefulMethods.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dump/UsefulMethods.java b/dump/UsefulMethods.java index a7a95ba..28d9dc5 100644 --- a/dump/UsefulMethods.java +++ b/dump/UsefulMethods.java @@ -12,8 +12,10 @@ public static boolean isDecimal(int x) { } /** - * @param n - * @return Returns every odd integer sum from n to 1. + * 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) {