Question

In: Computer Science

Q4: Write a program that takes as input two opposite corners of a rectangle: (x1,y1) and...

Q4: Write a program that takes as input two opposite corners of a rectangle: (x1,y1) and (x2,y2). Finally, the user is prompted for the coordinates of a third point (x,y). The program should print Boolean value True or False based on whether the point (x,y) lies inside the rectangle. If the point lies on the rectangle, the program should print False.

Program should work irrespective of the order in which the user inputs the opposite coordinates. program user should be able to input coordinates in all possible orders.

- Right upper coordinate first and then left lower coordinate

- Left lower coordinate first and then right upper coordinate

- Left upper coordinate first and then right lower coordinate

- Right lower coordinate first and then left upper coordinate

Solutions

Expert Solution

import java.util.Scanner;

public class RectanglePoint
{
// Driver code
public static void main(String[] args)
{
   Scanner sc = new Scanner(System.in);
   System.out.println("1 - Right upper coordinate first and then left lower coordinate\r\n" +
           "2 - Left lower coordinate first and then right upper coordinate\r\n" +
           "3 - Left upper coordinate first and then right lower coordinate\r\n" +
           "4 - Right lower coordinate first and then left upper coordinate");
  
   System.out.print("\nChoose one option :");
   int option = sc.nextInt();
  
   if(option==1 || option==2 || option==3 || option==4 ) {}
   else {
       System.out.println("Please Enter Correct Option. Exiting Thank You");
       System.exit(0);
   }
//   a program that takes as input two opposite corners of a rectangle: (x1,y1) and (x2,y2).
   System.out.print("Enter (x1,y1) :");
  
   int x1 = sc.nextInt(), y1 = sc.nextInt();
   System.out.print("Enter (x2,y2) :");
   int   x2 = sc.nextInt(), y2 = sc.nextInt();

   // given point
//   the user is prompted for the coordinates of a third point (x,y)
   System.out.print("Enter (x,y) to check inside or not :");
   int x = sc.nextInt(), y = sc.nextInt();

//   print Boolean value True or False based on whether the point (x,y) lies inside the rectangle.
//   If the point lies on the rectangle, the program should print False.
   // bottom-left and top-right
   // corners of rectangle
   if(option==2)
   {
   if (x > x1 && x < x2 &&
           y > y1 && y < y2)
       System.out.println("False");
   else
       System.out.println("True");
   }
  
   if(option==1)
   {
   if (x > y1 && x < y2 &&
           y > x1 && y < x2)
       System.out.println("False");
   else
       System.out.println("True");
   }
  
   if(option==3)
   {
   if (x < x1 && x > x2 &&
           y < y1 && y > y2)
       System.out.println("False");
   else
       System.out.println("True");
   }
  
   if(option==4)
   {
   if (x < y1 && x > y2 &&
           y < x1 && y > x2)
       System.out.println("False");
   else
       System.out.println("True");
   }
}
}


Related Solutions

Write a Python program that takes as input two numbers, the height, and width of a...
Write a Python program that takes as input two numbers, the height, and width of a rectangle. It then prints a rectangle with height lines and width characters in each line. The rectangle should contain information regarding its dimensions and area inside (centered vertically but not horizontally). If any part of the information (including two stars on each end and a space before and after the line) does not fit in the rectangle, then print the complete information after the...
Rectangle Information. This program displays information about a rectangle drawn by the user. Input: Two mouse...
Rectangle Information. This program displays information about a rectangle drawn by the user. Input: Two mouse clicks for the opposite comers of a rectangle. Output: Draw the rectangle. Print the perimeter and area of the rectangle. Formulas: area = (length)(width) perimeter = 2(length + width)
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...
Write a program that takes a date as input and outputs the date's season. The input...
Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring: March 20 - June 20 Summer:...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers for height and width and uses a nested for loop to make a rectangle out of asterixes. The creation of the rectangle (i.e. the nested for loop) should occur in a void function that takes in 2 parameters, one for height and one for width. Make sure your couts match the sample output (copy and paste from those couts so you don't make a...
Write a recursive ARM Assembly program that takes two integers as input and outputs the greatest...
Write a recursive ARM Assembly program that takes two integers as input and outputs the greatest common divisor. *I am using Eclipse DS-5 Community Workspace with A64 Instruction Set) Use the following algorithm: // Given two integers m and n: if (m < n) gcd(n, m) if n is a divisor of m gcd(m, n) = n else gcd (m, n) = gcd (n, m % n) Your program must be recursive. You must create a function that calls itself,...
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT