Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.NumberFormat;
- import java.text.DecimalFormat;
- public static void main(String[] args)
- {
- Scanner inFile = null;
- try
- {
- inFile = new Scanner(new File(INFILE_NAME));
- }
- catch(FileNotFoundException e)
- {
- System.out.println("Error: " + e.getMessage());
- System.exit(1);
- }
- int count = inFile.nextInt();
- while(count != -1)
- {
- int[] data = new int[count];
- String values = "";
- for(int index = 0; index < count; index += 1)
- {
- data[index] = inFile.nextInt();
- values += data[index];
- if(index + 1 < count)
- {
- values += ", ";
- }
- }
- int sum = askSum(data);
- System.out.println(printer(count, values, sum));
- count = inFile.nextInt();
- }
- inFile.close();
- }
- private static String printer(int count, String values, int sum)
- {
- NumberFormat formatter = new DecimalFormat("#0.00000");
- double realSqrt = Math.sqrt(sum);
- double estimatedSqrt = notLeibnizMethod(sum);
- String formattedSqrt = formatter.format(realSqrt);
- String formattedEstimation = formatter.format(estimatedSqrt);
- double diff = Math.abs(estimatedSqrt - realSqrt);
- NumberFormat diffFormatter = new DecimalFormat("#0.0000000");
- String formattedDiff = diffFormatter.format(diff);
- String toPrint = "Next Line has " + count + " number(s): " + values + "\n"
- + "Sum is: " + sum + ", My square root: " + formattedEstimation + ", Math.sqrt(): " + formattedSqrt + ", Diff: " + formattedDiff;
- return toPrint;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement