Question

In: Computer Science

Use Java (Geometry: point in a rectangle?) Write a program that prompts the user to enter...

Use Java

(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 to10 / 2 and its vertical distance to (0, 0) is less than or equal to 5.0 / 2. Test your program to cover all cases.)

Sample Run 1

Enter a point with two coordinates: 2 2
Point (2.0, 2.0) is in the rectangle

Sample run 2

Enter a point with two coordinates: 6 4
Point (6.0, 4.0) is not in the rectangle

Sample Run 3

Enter a point with two coordinates: -5.1 -2.4
Point (-5.1, -2.4) is not in the rectangle

Sample Run 4

Enter a point with two coordinates: -4.9 2.49
Point (-4.9, 2.49) is in the rectangle

Sample Run 5

Enter a point with two coordinates: -4.99 -2.499
Point (-4.99, -2.499) is in the rectangle

Class Name: Exercise03_23

If you get a logical or runtime error, please refer https://liveexample.pearsoncmg.com/faq.html.

Solutions

Expert Solution

**I request you to Please Provide the positive Rating**

SOURCE CODE:

import java.util.Scanner;

public class Exercise03_23{
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);

       // Prompt the user to enter a point (x, y)
       System.out.print("Enter a point with two coordinates: ");
       double x = input.nextDouble();
       double y = input.nextDouble();

       // Checking whether the point is within the rectangle and centered at (0, 0) with width 10 and height 5
       boolean withinRectangle = (Math.pow(Math.pow(x, 2), 0.5) <= 10 / 2 ) ||
                                       (Math.pow(Math.pow(y, 2), 0.5) <= 5.0 / 2);

       // Display results
       System.out.println("Point (" + x + ", " + y + ") is " +
           ((withinRectangle) ? "in " : "not in ") + "the rectangle");
   }
}

OUTPUT1::

Enter a point with two coordinates: 2
2
Point (2.0, 2.0) is in the rectangle

OUTPUT2::

Enter a point with two coordinates: 6
4
Point (6.0, 4.0) is not in the rectangle


OUTPUT3

Enter a point with two coordinates: -5.1
-2.4
Point (-5.1, -2.4) is not in the rectangle

OOTPUT4:

Enter a point with two coordinates: -4.9
2.49
Point (-4.9, 2.49) is in the rectangle


OUTPUT5

Enter a point with two coordinates: -4.99
-2.499
Point (-4.99, -2.499) is in the rectangle





Related Solutions

Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
java language NetBeans Write a program that prompts the user to enter the weight of a...
java language NetBeans Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
jgrasp environment, java write a complete program that prompts the user to enter their first name,...
jgrasp environment, java write a complete program that prompts the user to enter their first name, middle name, and last name (separately). print out thier name and initials, exactly as shown: your name is: John Paul Chavez your initials are: J. P. C. use string method chartAt() to extract the first (zero-th) character from a name(the name is a string type): username.charAt(0). thank you.
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result. Enter First number> 2 Enter second number> 6 The sum is 20
In Java please: 3.39 Lab 6e: CharacterOps Write a program that prompts the user to enter...
In Java please: 3.39 Lab 6e: CharacterOps Write a program that prompts the user to enter their first name. Say hello to the person. Then display whether their name begins with a vowel or consonant. If it is neither, perhaps a special character or a number digit, then print neither a vowel nor a consonant. Example 1: Enter your first name: Barbara Hello, Barbara! The first letter of your name, 'B', is a consonant. Example 2: Enter your first name:...
( USE C++ ) The program prompts the user to enter a word. The program then...
( USE C++ ) The program prompts the user to enter a word. The program then prints out the word with letters in backward order. For example, if the user enter "hello" then the program would print "olleh" show that it works .
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT