Guest User

Untitled

a guest
Feb 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package Pkg651;
  6. import java.util.*;
  7. import java.text.*;
  8. import java.io.*;
  9. /**
  10. *
  11. * @author Joswph
  12. */
  13. public class RootsTable {
  14. public static void main(String[] args) throws FileNotFoundException
  15. {
  16. //Text file creation/name
  17. PrintWriter out = new PrintWriter("TestData.txt");
  18. //# of times to increment by
  19. Scanner in = new Scanner(System.in);
  20. System.out.print("Enter a positive integer: ");
  21. int n = in.nextInt();
  22. out.println("Enter a positive integer: "+ n);
  23. //declare variables
  24. float square = 0;
  25. float cubed = 0;
  26. DecimalFormat precision = new DecimalFormat( "#.0000" );
  27. int breaker = 1;
  28. while ( n > breaker )
  29. { // start while
  30. System.out.println("Number Square Root Cube Root");
  31. out.println("Number Square Root Cube Root");
  32. while ( breaker <= n )
  33. {
  34.  
  35. //set variables to the counter/logic
  36. square = breaker;
  37. cubed = breaker;
  38. square = (float) Math.sqrt(breaker);
  39. cubed = (float) Math.pow(breaker, 1.0/3);
  40. //precision formatting to 4 decimals
  41. String FormattedCubed = precision.format(cubed);
  42. String FormattedSquare = precision.format(square);
  43. //user output
  44.  
  45. System.out.println( breaker + " " + FormattedSquare +
  46. " " + FormattedCubed );
  47. // output to text file
  48. out.println( breaker + " " + FormattedSquare +
  49. " " + FormattedCubed);
  50. ++breaker;
  51. }
  52. //for the user
  53.  
  54. System.out.print("Enter a positive integer"
  55. + "\nor type a 0 or any number less than 0 to exit: ");
  56. n = in.nextInt();
  57. // for the file
  58. out.println("Enter a positive integer "
  59. + "\nor type a 0 or any number less than 0 to exit: " + n);
  60. } // end of while
  61. //user
  62. System.out.println("***** Have a nice day! *****");
  63. // file
  64. out.println("***** Have a nice day! *****");
  65. // have to close the files!
  66. in.close();
  67. out.close();
  68. } // end main
  69. } // end class
  70.  
  71. /**
  72. run:
  73. Enter a positive integer: 5
  74. Number Square Root Cube Root
  75. 1 1.0000 1.0000
  76. 2 1.4142 1.2599
  77. 3 1.7321 1.4422
  78. 4 2.0000 1.5874
  79. 5 2.2361 1.7100
  80. Enter a positive integer
  81. or type a 0 or any number less than 0 to exit: 10
  82. Number Square Root Cube Root
  83. 6 2.4495 1.8171
  84. 7 2.6458 1.9129
  85. 8 2.8284 2.0000
  86. 9 3.0000 2.0801
  87. 10 3.1623 2.1544
  88. Enter a positive integer
  89. or type a 0 or any number less than 0 to exit: 15
  90. Number Square Root Cube Root
  91. 11 3.3166 2.2240
  92. 12 3.4641 2.2894
  93. 13 3.6056 2.3513
  94. 14 3.7417 2.4101
  95. 15 3.8730 2.4662
  96. Enter a positive integer
  97. or type a 0 or any number less than 0 to exit: -1
  98. ***** Have a nice day! *****
  99. BUILD SUCCESSFUL (total time: 8 seconds)
  100. */
Add Comment
Please, Sign In to add comment