I'm trying to practice writing a program which creates an object and does various tasks.
So far the tasks I want to program to do are to take a hypothetical triangle (it doesn't draw a literal triangle on the screen; it just shows the number of triangles there, the area of a triangle, and the perimeter of a triangle). I think I've almost removed the errors from the program, but there are three errors that I have to deal with. I'm not sure how to fix them, so any help is appreciated.


error 1:
TriangleMaker.java:7: cannot find symbol
symbol : constructor Triangle()
location: class Triangle
sample= new Triangle();

error 2:
symbol : constructor Triangle(float)
location: class Triangle
Triangle sTriangle= new Triangle(0.0F);

error 3:
Triangle.java:50: setLeg(float,float,float) in Triangle cannot be applied to (float)
sTriangle.setLeg(t.leftLeg+ t.rightLeg+t.baseLeg+t.height);
^

Here is the code I have worked on:

This is the class.

/*
This class demonstrates a class which
creates an object which takes values for the
left, right, and base legs of a triangle along with
the height of a triangle. This class can also
be used to generate the area and perimeter 
of a triangle when used in a program. It must be noted that this does not
literally draw a triangle, so any program which
uses this class does not need the AWP, SWT, or 
Swing toolkits.
*/


public class Triangle
	{
		private float leftLeg=0.0F;
		private float rightLeg=0.0F;
		private float baseLeg=0.0F;
		private float height=0.0F;
			
			// a constructor which creates the triangle object
			public Triangle(float aLeg, float bLeg, float cLeg , float hLeg)
				{
					leftLeg=aLeg;
					rightLeg=bLeg;
					baseLeg=cLeg;
					height=hLeg;
			
				}
			
			//gets values for the triangle and sends them to the Triangle method
			public float getLeg()
				{
					return leftLeg;
					return rightLeg;
					return baseLeg;
					return height;
				}
			
			// sets values for the left leg, right leg, base leg, and height variables
			public void setLeg(float leftLeg, float rightLeg, float baseLeg)
				{
					this.leftLeg= leftLeg;
					this.rightLeg=rightLeg;
					this.baseLeg=baseLeg;
					this.height= height;
				}
			
			//calculates the area of a triangle
			public float triArea(float baseLeg, float height )
				{
					float area; 
					area= 0.5f* this.baseLeg * this.height;
					return area;
				}
			//calculates the perimeter of a triangle
			public float triPerim(float leftLeg, float rightLeg, float baseLeg)
				{
					float perimeter; 
					perimeter= this.leftLeg + this.rightLeg + this.baseLeg;
					return perimeter;
				}
				
			// forms the final triangle	
			public Triangle formTri(Triangle t)
				{
					Triangle sTriangle= new Triangle(0.0F);
					sTriangle.setLeg(t.leftLeg+ t.rightLeg+t.baseLeg+t.height);
					return sTriangle;
				}
	}

Here's the main program.

// a sample program to test the Triangle class

public class TriangleMaker
	{
		public static void main(String[] args)
			{
			
			Triangle sample;// declares a Triangle object
			sample= new Triangle();
			sample.getLeg();// gets a value for the legs and height of a triangle
			System.out.println(sample);//prints out the values of the triangle
			
			}
	}
//This program is not finished, but I am trying to test the getLeg method
//in the Triangle class.

Recommended Answers

All 3 Replies

and what don't you understand? The compiler gives errors which couldn't be much clearer.

I understand the errors, I'm just not sure how to fix them.

I don't know why neither the main program nor the formTri method in the Trinagle aren't recognizing the constructor. And I'm not sure how to set up the equation in the formTri method so that it makes the final triangle.

They aren't recognizing the constructors because you have not written them. You have a single constructor available which takes 4 float parameters. If you need others then you will need to write them.

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.