December 2012 Posts

A Simple Hello World using Java

This is an example of a simple hello world using Java.

interface Printer {
    void print(Message message);    
}

class Message {
    private String message;
    
    public Message(String message) {
        this.message = message;    
    }
    
    public void print(Printer printer) {
        printer.print(this);    
    }
    
    public String toString() {
        return message;   
    }    
}

abstract class AbstractPrinterFactory {
    public static AbstractPrinterFactory getFactory(){
        return new SystemOutPrinterFactory();   
    }
    public abstract Printer getPrinter();    
}

class SystemOutPrinterFactory extends AbstractPrinterFactory {
    public Printer getPrinter() {
        return new SystemOutPrinter();   
    }    
}

class SystemOutPrinter implements Printer {
    public void print(Message message) {
        System.out.println(message);   
    }    
}

public class HelloWorld {
    public static void main(String[] args) {
        Message message = new Message("Hello, World!");
        AbstractPrinterFactory factory = AbstractPrinterFactory.getFactory();
        Printer printer = factory.getPrinter(); 
        message.print(printer);       
    }           
}

Just for fun (H)

Google+

eBook Apa yang Cocok bagi Pemula yang Baru Belajar Java?

Kebetulan lagi rame di kaskus programmer forum, tentang ebook yang cocok untuk pemula. Supaya ga jawab berulang-ulang, mending gw tulis disini aja deh.

Untuk melatih pondasi, coba baca bukunya donald knuth
Donald Knuth

lalu lanjut ke buku-nya deitel deitel, java how to program
Deitel Deitel

klo dah faseh naik ke bukunya gang of four, bruce eckel dan kathy sierra
Gang of Four

Bruce Eckel

kathy sierra

klo ada waktu iseng, buknya Joshua Bloch seru juga
Joshua Bloch

kemudian lanjut ke bukunya craig walls dan gavin king
Craig Walls

Gavin King

kalo dah faseh abis itu langsung ke bukunya Debu Panda dan Reza Rahman
EJB3

Semoga membantu untuk para calon-calon programmer diluar sana, Maju Terus Ilmu Pengetahuan Indonesia…!!

Google+

How To Check Window’s Opened Comm Port using Java

On my latest project, i need to connect my java application to a GSM Modem, in order to sending and receiving SMS text. But before my application connected to opened comm port, i need to check which port number is opened. So this is a small java class to check my opened comm port. Btw, im using rtxt libraries to help me communicate with the opened port.

import gnu.io.CommPortIdentifier;
import java.util.Enumeration;

public class Main {
    
    private void doSomething() {
        Enumeration portList = CommPortIdentifier.getPortIdentifiers();
        System.out.println(portList.hasMoreElements());
        while (portList.hasMoreElements()) {
            CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
            System.out.println(portId.getName());
        }
    }
    
    public static void main(String[] args) {
        Main main = new Main();
        main.doSomething();
    }
}

Dont forget to add rxtx libraries your java’s path. You can find the Window’s installation path, here.

Hope it helps other, have fun :)

Google+

A Weird Exception When Using Java’s BigDecimal, java.lang.ArithmeticException: Rounding necessary

I found this weird exception when im trying to convert double values to BigDecimal,

java.lang.ArithmeticException: Rounding necessary
	at java.math.BigDecimal.divideAndRound(BigDecimal.java:1439)
	at java.math.BigDecimal.setScale(BigDecimal.java:2394)
	at java.math.BigDecimal.setScale(BigDecimal.java:2437)

This is my java code

BigDecimal bgAvg = new BigDecimal(0);
bgAvg = bgAvg.add(new BigDecimal(myDoubleValue).setScale(2));

Somehow the error is gone after i changed my code into this

BigDecimal bgAvg = new BigDecimal(0);
bgAvg = bgAvg.add(BigDecimal.valueOf(myDoubleValue).setScale(2));

Hope it helped others, have fun with Java ;)

Google+

How to Set %JAVA_HOME% Variable on Apache Tomcat’s Catalina Script

Usually im using Apache Tomcat’s catalina script to launch and start Apache Tomcat, but sometimes the script wont run because of not having JAVA_HOME variables on your OS environment variables or you cant add new environment variables due to lack of privileges.

So instead of adding JAVA_HOME variable into my OS, i set the JAVA_HOME on my catalina scripts. This is how i do it, i add this script on the top of catalina.bat file.

set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.7.0_07

Note, there is no space before and after “=” sign.

Easy isnt it ;)

Google+