Sahil-Pattni
2/1/2017 - 9:20 PM

## Math Tools

Math Tools

``````import java.util.Scanner;

/**
* Created by Sahil Pattni on 31-Jan-17.
*/
public class MathTools
{
public static double absoluteValue(double n)
{
if (n < 0)
return n * -1;
else
return n;
}

public static double power(double base, int exponent)
{
double answer = base;
if (exponent == 0)
answer = 1;

else if (exponent < 0)
{
for (int i = 1; i < exponent * -1; i++)
answer *= base;
answer = 1/answer;
}

else
{
for (int i = 1; i < exponent; i++)
answer *= base;
}

return answer;
}

public static double nthRoot(double value, int root)
{
double answer = -1;
double initialGuess;
double nextGuess;
double difference = 100;
double tolerance = 0.000000001;

if (value == 0 || root == 0)
answer = 0;
else
{
if (root > 0)
initialGuess = value;
else
initialGuess = 1.0/value;

nextGuess =((1.0/(double) root)*((double)root - 1.0))*(initialGuess)+(value/(power(initialGuess,(root-1))));

while (difference > tolerance)
{

nextGuess = (1.0 / root) * ((root - 1) * (initialGuess) + (value / power(initialGuess, root - 1)));
difference = absoluteValue(nextGuess - initialGuess);
initialGuess = nextGuess;
}

answer = nextGuess;
}
return answer;
}

public static String scientificNotation(double n)
{
String number = Double.toString(n);
String answer = "Oops, something went wrong with the method scientificNotation";
int movementCounter = 0;

n = ((int)(n*1000000))/1000000.0;

if(absoluteValue(n) < 1)
{
for (int i = 0; i < number.length(); i++)
{
if((number.charAt(i) != '0') && (number.charAt(i) != '.'))
{
movementCounter += -1*(i-1);
System.out.println(number);
number = number.substring(i,i+1)+"."+number.substring(i+1,number.length());
System.out.println(number);
answer = ""+number;
break;

}
}
}
else if (n >= 10)
{
for (int i = 0; i < number.length(); i++)
{
if (number.charAt(i) == '.')
{
movementCounter += i-1;
number = number.substring(0, i) + number.substring(i+1, number.length());
number = number.substring(0,1) + "." + number.substring(1, number.length());
answer = ""+number;
break;
}
}
}
else
{
answer = ""+n;
}

answer += " x 10^" + movementCounter;
return answer;
}

public static void main(String[] args) {
MathTools m = new MathTools();
boolean isDone = false;
Scanner s = new Scanner(System.in);
boolean toMenu = false;

while (!isDone)
{
System.out.println("Select a number from the menu choices:");
System.out.println("1 - Absolute Value");
System.out.println("2 - Power");
System.out.println("3 - Nth Root");
System.out.println("4 - Scientific Notation");

int userChoice = s.nextInt();

if(userChoice < 1 || userChoice > 4)
System.out.println("invalid Input");
else
{
if(userChoice == 1)
{
System.out.println("***Absolute Value***");
System.out.println("Enter the value");
System.out.println(absoluteValue(s.nextDouble()));
}
else if (userChoice == 2)
{
System.out.println("***Power***");
System.out.println("Enter the value");
double base = s.nextDouble();
System.out.println("Enter the exponent");
int expoent = s.nextInt();
System.out.println(power(base, expoent));
}
else if (userChoice == 3)
{
System.out.println("***Nth Root***");
System.out.println("Enter the value");
double value = s.nextDouble();
System.out.println("Enter the root");
int root = s.nextInt();
System.out.println(nthRoot(value, root));
}
else if (userChoice == 4)
{
System.out.println("***Scientific Notation***");
System.out.println("Enter in the value");
System.out.println(scientificNotation(s.nextDouble()));
}
}

System.out.println("Return to the menu? (yes/no)");
String answer = s.next();

if (answer.equalsIgnoreCase("no"))
isDone = true;
else if (!answer.equalsIgnoreCase("yes"))
System.out.println("Invalid Input");
}

System.out.println("Exiting MathToolsClient...");
}
}
``````