5 Replies - 15230 Views - Last Post: 08 October 2012 - 11:22 PM Rate Topic: -----

#1 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Java code using to calculate the area of a triangle w/Heron's Form

Posted 01 October 2012 - 04:18 PM

I have to use methods to write a program to allow the user to input the 3 lengths of their triangle and then the program must output the area of the triangle using Heron's Formula to calculate the area. I think my code so far is correct because there are no errors but I keep getting the area =0. I think there may be something wrong with my calculation steps. If somebody could help me it would be much appreciated!

import java.util.Scanner;
public class HeronsFormula {
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the length of the three sides of your triangle: ");
		double a = input.nextDouble();
		double b = input.nextDouble();
		double c = input.nextDouble();
		System.out.println("The lengths of the sides of your triangle are: " + '\n' + a + '\n' + b + '\n' + c);
		System.out.println("The are of your triangle according to Heron's Formula is: " + getArea(a,b,c));
		
	}

	public static double getArea(double a, double b, double c) {
		double s = (a + b + c)/2;
		double x = ((s) * (s-a) * (s-B)/> * (s-c));
		double area = Math.sqrt(x);
		return area;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java code using to calculate the area of a triangle w/Heron's Form

#2 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Java code using to calculate the area of a triangle w/Heron's Form

Posted 01 October 2012 - 04:19 PM

On line 15, that 2 needs to be 2.0
Was This Post Helpful? 0
  • +
  • -

#3 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Re: Java code using to calculate the area of a triangle w/Heron's Form

Posted 01 October 2012 - 04:27 PM

Thanks! But it's still giving me the output as 0 /:

Thanks! But it's still giving me the output as 0 /:

and that "B" on line 16 is actually a lowercase b...idk how that happened sorry.
Was This Post Helpful? 0
  • +
  • -

#4 kellzor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

Re: Java code using to calculate the area of a triangle w/Heron's Form

Posted 02 October 2012 - 10:46 AM

i just put it in netbeans and it ran without a problem?
Was This Post Helpful? 0
  • +
  • -

#5 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: Java code using to calculate the area of a triangle w/Heron's Form

Posted 02 October 2012 - 10:55 AM

View Postnatecat, on 01 October 2012 - 07:19 PM, said:

On line 15, that 2 needs to be 2.0


That is not true. because the results of ( a + b + c ) will be a double value, double / int (or double / double) will result in a double value.

--

Brovahkiin501, first the B issue is a problem with our forums, don't worry about that. However what input are you using for your triangles. When I run your code, nothing breaks:
Enter the length of the three sides of your triangle: 3
4
5
The lengths of the sides of your triangle are: 
3.0
4.0
5.0
The are of your triangle according to Heron's Formula is: 6.0


Was This Post Helpful? 0
  • +
  • -

#6 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Re: Java code using to calculate the area of a triangle w/Heron's Form

Posted 08 October 2012 - 11:22 PM

HI everyone! Thank you so much for your help. I finally figured this out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1