Wednesday, July 25, 2012

import package.* vs. import package.class_name

  1. import java.util.*;
  2. public class ReadArgs {
  3.     public static void main(String[] args) {
  4.         Scanner input = new Scanner(System.in);
  5.         String text = input.next();
  6.         System.out.println(text);
  7.     }
  8. }
Code 1
  1. import java.util.Scanner;
  2. public class ReadArgs {
  3.     public static void main(String[] args) {
  4.         Scanner input = new Scanner(System.in);
  5.         String text = input.next();
  6.         System.out.println(text);
  7.     }
  8. }
Code 2

When you see two codes above, you will see 1 different in the first line. What is the difference with import java.util.* and import java.util.Scanner?
  1. import java.util.* will make all classes inside the java.util package visible and can be used in the class that import the package (in this case ReadArgs class). import.java.util.Scanner will make only Scanner class will be visible in the class that import the package.
  2. There is no different performance or size after the class compiled.
  3. Better to use import package.class_name for documentation and don't forget to order by class name for readability. There are drawback for this if you don't use tools or IDE such as Netbeans or Eclipse for build the code. You will get some difficulties when delete the unused class when the application get bigger. 
So, there is no different whether you use the import package.* and import package.class_name if we see on performance, and you can use the import package.class_name for readability and documentation purpose.


References:
Packages and Import

Friday, July 13, 2012

Playing with Pseudocode - Sequence of Numbers

If I have string of numbers like this "12345" for the input, and I want the output as a sequence of 3 numbers:
  1. 123
  2. 234
  3. 345
  4. 451
  5. 512
How to convert it to become Java Program?

First, let us make the pseudo-code before trying to code instead.
  1. Declare `n` = "12345"
  2. Declare `length` = length of `n`
  3. Declare `i` as integer
  4. Loop start from `i` = 0 to `length`- 1
    1. Display sub-string of `n` from `i` to `i` + 3

And convert it to code.
  1.  public class SequenceNumber {
  2.     public static void main(String[] args) {
  3.         String n = "12345";
  4.         int length = n.length();
  5.         int i;

  6.         for(i = 0; i < length; i++) {
  7.             System.out.println(n.substring(i, i + 3));
  8.         }
  9.     }
  10. }

 And let us check the output.
  1. 123
  2. 234
  3. 345
  4. Exception in thread "main" java.lang.StringIndexOutOfBoundException: String index out of range: 6
  5.     at java.lang.String.substring(String.java:1955)
  6.     at SequenceNumber.main(SequenceNumber.java:8)

 Wow, for the first time to code and we already make an exception, nice. Let's try figure out, in the line 8 of class Sequence Number the compiler said that "String index out of range: 6" that means when `i` = 3 it will get the sub-string of `n` from 3 to 6. If you try to display the `length` it will print 5, so the problem that make this exception is `i` + 3. Let's change the pseudo-code to make it avoid the exception.
  1. Declare `n` = "12345"
  2. Declare `length` = length of `n`
  3. Declare `i` as integer
  4. Declare `x` as integer
  5. Loop start from `i` = 0 to `length` - 1
    1. Check if `i` + 3 > `length`
      1. Set `x` = length of sub-string of `n` from `i`
      2. Display sub-string of `n` from `i` concat with sub-string of `n` from 0 to 3 - `x`
    2. Else
      1. Display sub-string of `n` from `i` to `i` + 3

And make some update in the current code.
  1. public class SequenceNumber {
  2.     public static void main(String[] args) {
  3.         String n = "12345";
  4.         int length = n.length();
  5.         int i;
  6.         int x;
  7.        
  8.         for (i = 0; i < length; i++) {
  9.             if (i + 3 > length) {
  10.                 x = n.substring(i).length();
  11.                 System.out.println(n.substring(i) + n.substring(0, 3 - x));
  12.             } else {
  13.                 System.out.println(n.substring(i, i + 3));
  14.             }
  15.         }
  16.     }
  17. }

If you want to make it shorter in the code, try this one.
  1. public class SequenceNumber {
  2.     public static void main(String[] args) {
  3.         char[] digits = Integer.toString(12345).toCharArray();
  4.         for (int i = 0; i < digits.length; i++) {
  5.             System.out.println("" + digits[i] + digits[(i + 1) % digits.length] + digits[(i + 2) % digits.length]);
  6.         }
  7.     }
  8. }
 Good luck...

References:
How can get the possible 3 digit combination from a number having more than 3 digits in java.