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);
}
}

Thursday, August 25, 2005

Like in C and C++, one uses header files to access functions and classes outside the main file, in Java one uses packages. One uses the import statement to call the classes from the package. The classes have to be declared as public in the package to make sure that accessibility is possible in the class where it is being called. The .java files inside a package has a statement for package declaration, i.e. “package ff”, here the name of the package is ff. To make sure that the package classes are accessible outside the classes they have to be present in the CLASSPATH. CLASSPATH is an environment variable for setting the path for all possible locations where java class files can be present. If one needs to import any class from any package then one need to write the import statement. For eg. “import ff.*;” statement will import classes from the package ff.

If there is a directory named ff

Under this directory there is a file abc.java

package ff;

public class abc {
public abc() {
System.out.println("Inside class abc");
}
}


Another java file is created named a1.java

import ff.*;

class a1 {
public static void main(String args[]) {
abc a = new abc();
}
}

The file a1.java when executed will print "Inside class abc" if it finds the ff directory in its classpath.

Saturday, August 13, 2005

Static variables and methods

In Java, when a variable in a class is declared static, memory is allocated for the particular variable even when the objects are declared. They can be accessed when the objects are not created out of that particular class. In Java when memory allocation is done, there is separate memory space kept for the class itself. If there are static variables in the class then their values are stored here.

For eg.

public class abc {
static private int a;
private int b;
abc(int x,int y)
{
a=x;
b=y;
}
}

In the above class as one can access the variable a by accessing abc.a. One does not have to create an object to access them. Even as one create multiple objects of the class, the value of ‘a’ in all the objects are same since they point to the same memory location.

If there are static methods in a class, then the variables in them must be static since static methods can be called without declaring the object of the class. Due to this if there is any non-static variable in the static method and the static method is called without having declared an object then it will access a variable which does not have any memory allocated to it. In Java, the main method is always kept static since it is called without having declared any object.

For eg.

public class xyz {
static int x;
int y;
public static void main(String args[])
{
x=5;
//y=7; - error
}
xyz(int a)
{
y=a;
}
}

Wednesday, July 13, 2005

Java is a high Object Oriented level language just like C++ and Smalltalk but it has a lot of added features. It provides facilities like Event Handling, Remote Method Invocation, connecting to any database with ease.

With java one can also do multithreading that allows a user to run more than one thread at a point of time. One can also catch run-time errors with exception handling facility that is provided in Java.

Java also provides a user to create Applets which can be used in the Internet for running any application directly from any website without fearing about any virus.

Java provides a lot of API that helps a user to create application with rich interfaces with a lot of features. This helps a user in creating applications with very good GUI.

Facilities like RMI (Remote Method Invocation) allow a user to run application in a remote network. Java is an also a good language for network programming due to the presence of packages for socket programming.

Java is a platform independent language as one can run a compiled java program (i.e. a .class file) anywhere one wants to where there is a JVM (Java Virtual Machine).

Commands for running a java program abc.java

Compiling a .java file

javac abc.java

After this a abc.class file is formed as the abc.java file is compiled.

Running a .class file

java abc

The above command will run the application by interpreting the abc.java file.