diff --git a/src/com/geekhub/java/lessons/CalculatorFactorial.java b/src/com/geekhub/java/lessons/CalculatorFactorial.java new file mode 100644 index 0000000..5fe36e0 --- /dev/null +++ b/src/com/geekhub/java/lessons/CalculatorFactorial.java @@ -0,0 +1,15 @@ +package com.geekhub.java.lessons; + +import com.geekhub.java.lessons.exceptions.FactorialOfNegativeNumberException; + +public class CalculatorFactorial { + + public int getFactorial(int n) throws FactorialOfNegativeNumberException{ + + if (n < 0) throw new FactorialOfNegativeNumberException(); + + if (n == 0) return 1; + + return n * getFactorial(n - 1); + } +} diff --git a/src/com/geekhub/java/lessons/CalculatorFibonacci.java b/src/com/geekhub/java/lessons/CalculatorFibonacci.java new file mode 100644 index 0000000..f4015ab --- /dev/null +++ b/src/com/geekhub/java/lessons/CalculatorFibonacci.java @@ -0,0 +1,22 @@ +package com.geekhub.java.lessons; + +public class CalculatorFibonacci { + + public int[] getFibonacci(int n) { + + if (n <= 0) return new int[0]; + + int [] fib = new int[n]; + + fib[0] = 1; + if (n>1) { + fib[1] = 1; + } + + for (int i = 2; i < fib.length; i++) { + fib[i] = fib[i-2]+fib[i-1]; + } + + return fib; + } +} diff --git a/src/com/geekhub/java/lessons/Main.java b/src/com/geekhub/java/lessons/Main.java new file mode 100644 index 0000000..06e9e4a --- /dev/null +++ b/src/com/geekhub/java/lessons/Main.java @@ -0,0 +1,66 @@ +package com.geekhub.java.lessons; + +import com.geekhub.java.lessons.exceptions.FactorialOfNegativeNumberException; +import com.geekhub.java.lessons.exceptions.NotNumberException; + +import java.util.Arrays; +import java.util.Scanner; + +public class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + int n = 0; + int menuItem; + boolean run = true; + while (run) { + printMenu(); + if (in.hasNextInt()) { + menuItem = in.nextInt(); + + switch (menuItem) { + case 1: + CalculatorFactorial calculatorFactorial = new CalculatorFactorial(); + try { + System.out.println("input n:"); + n = in.nextInt(); + System.out.println(n + "! = " + calculatorFactorial.getFactorial(n)); + } catch (FactorialOfNegativeNumberException e) { + System.out.println("error: factorial of negative number"); + } + break; + case 2: + System.out.println("input n:"); + n = in.nextInt(); + CalculatorFibonacci calculatorFibonacci = new CalculatorFibonacci(); + int[] sequenceFib = calculatorFibonacci.getFibonacci(n); + System.out.println("sequence " + n + " number(s) fibonacci:"); + System.out.println(Arrays.toString(sequenceFib)); + break; + case 3: + System.out.println("input number:"); + n = in.nextInt(); + NumberToWord memberToWord = new NumberToWord(); + try { + System.out.println(n + " converted to " + memberToWord.toWord(n)); + } catch (NotNumberException e) { + System.out.println("value is not number"); + } + break; + default: + run = false; + } + } else run = false; + } + } + + private static void printMenu() { + System.out.println("==== Menu: ===="); + System.out.println("1 - calculate factorial"); + System.out.println("2 - calculate fibonacci"); + System.out.println("3 - number to word convert"); + System.out.println("any other symbol - exit"); + System.out.println("==============="); + System.out.println("select menu item:"); + } +} diff --git a/src/com/geekhub/java/lessons/NumberToWord.java b/src/com/geekhub/java/lessons/NumberToWord.java new file mode 100644 index 0000000..45cd719 --- /dev/null +++ b/src/com/geekhub/java/lessons/NumberToWord.java @@ -0,0 +1,34 @@ +package com.geekhub.java.lessons; + +import com.geekhub.java.lessons.exceptions.NotNumberException; + +public class NumberToWord { + + public String toWord(int n) throws NotNumberException { + + switch (n) { + case 0: + return "zero"; + case 1: + return "one"; + case 2: + return "two"; + case 3: + return "three"; + case 4: + return "four"; + case 5: + return "five"; + case 6: + return "six"; + case 7: + return "seven"; + case 8: + return "eight"; + case 9: + return "nine"; + default: + throw new NotNumberException(); + } + } +} diff --git a/src/com/geekhub/java/lessons/exceptions/FactorialOfNegativeNumberException.java b/src/com/geekhub/java/lessons/exceptions/FactorialOfNegativeNumberException.java new file mode 100644 index 0000000..3120221 --- /dev/null +++ b/src/com/geekhub/java/lessons/exceptions/FactorialOfNegativeNumberException.java @@ -0,0 +1,4 @@ +package com.geekhub.java.lessons.exceptions; + +public class FactorialOfNegativeNumberException extends Exception { +} diff --git a/src/com/geekhub/java/lessons/exceptions/NotNumberException.java b/src/com/geekhub/java/lessons/exceptions/NotNumberException.java new file mode 100644 index 0000000..219d04b --- /dev/null +++ b/src/com/geekhub/java/lessons/exceptions/NotNumberException.java @@ -0,0 +1,4 @@ +package com.geekhub.java.lessons.exceptions; + +public class NotNumberException extends Exception { +}