Skip to content

Commit

Permalink
Incarnation#2 package is created with calculations using library
Browse files Browse the repository at this point in the history
methods.
  • Loading branch information
m_japa authored and m_japa committed Jul 29, 2016
1 parent 53edc92 commit 2b9faa8
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.concordia.soen6441.src;
package com.concordia.soen6441.incarnation1;

public class CheersConfig {
public class CheersConfig_I1 {
public final static double ALPHA = 2.304129659127962;
public final static int PRECISION_OUTPUT_MIN = 1;
public final static int PRECISION_OUTPUT_MAX = 3;
public final static int RADIUS_MIN = 2;
public final static int RADIUS_MAX = 5;
public final static int PRECISION_OUTPUT_MAX = 5;
// public final static int RADIUS_MIN = 2;
// public final static int RADIUS_MAX = 5;
public final static double STRAIGHT_ANGLE_SIZE = 180.0;

public final static double SQRT_PRECISION = 0.000000000001;
Expand Down
30 changes: 30 additions & 0 deletions src/com/concordia/soen6441/incarnation1/CheersException_I1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.concordia.soen6441.incarnation1;

public class CheersException_I1 extends Exception
{
private static final long serialVersionUID = -3594829237542405847L;

public CheersException_I1()
{
}

public CheersException_I1(String message)
{
super(message);
}

public CheersException_I1(String message, Throwable cause)
{
super(message, cause);
}

public CheersException_I1(Throwable cause)
{
super(cause);
}

public CheersException_I1(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace)
{
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package com.concordia.soen6441.src;
import com.concordia.soen6441.src.CheersConfig;
package com.concordia.soen6441.incarnation1;
import com.concordia.soen6441.incarnation1.CheersConfig_I1;

public class CheersMath {
public class CheersMath_I1 {

private int precision;
private int precisionOutput;
private double radius;
private double pi;

public CheersMath(double radius, int precision, int precisionOutput) throws CheersException {
if(radius < CheersConfig.RADIUS_MIN || radius > CheersConfig.RADIUS_MAX)
throw new CheersException("The Radius must be between 2 and 5.");
public CheersMath_I1(double radius, int precision, int precisionOutput) throws CheersException_I1 {
if(radius < 0)
throw new CheersException_I1("The Radius canot be negative.");
if(precision < 0)
throw new CheersException("The Precision value cannot be negative.");
if(precisionOutput < CheersConfig.PRECISION_OUTPUT_MIN || precisionOutput > CheersConfig.PRECISION_OUTPUT_MAX)
throw new CheersException("The Precision value must be between "+CheersConfig.PRECISION_OUTPUT_MIN+" and "+CheersConfig.PRECISION_OUTPUT_MAX+".");
throw new CheersException_I1("The Precision value cannot be negative.");
if(precisionOutput < CheersConfig_I1.PRECISION_OUTPUT_MIN || precisionOutput > CheersConfig_I1.PRECISION_OUTPUT_MAX)
throw new CheersException_I1("The Precision value must be between "+CheersConfig_I1.PRECISION_OUTPUT_MIN+" and "+CheersConfig_I1.PRECISION_OUTPUT_MAX+".");
try{
this.precision = precision; // TODO: cast precision to long only, no double, string or float allowed
this.radius = radius;
this.pi = pi();
this.pi = getPi();
} catch (NumberFormatException e) {
System.out.println("The Radius or Precision have the wrong number format.");
}
}

protected double convertDegreeToRadian(double deg) {
return deg / CheersConfig.STRAIGHT_ANGLE_SIZE * pi;
return deg / CheersConfig_I1.STRAIGHT_ANGLE_SIZE * pi;
}

protected double round(double nb) {
Expand All @@ -37,7 +37,7 @@ protected double round(double nb) {
}

protected double roundToTwo(double nb){
int prec = precisionOutput;
int prec = 10^precisionOutput;
nb *= prec;
nb = (int) nb;
nb /= prec;
Expand All @@ -46,7 +46,7 @@ protected double roundToTwo(double nb){

// 1 - x^2/2! + x^4/4! - x^6/6! + ...
// Computing cos(x) using Taylor Series
protected double cos(double x) {
protected double getCos(double x) {
int i = 1;
double sum = 1.0, term = 1.0;
do {
Expand All @@ -60,15 +60,15 @@ protected double cos(double x) {
}
}
i++;
} while (i <= CheersConfig.COS_ITERATIONS);
} while (i <= CheersConfig_I1.COS_ITERATIONS);
return round(sum);
}

// 4(1 - 1/3 + 1/5 - 1/7 + ...)
// Gregory-Leibniz
// http://functions.wolfram.com/Constants/Pi/02/
protected double pi() {
double piValue = 0, flip = -1, prec = CheersConfig.PI_PRECISION;
protected double getPi() {
double piValue = 0, flip = -1, prec = CheersConfig_I1.PI_PRECISION;
int n = 1;
while (n <= prec) {
flip = -1 * flip;
Expand All @@ -81,17 +81,17 @@ protected double pi() {
// http://mathcentral.uregina.ca/QQ/database/QQ.09.00/roble1.html
// using Newton's method, we can solve f(x) = sin(alpha) - alpha + pi/2 when
// f(x) = 0
protected double alpha() {
protected double getAlpha() {
double alpha = 1.0;
for (int i = 0; i < CheersConfig.ALPHA_ITERATIONS; i++) { // after 78th iteration, it converges to
for (int i = 0; i < CheersConfig_I1.ALPHA_ITERATIONS; i++) { // after 78th iteration, it converges to
// 2.304129659127962
alpha = alpha - ((pi / 2 - alpha + sin(alpha)) / (-1 + cos(alpha))); // next x = current x - fx/fx'
alpha = alpha - ((pi / 2 - alpha + getSin(alpha)) / (-1 + getCos(alpha))); // next x = current x - fx/fx'
}
return round(alpha); // 2.304129659127962
}

// Computing sin(x) using Taylor Series
protected double sin(double x) {
protected double getSin(double x) {
double term = 1.0;
int i = 0;
double sum = 0.0;
Expand All @@ -110,8 +110,8 @@ protected double sin(double x) {

// L = 2R(1 – cos(α/2))
// α – sin(α) = π/2.
public double getLength() throws CheersException{
double cosValue = cos(CheersConfig.ALPHA / 2);
public double getLength() throws CheersException_I1{
double cosValue = getCos(CheersConfig_I1.ALPHA / 2);
return roundToTwo(2 * radius * (1 - cosValue));
}
}
30 changes: 0 additions & 30 deletions src/com/concordia/soen6441/src/CheersException.java

This file was deleted.

16 changes: 8 additions & 8 deletions src/com/concordia/soen6441/test/CheersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import java.lang.Math;

import com.concordia.soen6441.src.*;
import com.concordia.soen6441.incarnation1.*;

public class CheersTest extends CheersMath{
public class CheersTest extends CheersMath_I1{

private static final double RADIUS = 5.0;
private static final int PRECISION = 20;
Expand All @@ -16,7 +16,7 @@ public class CheersTest extends CheersMath{
private static final boolean TEST_ALPHA = true;
static CheersTest test;

public CheersTest(double radius, int precision,int precisionOutput) throws CheersException {
public CheersTest(double radius, int precision,int precisionOutput) throws CheersException_I1 {
super(radius, precision,precisionOutput);
}

Expand All @@ -27,7 +27,7 @@ private static void compareCos(double angdeg) {
System.out.println("rad = " + rad);
System.out.println("deg = " + angdeg);
System.out.println("javaCos = " + Math.cos(rad));
System.out.println("cheersCos = " + test.cos(rad));
System.out.println("cheersCos = " + test.getCos(rad));
System.out.println("=======================================\n");
}

Expand All @@ -38,24 +38,24 @@ private static void compareSin(double angdeg) {
System.out.println("rad = " + rad);
System.out.println("deg = " + angdeg);
System.out.println("javaSin = " + Math.sin(rad));
System.out.println("cheersSin = " + test.sin(rad));
System.out.println("cheersSin = " + test.getSin(rad));
System.out.println("=======================================\n");
}

private static void comparePi() {
System.out.println("Test - Comparing PI in Cheers and Native\n");
System.out.println("javaPi = " + Math.PI);
System.out.println("cheersPi = " + test.pi());
System.out.println("cheersPi = " + test.getPi());
System.out.println("=======================================\n");
}

private static void showAlpha() {
System.out.println("Test - Compute alpha value in Cheers\n");
System.out.println("cheersAlpha = " + test.alpha());
System.out.println("cheersAlpha = " + test.getAlpha());
System.out.println("=======================================\n");
}

public static void main(String args[]) throws CheersException {
public static void main(String args[]) throws CheersException_I1 {

test = new CheersTest(RADIUS, PRECISION,PRECISIONOUTPUT);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.concordia.soen6441.ui;

import com.concordia.soen6441.src.*;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class CheersUi {
import com.concordia.soen6441.incarnation1.*;

public class CheersUi_I1 {

public static void main(String args[]) throws CheersException {
public static void main(String args[]) throws CheersException_I1 {
try {
double radius = 0;
int precision = 0;
Expand All @@ -27,10 +27,10 @@ public static void main(String args[]) throws CheersException {
System.out.println("Invalid operation type.");
}

if (operation == CheersConfig.OPERATION_CALCULATE || operation == CheersConfig.OPERATION_EXIT) {
if (operation == CheersConfig_I1.OPERATION_CALCULATE || operation == CheersConfig_I1.OPERATION_EXIT) {
switch (operation) {
case CheersConfig.OPERATION_CALCULATE:
System.out.println("Please enter the Radius:");
case CheersConfig_I1.OPERATION_CALCULATE:
System.out.println("Please enter the Radius (in Inches):");
// TODO: make parseFloat native and control radius not
// to be
// too much small or big
Expand All @@ -39,16 +39,16 @@ public static void main(String args[]) throws CheersException {
// add as fact
// TODO: Come up with our own decimal format function
radius = Double.parseDouble(br.readLine());
System.out.println("Please enter the precision of intermediate values calculation:");
System.out.println("Please enter the precision of intermediate values calculation (1 to 5):");
precision = Integer.parseInt(br.readLine());
System.out.println("Please enter the precision for the output:");
System.out.println("Please enter the precision for the output (1 to 5):");
precisionOutput = Integer.parseInt(br.readLine());
System.out.println("Calculating the length. Please wait...");
CheersMath cheersMath = new CheersMath(radius, precision, precisionOutput);
CheersMath_I1 cheersMath = new CheersMath_I1(radius, precision, precisionOutput);
System.out.println("The two coasters need to be moved " + cheersMath.getLength()
+ " inch far from each other");

case CheersConfig.OPERATION_EXIT:
case CheersConfig_I1.OPERATION_EXIT:
System.out.println("Program Cheers Ended!");
System.exit(0);
}
Expand Down

0 comments on commit 2b9faa8

Please sign in to comment.