Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2008

Tip: Looking for answers? Try searching our database.

Need a pretty method

Thread view: 
fernhom@gmail.com - 23 Feb 2008 18:16 GMT
How do I make my count() method prettier?
TIA fh

public class ObjectCounter {

 Object a;
 Object b;

 public ObjectCounter() {
   Object a = null;
   Object b = null;
 }

 public int count() {
   if(a == null && b == null)
     return 0;
   else if(a != null && b !== null)
     return 2;
   else
     return 1;
 }

 public Object getObjectA() {
   return a;
 }

 public Object getObjectB() {
   return b;
 }

 public void setObjectA(Object a) {
   this.a = a;
 }

 public void setObjectB(Object b) {
   this.b = b;
 }
}
Lew - 23 Feb 2008 18:21 GMT
> How do I make my count() method prettier?

Depends on what you find "pretty".  You can certainly avoid testing each
variable twice, though.

>   public int count() {
>     if(a == null && b == null)
[quoted text clipped - 4 lines]
>       return 1;
>   }

public int count()
{
 return (a == null? 0 : 1) + (b == null? 0 : 1);
}

Signature

Lew

Patricia Shanahan - 23 Feb 2008 18:26 GMT
>> How do I make my count() method prettier?
>
[quoted text clipped - 14 lines]
>  return (a == null? 0 : 1) + (b == null? 0 : 1);
> }

public int count(){
  int result = 0;
  if(a != null){
    result++;
  }
  if(b != null){
    result++;
  }
  return result;
}

also avoids testing twice. I find it a bit more readable, and it is
obvious how to extend it to deal with more references, for example an array.

Patricia
fernhom@gmail.com - 23 Feb 2008 18:35 GMT
> > fern...@gmail.com wrote:
> >> How do I make my count() method prettier?
[quoted text clipped - 30 lines]
> also avoids testing twice. I find it a bit more readable, and it is
> obvious how to extend it to deal with more references, for example an array.

Simple as result + 1 :)
yep,could need to extend
thanks
> Patricia
fernhom@gmail.com - 23 Feb 2008 18:27 GMT
> fern...@gmail.com wrote:
> > How do I make my count() method prettier?
[quoted text clipped - 16 lines]
>
> }

Simple as 1+1 :)
thanks
> Lew
Daniel Pitts - 24 Feb 2008 18:21 GMT
> How do I make my count() method prettier?
> TIA fh
[quoted text clipped - 34 lines]
>   }
> }

public class ObjectCounter {
  Map<String, Object> obj = new HashMap<String, Object>();

  public int count() {
    return obj.size();
  }

  // ...
}

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.