Advertisement
kthomer

Assignment 8 tips

Apr 19th, 2011
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. import java.text.NumberFormat;
  2. import java.text.DecimalFormat;
  3.  
  4. public static void main(String[] args)
  5. {
  6.     Scanner inFile = null;
  7.     try
  8.     {
  9.         inFile = new Scanner(new File(INFILE_NAME));
  10.     }
  11.     catch(FileNotFoundException e)
  12.     {
  13.         System.out.println("Error: " + e.getMessage());
  14.         System.exit(1);
  15.     }
  16.     int count = inFile.nextInt();
  17.     while(count != -1)
  18.     {
  19.         int[] data = new int[count];
  20.         String values = "";
  21.         for(int index = 0; index < count; index += 1)
  22.         {
  23.             data[index] = inFile.nextInt();
  24.             values += data[index];
  25.             if(index + 1 < count)
  26.             {
  27.                 values += ", ";
  28.             }
  29.         }
  30.         int sum = askSum(data);
  31.         System.out.println(printer(count, values, sum));
  32.         count = inFile.nextInt();
  33.     }
  34.  
  35.     inFile.close();
  36. }
  37.  
  38. private static String printer(int count, String values, int sum)
  39. {
  40.     NumberFormat formatter = new DecimalFormat("#0.00000");
  41.     double realSqrt = Math.sqrt(sum);
  42.     double estimatedSqrt = notLeibnizMethod(sum);
  43.         String formattedSqrt = formatter.format(realSqrt);
  44.     String formattedEstimation = formatter.format(estimatedSqrt);
  45.     double diff = Math.abs(estimatedSqrt - realSqrt);
  46.     NumberFormat diffFormatter = new DecimalFormat("#0.0000000");
  47.     String formattedDiff = diffFormatter.format(diff);
  48.     String toPrint = "Next Line has " + count + " number(s): " + values + "\n"
  49.     + "Sum is:     " + sum + ", My square root:    " + formattedEstimation + ", Math.sqrt():    " + formattedSqrt + ", Diff: " + formattedDiff;
  50.     return toPrint;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement