Question

In: Computer Science

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 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↵

M6 A1

(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 of days elapsed since today: 53↵
Today is Thursday and the future day is Monday↵

SAMPLE RUN #2: java FindFutureDates

Enter today's day: 8↵
Enter the number of days elapsed since today: 99↵
Today is an invalid starting day Today's day must be 0-6↵

Solutions

Expert Solution

ANS INSIDE THE TRIANGLE:

JAVA CODE:

import java.util.Scanner;
public class InsideTheTriangle {
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       //ALREADY GIVEN POINTS
       int x1 = 200,y1 = 0;//POINT A
       int x2 = 0,y2 = 100;//POINT B
       int x3 = 0,y3 = 0;//POINT C
       //TAKING USER INPUT FOR POINT P
       System.out.print("Enter a point's x and y coordinates:");
       int x = scan.nextInt();
       int y = scan.nextInt();
       //AREA of TRIANGLE ABC
       double area_abc = Math.abs((x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))/2.0);
       //AREA of TRIANGLE PAB
       double area_pab = Math.abs((x1*(y2-y)+x2*(y-y1)+x*(y1-y2))/2.0);
       //AREA of TRIANGLE PBC
       double area_pbc = Math.abs((x*(y2-y3)+x2*(y3-y)+x3*(y-y2))/2.0);
       //AREA of TRIANGLE PAC
       double area_pac = Math.abs((x*(y3-y1)+x3*(y1-y)+x1*(y-y3))/2.0);
       System.out.println(area_abc);
       System.out.println(area_pab);
       System.out.println(area_pac);
       System.out.println(area_pbc);
       if(area_pab+area_pac+area_pbc==area_abc)
           System.out.println("The point is in the triangle");
       else
           System.out.println("The point is NOT in the triangle");
   }
}

All the explanation about the code is given in comments.
Steps:

1) We have calculated the area of triangle ABC.

2) We have calculated the area of triangle PAB.

3) We have calculated the area of triangle PBC.

4) We have calculated the area of triangle PAC.

And then we compared the area of triagnle ABC with the sum of Area of triangle PAB,PBC,PAC

if sum(area_of_triagle(PAB,PBC,PAC))==area_of_triagle(ABC) then the point is inside the triagnle otherwise it is not inside the triagnle.

ANS for FindFutureDates

JAVA CODE:

import java.util.Scanner;
public class FindFutureDates {
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print("Enter today's day:");
       int today = scan.nextInt();
       System.out.print("Enter the number of days elapsed since today:");
       int futureDay = scan.nextInt();

//String Array to store days of week.
       String days[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
       if(today>6 || today<0)
           System.out.println("Today is an invalid starting day Today's day must be 0-6");
       else
       {
           System.out.println("Today is "+days[today]+" and the future day is "+days[(today+futureDay)%7]);
       }
   }
}

Steps:

1) We checked that if today's day is a valid or not (i.e. today's date 0-6)

If valid then we will print today's day and calculate the future day and will print it

else we will print that it is a invalid day.


Related Solutions

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...
Three point charges are placed at the corners of a right-angle triangle, as shown in the...
Three point charges are placed at the corners of a right-angle triangle, as shown in the figure. The masses, charges and coordinates of the three objects are given as follows: Mass (g): Charge (μC): Coordinate (mm): ?1 = 2.30 ?1 = −1.25 ?1 = (0; 6.00) ?2 = 0.15 ?2 = +0.55 ?2 = (0; 0) ?3 = 1.50 ?3 = −2.05 ?3 = (4.00; 0) (a) Determine the coordinate of the centre of mass of the system. (b) Calculate...
Let a circle inside triangle DEF have a radius = 3, and let it be tangent...
Let a circle inside triangle DEF have a radius = 3, and let it be tangent to EF at point Z. Suppose |EZ| = 6 and |FZ| = 7. What are the lengths of d, e, and f?
Three charges are arranged so that each is at the vertex of a 3-4-5 right triangle....
Three charges are arranged so that each is at the vertex of a 3-4-5 right triangle. the charge opposite the short side is Q1 = -11 nC. The charge opposite the long side is Q2 = 5 nC. The charge opposite the hypotenuse is Q3 = 16 nC. The hypotenuse is H = 0.45. 1) What is the magnitude of the electric force on Q3 due to Q1? Q3 due to Q2? Q3 due to Q1 and Q2? 2) What...
Given a right triangle with one leg length of a, one leg length of x-3 and...
Given a right triangle with one leg length of a, one leg length of x-3 and the hypo is x, how does a affect x? Shown on a graph. I solved for a, it is equal to the square root of 6x-9.... I am having a difficult time interpreting how a affects x.. I am not sure how to graph
In triangle PQR right-angled at Q , PQ = 3 cm and PR = 6 cm. Determine ∠QPR
In triangle PQR right-angled at Q , PQ = 3 cm and PR = 6 cm. Determine ∠QPR
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4)...
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4) Circle 5) TestAllShapes (create 1 object of each type and print the Area for each of them.)
Suppose θ is an acute angle in a right triangle. Given tan(θ)=1/5, evaluate: 1-sin^2(θ)
Suppose θ is an acute angle in a right triangle. Given tan(θ)=1/5, evaluate: 1-sin^2(θ)
Java Chapter 12.29 (Rename files) suppose you have a lot of files in a directory named...
Java Chapter 12.29 (Rename files) suppose you have a lot of files in a directory named Exercisei_j, where i and j are digits. Write a program that pads a 0 before j if j is a single digit. For example, a file named Exercise2_1 in a directory will be renamed to Exercise2_01. In Java, when you pass the symbol * from the command line, it refers to all files in the directory (see Supplement III.V). Use the following command to...
The base of a right pyramid is an equilateral triangle of perimeter 8 cm and the height of the pyramid is 30√3 cm . Find the area and volume of the pyramid.
The base of a right pyramid is an equilateral triangle of perimeter 8 cm and the height of the pyramid is 30√3 cm . Find the area and volume of the pyramid.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT