# Area of a Triangle

Page 1 of 1

## 6 Replies - 6774 Views - Last Post: 31 January 2008 - 05:56 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=42006&amp;s=0570024a28768c101cc3821073ed8197&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 CrazyJ

Reputation: 0
• Posts: 51
• Joined: 15-October 07

# Area of a Triangle

Posted 29 January 2008 - 08:46 PM

Hello all, I am trying to finish up what should be a rather simple program to take input for Height and Base of a triangle and output the Area. I am recieving an error on my Triangle constructor and am not real sure why. Any suggestions are appreciated.

Main
```public static void main(String[] args) {

double height, base;
//		===============
Triangle triHt;
Triangle triBase;
//		=================
Scanner scannerObject = new Scanner(System.in);

System.out.println("Please enter triangle Height in inches.");
height = scannerObject.nextFloat();
System.out.println("The height entered is " + height);

triHt = new Triangle("triHt",
height);
//		======================================
System.out.println("Please enter triangle Base in inches.");
base = scannerObject.nextFloat();
System.out.println("The base entered is " + base);

triBase = new Triangle("triBase",
base);
//		========================================
Triangle.Speak();
//		=================
System.out.println("The area of the triangle is: " + Triangle.Area());

}// main

```

Class
```public class Triangle {
//	===================
//	Instance Variables
//	===================
private double ht, bas;
//	===================
//	===========
//	Constructor
//	============================================
public Triangle(String triName, double triHt,
double triBase)
{

ht = triHt;
bas = triBase;

}//Constructor Triangle
//	=======================
//	===============
//	Accessor method
//	=======================

//	=========================
public double GetHeight()
{
return ht;

}// accessor method:GetHeight
//	=============================
public double GetBase()
{
return bas;

}// accessor method:GetBase
//	===========================

public void Speak()
{
System.out.println();
System.out.println("Triangle height =  " + ht);
System.out.println("Triangle base = " + bas);
System.out.println();

}// Speak
//	=========

public static double Area()
{
return (ht * bas/2);
}// method Area

}//class Triangle

```

Thanks!

Is This A Good Question/Topic? 0

## Replies To: Area of a Triangle

### #2 dontKnowJava

Reputation: 1
• Posts: 221
• Joined: 29-September 07

## Re: Area of a Triangle

Posted 29 January 2008 - 09:02 PM

u get an error because u dont have a triangle construct that takes 2 params. u only have 1 that takes 3 Triangle(String, double, double). so either overload ur construct to take 2 params or add a 3rd param in ur main

This post has been edited by dontKnowJava: 29 January 2008 - 09:04 PM

### #3 CrazyJ

Reputation: 0
• Posts: 51
• Joined: 15-October 07

## Re: Area of a Triangle

Posted 29 January 2008 - 10:29 PM

dontKnowJava, on 29 Jan, 2008 - 09:02 PM, said:

u get an error because u dont have a triangle construct that takes 2 params. u only have 1 that takes 3 Triangle(String, double, double). so either overload ur construct to take 2 params or add a 3rd param in ur main

I tried that and it didn't help much. I suspect that I have more issues than that in my code, I'm just getting started with Java so it is a challenge for me to root it out. Thanks for replying though...

### #4 baavgai

• Dreaming Coder

Reputation: 7183
• Posts: 14,970
• Joined: 16-October 07

## Re: Area of a Triangle

Posted 30 January 2008 - 04:41 AM

CrazyJ, on 30 Jan, 2008 - 12:29 AM, said:

dontKnowJava, on 29 Jan, 2008 - 09:02 PM, said:

u get an error because u dont have a triangle construct that takes 2 params. u only have 1 that takes 3 Triangle(String, double, double). so either overload ur construct to take 2 params or add a 3rd param in ur main

I tried that and it didn't help much. I suspect that I have more issues than that in my code, I'm just getting started with Java so it is a challenge for me to root it out. Thanks for replying though...

I second dontKnowJava.

You either mean to call, new Triangle("triBase", base); in which case you need public Triangle(String triName, double triBase) as a valid constructor. Or...

Hmm, you call with "trHeightl" as well? Two contructors of (String, double) aren't going to help you. I don't think either of your constructor calls are valid.

Did you mean something like this?
```Triangle validTriangle = new Triangle("triValid", height, base);
validTriangle.Speak();
System.out.println("The area of the triangle is: " + validTriangle.Area());

```

Looking at your code, I don't think you're real clear on instances / objects. You cannot do something like Triangle.Speak();, because it's an object method, not a class / static method.

Hope this helps.

### #5 CrazyJ

Reputation: 0
• Posts: 51
• Joined: 15-October 07

## Re: Area of a Triangle

Posted 31 January 2008 - 02:40 PM

baavgai, on 30 Jan, 2008 - 04:41 AM, said:

CrazyJ, on 30 Jan, 2008 - 12:29 AM, said:

dontKnowJava, on 29 Jan, 2008 - 09:02 PM, said:

u get an error because u dont have a triangle construct that takes 2 params. u only have 1 that takes 3 Triangle(String, double, double). so either overload ur construct to take 2 params or add a 3rd param in ur main

I tried that and it didn't help much. I suspect that I have more issues than that in my code, I'm just getting started with Java so it is a challenge for me to root it out. Thanks for replying though...

I second dontKnowJava.

You either mean to call, new Triangle("triBase", base); in which case you need public Triangle(String triName, double triBase) as a valid constructor. Or...

Hmm, you call with "trHeightl" as well? Two contructors of (String, double) aren't going to help you. I don't think either of your constructor calls are valid.

Did you mean something like this?
```Triangle validTriangle = new Triangle("triValid", height, base);
validTriangle.Speak();
System.out.println("The area of the triangle is: " + validTriangle.Area());

```

Looking at your code, I don't think you're real clear on instances / objects. You cannot do something like Triangle.Speak();, because it's an object method, not a class / static method.

Hope this helps.

Well, thank you for your help. I have managed to get this to run without errors now, but it still does not calculate Area, and the Speak method doesn't work. I feel that I am missing the obvious here so I am going to hit the books some. Any further suggestion on this would be appreciated.
```public static void main(String[] args) {

double height, base = 0;
//		===============
//Triangle triHt;
//Triangle triBase;
//		=================
Scanner scannerObject = new Scanner(System.in);

System.out.println("Please enter triangle Height in inches.");
height = scannerObject.nextFloat();
System.out.println("The height entered is " + height);

Triangle triHt = new Triangle(height, base);
//		======================================
System.out.println("Please enter triangle Base in inches.");
base = scannerObject.nextFloat();
System.out.println("The base entered is " + base);

Triangle triBase = new Triangle(height, base);
//		========================================
triHt.Speak();
triBase.Speak();
//		=================
System.out.println("The area of the triangle is: " + triHt.Area());

}// main

}// class GeometryQuiz

```

Triangle class
```public class Triangle {
//	===================
//	Instance Variables
//	===================
private double ht, bas;
//	===================
//	===========
//	Constructor
//	============================================
public Triangle(double triHt, double triBase)
{

ht = triHt;
bas = triBase;

}//Constructor Triangle
//	=======================
//	===============
//	Accessor method
//	=======================

//	=========================
public double GetHeight()
{
return ht;

}// accessor method:GetHeight
//	=============================
public double GetBase()
{
return bas;

}// accessor method:GetBase
//	===========================

public void Speak()
{
System.out.println();
System.out.println("Triangle height =  " + ht);
System.out.println("Triangle base = " + bas);
System.out.println();

}// Speak
//	=========

public double Area()
{
return (ht * bas/2.0);
}// method Area

}//class Triangle

```

This is the output:

Please enter triangle Height in inches.
8
The height entered is 8.0
Please enter triangle Base in inches.
5
The base entered is 5.0

Triangle height = 8.0
Triangle base = 0.0

Triangle height = 8.0
Triangle base = 5.0

The area of the triangle is: 0.0

This post has been edited by CrazyJ: 31 January 2008 - 03:01 PM

### #6 CrazyJ

Reputation: 0
• Posts: 51
• Joined: 15-October 07

## Re: Area of a Triangle

Posted 31 January 2008 - 03:53 PM

OKAY! I got it. Thank you dontknowjava and baavgai for your help. I learned a good lesson on Java classes with this program.

Now, let's say I want to add code to output the area of a rectangle in the same program, how could I implement it....avoiding redundancy?

```public static void main(String[] args) {

double height, base;
//		====================
Triangle validTriangle;
//		=======================
Scanner scannerObject = new Scanner(System.in);

System.out.println("Please enter triangle Height in inches.");
height = scannerObject.nextFloat();
System.out.println("The height entered is " + height);
//		======================================================
System.out.println("Please enter triangle Base in inches.");
base = scannerObject.nextFloat();
System.out.println("The base entered is " + base);
//		==================================================

//		===========================================
validTriangle = new Triangle(height, base);
//		===========================================

//		======================
validTriangle.Speak();
//		======================
System.out.println("The area of the triangle is: " + validTriangle.Area());

}// main

}// class GeometryQuiz

```

```public class Triangle {
//	===================
//	Instance Variables
//	===================
private double ht, bas;
//	===================
//	===========
//	Constructor
//	============================================
public Triangle(double triHt, double triBase)
{

ht = triHt;
bas = triBase;

}//Constructor Triangle
//	=======================
//	===============
//	Accessor method
//	=======================

//	=========================
public double GetHeight()
{
return ht;

}// accessor method:GetHeight
//	=============================
public double GetBase()
{
return bas;

}// accessor method:GetBase
//	===========================

public void Speak()
{
System.out.println();
System.out.println("Triangle height =  " + ht);
System.out.println("Triangle base = " + bas);
System.out.println();

}// Speak
//	=========

public double Area()
{
return (ht * bas/2.0);
}// method Area

}//class Triangle

```

### #7 baavgai

• Dreaming Coder

Reputation: 7183
• Posts: 14,970
• Joined: 16-October 07

## Re: Area of a Triangle

Posted 31 January 2008 - 05:56 PM

CrazyJ, on 31 Jan, 2008 - 05:53 PM, said:

Now, let's say I want to add code to output the area of a rectangle in the same program, how could I implement it....avoiding redundancy?

You've already got that Speak method? Just put it in there.

Inside speak:
```System.out.println("Triangle area = " + Area());

```