Question

In: Computer Science

Use SELECTION, DO NOT USE METHODES AND ARRAYS. (Inside The Triangle) Suppose a right triangle is...

Use SELECTION, DO NOT USE METHODES AND ARRAYS.

(Inside The Triangle)
Suppose a right triangle is placed in a plane as shown in the book. The right-angle point is placed at (0, 0), and the
other two points (x,y) are placed at (200, 0), and (0, 100). Write a program that prompts the user to enter a point with
x- and y-coordinates and determines whether the point is inside the triangle.

*** Methods are not allowed for this assignment. Do each calculation in the main.

Here is the equation for finding the area of a triangle.

abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/ 2.0 );

Here is some pseudocode that may help.

check whether the point P(10, 15)

           lies inside the triangle formed by  

           A(200, 0), B(0, 100) , C(0, 0) , P(userInputX,userInputY)

Calculate area of triangle ABC

Calculate area of triangle PBC

Calculate area of triangle PAC  

Calculate area of triangle PAB   

Check if sum of PBC, PAC and PAB is same as ABC

SAMPLE RUN #1: java InsideTheTriangle  

Enter a point's x and y coordinates: 200 0↵
The point is in the triangle↵

SAMPLE RUN #2: java InsideTheTriangle  

Enter a point's x and y coordinates: 900 0↵
The point is NOT in the triangle↵

PLEASE SELECTIONS, DONOT USE ARRAYS AND METHODES .

Solutions

Expert Solution

import java.util.Scanner;
public class InsideTheTriangle
{
public static void main(String[] args)
{
    //declare three points
    int x1=200,y1=0,x2=0,y2=100,x3=0,y3=0;
    Scanner input=new Scanner(System.in);
    System.out.print("Enter a point's x and y coordinates: ");
    int px=input.nextInt();
    int py=input.nextInt();
    //calculate area of triangle ABC
    double area1=Math.abs((x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))/2.0);
    //calculate area of triangle PBC
    double area2=Math.abs((px*(y2-y3)+x2*(y3-py)+x3*(py-y2))/2.0);
     //calculate area of triangle PAC
    double area3=Math.abs((px*(y1-y3)+x1*(y3-py)+x3*(py-y1))/2.0);
     //calculate area of triangle PAB
    double area4=Math.abs((px*(y1-y2)+x1*(y2-py)+x2*(py-y1))/2.0);
    if(area1==area2+area3+area4)
    System.out.println("The point is in the triangle");
    else
    System.out.println("The point is NOT in the triangle");
}
}

Output


Related Solutions

Chapter 3 Java Selections M6 A3 (Inside The Triangle) Suppose a right triangle is placed in...
Chapter 3 Java Selections M6 A3 (Inside The Triangle) Suppose a right triangle is placed in a plane as shown in the book. The right-angle point is placed at (0, 0), and the other two points (x,y) are placed at (200, 0), and (0, 100). Write a program that prompts the user to enter a point with x- and y-coordinates and determines whether the point is inside the triangle. *** Methods are not allowed for this assignment. Do each calculation...
A triangle has side lengths of 3cm, 4cm, and, 6cm. Is it a right triangle explain?
A triangle has side lengths of 3cm, 4cm, and, 6cm. Is it a right triangle explain?
plot each point and form the triangle ABC. Verify that the triangle ABC is a right...
plot each point and form the triangle ABC. Verify that the triangle ABC is a right triangle. Find its area. A= (-5, 10) B (2,7) C (-1,0)
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
triangle ABC is a right-angled triangle with the size of angle ACB equal to 74 degrees.
 triangle ABC is a right-angled triangle with the size of angle ACB equal to 74 degrees. The lengths of the sides AM, MQ and QP are all equal. Find the measure of angle QPB.
a) The triangle △ ABC is right-angled with right angle at corner C and angle α...
a) The triangle △ ABC is right-angled with right angle at corner C and angle α at corner A. Calculate a = | BC |, given that c = | AB | = 8, and that tan α = 12. Calculate a= ? b) In the triangle △ ABC before the designations a = | BC |, b = | CA |, c = | AB |, and ∠A = α, ∠B = β and ∠C = γ. Find c,...
Use Selections, DO NOT USE ARRAYS AND METHODS. (Find future dates) Write a program that prompts...
Use Selections, DO NOT USE ARRAYS AND METHODS. (Find future dates) Write a program that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1, ..., and Saturday is 6). Also prompt the user to enter the number of days after today for a future day and display the future day of the week. ** Can not use java.time.DayOfWeek; SAMPLE RUN #1: java FindFutureDates Enter today's day: 4↵ Enter the number...
Find a b c of a right triangle. with a perimeter of 18.
Find a b c of a right triangle. with a perimeter of 18.
Do not use arrays and code a program that reads a sequence of positive integers from...
Do not use arrays and code a program that reads a sequence of positive integers from the keyboard and finds the largest and the smallest of them along with their number of occurrences. The user enters zero to terminate the input. If the user enters a negative number, the program displays an error and continues. Sample 1: Enter numbers: 3 2 6 2 2 6 6 6 5 6 0 Largest Occurrences Smallest Occurrences 6 5 2 3. Do not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT