Programming

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)

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 🙂

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 😉

Random String Generator Using Java

This is a simple method to create a random string result, the first method is create string between A-Z and 0-9 while the second one only provide hexadecimal characters.

package com.edw.main;

import java.util.UUID;
import org.apache.commons.lang.RandomStringUtils;

public class Main {
    public static void main(String[] args) {
        // one
        System.out.println(RandomStringUtils.randomAlphanumeric(32).toUpperCase());
        
        // two
        System.out.println(UUID.randomUUID().toString().replace("-", "").toUpperCase());
    }
}

This is the screenshot of my Netbeans Project.

and this is the result,

Hope it helped others, good luck.