Task02: An orange square is inscribed inside a circle as shown in the diagram be
ID: 3910864 • Letter: T
Question
Task02: An orange square is inscribed inside a circle as shown in the diagram below. Write an interactive Java program that prompts for and reads the area of the square in square centimeters. It then calculates and prints the area of the grean square centimeters. display an appropriate eror message in case such an Exception is thrown. Note: Your program must not use selection statements. Your program must cateh java.utilInputMismatchException and Sample program runs Enter the area of the square [sq cm]: t Enter the area of the square [sq ?]: 16.0 Error: Invalid input Enter the area f the squa re [sq cn]: 26.75 | Enter the area of the square [sq cm): -56.0 Gre n area = 15.27 square cm Green area9.13 square cm Green area = NaN square cm Note: The last output error is due to passing a negative number to the square root method. This eior can be avoided by using selection statement. We will cover selection statements later on in the courseExplanation / Answer
import java.util.Scanner; public class PeopleInGroup { public static int getGroupSize(double p) { double a = 1, b = -1, c = -2*(Math.log10(1-p)/Math.log10(364.0/365.0)); double d = Math.sqrt(b*b - 4*a*c); double r1 = (-b + d) / (2*a), r2 = (-b - d) / (2*a); return (int) Math.max(r1, r2); } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter the probability [0, 1) : "); double p = in.nextDouble(); System.out.printf("The probability that 2 people in a group of %d people have the same birthday is %.7f ", getGroupSize(p), p); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.