I need to create a program that prompts for the lengths of a triangle and computes it. I have the equations figured out (bottom of code). What I am stuck on though is, well, getting it to work.

Any help here would be FAN-FREAKING-TASTIC!

PS.

I won't lie, this is a homework assignment. What you see below is what I have completed on my own so far. Just need a little help working out the bugs >.<

import java.util.*;

public class Triangle {
 public static void main(String[] args) {
   Scanner console =  new Scanner(System.in);    
   
   System.out.print("Length A?  ");
   int a = console.nextInt(); 
   System.out.print("Length B?  ");
   int b = console.nextInt(); 
   System.out.print("Length C?  ");
   int c = console.nextInt();   
 }         
 public static computeAngleC(double a, double b, double c) {
        int angle = Math.acos((a * a + b * b - c * c) / (2 * a * b));
        // convert to degrees and return
        return Math.toDegrees(angle);
 }
 public static double computeAngleB(double a, double b, double c) {
        double angle = Math.acos((a * a + c * c - b * b) / (2 * a * c));
        // convert to degrees and return
        return Math.toDegrees(angle);
 }
 public static double computeAngleA(double a, double b, double c) {
        double angle = Math.acos((b * b + c * c - a * a) / (2 * b * c));
        // convert to degrees and return
        return Math.toDegrees(angle);
 }
}
 
/*computeAngle C = arcos(a2 + b2 - c2) / (2 ab);
computeAngle B = arcos(a2 + c2 - b2) / (2 ac);
computeAngle A = arcos(b2 + c2 - a2) / (2 bc);*/

Recommended Answers

All 6 Replies

You are not doing anything with the 3 lengths that you request from the user, You still need to use the methods you created. Also, on your first method you have not specified a return type in the method declaration, so that should cause a compile error.

How do you get the methods to calculate the lenghts?

First of all, change "public static computeAngleC()" to "public static double computeAngleC()"
Next, change "int angle" to "double angle"

That should fix the errors, then put the parameters through the functions and print the results.

BTW, in the main method, you may want to change the int's to double's, so the user can enter decimal angles, too.

import java.util.*;

public class Triangle {
 public static void main(String[] args) {
   Scanner console =  new Scanner(System.in);  
   
   System.out.println("This program prompts/computes sides of a triangle.");
   
   System.out.print("Length A?  ");
   double a = console.nextDouble();
   double sum = 0;
   System.out.print("Length B?  ");
   double b = console.nextDouble(); 
   System.out.print("Length C?  ");
   double c = console.nextDouble();  
   sum += a + b + c;
   
   //Compute result    
   System.out.println("Length A =  " +(b * b + c * c - a * a) / (2 * b * c));
   System.out.println("Length B =  " +(a * a + c * c - b * b) / (2 * a * c));
   System.out.println("Length C =  " +(a * a + b * b - c * c) / (2 * a * b)); 
   System.out.println("Sum of Triangle= " +sum);  
   System.out.println();
 }   
   
}

I guess im not understanding how to use a method in a scanner that is separate from the scanner. If it wasn't obvious already, im extremely new.

I can get the output I want just using a scanner, however, I am wanting to learn how to use a method I created in the scanner. Sooo ... Im wanting to be prompted for the length of the triangles, then im wanting the program to compute the equation and give an outputted answer.

I think this is what you are asking:
You want to use the values obtained from the user input inside the methods you created for calculations?

This is simply a matter of calling those methods, using the variables you want to pass into that call as paramaters.
So, inside your main method, you would call a method as follows:

import java.util.*;

public class Triangle {
 public static void main(String[] args) {
   Scanner console =  new Scanner(System.in);  
   
   System.out.println("This program prompts/computes sides of a triangle.");
   
   System.out.print("Length A?  ");
   double a = console.nextDouble();
   double sum = 0;
   System.out.print("Length B?  ");
   double b = console.nextDouble(); 
   System.out.print("Length C?  ");
   double c = console.nextDouble();  


   double lengthB = computeAngleB(a,b,c); //this passes the variables, a,b,c into the //computeAngleB method and because the method has a return type. You need to //store the returned value in a variable of the same type.



 }   
   
}

Bad net caused a double post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.