Last active
January 28, 2016 20:57
-
-
Save christopherturner/1462ec5ce3be60923010 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
// Written by Christopher Turner.- | |
import java.util.*; | |
public class NthRoot { | |
public static void main(String[] args) { | |
Calculator rootFinder = new Calculator(); | |
double radical = (double) rootFinder.getRadical(); | |
int n = rootFinder.getN(); | |
int requestedError = rootFinder.getRequestedError(); | |
double xtoN = radical; //Creates a variable for the final result, initially setting it equal to the input | |
double calculatedError = Math.pow(10, -(requestedError)); //Applies the base to the variable exponent to get the maximum allowed degree error | |
double technicalResult = Math.pow(radical, (double) 1.0 / requestedError); | |
while (Math.abs(technicalResult - xtoN) > calculatedError) { //Runs while the current error is greater than the allowed error | |
xtoN = xtoN - ((Math.pow(xtoN,n) - radical) / (n * Math.pow(xtoN,n-1))); //Calculates the iterative result | |
} | |
System.out.print("Your result is: "); //Output | |
System.out.printf("%." + requestedError + "f", xtoN); //Output | |
} | |
} | |
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:"); //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 < 1 || radical > 1000) { //Asks for new input if not within specified range | |
System.out.println("Please enter a number between 1 and 1,000:"); //Output | |
} | |
} | |
return radical; | |
} | |
public int getN() { | |
System.out.println("Please enter n for the nth root:"); //Output | |
while (n < 1 || n > 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 | |
} | |
n = input.nextInt(); //Assigns the radical variable to user input | |
if (n < 1 || n > 1000) { //Asks for new input if not within specified range | |
System.out.println("Please enter a number between 1 and 1,000:"); //Output | |
} | |
} | |
return n; | |
} | |
public int getRequestedError() { | |
System.out.println("Please enter your error tolerance:"); //Ouput | |
while (requestedError < 3 || requestedError > 14) { //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 < 3 || requestedError > 14) { //Asks for new input if not within specified range | |
System.out.println("Please enter an error tolerance between 3 and 14:"); //Output | |
} | |
} | |
return requestedError; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment