hi
i write code that is mesure the area of traingle
with two constructor ... with argument and default constructor
and set ,, get , high and length ,,
and tostring , equals methods
but i think is there a logical error

class file

public class Traingle {
	private int length;
		private int high;
	public Traingle (){
		length=10;
		high=11;
	}
public Traingle (int l,int w){
	length=l;
	high=w;
}
public void setLength(int l){
	length=l;
}
public void setHigh(int w){
	high=w;
}
public int getLength()
{
	return length;
}
public int getHigh()
{
	return high;
}
public boolean equals(Traingle obj){
	return((length==obj.length)&&(high==obj.high));
}
public String toString(){
	return "("+length+","+high+")";

}
public int areaTringle( ){
	int area=0;
	area=(high*length)/2;
return area;
}
}

main

public class Test{

  public static void main(String [] args){
  	Traingle o=new Traingle ();
  	System.out.print(o);
  	System.out.println();
  	System.out.print("area is "+o.areaTringle());
	Traingle pos =new Traingle ();
	if(o.equals(pos))
System.out.println("\n are equals");
else
System.out.println("not equals");
  }

}

and what os the copy constructor ,how i can writed ,
and if you can explain equals method how its work ..

equals method looks OK to me - if you wrote it why do you need an explanation of how it works?
A copy constructor takes one parameter, which is an existing instance of the class (Triangle). It populates the variables of the new instance by copying the values from the other one - thus the new instance is a copy of the other one. Have a look at the fkirst part of this:
http://www.javapractices.com/topic/TopicAction.do?Id=12

yes i wrote equals ,, but if you run my code you will find some errors ...
it is give me alwayse they are equals .and thats error

You have defined "equals" for two Triangles to mean that they have the same lengths and heights. In your test case you create two Triangles using the default constructor, which means they both have length=10 high=11, so you consider them to be equal.
If you change (eg) Traingle pos =new Traingle (); to
Traingle pos =new Traingle(20, 30);
then the lengths and heights will be different and the Triangles will not be equal.

Traingle pos =new Traingle (30,30);
it is give me not equal :(

Why the sad face? One trinagle is 10,11 the other is 30,30, so the two triangles are not equal. That's right.

haha
because i thinking with different things ,,
just confused
thanks alot ..
can you write a copy constructor for this class ,,
i didn't know what its form .

Of course I can write a copy constructor... but this is your homework. Re-read the previous post - it's enough info.
(I'm going out now, bye).

ok correct this :
public Traingle (Traingle t){
length =t.length;
high=t.high;
it is true or not ....
copy constructor has object from type Traingle class ...

what the goal of copy constructor ..
its helpful for what ???


.. bye
and i waiting your answer ///

public Traingle (Traingle t){
length =t.length;
high=t.high;

i need to know is this copy constructor

Yes, that's a copy constructor (assuming you add the closing } ).
It's useful if you want to make a copy of a Triangle that you've already got.

I'll answer for him:

First of all, please write with normal sentences, and capital letters, like me. It makes your text a lot more humanly readable.

Your constructor is right.
You copy something by saying

Triangle triangle2 = triangle.copy();

So, make a copy method, that returns a Triangle object, as a copy of the current object.

Who are you answering for?
OP was asked for a copy constructor, not a copy method. Please read the whole thread before contributing.

The guy who started answering, that was you. Never mind.
To make a copy method, you need a copy constuctor.
I thought he wanted a copy method, in which he uses his constuctor. Why would you only want a copy constuctor? In my opinion, using copy() looks far cleaner.

// My idea:
t1 = new Triangle (20,20);
t2 = t1.copy();
t3 = t2.copy();
t4 = t1.copy();
t5 = t3.copy();

// in stead of

t1 = new Triangle (20,20);
t2 = new Triangle (t1);
t3 = new Triangle (t2);
t4 = new Triangle (t1);
t5 = new Triangle (t3);
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.