Created
January 19, 2016 01:58
-
-
Save christopherturner/1e0c6cc5028bf5ac1d0a to your computer and use it in GitHub Desktop.
Midterm Project - Christopher Turner - Flintridge Preparatory School
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; //Imports | |
public class NthRoot { | |
public static void main(String[] args) { | |
Calculator rootFinder = new Calculator(); //Creates a new calculator object from the class (simply for modularizing my code) | |
double radical = (double) rootFinder.getRadical(); //Gets the number x that you want to find the nth root of | |
int n = rootFinder.getN(); //Gets the number n to find the nth root of "radical" | |
int requestedError = rootFinder.getRequestedError(); //Gets the integer P to find the error tolerated in the form 10E-P | |
double xtoN = radical; //Creates a variable for the final result, initially setting it equal to the input | |
double calculatedError = Math.pow(10, -(requestedError)); //Inserts the requestedError as the exponent P in 10E-P, to calculate the threshold where an answer over is wrong but an answer under is correct | |
while (Math.abs(radical - Math.pow(xtoN,n)) > calculatedError) { //Runs while the difference between the inital input and the result to the nth power are greater than the error threshold | |
xtoN = xtoN - ((Math.pow(xtoN,n) - radical) / (n * Math.pow(xtoN,n-1))); //Calculates the iterative result based on Newton's method | |
} | |
System.out.print("Your result is: "); //Output | |
System.out.printf("%." + requestedError + "f", xtoN); //Output, formatted to the proper number of decimal places | |
} | |
} | |
public class Calculator { | |
Scanner input = new Scanner(System.in); | |
int radical = -1; //Initializes a variable for the number a user would like to find the root of | |
int n = -1; //Initializes a variable for the number a user would like to find the root of | |
int requestedError = -1; //Initiatizes a variable for the number of decimal places a result will be accurate to | |
public int getRadical() { | |
System.out.println("Please enter a number to find the root of (for example, '500'):"); //Output | |
while (radical < 1 || radical > 1000) { //Restricts calculuations to between 1 and 1000. | |
while (input.hasNextInt() == false) { //Runs while the input doesn't include an integer | |
input.nextLine(); //Advances to next line | |
} | |
radical = input.nextInt(); //Assigns the radical variable to user input | |
if (radical > 0 && radical < 1001) { //Asks for new input if not within specified range | |
System.out.println("You've chosen to find the nth root of " + radical + "."); //Output | |
} | |
else { | |
System.out.println("A default value of 500 has been chosen for you."); //Output | |
radical = 500; | |
} | |
} | |
return radical; | |
} | |
public int getN() { | |
System.out.println("Please enter n for the nth root (for example, '2'):"); //Output | |
while (n < 1 || n > 100) { //Restricts calculuations to between 1 and 100. | |
while (input.hasNextInt() == false) { //Runs while the input doesn't include an integer | |
input.nextLine(); //Advances to next line | |
} | |
n = input.nextInt(); //Assigns the radical variable to user input | |
if (n > 0 && n < 101) { //Asks for new input if not within specified range | |
System.out.println("You've chosen the " + n + " root."); //Output | |
} | |
else { | |
System.out.println("A default n of 2 has been chosen for you."); //Output | |
n = 2; //Sets to a default | |
} | |
} | |
return n; | |
} | |
public int getRequestedError() { | |
System.out.println("Please enter P, your error tolerance in the form '10E-P, between 1 and 15:'"); //Ouput | |
while (requestedError < 1 || requestedError > 15) { //Restricts range of calculations | |
while (input.hasNextInt() == false) { //Runs while the input doesn't include an integer | |
input.nextLine(); //Advances to next line | |
} | |
requestedError = input.nextInt(); //Assigns the radical variable to user input | |
if (requestedError > 0 && requestedError < 16) { //Asks for new input if not within specified range | |
System.out.println("You've chosen an error tolerance of 10E-" + requestedError + "."); //Output | |
} | |
else { | |
System.out.println("A default error tolerance of 10E-5 has been chosen for you."); //Output | |
requestedError = 5; //Sets to a default | |
} | |
} | |
return requestedError; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment