Tuesday, January 7, 2014

Iterative solution to the Fibonacci sequence in COBOL

I've decided to learn cobol by contributing to Rosetta Code, a site that aims to show each as many different programming language's solution to a given problem.

Below is the iterative solution to the fibonacci sequence, which takes in input from the user's standard input keyboard.

View on Rosetta Code

Program-ID. Fibonacci-Sequence.
Data Division.
Working-Storage Section.
  01  FIBONACCI-PROCESSING.
    05  FIBONACCI-NUMBER  PIC 9(36)   VALUE 0.
    05  FIB-ONE           PIC 9(36)   VALUE 0.
    05  FIB-TWO           PIC 9(36)   VALUE 1.
  01  DESIRED-COUNT       PIC 9(4).
  01  FORMATTING.
    05  INTERM-RESULT     PIC Z(35)9.
    05  FORMATTED-RESULT  PIC X(36).
    05  FORMATTED-SPACE   PIC x(35).
Procedure Division.
  000-START-PROGRAM.
    Display "What place of the Fibonacci Sequence would you like (<173)? " with no advancing.
    Accept DESIRED-COUNT.
    If DESIRED-COUNT is less than 1
      Stop run.
    If DESIRED-COUNT is less than 2
      Move FIBONACCI-NUMBER to INTERM-RESULT
      Move INTERM-RESULT to FORMATTED-RESULT
      Unstring FORMATTED-RESULT delimited by all spaces into FORMATTED-SPACE,FORMATTED-RESULT
      Display FORMATTED-RESULT
      Stop run.
    Subtract 1 from DESIRED-COUNT.
    Move FIBONACCI-NUMBER to INTERM-RESULT.
    Move INTERM-RESULT to FORMATTED-RESULT.
    Unstring FORMATTED-RESULT delimited by all spaces into FORMATTED-SPACE,FORMATTED-RESULT.
    Display FORMATTED-RESULT.
    Perform 100-COMPUTE-FIBONACCI until DESIRED-COUNT = zero.
    Stop run.
  100-COMPUTE-FIBONACCI.
    Compute FIBONACCI-NUMBER = FIB-ONE + FIB-TWO.
    Move FIB-TWO to FIB-ONE.
    Move FIBONACCI-NUMBER to FIB-TWO.
    Subtract 1 from DESIRED-COUNT.
    Move FIBONACCI-NUMBER to INTERM-RESULT.
    Move INTERM-RESULT to FORMATTED-RESULT.
    Unstring FORMATTED-RESULT delimited by all spaces into FORMATTED-SPACE,FORMATTED-RESULT.
    Display FORMATTED-RESULT.

Monday, January 6, 2014

How to ping a site or address with Java

Java does not natively have the ability to send ICMP messages, so we need to send them using the system's built in ping tool.

I added a simple ping utility to JavaUtils, which you can download the jar from SourceForge and GitHub:

JavaUtils JAR and source download on SourceForge
JavaUtils JAR and source download on GitHub

Then, you can test ping reliability with the built in isNetworkAddressReachable(String);

Here is how to use the JavaUtils class with NetBeans:

Select Properties of your project:

Under Libraries, select Add JAR/Folder

Select the javautils.jar, and click Choose.

Then, click OK.

Now, you're ready to use JavaUtils. Below is an example of its use:

import java.io.IOException;
import javautils.network.Ping;

public class Testing {
    
    public static void main(String[] args) {
        try {
            System.out.print("Is google.com reachable? ");
            System.out.println(Ping.isNetworkAddressReachable("google.com"));
        } catch (IOException ioe) {
            // Something is wrong with the network
        } catch (InterruptedException ie) {
            // The system waitFor process was interrupted
        }
    }
}

The output:

run:
Is google.com reachable? true
BUILD SUCCESSFUL (total time: 0 seconds)

Friday, January 3, 2014

Network Ping Tester

Network Ping Tester is based on a previous application I developed and mentioned in a previous post, Simple Network Tester. The first revision of this graphical ping tool was rough around the edges and had some errors that I ran into. This version is a much cleaner implementation, and has a few additions.

Download at SourceForge.com


Features

  • Uses system ping utility
  • Ping results displayed
  • Latency information and averages
  • Average deviation
  • Variable number of connections

The tool as it stands now is complete. If there is interest in it, I would be happy to develop it further. It could use graphs and data dumping to a .csv or similar. 


If anyone finds the tool useful, let me know. I'll also be glad to develop any features you would find useful.