Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package Pkg651;
- import java.util.*;
- import java.text.*;
- import java.io.*;
- /**
- *
- * @author Joswph
- */
- public class RootsTable {
- public static void main(String[] args) throws FileNotFoundException
- {
- //Text file creation/name
- PrintWriter out = new PrintWriter("TestData.txt");
- //# of times to increment by
- Scanner in = new Scanner(System.in);
- System.out.print("Enter a positive integer: ");
- int n = in.nextInt();
- out.println("Enter a positive integer: "+ n);
- //declare variables
- float square = 0;
- float cubed = 0;
- DecimalFormat precision = new DecimalFormat( "#.0000" );
- int breaker = 1;
- while ( n > breaker )
- { // start while
- System.out.println("Number Square Root Cube Root");
- out.println("Number Square Root Cube Root");
- while ( breaker <= n )
- {
- //set variables to the counter/logic
- square = breaker;
- cubed = breaker;
- square = (float) Math.sqrt(breaker);
- cubed = (float) Math.pow(breaker, 1.0/3);
- //precision formatting to 4 decimals
- String FormattedCubed = precision.format(cubed);
- String FormattedSquare = precision.format(square);
- //user output
- System.out.println( breaker + " " + FormattedSquare +
- " " + FormattedCubed );
- // output to text file
- out.println( breaker + " " + FormattedSquare +
- " " + FormattedCubed);
- ++breaker;
- }
- //for the user
- System.out.print("Enter a positive integer"
- + "\nor type a 0 or any number less than 0 to exit: ");
- n = in.nextInt();
- // for the file
- out.println("Enter a positive integer "
- + "\nor type a 0 or any number less than 0 to exit: " + n);
- } // end of while
- //user
- System.out.println("***** Have a nice day! *****");
- // file
- out.println("***** Have a nice day! *****");
- // have to close the files!
- in.close();
- out.close();
- } // end main
- } // end class
- /**
- run:
- Enter a positive integer: 5
- Number Square Root Cube Root
- 1 1.0000 1.0000
- 2 1.4142 1.2599
- 3 1.7321 1.4422
- 4 2.0000 1.5874
- 5 2.2361 1.7100
- Enter a positive integer
- or type a 0 or any number less than 0 to exit: 10
- Number Square Root Cube Root
- 6 2.4495 1.8171
- 7 2.6458 1.9129
- 8 2.8284 2.0000
- 9 3.0000 2.0801
- 10 3.1623 2.1544
- Enter a positive integer
- or type a 0 or any number less than 0 to exit: 15
- Number Square Root Cube Root
- 11 3.3166 2.2240
- 12 3.4641 2.2894
- 13 3.6056 2.3513
- 14 3.7417 2.4101
- 15 3.8730 2.4662
- Enter a positive integer
- or type a 0 or any number less than 0 to exit: -1
- ***** Have a nice day! *****
- BUILD SUCCESSFUL (total time: 8 seconds)
- */
Add Comment
Please, Sign In to add comment