jump to navigation

Passing arrays to methods (Java) September 26, 2006

Posted by deltawing in Java.
add a comment

This confuses people from time to time. Let’s clear it up now.

to declare an array you do this:

int[] somearray = new int[20];

or if you prefer,
int somearray[] = new int[20] ;

doSomething( somearray ); //when passing an array, do not include the square backets

public void doSomething( int somearray [] ) //when receiving an array, have the brackets in place. Also, do not include the array size in the brackets.

{..}

Essence of OOP – Interfaces September 13, 2006

Posted by deltawing in Uncategorized.
add a comment

OOP can be confusing, and the concept of interfaces is one that has always confused me. Note that I’m not talking about GUIs, I’m talking about interfaces and polymorphism.

Examples are the best way to explain things.

This morning my tutor at university explained it excellently (and humourously), so I will simply use his example:

There is a Postbox class. This class has the responsibility of posting
things. Thus, it would have a post() method. It accepts the object you would like to post, and posts it.

Then, you have a Cat class. This class represents a cat. If we want to post the Cat, then we’ll have to create a cat Object in our Main or Driver class, then pass it to the Postbox class. Then, the Postbox class must accept a Cat object.

All good to this point. What then, if we would like to post a goldfish? We would create a Goldfish class. Uh oh, the Postbox class only accepts objects of type Cat. This won’t work.

We need a Postable interface. The interface is simply a class that has method signatures (but no implementation). It has no code and no brackets, just a semicolon at the end.

We then have the Cat and Goldfish class both implement the Postable interface. Note that, once you implement an interface, you must implement all the methods of the interface. If you don’t, then the program will not work.

Finally, in the Main class you can do something like this:

————————————————- 

PostBox postbox = new PostBox() 

Postable item = new Cat();

postbox.post(item) where item

 or

PostBox postbox = new PostBox() 

Cat my_cat = new Cat();

postbox.post(my_cat);

————————————————-

Here’s the interface code:

public interface Postable

{

public String get_name();
public String get_destination();
public double get_weight();
public boolean is_animal(); 

}

Thanks to my tutor for explaining :) Whether I regurgitated it correctly here, I’m not 100% sure, but I think so!

Essence of OOP September 13, 2006

Posted by deltawing in Computer Science.
add a comment

For those confused, a simple breakdown of the OOP concept:

  • Encapsulation – the hiding of information
  • Inheritance – a class extends another class
  • Polymorphism – several methods have the same name, or, several methods have the same name but different parameters. Overriding Vs. Overloading.

Misc:

Overriding Vs. Overloading
Overriding methods where you have the same arguments for the overriding method, as opposed to overloading where you have different arguments. ?Overriding used with inheritance?

Reference:

http://www.developer.com/tech/article.php/974871

Algorithms and Efficiency Part I September 5, 2006

Posted by deltawing in Computer Science.
add a comment

How do you measure the complexity and efficiency of code? This is a summary of what I learnt in my lecture notes. Imagine you are searching for a name in a phone directory. You can do this in 2 ways:

  1. Linear search – you flip through the phone book from the first page to the last, looking for the name.
  2. Binary search – you open a book in the middle and start from there. If the name you are looking for alphabetically precedes the current alphabetical index, then from now on ignore the entire right hand side of the phone book. Then, you simply repeat the process for the remaining left hand side of the book and so on until the name is found.

With linear search, the average time that it will take to search the directory is expressed by (n / 2) t, and n * t is the worst case, where n is the number of names in the directory and t is time that is needed to read each name.

With binary search, the worst case time that it wil take to search the directory is expressed by (Logn + 1) t

Assuming that it takes 1 second to look at and compare a name in the phone book, let us compare these 2 methods:

Linear Search
To search through 3 million names, it will take a linear search on average 17.36 days to find the name.

Binary Search

To search through 3 million names, it will take a binary search no longer than 21.52 seconds to find the name.
To be continued.