Thursday, November 27, 2008

Java Reference Book - (Good for Newbies)





You may often wonder what is the best book for beginners who just started to learn Java. You might have come across book which is too hard to understand and at the end you don't even have a clue what the book is talking about. I have some suggestion on what book is best for newbies. There are two books that I would recommend you. The name of the book is Java How To Program, sixth edition,by Harvey&Paul and Java Programming: From Problem Analysis to Problem Design by D.S. Malik. I had used this books before.

There are differences between the two books. The book written by D.S. Malik provide examples that is similar to his C++ book. Therefore, for those who have been using his C++ programming book to refer to before will not find any differences in this book. Even by that, the fact that this book provide in-depth and easy-to-learn explanation of the programming basic. If you are new to the programming world this book is the right choice for you. The example provided by the book is also simple and basic, but on the other hand, it is too simple and basic that it comes to a part where even a simple component like JSlider* component example is not provided in this book.

On the other hand, The Harvey&Paul book provide an in-depth explanation of its example. There are a lot of component explanation and example in this book compare to the D.S. Malik book. This book however, is not suitable for people who have no knowledge in programming basic. As its explanation in the programming basic is somehow difficult to understand for me. It is suitable who are new to the Java language and have a basic in programming.

More information on these book:
Java How To Program, sixth edition,by Harvey&Paul
Java Programming: From Problem Analysis to Problem Design by D.S. Malik

*JSlider enable the user to select from a range of integer values

Wednesday, November 26, 2008

Making Right Angle Triangle

It has been a while since I've post coding in this blog. I was busy with my semester exam and assignments. These code uses for loops to generate a triangle when it is execute.

Here is a sample code that I have done:

import javax.swing.JOptionPane;

public class TriangleMaker

{

public static void main(String[] args)

{

String output = "", num;

int n = 0;

num = JOptionPane.showInputDialog("Enter any number between 1 to 20:");

n = Integer.parseInt(num);

if(n > 0 && n <>

{

nextRow:

for(int row = 1; row <= n; row++)

{

output += "\n";

for(int column = 1; column <= (n * 2); column++)

{

if(column > row)

continue nextRow;

output += "* ";

}

}

JOptionPane.showMessageDialog(null, output, "Triangle Make", JOptionPane.INFORMATION_MESSAGE);

}

else

JOptionPane.showMessageDialog(null, "You have input an invalid value", "Invalid value", JOptionPane.ERROR_MESSAGE);

System.exit(0);

}

}

In this code, the labeled for loop starts at the nextRow label. When the if structure in the inner for loop detects that column is greater than row, the labeled continue statement skips the current iteration of the outer loop marked with the given label. Even though the inner for loop counts greater than the outer loop, the number of * characters output on a row never exceeds the value of row.

Monday, November 17, 2008

Zero Error??Think Again!!

Have you ever made a program where there is no fault that you can find in the coding you've made? One of the example of the program that I thought there was no fault in the coding and yet, when I run the program there is this one error that I just thought at that time was never an error. When I go through my coding for like a thousand time, there is no logic errors. I even ask my dad(FYI my dad is a programmer too.) to go through the coding. He too cannot find the errors. Here is part of the program that have an error:


An overview of what my program was intended to do. It was supposedly to count the amount of money a user have input into the vending machine and check the money inserted with the price of the drinks that the user have chosen.


if(price == 0.00)

inform.setText(" Thank you for using the vending machine. \n Hope to see you again. \n You have no more balance.");

else if(price <>

inform.setText("Thank you for using the vending machine. \n Hope to see you again.\n You have another RM " +twoDigits.format(price));

else if(price > 0.00)

inform.setText("You have input an insufficient amount of money in the vending machine. \n You have to input another RM " +twoDigits.format(price));


Anyone who read this would never find the logical problem. I am sure of it. After a hard time trying to debug this code, I have found that the compiler that I was using (BlueJ) have count the price not equals to 0.00 but it equals to 2.7755575615628914E-17 and when we set the value to be of two decimal format it will show that it is 0.00 but in reality it actually equals to 2.7755575615628914E-17. Therefore, when you have encountered such an error you should check back your program.