Click to See Complete Forum and Search --> : how to add more methods to class methods


umen
September 4th, 2006, 03:45 AM
Hello all
I don’t know how exactly explain my problem but I will try .
I have base class that has some methods that I use on the jsp page.
my base class looks like this :

class base {
public static String getName(){
String myName = new String("miki");
return myName;
}
public static int getAge(){
int myAge = new Int(30);
return myAge;
}
}


now I will like this methods to have more methods for example toBoolean()
so if I will do :
<%
base.getAge().toBoolean();
or
base.getAge().toBoolean();
%>

it will return me true or false if age is less then 0 so false
and if the name string len is 0 also flase
and other wise true ;

how can I implement this functions on my methods?
hope I made my self clear
thanks

dlorde
September 4th, 2006, 05:32 AM
I think the best answer is that the constructor of class Base (and any setter methods) should not allow invalid data to be entered in the first place. Then you can always be certain that the data in your class is valid.
public class Base {
String name;
Date dateOfBirth;

// constructor check for invalid data and throws exception
public Base(String name, int age) {
if (name == null || name.length() < 1) {
// throw InvalidParameterException(...);
}
if (age < 0 || age > 150) {
// throw InvalidParameterException(...);
}
// data is valid, so store it
this.name = name;

// convert age to date of birth
dateOfBirth = getDOBFromAge(age);
}
...
}
If you really must have public class methods to check that the age or name are valid, add them to the Base class, or subclass it. The naming convention for boolean methods uses 'isCondition', so you might call them isValidName() and isValidAge().

There are one or two other things to consider:

There isn't much point storing someone's age, because it changes as time goes by. Much better to store their date of birth, and calculate the age when asked for it - this is what computers are good at. Most applications would request the date of birth in the first place, rather than the age.

Also it doesn't make sense to make the getName() and getAge() methods static - this would mean that you can't have different instances of the class return different names and ages!

Also you don't have to wrap a String literal in a new String to return it. String literals are fully-fledged Strings in Java.

Also, the numeric literal 30 will be treated as an int automatically, you can return it directly from an int method. It makes no sense to wrap it in a new 'Int' (whatever that is - did you mean Integer?).

Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable techniques to solve it...
A. Aho and J. Ullman

umen
September 4th, 2006, 07:03 AM
hello and thanks for the reply
i only gave simple example about the age and name it is only example for other things similar that handle real data
It have to static, because of the application im working on this is afact i cant change it .
i need some how to extend the given class to what i said in the other post.
i wander if it can be done .

dlorde
September 4th, 2006, 11:53 AM
As I said in my previous post, "If you really must have public class methods to check that the age or name are valid, add them to the Base class, or subclass it. The naming convention for boolean methods uses 'isCondition', so you might call them isValidName() and isValidAge()".

If you can't add them to the base class or a subclass, you could write a wrapper class that forwards all the Base class methods and adds the two boolean checking methods, or you could just do the tests inline in your JSP code (or preferably in a tag).

You can chain method calls similar to the way you indicated in your first post, if the first method returns an object - then you can chain a call to one of that object's methods. For example, you could do this:int nameLength = Base.getName().getLength();This calls Base.getName() to get the name String, then calls getLength() on the name String. However, I don't think this is going to be much use to you in this context.

There is nothing so useless as doing efficiently that which should not be done at all...P. Drucker