0

I am trying to make a program that calculates the area of a triangle given the values for all three sides. When I run my program with my runner file my area comes out to " 0.0"

Here is the classes full code (also I know I did the same code for setSides and triangle but my instructor gave us this shell and I didn't know what to put in there)

import java.util.Scanner; 
import java.lang.Math.*;

public class Triangle
{
 private int sideA;
 private int sideB;
 private int sideC;
 private double theArea; 
 private double s; 
 private double perimeter; 

 public Triangle()
 {
     sideA = 1;
     sideB = 1;
     sideC = 1;
 }

 public Triangle(int a, int b, int c)
 {
   sideA = (int) a;
   sideB = (int) b;
   sideC = (int) c;   
 }


 public void setSides(int a, int b, int c)
 {
   sideA = (int) a;
   sideB = (int) b;
   sideC = (int) c;
 }

 private double calcPerimeter()
 {
  double perimeter = sideA + sideB + sideC;
        return perimeter;
 }



private double calcArea()
 {
  double s = calcPerimeter() / 2;
  double theArea = (Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC))); 
  return theArea;
 }

 public void print()
 {
    System.out.println("Area == " + theArea); 
 }
}

3 Answers3

2

I did an system out on your statement. What you are using is not an subtract operator. Please fix that. It will work. Also define the permiter.

System.out.println((int)'–');

output:
8211

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
1

Is it possible that you copied some of your source code from a document that isn't a simple text document?

If so, then your problem is that your minus signs are not the proper character. Try re-typing them in a text editor.

ED: Also, a minus sign should be surrounded by spaces. When I first looked at this, it threw me.

In your revised code, you can be square rooting a negative number, which would through you in a really bad state.

Thom
  • 14,013
  • 25
  • 105
  • 185
  • This was exactly what I was about to ask. It appears there is some wonky formatting going on here. +1 for proper formatting tips =) – Jeff B Sep 21 '15 at 13:08
  • @DanielStephens98 You don't define perimeter. instead of what you have put 'double s = calcPerimeter()/2;' – Jeff B Sep 21 '15 at 13:19
  • @TheThom when I instantiate " private double perimeter; " it fixes the error but now when I go ahead and run the program with my runner my area just come out to " 0.0 " – Daniel Stephens98 Sep 21 '15 at 13:27
  • @DanielStephens98 Did you attempt the fix I posted before your comment? – Jeff B Sep 21 '15 at 13:48
  • @TheThom I downloaded it and used it but still my output is coming out to "0.0' – Daniel Stephens98 Sep 21 '15 at 14:10
0

Remove:

private double perimeter; 

And change:

 private double calcArea()
 {
  double s = perimeter / 2;
  double theArea = (Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC))); 
  return theArea;
 }

To:

 private double calcArea()
 {
  double s = calcPerimeter() / 2;
  double theArea = (Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC))); 
  return theArea;
 }

Here is what is happening. You declare a private double called perimeter. Later in the code you once again declare a double called perimeter. When you do this, things get really confusing! Make sure you only declare variables with the same name ONCE, and reference them thereafter.

Jeff B
  • 975
  • 8
  • 25