Saturday 11 June 2016

Chapter 3 Exercise 22, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

(Geometry: point in a circle?) Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the circle centered at (0, 0) with radius 10. For example, (4, 5) is inside the circle and (9, 9) is outside the circle, as shown in Figure 3.7a. (Hint: A point is in the circle if its distance to (0, 0) is less than or equal to 10. The formula for computing the distance is
(x2x1)2+(y2y1)2
 . Test your program to cover all cases.) Two sample runs are shown below.



import java.util.Scanner;
 
 
 
public class ProgrammingEx3_22 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter a point with two coordinates:");
  double x = input.nextDouble();
  double y = input.nextDouble();
 
  double d = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  String s = " ";
 
  if (d >= 10) {
   s = " not ";
  }
 
  System.out.print("Point " + x + ", " + y + " is" + s
    + "in the circle");
 
 }
 
}

No comments :

Post a Comment