Area of a Triangle

Constructor undefined

Page 1 of 1

6 Replies - 3496 Views - Last Post: 31 January 2008 - 05:56 PM Rate Topic: -----

#1 CrazyJ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 CrazyJ  Icon User is offline

  • D.I.C Head

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

Re: Area of a Triangle

Posted 29 January 2008 - 10:29 PM

View PostdontKnowJava, 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...
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Area of a Triangle

Posted 30 January 2008 - 04:41 AM

View PostCrazyJ, on 30 Jan, 2008 - 12:29 AM, said:

View PostdontKnowJava, 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.
Was This Post Helpful? 0
  • +
  • -

#5 CrazyJ  Icon User is offline

  • D.I.C Head

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

Re: Area of a Triangle

Posted 31 January 2008 - 02:40 PM

View Postbaavgai, on 30 Jan, 2008 - 04:41 AM, said:

View PostCrazyJ, on 30 Jan, 2008 - 12:29 AM, said:

View PostdontKnowJava, 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

Was This Post Helpful? 0
  • +
  • -

#6 CrazyJ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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. :blink:

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


Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Area of a Triangle

Posted 31 January 2008 - 05:56 PM

View PostCrazyJ, 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());


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1