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
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(θ)
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.
Java Programming 5th edition Chapter 10 Inheritance Programs Part 1 Number 3 on page 719 which...
Java Programming 5th edition Chapter 10 Inheritance Programs Part 1 Number 3 on page 719 which creates a Point class with 2 instance variables; the xCoordinate and yCoordinate. It should have a default constructor and a values constructor. Also include a set method that sets both attributes, a get method for each attribute, and a method that redefines toString() to print the attributes as follows. point: (x, y) Part 2 Do number 4 on page 719 which creates a Circle...
Chapter 3. 40. How do you suppose the demographics of an aging population of “Baby Boomers”...
Chapter 3. 40. How do you suppose the demographics of an aging population of “Baby Boomers” in the United States will affect the demand for milk? Justify your answer. Chapter 4. 21. Other than the demand for labor, what would be another example of a “derived demand?”
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT