(Geometry: point in a rectangle?) Write a program that prompts the user to enter a point (x,y) and checks whether the point is within the rectangle centered at (0,0) with width 10 and height 5. For example (2,2) is inside the rectangle and (6,4) is outside the rectangle. (Hint: A point is in the rectangle if its horizontal distance to (0,0) is less than or equal to 10/2 and its vertical distance is less than or equal to 5.0/2.) (You must use JOptionPane class to get input from user.)
Sample runs:
Enter a point with two coordinates: 2 2
Point (2.0, 2.0) is in the rectangle
Enter a point with two coordinates: 6 4
Point (6.0, 4.0) is not in the rectangle
Rectangle.java
/* Geometry: point in a rectangle? */ // Import JOptionPane. import javax.swing.JOptionPane; public class Rectangle { public static void main(String[] args) { // Create variables. double x = 0; double y = 0; // Getting x point. x = Double.parseDouble(JOptionPane.showInputDialog("Enter the X point")); // Getting y point. y = Double.parseDouble(JOptionPane.showInputDialog("Enter the Y point ")); // Checking conditions. if(x <= 5.0 && x >= -5.0 && y <= (5.0/2) && y >= (-5.0/2)){ // The point is in the rectangle. JOptionPane.showMessageDialog(null,"The point ("+ x +","+y+") is in the rectangle.","Result",JOptionPane.INFORMATION_MESSAGE); }else{ // The point is not in the rectangle. JOptionPane.showMessageDialog(null,"The point ("+ x +","+y+") is not in the rectangle.","Result",JOptionPane.INFORMATION_MESSAGE); } } }
Yorum Yok:
Yorum Yap:
Yorum yapabilmek için giriş yapmalısınız.