About two years ago, in Data Structures and Algorithms, our lecturer briefly talked about the “If-then-else” statement and how much expensive it is as an operation. Leaving us with many questions, I extensively researched the topic and found out that the word “expensive” is, well, really relative. Nevertheless, a particular student found a way to avoid the cost…
Code
This category contains a set of programming-related posts. New posts are available usually 10 minutes after I find a hard-to-figure-out bug in my code.
CodeSOD: How to avoid the expensive “If-then-else” statement
Custom JPanel cell with JButtons in JTable
If you ever wanted to add a JPanel with various interactive components (e.g. JButtons, JCheckBoxes etc.) in a JTable cell and could not figure out how to make them work, then this post is for you. Otherwise, continue googling ;)
How many access levels does Java have anyway?
Before I start with my new post, I would like to talk a little about my previous one: “Rule for public void method signatures“. As it turns out, this is a proposal for Java 7 called “chained invocations”. You can find more details here. There is also another way of doing this called “Double Brace Initialization“:
JPanel panel = new JPanel() {{ setLayout(new BorderLayout()); setBorder(BorderFactory.createEmptyBorder()); }};
At first site, it seems useful in a number of situations (i.e. Graphical User Interfaces), on the other hand, many suggest that this should not be abused as it creates ugly and cluttered code.
Now, let’s put that in the side and go back to what you have learned in Java programming.
Idea: Rule for “public void” method signatures
I don’t remember in which book I read this, but the book created a wrapper class for MenuItem that when calling a setter it would return the class itself. So I started thinking: Would it be a bad practice if every time I created a class I would replace a public void with a public MyClass? In other words, use this as a general rule. Why don’t we see?
Dive In: JSnake
Continuing on the Dive In series, we are going one step further and creating a small game using animation: a clone of the classic video game Snake. As always, this will be as small as possible and many many changes can be made (which I’ll once again try to point out). We’ll start from design all the way to implementing and testing it using the easy Water Fall model. You can run the game and download the source code at my JavaCorner.
So, without further ado, let’s start!
Playing with Try, Catch and Finally
I read a blog post (Greek) recently that described the path Java takes when executing a try, catch, finally block. But before I continue I would like you to consider the following code. What will be printed out?
public class ErrorClass { public static void main(String[] args) { System.out.println(getMessage()); } private static String getMessage() { String toReturn = "Initializing"; try { throwException(); toReturn += "+No Exception"; return toReturn; } catch (Exception e) { toReturn += "+Catch"; return toReturn +" from Catch"; } finally { toReturn += "+Finally"; return toReturn +" from Finally"; } } private static String throwException() throws Exception { throw new Exception("original exception"); } }
a) “Initializing+No Exception”
b) “Initializing+Catch from Catch”
c) “Initializing+Catch+Finally from Catch”
d) “Initializing+Catch+Finally from Finally”
Dive In: JSameGame
In this post I’ll create a small game project in Java from scratch based on the game SameGame. There are numerous of ways to create this game. I’ll try to make it as simple as possible and use Good Practices as much as I can. We’ll go step by step all the way from designing the idea to creating and testing it. Because this is a small project, we will be using the Waterfall Model. This post is dedicated to a friend of mine, Niko (see, it’s that simple! ;) ). The game along with the source code can be found at pek’s Java Corner.
So, let’s begin!
