Write a program in JAVA that will input the lengths of the two legs of a triangl
ID: 3876110 • Letter: W
Question
Write a program in JAVA that will input the lengths of the two legs of a triangle using Dialog Boxes. Then calculate the hypotenuse and the six trig functions for one of the angles of the triangle. Display all information in a Message Box.
All calculations should be done using Methods. Your program should be well documented both internally and externally.
Internal Documentation 3
Use of Methods 3
Efficient Coding 3
Correctness of Program 7
Input (GUI’s) 2
Output (GUI’s) 2
Explanation / Answer
code is explained through comments after almost every line of code, so please refer to code and for any further query about this please comment
//***************code***********************
//import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class Hypoteneus {
public static void main(String[] args) {
// TODO Auto-generated method stub
String fn=JOptionPane.showInputDialog("Enter First Number"); //dialog box for asking user input ---first side of a triangle
String sn=JOptionPane.showInputDialog("Enter Second Number"); //dialog box for asking user input ---Second side of a triangle
double a=Double.parseDouble(fn); //converting string into double
double b=Double.parseDouble(sn); //converting string into double
double sq1=Math.pow(a, 2); //method to calculate square of a number --- calculating square of first side
double sq2=Math.pow(b, 2); //calculating square of second side
double hyp=Math.sqrt(sq1+sq2); //Method used from Maths class....calculates square root o a number
JOptionPane.showMessageDialog(null, "Hypotenueus is "+hyp); //dialog box to show hypoteneus
JOptionPane.showMessageDialog(null, "Sin theta is: "+sin_calc(b,hyp)+" Cos theta is: "+cos_calc(a, hyp)+" Tan theta is "+tan_calc(a, b)+" Cot theta is: "+cot_calc(a, b)+" Cosec theta is: "+cosec_calc(b, hyp)+" Sec theta is "+sec_calc(a, hyp));
//JOptionPane.showMessageDialog(null, sin_calc(b, hyp));
}
//Methods to calculate all six trigonometric functions
// all answers are calculated with the help of known sides
public static double sin_calc(double b, double hyp)
{
return (b/hyp);
}
public static double cos_calc(double a, double hyp)
{
return (a/hyp);
}
public static double tan_calc(double a, double b)
{
return (b/a);
}
public static double cot_calc(double a, double b)
{
return (a/b);
}
public static double cosec_calc(double b, double hyp)
{
return (hyp/b);
}
public static double sec_calc(double a, double hyp)
{
return (hyp/a);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.