0% found this document useful (0 votes)
20 views4 pages

张福海

This document describes a program that calculates the three angles of a triangle given the x- and y-coordinates of its three corner points as input. It explains the problem, input/output, design, provides the Java source code, and describes how to test the program by compiling it, running it with different valid and invalid inputs, and ensuring the outputs are correct.

Uploaded by

itcanva0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

张福海

This document describes a program that calculates the three angles of a triangle given the x- and y-coordinates of its three corner points as input. It explains the problem, input/output, design, provides the Java source code, and describes how to test the program by compiling it, running it with different valid and invalid inputs, and ensuring the outputs are correct.

Uploaded by

itcanva0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

台州学院

电子与信息工程学院课后作业

班级 二 学号 2362720049 姓名张福海

作业日期: 二零二四 年三 月 二十七 日

Week4 Computing Angles of a Triangle

Project: Computing Angles of a Triangle


Problem Description:

Write a program that prompts the user to enter the x- and y-coordinates of the three
corner points in a triangle and then displays the triangle’s angles.
Analysis:
(Describe the problem including input and output in your own words.)

The program you provided prompts the user to enter the x- and y-coordinates of three
corner points of a triangle. After receiving the input, it calculates the three sides of the
triangle using the distance formula and then calculates the three angles of the triangle
using trigonometric formulas.

Input: The user needs to input six values: x and y coordinates of each of the three
points.
Output: The program displays the measures of the three angles of the triangle formed
by the given points.

Design:
(Describe the major steps for solving the problem.)

● The user is prompted to enter the x- and y-coordinates of the three points that form
the corners of the triangle.
● The program then computes the lengths of the three sides of the triangle using the
distance formula.
● Using the lengths of the sides, the program calculates the measures of the three
angles of the triangle using trigonometric functions.
● Finally, it displays the calculated angles.

Coding: (Copy and Paste Source Code here. Format your code using Courier
10pts)

import java.util.Scanner;

public class ComputeAngles {


public static void main (String [] args) {
Scanner input = new Scanner (System.in);

//Prompt the user to enter three points


System.out.print("Enter three points: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double x3 = input.nextDouble();
double y3 = input.nextDouble();
// Compute three sides
double a = Math.sqrt ((x2 - x3) * (x2 - x3)
+ (y2 - y3) * (y2 - y3));
double b = Math.sqrt ((x1 - x3) * (x1 - x3)
+ (y1 - y3) * (y1 - y3));
double c = Math. sqrt ((x1- x2) * (x1 - x2)
+ (y1 - y2) * (y1 - y2));

// Compute three angles


double A = Math.toDegrees (Math.acos((a * a - b * b - c * c)
/ (-2 * b * c)));
double B = Math.toDegrees (Math.acos((b * b - a * a - c * c)
/ (-2 * a * c)));
double C = Math.toDegrees (Math.acos((c * c - b * b - a * a)
/ (-2 * a * b)));

//Display results
System.out.println("The three angles are " +
Math.round(A * 100) / 100.0 + " " +
Math.round(B * 100) / 100.0 + " " +
Math.round(C * 100) / 100.0);
}
}

Testing: (Describe how you test this program)


● Compile the Program: Compile the provided Java program (ComputeAngles.java)
using a Java compiler. If there are any syntax errors, address them before
proceeding.
● Run the Program: Execute the compiled program. You can do this by running the
compiled Java class file. After running the program, it will prompt you to enter the
x- and y-coordinates of the three corner points of a triangle.
● Enter Input: Input the x- and y-coordinates of the three corner points of a triangle
as prompted by the program. Ensure that you provide valid numerical input.
● Observe Output: After entering the coordinates, the program will calculate the
angles of the triangle formed by those coordinates and display them. Ensure that the
displayed angles seem reasonable and match your expectations.
● Test with Various Input: Test the program with different sets of input coordinates.
Try different types of triangles and ensure that the program computes the angles
correctly for each case.
● Boundary Cases: Test the program with boundary cases, such as when the points
are collinear, when two points overlap, or when the points are very close to each
other, to ensure the program handles such scenarios gracefully.
● Invalid Input Handling: Test the program's behavior when invalid input is
provided, such as non-numeric input or fewer/more than six input values. Ensure
that the program handles such cases appropriately.
● Edge Cases: Consider testing edge cases, such as when the triangle formed is
degenerate, to verify that the program behaves as expected in such situations.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy