Saturday, June 27, 2009

constructor tricks: constructor overloading, calling constructor from constructor


Did I catch your attention with the megan fox picture?
I assume that the answer is yes, great.

Constructor overloading is not really different from a method overloading.
In practice, all you have to do is to give different arguments to similar methods.
For a Fruit class,
  Fruit(String name_, int count_) 
and
  Fruit(String name_, int count_, String color_) 
are both constructors. They are similar in almost every way; only difference is that the second one has a color attribute.

Now, let's move to a more tricky subject. Calling a constructor from another constructor. You can ask why would I want to do that. Let's see it on our example:

 public class Fruit {

int count;
String name, color;
final static String SEPARATOR = ", ";
// the part below is where I use constructor overloading
Fruit(String name_, int count_){
name = name_;
count = count_;
}

Fruit(String name_, int count_, String color_){
name = name_;
count = count_;
color = color_;
}
// the part above is where I use constructor overloading

public String toString(){
String returnValue = "name: "+name+SEPARATOR+"count: "+count;
if(color != null)
returnValue += SEPARATOR+"color: "+color;

return returnValue;
}

public static void main(String[] args) {
// create three apples which I dont care the color
Fruit apples = new Fruit("apple", 3);
System.out.println(apples);

// create two yellow melons
Fruit melons = new Fruit("melon", 2, "yellow");
System.out.println(melons);
}

}


As you can see the example is pretty straightforward. This is the fruit class. There are two mandatory and one optional attribute related to my class. Fruit name and fruit count are mandatory and the color is optional. So to have this effect, I used constructor overloading.

Now lets see why would I want to call a constructor from another constructor. As you can see both constructors in the example share the mandatory attributes. So I can easily call the first constructor from the second one (by using this() keyword) and remove the code that has a reoccurence. The below code is improved in readability as you can see.

 public class Fruit {

int count;
String name, color;
final static String SEPARATOR = ", ";

Fruit(String name_, int count_){
name = name_;
count = count_;
}
/*
removed two redundant lines and added "this" to call the constructor above.
we give its essential parameters (name and count) by passing them
to "this" as arguments.
*/

Fruit(String name_, int count_, String color_){
this(name_, count_);
color = color_;
}

public String toString(){
String returnValue = "name: "+name+SEPARATOR+"count: "+count;
if(color != null)
returnValue += SEPARATOR+"color: "+color;

return returnValue;
}

public static void main(String[] args) {
Fruit apple = new Fruit("apple", 3);
System.out.println(apple);

Fruit melon = new Fruit("melon", 2, "yellow");
System.out.println(melon);
}

}

Sunday, June 14, 2009

resizing pictures in batch

You got photos of last night and you want to submit to facebook or send them to your friends. But there's a little problem: the pictures are too big for uploading. You can resize them so they'll take less space and then it'll be less painful to post it somewhere or to send it to some friend of yours. But there's another problem. Will you resize the pictures one by one? What if there are more than a dozen?

Fotosizer is a batch image resizer that will solve your problems. You can choose the new size (while maintaining the aspect ratio if you want) and the output file format (jpg, bmp, gif, tiff, png). Plus fotosizer is a freeware. I highly recommend it.

Wednesday, May 27, 2009

vncconfig's copy/paste bug

I'm doing my coding on a workstation by connecting to it with a vnc client. To be able to do copy/paste or cut/paste between my computer and the workstation I use vncconfig. But there are times it does not work at all. You copy your text on one computer and when you try to paste it on the other computer, nothing happens. I discovered a workaround for this. Assume that you're trying to copy/paste from computer A to computer B and nothing happens. Open the screen of computer B, copy some text from it (anything). Then open computer A and copy the desired text, later paste it in computer B. It'll do the paste now.

Monday, May 4, 2009

esoteric programming languages

Wikipedia has an awesome article about esoteric programming languages. Only few of them is designed for being adopted for real world problems. But the majority of them are quite interesting and entertaining. Take a look at Brainfuck for instance. You won't meditate much on the naming of this programming language after seeing the Hello World example.


++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++
..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

Monday, April 27, 2009

recommendation for people with desire to throw money away: burningairlines.com

About half a year ago, I ordered bunch of t-shirts from burningairlines.com. I never got these t-shirts and neither burningairlines, nor usps did care what happened to my order. If you got loads of money to throw away, I highly recommend you to shop from burningairlines; satisfaction guaranteed.