In: Computer Science
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↵
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.