Moving directories

master
sgoudham 4 years ago
parent 373c28e1b5
commit 0c25d47b36
No known key found for this signature in database
GPG Key ID: EF51A29A50FB754C

@ -1,28 +0,0 @@
public class Adder extends CalculateBase implements MathProcessing {
public Adder() {}
public Adder(double leftVal, double rightVal) {
super(leftVal, rightVal);
}
@Override
public void calculate() {
setResult(getLeftVal() + getRightVal());
}
// Using interfaces
@Override
public String getKeyWord() {
return "add";
}
@Override
public double doCalculation(double leftVal, double rightVal) {
setLeftVal(leftVal);
setRightVal(rightVal);
calculate();
return getResult();
}
}

@ -1,43 +0,0 @@
public abstract class CalculateBase {
private double leftVal;
private double rightVal;
private double result;
public abstract void calculate();
public CalculateBase() {}
public CalculateBase(double leftVal, double rightVal) {
this.leftVal = leftVal;
this.rightVal = rightVal;
}
// Getters and setters
public double getLeftVal() {
return leftVal;
}
public void setLeftVal(double leftVal) {
this.leftVal = leftVal;
}
public double getRightVal() {
return rightVal;
}
public void setRightVal(double rightVal) {
this.rightVal = rightVal;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
}

@ -1,13 +0,0 @@
public class Divider extends CalculateBase {
public Divider() {}
public Divider(double leftVal, double rightVal) {
super(leftVal, rightVal);
}
@Override
public void calculate() {
setResult(getLeftVal() / getRightVal());
}
}

@ -1,29 +0,0 @@
public class DynamicHelper {
private final MathProcessing[] handlers;
public DynamicHelper(MathProcessing[] handlers) {
this.handlers = handlers;
}
public void process(String statement) {
String[] parts = statement.split(MathProcessing.SEPARATOR);
String keyword = parts[0];
double leftVal = Double.parseDouble(parts[1]);
double rightVal = Double.parseDouble(parts[2]);
MathProcessing theHandler = null;
for(MathProcessing handler : handlers) {
if(keyword.equalsIgnoreCase(handler.getKeyWord())) {
theHandler = handler;
break;
}
}
double result = theHandler.doCalculation(leftVal, rightVal);
if (theHandler.doFormatting() != null)
System.out.println(theHandler.doFormatting());
else
System.out.println("result = " + result);
}
}

@ -1,109 +0,0 @@
public class MathEquation {
private double leftVal;
private double rightVal;
private char opCode;
private double result;
private static int numOfCalculations;
private static double sumOfResults;
// Empty constructor to allow us to use MathEquation without needing to pass in anything
public MathEquation() {}
// This only exists as a demo for chaining constructors
public MathEquation(char opCode) {
this.opCode = opCode;
}
// Demonstrating how chaining constructors works
public MathEquation(char opCode, double leftVal, double rightVal) {
this(opCode);
this.leftVal = leftVal;
this.rightVal = rightVal;
}
// Method to execute the calculations
public void execute() {
switch (this.opCode) {
case 'a':
this.result = this.leftVal + this.rightVal;
break;
case 's':
this.result = this.leftVal - this.rightVal;
break;
case 'm':
this.result = this.leftVal * this.rightVal;
break;
case 'd':
this.result = this.rightVal != 0 ? this.leftVal / this.rightVal : 0.0d;
break;
default:
System.out.println("Invalid opCode: " + this.opCode);
this.result = 0.0d;
break;
}
numOfCalculations++;
sumOfResults += result;
}
public void execute(double leftVal, double rightVal) {
this.leftVal = leftVal;
this.rightVal = rightVal;
execute();
}
public void execute(int leftVal, int rightVal) {
this.leftVal = leftVal;
this.rightVal = rightVal;
execute();
result = (int) result;
}
public static double getAverageResult() {
return sumOfResults / numOfCalculations;
}
public MathEquation(double leftVal, double rightVal, char opCode, double result) {
this.leftVal = leftVal;
this.rightVal = rightVal;
this.opCode = opCode;
this.result = result;
}
// Setters
public double getLeftVal() {
return leftVal;
}
public void setLeftVal(double leftVal) {
this.leftVal = leftVal;
}
public double getRightVal() {
return rightVal;
}
public void setRightVal(double rightVal) {
this.rightVal = rightVal;
}
public char getOpCode() {
return opCode;
}
public void setOpCode(char opCode) {
this.opCode = opCode;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
}

@ -1,6 +0,0 @@
public enum MathOperation {
ADD,
SUBTRACT,
DIVIDE,
MULTIPLY
}

@ -1,9 +0,0 @@
public interface MathProcessing {
String SEPARATOR = " ";
String getKeyWord();
double doCalculation(double leftVal, double rightVal);
default String doFormatting() {
return null;
}
}

@ -1,13 +0,0 @@
public class Multiplier extends CalculateBase {
public Multiplier() {}
public Multiplier(double leftVal, double rightVal) {
super(leftVal, rightVal);
}
@Override
public void calculate() {
setResult(getLeftVal() * getRightVal());
}
}

@ -1,125 +0,0 @@
import java.util.Scanner;
public class NewCalcEngine {
public static void main(String[] args) {
// performCalculations();
// executeCalculations();
// executeInteractively();
dynamicInteractivity();
}
private static void dynamicInteractivity() {
DynamicHelper dynamicHelper = new DynamicHelper(new MathProcessing[] {
new Adder(),
new PowerOf()
});
Scanner in = new Scanner(System.in);
System.out.println("Enter In Your Operation Please: \n");
String userInput = in.nextLine();
in.close();
dynamicHelper.process(userInput);
}
private static void executeInteractively() {
Scanner in = new Scanner(System.in);
System.out.println("Please Enter In A Calculation: \n");
String userInput = in.nextLine();
in.close();
String parts[] = userInput.split(" ");
performOperation(parts);
}
private static void performOperation(String[] parts) {
MathOperation mathOperation = MathOperation.valueOf(parts[0].toUpperCase());
double leftVal = Double.parseDouble(parts[1]);
double rightVal = Double.parseDouble(parts[2]);
CalculateBase calculateBase = createCalculation(mathOperation, leftVal, rightVal);
calculateBase.calculate();
System.out.println("\nOperation Performed: " + mathOperation);
System.out.println("Result: " + calculateBase.getResult());
}
private static CalculateBase createCalculation(MathOperation mathOperation, double leftVal, double rightVal) {
CalculateBase calculation = null;
switch (mathOperation) {
case ADD:
calculation = new Adder(leftVal, rightVal);
break;
case MULTIPLY:
calculation = new Multiplier(leftVal, rightVal);
break;
case DIVIDE:
calculation = new Divider(leftVal, rightVal);
break;
case SUBTRACT:
calculation = new Subtractor(leftVal, rightVal);
break;
}
return calculation;
}
private static void executeCalculations() {
CalculateBase[] calculations = {
new Divider(100.0d, 50.0d),
new Adder(25.0d, 92.0d),
new Subtractor(225.0d, 17.0d),
new Multiplier(11.0d, 3.0d)
};
System.out.println("Array Calculations");
for (CalculateBase calculation: calculations) {
calculation.calculate();
System.out.println("Result: " + calculation.getResult());
}
}
private static void doCalculation(CalculateBase calculate, double leftVal, double rightVal) {
calculate.setLeftVal(leftVal);
calculate.setRightVal(rightVal);
calculate.calculate();
System.out.println("The result of the calculation is: " + calculate.getResult());
}
private static void performCalculations() {
MathEquation[] mathEquation = new MathEquation[4];
mathEquation[0] = new MathEquation('d', 100.0d, 50.0d);
mathEquation[1] = new MathEquation('a', 25.0d, 92.0d);
mathEquation[2] = new MathEquation('s', 225.0d, 17.0d);
mathEquation[3] = new MathEquation('m', 11.0d, 3.0d);
for (MathEquation equation : mathEquation) {
equation.execute();
System.out.println("Result = " + equation.getResult());
}
System.out.println("Average Result = " + MathEquation.getAverageResult());
System.out.println("\nUsing Execute Method Overloads\n");
MathEquation mathEquationOverload = new MathEquation('d');
double leftDouble = 9.0d;
double rightDouble = 4.0d;
mathEquationOverload.execute(leftDouble, rightDouble);
System.out.println("Overloaded result with doubles: " + mathEquationOverload.getResult());
int leftInt = 9;
int rightInt = 4;
mathEquationOverload.execute(leftInt, rightInt);
System.out.println("Overloaded result with ints: " + mathEquationOverload.getResult());
}
}

@ -1,21 +0,0 @@
public class PowerOf implements MathProcessing {
private String formattedOutput;
@Override
public String getKeyWord() {
return "power";
}
@Override
public String doFormatting() {
return formattedOutput;
}
@Override
public double doCalculation(double leftVal, double rightVal) {
double product = Math.pow(leftVal, rightVal);
formattedOutput = leftVal + " Has Been Raised To The Power Of " + rightVal + " Which Results In: " + product;
return product;
}
}

@ -1,13 +0,0 @@
public class Subtractor extends CalculateBase {
public Subtractor() {}
public Subtractor(double leftVal, double rightVal) {
super(leftVal, rightVal);
}
@Override
public void calculate() {
setResult(getLeftVal() - getRightVal());
}
}

@ -1,9 +0,0 @@
package src;
public interface Animal {
String animalName();
String animalSound();
double animalPrice();
}

@ -1,38 +0,0 @@
package src;
public class AnimalHelper {
private final Animal[] animals;
private double averagePrice;
public AnimalHelper(Animal[] animals) {
this.animals = animals;
}
public void displayInformation() {
/* Display the animal information */
int total = 0;
double sum = 0.0;
for (Animal animal : animals) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("\n\nName: " + animal.animalName());
stringBuilder.append("\nSound: " + animal.animalSound());
stringBuilder.append("\nPrice: £" + animal.animalPrice());
System.out.println(stringBuilder.toString());
total++;
sum += animal.animalPrice();
}
averagePrice = sum / total;
}
public void displayAveragePrice() {
/* Display the average price of all the animals */
System.out.printf("%nThe Average Price For All The Animals Are: £%.2f", averagePrice);
}
}

@ -1,17 +0,0 @@
package src;
public class AnimalMain {
public static void main(String[] args) {
displayAnimals();
}
private static void displayAnimals() {
// Creating an array of animal objects
AnimalHelper helper = new AnimalHelper(new Animal[] { new Cow(), new Cat(), new Dog(), new Horse() });
// Displaying information about the animals
helper.displayInformation();
helper.displayAveragePrice();
}
}

@ -1,20 +0,0 @@
package src;
public class Cat implements Animal {
@Override
public String animalName() {
return "Cat";
}
@Override
public String animalSound() {
return "Meoooww";
}
@Override
public double animalPrice() {
return 29.99;
}
}

@ -1,20 +0,0 @@
package src;
public class Cow implements Animal {
@Override
public String animalName() {
return "Cow";
}
@Override
public String animalSound() {
return "Moooo";
}
@Override
public double animalPrice() {
return 10.99;
}
}

@ -1,20 +0,0 @@
package src;
public class Dog implements Animal {
@Override
public String animalName() {
return "Dog";
}
@Override
public String animalSound() {
return "Woof Woof";
}
@Override
public double animalPrice() {
return 15.99;
}
}

@ -1,20 +0,0 @@
package src;
public class Horse implements Animal {
@Override
public String animalName() {
return "Horse";
}
@Override
public String animalSound() {
return "Neighh";
}
@Override
public double animalPrice() {
return 59.99;
}
}

@ -1,18 +0,0 @@
package src;
public class ByFives implements Counter {
private int num = 5;
private int nextNum;
@Override
public int countBy() {
return num;
}
@Override
public int getNextNum() {
return nextNum += num;
}
}

@ -1,18 +0,0 @@
package src;
public class ByFours implements Counter {
private int num = 4;
private int nextNum;
@Override
public int countBy() {
return num;
}
@Override
public int getNextNum() {
return nextNum += num;
}
}

@ -1,18 +0,0 @@
package src;
public class ByOnes implements Counter {
private int num = 1;
private int nextNum;
@Override
public int countBy() {
return num;
}
@Override
public int getNextNum() {
return nextNum += num;
}
}

@ -1,18 +0,0 @@
package src;
public class ByThrees implements Counter {
private int num = 3;
private int nextNum;
@Override
public int countBy() {
return num;
}
@Override
public int getNextNum() {
return nextNum += num;
}
}

@ -1,18 +0,0 @@
package src;
public class ByTwos implements Counter {
private int num = 2;
private int nextNum;
@Override
public int countBy() {
return num;
}
@Override
public int getNextNum() {
return nextNum += num;
}
}

@ -1,7 +0,0 @@
package src;
public interface Counter {
int countBy();
int getNextNum();
}

@ -1,34 +0,0 @@
package src;
public class CounterHelper {
private Counter[] counters;
private Counter userCounter = null;
public CounterHelper(Counter... counters) {
this.counters = counters;
}
public void doInteractive(int userInput) {
// Find the correct counter to count by.
// When no counter is found, display error message to user and quit the program
for (Counter counter: counters) {
if (userInput == counter.countBy())
this.userCounter = counter;
}
if (userCounter == null) {
System.out.println("Counter Not Found");
System.exit(0);
}
}
public void displayCounter(int iterations) {
// Show the counter for the number of iterations that the user inputted
System.out.println();
for (int i = 0; i < iterations; i++) {
System.out.println(userCounter.getNextNum());
}
}
}

@ -1,40 +0,0 @@
package src;
import java.util.Scanner;
public class CounterMain {
public static void main(String[] args) {
countUp();
countUpInteractively();
}
private static void countUp() {
ByTwos twos = new ByTwos();
ByFives fives = new ByFives();
// Display the first 5 numbers for each counter defined
for (int i = 0; i < 5; i++) {
System.out.println("Counting By " + twos.countBy());
System.out.println(twos.getNextNum());
System.out.println("Counting By " + fives.countBy());
System.out.println(fives.getNextNum());
}
}
private static void countUpInteractively() {
Scanner in = new Scanner(System.in);
System.out.println("Enter In The Number That You Want To Count:\n");
int userInput = in.nextInt();
CounterHelper helper = new CounterHelper(new ByOnes(), new ByTwos(), new ByThrees(), new ByFours(),
new ByFives());
helper.doInteractive(userInput);
System.out.println("Enter In The Number Of Times You Want To Count:\n");
int userIterations = in.nextInt();
helper.displayCounter(userIterations);
in.close();
}
}
Loading…
Cancel
Save