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.

Monday, September 15, 2008

Converting From Numbers To Character

This program ask for user to input an integer between 0 to 35. If the user input integer between 0 to 9, the program output the corresponding integer. However, if the user input is greater than 9, the program will have to execute in the following form: (e.g. : If the integer is 10 --> output is 'A', if the integer is 11 --> output is 'B', and so on and 35 --> output is 'Z')

Sample Code:

import java.util.*;

public class CheckNumber
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num2Change, newNum;

System.out.println("Please enter an integer between 0 to 35: ");
num2Change = sc.nextInt();

while(num2Change >= 0 && num2Change <>
{
if(num2Change >= 0 && num2Change <>
{
System.out.println("Output is: " +num2Change);
}
else
if(num2Change >= 10 && num2Change <>
{
newNum = num2Change + 55;
System.out.println("Output is: " +(char)(newNum));
}
System.out.println("Please enter an integer between 0 to 35: ");
num2Change = sc.nextInt();
}
System.out.println("You have input an invalid integer.");
}
}

Saturday, September 13, 2008

Converting From Roman Numeral To Decimal

This program converts Roman numerals into decimal equivalents. The program prompts the user for a Roman numeral, which is entered as a string. After converting the Roman numeral to decimal, the program prints the decimal numeral. The program halts with an error message if the user enters a string that is not a valid Roman numeral. Assume that each Roman numeral has a maximum length of 10. The following table gives the Roman symbols and their decimal equivalents:

Roman

Decimal

M

1,000

D

500

C

100

L

50

X

10

V

5

I

1


The algorithm for converting a Roman numeral R1, R2, …, Rn to decimal is

1. Set i to 1, where i is the position of the symbol currently being scanned.

2. Set convert to 0. At the conclusion of the algorithm, convert is the decimal value of the Roman numeral.

3. If i = n, add the decimal value of Rn to convert and stop.

4. If Ri ≥ Ri+1 in decimal, add the decimal value of Ri to convert, set i to i+1, and go to step 3.

5. If Ri <>i+1 in decimal, subtract the decimal value of Ri from convert, set i to i+1, and go to step 3.

Example: The decimal value of the Roman numeral XIV is 14. Initially, convert is 0. Because X’s value id greater than I’s, we add 10 to convert so that convert now equals 10. Because I’s value is less than V’s, we subtract 1 from convert, which now equals 9. Because V is the last numeral, we add 5 to convert to obtain 14.

Your program should consist of a class, say RomanType. An object of the type RomanType should do the following:

a. Store the number as a Roman numeral.

b. Convert and store the number into decimal.

c. Print the number as a Roman numeral and decimal number.


Sample Code:

Method in class RomanType is for converting the value of the roman to decimal.

import java.util.*;
import javax.swing.*;

public class RomanType
{//open class RomanType
private String roman;
private char[] num = new char[10];

public RomanType()

{//open constructor

roman = "";
}//close constructor

public void setRom()//method to ask the user to set the roman numeral they want to convert
{//open method setRom()
roman = JOptionPane.showInputDialog(null,"This is a Roman Numeral converter \nPlease enter a valid Roman number:");
}//close method setRom()

public void romToDec()//method to convert roman numeral to decimal

{//open method romToDec()
if (roman.length() == 0)
JOptionPane.showMessageDialog(null,"You leaved a blank. Please fill in a Roman Number.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE);
if (roman.length() > 10)
JOptionPane.showMessageDialog(null,"You have entered more than 10 Roman Numbers \n Please enter a number which is less than 10 numbers in length", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE);
else
{
char[] romArray = roman.toCharArray() ;//convert String to an array of char
for (int i = 0 ; i < style="color: rgb(192, 192, 192);">//assingning array romArray to array num
{
if
(romArray[i] == 'm' || romArray[i] == 'M')
num[i] = 1000 ;
else if (romArray[i] == 'd' || romArray[i] == 'D')
num[i] = 500 ;
else if (romArray[i] == 'c' || romArray[i] == 'C')
num[i] = 100 ;
else if (romArray[i] == 'l' || romArray[i] == 'L')
num[i] = 50 ;
else if (romArray[i] == 'x' || romArray[i] == 'X')
num[i] = 10 ;
else if (romArray[i] == 'v' || romArray[i] == 'V')
num[i] = 5 ;
else if (romArray[i] == 'i' || romArray [i] == 'I')
num[i] = 1 ;
else
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE);
}
}
}// close method romToDec()

public int convert()//method to count the whole converted roman numeral to decimal
{//open method convert()
int convert = 0;

if (num[0] <= num[1] && num[2] > num[1])

JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
else if (num[1] <= num[2] && num[3] > num[2])
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
else if (num[2] <= num[3] && num[4] > num[3])
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
else if (num[3] <= num[4] && num[5] > num[4])
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
else if (num[4] <= num[5] && num[6] > num[5])
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
else if (num[5] <= num[6] && num[7] > num[6])
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
else if (num[6] <= num[7] && num[8] > num[7])
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
else if (num[7] <= num[8] && num[9] > num[8])
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
//check the validity of the roman numeral. Example of invalid roman numeral is IIIV
else
{
for (int i = 0; i <= 8; i++)//count the whole roman numeral { if(i != roman.length() - 1)
{
if (num[i] >= num[i + 1])

convert = convert + num[i];
else
convert = convert - num[i];
}
if (i == roman.length() - 1)
convert = convert + num[roman.length() - 1];
}
}
return convert;//return the decimal value of the roman numeral
}//close method convert()
}//close class RomanType


Here is the main program.

import java.util.*;
import javax.swing.*;

public class Application
{//open class Application
public static void main (String [] args)
{//open main
int n = JOptionPane.showConfirmDialog(null, " Enter/Proceed with this program", "Enter/Proceed Program", JOptionPane.YES_NO_OPTION);
//ask user if they want to proceed or not
while(n == JOptionPane.YES_OPTION)
{
String output;

RomanType romanType = new RomanType();

romanType.setRom();
romanType.romToDec();

output = "The conversion from roman numeral to decimal is, " +romanType.convert();
JOptionPane.showMessageDialog(null, output, "Roman Value", JOptionPane.INFORMATION_MESSAGE);//output the decima value of the roman numeral

n = JOptionPane.showConfirmDialog(null, " Enter/Proceed with this program", "Enter/Proceed Program", JOptionPane.YES_NO_OPTION);
}
}//close main
}//close class Application




How To Output Multiple Elements Per Line From An Array

This program output multiple elements per line from an array.

Sample Question:

Declare an array myList with 40 elements of type int. Then initialized the first 20 elements of the array with the square of the index variable. Then the last 20 elements of the array value is initialized to four times the index variable. Output five elements per line.

Sample Code:


public class ArrayMyList
{
public static void main(String[] args)
{
int arraySize = 40;
int lineToOutput = 5;
int[] myList = new int[
arraySize];

for(int i = 0; i <
arraySize; i++)
{ if(i < 20)
myList[i] = i * i;
else
myList[i] = i * 4;


if((i + 1) % lineToOutput == 0)
System.out.print(array[i]+ "\n");
else
System.out.print(array[i]+ "\t");
}
}
}