Answered step by step
Verified Expert Solution
Question
1 Approved Answer
4. Type the following code into your Lab2 class import java.util.Scanner; public class Lad2 { public static void main(String args[]) { Scanner input = new
4. Type the following code into your Lab2 class import java.util.Scanner; public class Lad2 { public static void main(String args[]) { Scanner input = new Scanner(System.in); // Reads from the console II Put all of your code here. 5. Press the "Compile button and make sure there are no syntax errors. Put all of your code for this lab inside the main method, as indicated in the code above Follow the instructions in the Requirements section to see what you have to do Requirements: Write code so that your program does the following: 1. Print out an approximation of the mathematical constant #. You must compute an approximation of , accurate out to at least two digits past the decimal point, using the following (inefficient) formula: 1 1 1 = 4x 1 - + 1 1 + 7 9 -) 3 11 DO NOT just print out a String-literal of an approximation, i.e., DO NOT JUST DO THIS: System.out.println("3.14159265359"); Instead, compute the above expression out to as many terms as it takes to get at least two digits past the decimal point correct. You cannot use explicit type casting for computing your approximation of . 2. Prompt the user to which digit past the decimal point in your approximation that they would like for your program to print out. 3. Print out the requested digit, just by itself on its own line. Your program must print out the correct digit (relative to your approximation of 1, out to the fifteenth digit past the decimal point (the first digit past the decimal point will be counted as digit 1). See the Hints section. 4. Do not do any computation steps inside of print statements. 5. Every line of code in your program must be less than or equal to 78 characters in width. 6. Your input / output format should match the input / output format shown in the Sample runs section. 7. ONLY use concepts covered up through zc2, in addition to any code that I provide for you. Hints: Assuming something is an integer type (int or long), then the expression something % 10 evaluates to the right most digit of something. The expression something * 10 evaluates to a number comprised of the digits of something shifted left one position, with a 0 occupying the right most digit position. You can and should use Math.pow to compute a power of ten, perhaps based on user in- put, like this: Math.pow(10, ???); Just be careful, because the previous call to Math.pow evaluates to a double. Sample runs: of 3 Pages 2 6. Your input / output format should match the input y output format shown in the Sample runs section. 7. ONLY use concepts covered up through zc2, in addition to any code that I provide for you. Hints: Assuming something is an integer type (int or long), then the expression something % 10 evaluates to the right most digit of something, The expression something * 10 evaluates to a number comprised of the digits of something shifted left one position, with a 0 occupying the right nost digit position. You can and should use Math.pow to compute a power of ten, perhaps based on user in- put, like this: Math.pow(10, ???); Just be careful, because the previous call to Math.pow evaluates to a double. Sample runs: 3.1493444751246202 Which digit past the decimal point do you want? 1 1 3.1493444751246202 Which digit past the decimal point do you want? 7 4 3.1493444751246202 Which digit past the decimal point do you want? 10 1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started