diff --git a/.gitignore b/.gitignore index 539bdba..7345e19 100644 --- a/.gitignore +++ b/.gitignore @@ -53,4 +53,11 @@ local.properties # Scala IDE specific (Scala & Java development for Eclipse) .cache-main .scala_dependencies -.worksheet \ No newline at end of file +.worksheet +*.xml + +# IDE folders +.idea/ + +#iml +GitAssignmentObserver.iml diff --git a/README.md b/README.md new file mode 100644 index 0000000..100deff --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# Git Assignment for Team 15 + +| Name | ID | Task | Completed? | Bonus Design? | +| ----------- | ------------ | ----- | ---------- | ------------- | +| @Moustafa878 | 20170285 | SphereArea | :heavy_check_mark: | :x: | +| @devmagdyy | 20160219 | MultiplicationSeries | :heavy_check_mark: | :heavy_check_mark: | +| @amrmagdy12 | 20170188 | Fibonacci | :heavy_check_mark: | :heavy_check_mark: | +| @Marim-medhat | 20170282 | CircleArea | :heavy_check_mark: | :heavy_check_mark: | +| @AbdulelahAdam | 20170416 | SphereVolume | :heavy_check_mark: | :heavy_check_mark: | +| @mahmoud-mohamed-abozied | 20170270 | SummationSeries | :heavy_check_mark: | :heavy_check_mark: | +| @Abdulazizsayed | 20170154 | 2PowerN | :heavy_check_mark: | :x: | +| @ahmed-dardery | 20170034 | SphereCircumference | :heavy_check_mark: | :heavy_check_mark: | +| @Mina1998 | 20170307 | CircleVolume | :heavy_check_mark: | :heavy_check_mark: | +| @fatima196 | 20170193 | CircleCircumference | :heavy_check_mark: | :heavy_check_mark: | diff --git a/src/CircleArea.java b/src/CircleArea.java new file mode 100644 index 0000000..7c7c501 --- /dev/null +++ b/src/CircleArea.java @@ -0,0 +1,16 @@ +public class CircleArea extends DoubleSubscriber { + @Override + public void preProcess(int input) throws Exception { + System.out.print("Hello, I'm CircleArea , and I'm notified with: " + input + " as radius. "); + if (input < 0) throw new Exception("Negative radius is invalid!"); } + + @Override + public double doCalculation(int input) { + return Math.PI * input *input; + } + + @Override + public void printAnswer(double output) { + System.out.println("Circle Area is: " + output); + } +} diff --git a/src/CircleCircumference.java b/src/CircleCircumference.java new file mode 100644 index 0000000..393ab55 --- /dev/null +++ b/src/CircleCircumference.java @@ -0,0 +1,17 @@ +public class CircleCircumference extends DoubleSubscriber { + + @Override + public void preProcess(int input) throws Exception { + System.out.print("Hello, I'm CircleCircumference, and I'm notified with: " + input + " as radius. "); + if (input < 0) throw new Exception("Negative radius is invalid!"); } + + @Override + public double doCalculation(int input) { + return 2 * Math.PI * input; + } + + @Override + public void printAnswer(double output) { + System.out.println("Circle Circumference is: " + output); + } +} \ No newline at end of file diff --git a/src/CircleVolume.java b/src/CircleVolume.java new file mode 100644 index 0000000..4c2269f --- /dev/null +++ b/src/CircleVolume.java @@ -0,0 +1,17 @@ +public class CircleVolume extends DoubleSubscriber { + @Override + public void preProcess(int input) throws Exception { + System.out.print("Hello, I'm CircleVolume, and I'm notified with: " + input + " as radius. "); + if (input < 0) throw new Exception("Negative radius is invalid!"); + } + + public double doCalculation(int input) { + return (4.0 / 3.0) * Math.PI * Math.pow(input, 3); + } + + @Override + public void printAnswer(double output) { + System.out.println("Circle Volume is: " + output); + } + +} diff --git a/src/DoubleSubscriber.java b/src/DoubleSubscriber.java new file mode 100644 index 0000000..954466e --- /dev/null +++ b/src/DoubleSubscriber.java @@ -0,0 +1,13 @@ +public abstract class DoubleSubscriber implements ISubscriber { + public void notifySubscriber(int input) throws Exception { + preProcess(input); + double ans = doCalculation(input); + printAnswer(ans); + } + + public abstract void preProcess(int input) throws Exception; + + public abstract double doCalculation(int input); + + public abstract void printAnswer(double output); +} diff --git a/src/Fibonacci.java b/src/Fibonacci.java new file mode 100644 index 0000000..8fb5672 --- /dev/null +++ b/src/Fibonacci.java @@ -0,0 +1,49 @@ +public class Fibonacci extends IntegerSubscriber{ + + @Override + public void preProcess(int input) throws Exception { + + if(input < 1 ) throw new Exception("0 and Negative values are invalid ") ; + + System.out.println("I'am Fibonnacii and I'am notified with >> " + input); + } + + @Override + public int doCalculation(int n) { + + if (n == 1 || n == 2) return 1 ; + + int pascTriangle[][] = new int [n][n] ; + + // drawing pascal's triangle as a base for Fibonnacii + + for (int i = 2; i < n; i++) { + pascTriangle[i][0] = 1; + for (int j = 1; j <= i; j++) { + pascTriangle[i][j] = pascTriangle[i - 1][j - 1] + pascTriangle[i - 1][j]; + } + pascTriangle[i][i] = 1; + } + + int i = n - 1; + int j = 0; + int result = 0; + + while (j <= i) + { + result += pascTriangle[i][j]; + i--; + j++; + + } + + return result ; + + } + + @Override + public void printAnswer(int out) { + + System.out.println("the answer is " + out ); + } +} diff --git a/src/ISubscriber.java b/src/ISubscriber.java index d3a605c..bc7bb57 100644 --- a/src/ISubscriber.java +++ b/src/ISubscriber.java @@ -1,4 +1,4 @@ public interface ISubscriber { - public abstract void notifySubscriber(String input); + public abstract void notifySubscriber(int input) throws Exception; } diff --git a/src/IntegerSubscriber.java b/src/IntegerSubscriber.java new file mode 100644 index 0000000..32e28e3 --- /dev/null +++ b/src/IntegerSubscriber.java @@ -0,0 +1,13 @@ +public abstract class IntegerSubscriber implements ISubscriber { + public void notifySubscriber(int input) throws Exception { + preProcess(input); + int ans = doCalculation(input); + printAnswer(ans); + } + + public abstract void preProcess(int input) throws Exception; + + public abstract int doCalculation(int input); + + public abstract void printAnswer(int output); +} diff --git a/src/Main.java b/src/Main.java index 9b8c2b0..3587ba3 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,17 +1,59 @@ import java.util.Scanner; public class Main { - private static ISubscriber subscribers [] = { - new SimpleSubscriber(), - new ReallySimpleSubscriber(), - }; - public static void main(String[] args) { - Topic mathTopic = new Topic(); - for (ISubscriber sub : subscribers) { - mathTopic.addSubscriber(sub); - } - Scanner sc = new Scanner(System.in); - String input = sc.next(); - mathTopic.dispatchEvent(input); - } -} + private static ISubscriber subscribers[] = { + new MultiplicationSeries(), + new SummationSeries(), + + new CircleCircumference(), + new CircleArea(), + new CircleVolume(), + + new SphereCircumference(), + new SphereArea(), + new SphereVolume(), + + new Fibonacci(), + new TwoPowerN(), + }; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + Topic mathTopic = new Topic(); + for (ISubscriber sub : subscribers) { + mathTopic.addSubscriber(sub); + } + + try { + while (true) { + + + for (int i = 0; i < subscribers.length; ++i) { + System.out.println((i + 1) + " - " + subscribers[i].getClass().getName()); + } + System.out.println("0 - Run all functions"); + System.out.println("X - Exit application"); + + System.out.println("Choose your option:"); + String input = sc.next(); + if (input.equals("X")) break; + int choice = Integer.parseInt(input); + + System.out.println("Enter input number: "); + int n = Integer.parseInt(sc.next()); + + if (choice < 0 || choice > subscribers.length) throw new Exception("Invalid choice."); + if (choice != 0) + subscribers[choice - 1].notifySubscriber(n); + else + mathTopic.dispatchEvent(n); + + } + } catch (NumberFormatException ignored) { + System.out.println("Your input is not a valid integer."); + } catch (Exception ex) { + System.out.println(ex.getMessage()); + } + } +} \ No newline at end of file diff --git a/src/MultiplicationSeries.java b/src/MultiplicationSeries.java new file mode 100644 index 0000000..89a3c7f --- /dev/null +++ b/src/MultiplicationSeries.java @@ -0,0 +1,22 @@ +public class MultiplicationSeries extends IntegerSubscriber { + + @Override + public void preProcess(int input) throws Exception { + System.out.println("Hello, I'm MultiplicationSeries, and I'm notified with: " + input + " as input. "); + if (input < 0) throw new Exception("Negative input is invalid!"); + } + + @Override + public int doCalculation(int input) { + int result = 1; + for (int i = 1; i <= input; i++) { + result = result * i; + } + return result; + } + + @Override + public void printAnswer(int output) { + System.out.println("The Multiplication Series = " + output); + } +} \ No newline at end of file diff --git a/src/ReallySimpleSubscriber.java b/src/ReallySimpleSubscriber.java deleted file mode 100644 index fb1114a..0000000 --- a/src/ReallySimpleSubscriber.java +++ /dev/null @@ -1,8 +0,0 @@ - -public class ReallySimpleSubscriber implements ISubscriber { - @Override - public void notifySubscriber(String input) { - // TODO Auto-generated method stub - System.out.println("Hello, I am really a simple subscriber and I am notified with " + input); - } -} diff --git a/src/SimpleSubscriber.java b/src/SimpleSubscriber.java deleted file mode 100644 index 5b0e4aa..0000000 --- a/src/SimpleSubscriber.java +++ /dev/null @@ -1,10 +0,0 @@ - -public class SimpleSubscriber implements ISubscriber { - - @Override - public void notifySubscriber(String input) { - // TODO Auto-generated method stub - System.out.println("Hello, I am a simple subscriber and I am notified with " + input); - } - -} diff --git a/src/SphereArea.java b/src/SphereArea.java new file mode 100644 index 0000000..2eef3ce --- /dev/null +++ b/src/SphereArea.java @@ -0,0 +1,16 @@ + +public class SphereArea implements ISubscriber { + @Override + public void notifySubscriber(int input) { + + + double radius = input; + double Area = 4 * (Math.PI) * Math.pow(radius, 2); + System.out.println("The Area of Sphere = " + Area + " square meters"); + + + } + +} + + diff --git a/src/SphereCircumference.java b/src/SphereCircumference.java new file mode 100644 index 0000000..6b6868c --- /dev/null +++ b/src/SphereCircumference.java @@ -0,0 +1,16 @@ +public class SphereCircumference extends DoubleSubscriber { + @Override + public void preProcess(int input) throws Exception { + System.out.print("Hello, I'm SphereCircumference, and I'm notified with: " + input + " as radius. "); + if (input < 0) throw new Exception("Negative radius is invalid!"); + } + + public double doCalculation(int input) { + return 2 * Math.PI * input; + } + + @Override + public void printAnswer(double output) { + System.out.println("Sphere Circumference is: " + output); + } +} \ No newline at end of file diff --git a/src/SphereVolume.java b/src/SphereVolume.java new file mode 100644 index 0000000..950e542 --- /dev/null +++ b/src/SphereVolume.java @@ -0,0 +1,34 @@ +import java.lang.Math; +public class SphereVolume extends DoubleSubscriber { + + private static double value = 4.0/3.0; + + @Override + public void preProcess(int input) throws Exception { + System.out.print("Hello, I'm SphereVolume, and I'm notified with: " + input + " as radius. "); + if (input < 0) throw new Exception("Negative radius is invalid!"); + + } + + + + @Override + public double doCalculation(int input) { + return value*(Math.PI)*Math.pow(input, 3); + + } + + + + @Override + public void printAnswer(double output) { + System.out.println("Sphere Volume is: " + output); + + + } + + + + + +} diff --git a/src/SummationSeries.java b/src/SummationSeries.java new file mode 100644 index 0000000..36fd5d3 --- /dev/null +++ b/src/SummationSeries.java @@ -0,0 +1,19 @@ +public class SummationSeries extends IntegerSubscriber { + + @Override + public void preProcess(int input) throws Exception { + System.out.print("Hello, I'm SummationSeries, and I'm notified with: " + input + " as input. "); + if (input < 0) throw new Exception("Negative input is invalid!"); + } + + @Override + public int doCalculation(int input) { + return (input * (input + 1)) / 2; + } + + @Override + public void printAnswer(int output) { + System.out.println("The Summation Series = " + output); + } + +} diff --git a/src/Topic.java b/src/Topic.java index 8fa2e2e..f234958 100644 --- a/src/Topic.java +++ b/src/Topic.java @@ -2,24 +2,28 @@ import java.util.List; public class Topic { - - private List subscribers; - - public Topic() { - subscribers = new ArrayList(); - } - - public void addSubscriber(ISubscriber sub) { - subscribers.add(sub); - } - - public List getSubscribers() { - return subscribers; - } - - public void dispatchEvent(String input) { - for (ISubscriber sub : this.getSubscribers()) { - sub.notifySubscriber(input); - } - } + + private List subscribers; + + public Topic() { + subscribers = new ArrayList(); + } + + public void addSubscriber(ISubscriber sub) { + subscribers.add(sub); + } + + public List getSubscribers() { + return subscribers; + } + + public void dispatchEvent(int input) { + for (ISubscriber sub : this.getSubscribers()) { + try { + sub.notifySubscriber(input); + } catch (Exception ex) { + System.out.println(ex.getMessage()); + } + } + } } diff --git a/src/TwoPowerN.java b/src/TwoPowerN.java new file mode 100644 index 0000000..6fd5f82 --- /dev/null +++ b/src/TwoPowerN.java @@ -0,0 +1,13 @@ + +public class TwoPowerN implements ISubscriber { + @Override + public void notifySubscriber(int input) { + int p = input, res = 2; + + for (int i = 1; i < p; i++) { + res += res; + } + + System.out.println("Two power N is " + res); + } +} \ No newline at end of file