i have a question...

hey,

for example, lets say ive got the following

public class circle{

protected double x , y, radius;

public circle (double x, double y, double r) {

this.x=x; this.y=y; radius=r;

}//this is the constructor

public circle (double r){

radius=r; x=0.0; y=0.0; }

public circle(){

radius=1.0; x=0.0; y=0.0}// this is the standard constuctor

now, it pretty easy cos its a circle. How can i do the same but for a triangle. My problem is that there are 3 sides and i dont know how to define them. So there are 3 points i need to define in the standard constructor. Im not sure about that part.

Any help is very appreciated.

[693 byte] By [RussianDude] at [2007-9-30 21:58:02]
# 1
Maybe something like Triangle(Point one, Point two, Point three)?
nasch_ at 2007-7-7 3:25:43 > top of Java-index,Security,Event Handling...
# 2
yeah cool, but how do i define those 3 points in the standard constructor. The problem is that every point has 2 coordinates (2D). So lets say i want 1st point to be (-1;0) 2nd point to be (1;0) and 3rd point to be (1;0) in the standard constructor. How can i do that?thanks
RussianDude at 2007-7-7 3:25:43 > top of Java-index,Security,Event Handling...
# 3
in the example above, there is only 1 point, so i have one x , one y coordinate and the radius defined. Iwhat about 3 points?
RussianDude at 2007-7-7 3:25:43 > top of Java-index,Security,Event Handling...
# 4
new Triangle(new Point(-1,0),new Point(1,0),new Point(1,1));
nasch_ at 2007-7-7 3:25:43 > top of Java-index,Security,Event Handling...
# 5

ive done that but i get an error message , heres the code

public class Triangle{

protected double x; //Point 1

protected double y; //Point 2

protected double z; //Point 3

//Konstruktor

public Triangle(double x,double y,double z)

{this.x=x;this.y=y;this.z=z;}//Konstruktor

//Methoden:

//For Point 1

public double getX(){return x;}

public void setX(double x){this.x=x;}

//For Point 2

public double getY(){return y;}

public void setY(double y){this.y=y;}

//For Point 3

public double getZ(){return z;}

public void setZ(double z){this.z=z;}

//Konstruktor

public Triangle(){this(x=(-1,0),y=(1,0),z=(0,1));}

}

RussianDude at 2007-7-7 3:25:43 > top of Java-index,Security,Event Handling...
# 6

What I wrote and what you wrote are not at all the same, so you have not done what I suggested. Maybe you thought I was writing pseudocode? The code that I wrote will create three new Point objects and pass them to the Triangle constructor. That constructor will need to do something with them (such as store them in instance fields).

Resources for Beginners

[url=http://java.sun.com/docs/books/tutorial/]Sun's basic Java tutorial[/url]

[url=http://java.sun.com/learning/new2java/index.html]Sun's New To Java Center[/url]. Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.

[url=http://javaalmanac.com]http://javaalmanac.com[/url]. A couple dozen code examples that supplement [url=http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance]The Java Developers Almanac[/url].

[url=http://www.jguru.com]jGuru[/url]. A general Java resource site. Includes FAQs, forums, courses, more.

[url=http://www.javaranch.com]JavaRanch[/url]. To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.

Bruce Eckel's [url=http://mindview.net/Books/DownloadSites]Thinking in Java[/url] (Available online.)

Joshua Bloch's [url=http://www.amazon.co.uk/exec/obidos/Author=Bloch,%20Josh]Effective Java[/url]

Bert Bates and Kathy Sierra's [url=http://www.amazon.com/exec/obidos/tg/detail/-/0596004656?v=glance]Head First Java[/url]. This one has been getting a lot of very positive comments lately.

nasch_ at 2007-7-7 3:25:43 > top of Java-index,Security,Event Handling...
# 7

according to your way, you would be better off

public Triangle(

double x1, double x2,

double y1, double y2,

double z1, double z2) {

// assign plots

}

in three months when u have a grasp of what you are doing, you can do

class Point(int x, int y) {

// etc

}

public Triangle(Point n1, Point n2, Point n3) { ; }

In six months you will realize there is already a Point class.

In nine months you will regard nasch advice as downright swarmy.

talon747 at 2007-7-7 3:25:43 > top of Java-index,Security,Event Handling...
# 8
Swarmy? Maybe smarmy, as in sleek (definition 2 from The American Heritage Dictionary)?
nasch_ at 2007-7-7 3:25:43 > top of Java-index,Security,Event Handling...
# 9
thanks guys
RussianDude at 2007-7-7 3:25:43 > top of Java-index,Security,Event Handling...