The Artima Developer Community
Sponsored Link

Java Answers Forum
Build a Constructor

6 replies on 1 page. Most recent reply: Apr 15, 2002 6:11 AM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Thang Nguyen

Posts: 18
Nickname: thang
Registered: Apr, 2002

Build a Constructor Posted: Apr 14, 2002 7:33 PM
Reply to this message Reply
Advertisement
I have an empty constructor for a class, called Point.
It is like below:

public class Point {
//ordered pair (x, y)
private int x;
private int y;
public Point(){
x=0;
y=0;
}
...
} ///:~

My question is: should I set the values of co-ordinates x and y to zero or ask the user (via the keyboard or so) to enter the values for x, y?
I am very new to Java. I would appreciate your answer. - Thang


Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Build a Constructor Posted: Apr 14, 2002 8:05 PM
Reply to this message Reply
java already has a class called Point.

java.awt.Point. Make use of it.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Build a Constructor Posted: Apr 14, 2002 10:12 PM
Reply to this message Reply
Singh, I think you missed the point.

Thang, you asked whether to set x and y in your program, or ask for input. Of course, it would generally be better to get that as input, rather than hardcoding it. You could either prompt for input (which would be different for a console app, vs. a GUI app), or you could allow it to be specified on the command line (get it from the args array). Either way, you will have to subsequently convert it from String format to int (or float, or double, etc.) using Integer.parseInt() (or Float.parseFloat(), etc.).

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Point(ed) Posted: Apr 14, 2002 10:16 PM
Reply to this message Reply
Maybe. But, the fact that the person is a java newbie and probably would not have known about the existence of class Point I just "pointed" him to that class.

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Point(ed) Posted: Apr 15, 2002 12:24 AM
Reply to this message Reply
One more remark (but nothing personnal, he guys !)

If one has the following code :

/**
*/
public class Point
{
/**
*/
int x, y;
Point ()
{
}
/**
*/
Point (int anX, int aY)
{
x = anX;
y = aY;
}
}

With the default constructor the x & y are de facto initialized to the default value for the int's.

Hence there is no need for a default initialization in the code !
At least an initialization to zero.

Thomas,

Thang Nguyen

Posts: 18
Nickname: thang
Registered: Apr, 2002

Re: Build a Constructor Posted: Apr 15, 2002 5:54 AM
Reply to this message Reply
Signh, I did not know the existence of Point actually. I will find it out.

Matt, very informative answer.

Thomas, I have tried bulding constructors with a list of arguments. However, the problems is that in my class no one knows what we should deal with the empty constructors and more, with the list of arguments in other constructors, what should we do if the values of these arguments are not valid for the use within the constructor or class? In the case that invalid values passed in as arguments are detected, should I assign the programmer default values, such as for int, value is 0, and double is 0.0, and float is 0.0f, and string or char, and so on, is "Not defined"

Anyways, I have to say that it is very interesting to be here in the Forums which I just found last night. I spend hours here just to read people's, like you guys answers to different questions. Thanks a lot! - Thang

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Build a Constructor Posted: Apr 15, 2002 6:11 AM
Reply to this message Reply
>
> However, the problems is that in my class
> no one knows what we should deal with the empty
> constructors and more, with the list of
> arguments in other constructors, what should
> we do if the values of these arguments are not
> valid for the use within the constructor or class?
>

Well if that's is the case, just make it private, so none can use it !

Flat View: This topic has 6 replies on 1 page
Topic: Event Queue Behavior Previous Topic   Next Topic Topic: Linkedlist?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use