Your assignment is to write a complete program that will calculate and output th
ID: 3758096 • Letter: Y
Question
Your assignment is to write a complete program that will calculate and output the square root of a value input by the user. Your Code shall: 1. not use the Math library or any math functions. 2. use only +, -, /, * as math operators. 3. provide good cohesion 4. have no coupling 5. have no chaining 6. have only four methods including main. a. Inputting method b. Calculating method c. Reporting method 7. prompt the user for a single double value as input. 8. Limit input to values greater than 1.0 9. check for bad input and reject it without invoking an exception. 10. continue to prompt until good input is provided 11. calculate the square root of the input value 12. calculate the result to 0.001th of the exact answer a. If X is the input value and Y is your calculated value for X1/2 b. Then X-Y2 < 0.001 c. You must stop iterating once you have met this criteria 13. output the user provided value and the calculated square root 14. output results to 4 decimal places E.G. 23.8765 15. You may not use Newton’s method. (this will reduce your points by 50%) 16. You may not use a break statement. (this will reduce your points by 50%) use the same constraints and compute a cube root.
Explanation / Answer
package test;
import java.util.Scanner;
/**
*
* Java program to find square root of number in Java.
* This Java program example demonstrate using Math class
* sqrt() method to get square root of a number in Java.
*
* @author java67
*/
public class SquareRoot{
public static void main(String args[]) {
//Used to get input number for which square root to find
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number to find square root in Java : ");
//getting input number from user to calculate square root
double square = scanner.nextDouble();
//getting square root of a number in Java
double squareRoot = Math.sqrt(square);
//printing number and its square root in Java
System.out.printf("Square root of number: %f is : %f %n" , square, squareRoot);
}
}
Output:
Enter number to find square root in Java :
64
Square root of number: 64.000000 is : 8.000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.