Monday, September 05, 2005

BigInteger and BigDecimal

Arbitrary mathematical calculations cannot be represented with Java’s primitive data types like float and double. Due to this the java.math package provides BigInteger and BigDecimal.

BigInteger supports arbitrary-precision integers. This means it can accurately represent integral values of any size without losing any information during operations.

For example,

import java.math.*;

class Blog6 {
public static void main(String args[]) {
// Create BigInteger with a string
BigInteger bi1 = new BigInteger("1234567890123456890");
/* float f=1234567890123456890; - will show a compilation error as number is too large */
// Create BigInteger with a long
BigInteger bi2 = BigInteger.valueOf(123L);
bi1 = bi1.add(bi2);
System.out.println(bi1);
bi1 = bi1.multiply(bi2);
System.out.println(bi1);
bi1 = bi1.subtract(bi2);
System.out.println(bi1);
bi1 = bi1.divide(bi2);
System.out.println(bi1);
bi1 = bi1.negate();
System.out.println(bi1);
int exponent = 2;
bi1 = bi1.pow(exponent);
System.out.println(bi1);
}
}

The output of the above program will be :-

1234567890123457013
151851850485185212599
151851850485185212476
1234567890123457012
-1234567890123457012
1524157875323884225636330993811968144



BigDecimal is for arbitrary-precision fixed-point numbers.

For example,

import java.math.*;

class Blog61 {
public static void main(String args[]) {
BigDecimal bdi1 = new BigDecimal("1234567890123.456890");
/*float f=1234567890123.456890; - will show a compilation error due to loss of precision */
// Create via a long
BigDecimal bd2 = BigDecimal.valueOf(123L,2);
bd1 = bd1.add(bd2);
System.out.println(bd1);
bd1 = bd1.multiply(bd2);
System.out.println(bd1);
bd1 = bd1.subtract(bd2);
System.out.println(bd1);
bd1 = bi1.divide(bd2,6);
System.out.println(bd1);
bd1 = bdi1.negate();
System.out.println(bd1);
}
}

The output of the above program will be :-

1234567890124.686890
1518518504853.36487470
1518518504852.13487470
1234567890123.68689000
-1234567890123.68689000

Calculations with such large numbers with such huge decimal values are not possible with any of the primitive datatypes in java like int, float or double. But the BigInteger and BigDecimal classes allows the user to do calculations with such number as shown.
Project using Java – Course Registration System

This semester in SCIT for our practical experience in Java we are developing a Course Registration System in Java.

The application would enable students to register and unregister for any course that is available in the institute. The application allows the students to go through the courses and the faculties with their timings of lecture. The business logic which doesn’t allow a student to register for two courses which might have lectures at the same period is also being implemented. A student can register for a maximum of three courses. There are limitations of seats of every course for which students who come later on are put in the waiting list.

Our mentor for the project is Mr. Parag Shah, who is the Head of Adaptive Software Solutions.

We are a team of four:
1) Gopinath Cataram (IT054012)
2) Ishan Vaish (IT054013)
3) Manaspratim Mitra (IT054014)
4) Vinay Jain (IT054029)

The project work has already started, if the is any changes in the project, the blog will be updated accordingly.
Serialization in Java

Serialization means converting any objects into stream of bytes which can be stored files. In java, objects can be serialized and stored into any output stream. Serialized objects can be stored into files or it can be send across a network. An object can only be serialized if its class implements the Serializable interface which is present in the package java.io.*. If the class does not implement the Serializable interface, then its object cannot be serialized. Serialization in java helps in RMI where methods which might receive values do some process in some other machine and may return values to the caller machine. One needs to make sure that the values which are sent or received are Marshalled form.

An example of a class that can be serialized :-

class abc implements java.io.Serializable {
private int x;
abc (int a) {
x=a;
}
}


An object of the above class can be serialized into any output as it implements the Serializable interface.

An example where an object is being serialized.

import.io.*;

class xyz {
public static void main(String args[]) {
ObjectOutput o = new ObjectOutputStream(
new FileOutputStream("file1"));
abc a=new abc();
o.writeObject(a);
}
}