The Java Platform – An Overview Monday, May 5 2008 

A platform is the hardware or software environment in which a program runs. We’ve already mentioned some of the most popular platforms like Microsoft Windows, Linux, Solaris OS, and Mac OS. Most platforms can be described as a combination of the operating system and underlying hardware. The Java platform differs from most other platforms in that it’s a software-only platform that runs on top of other hardware-based platforms.

The Java platform has two components:

  • The Java Virtual Machine
  • The Java Application Programming Interface (API)

You’ve already been introduced to the Java Virtual Machine; it’s the base for the Java platform and is ported onto various hardware-based platforms.

The API is a large collection of ready-made software components that provide many useful capabilities. It is grouped into libraries of related classes and interfaces; these libraries are known as packages. The next section, What Can Java Technology Do? highlights some of the functionality provided by the API.

The API and Java Virtual Machine insulate the program from the underlying hardware.

As a platform-independent environment, the Java platform can be a bit slower than native code. However, advances in compiler and virtual machine technologies are bringing performance close to that of native code without threatening portability.

Advertisements

Java – a Simple Introduction Monday, May 5 2008 

The Java Programming Language

The Java programming language is a high-level language that can be characterized by the following:

· Simple

· Architecture neutral

· Object oriented

· Portable

· Distributed

· High performance

· Multithreaded

· Robust

· Dynamic

· Secure

Each of the preceding keywords is explained in The Java Language Environment , a white paper written by James Gosling and Henry McGilton.

In the Java programming language, all source code is first written in plain text files ending with the .java extension. Those source files are then compiled into .class files by the javac compiler. A .class file does not contain code that is native to your processor; it instead contains bytecodes — the machine language of the Java Virtual Machine1 (Java VM). The java launcher tool then runs your application with an instance of the Java Virtual Machine.

Because the Java VM is available on many different operating systems, the same .class files are capable of running on Microsoft Windows, the Solaris TM Operating System (Solaris OS), Linux, or Mac OS. Some virtual machines, such as the Java HotSpot virtual machine, perform additional steps at runtime to give your application a performance boost. This include various tasks such as finding performance bottlenecks and recompiling (to native code) frequently used sections of code.

Square Root Table Using Applet Monday, May 5 2008 

SIMPLE APPLET

//SqrtList.java

import java.awt.*;

public class SqrtList extends java.applet.Applet {
    public void paint(Graphics g) {
	g.setFont(new Font("TimesRoman", Font.PLAIN, 12));
	for (float i = 1; i <= 10; i++)
	    g.drawString("sqrt(" + i + ") = " + Math.sqrt(i), 0, 
		(int)i * 16 - 3);
    }
}

This is the code that specifies the Java code to run:

<APPLET CODEBASE=”java” CODE=”SqrtList” WIDTH=160 HEIGHT=162> <EM>You need a Java-aware browser</EM> </APPLET>

The <APPLET> tag specifies the class to load (the CODE= field), URL information (the CODEBASE= field) and the size of the region the applet will own. Notice that Java doesn’t exactly integrate with the rest of the page. Within that region of the page Java is king: it decides background color and fonts and does all the mouse and keyboard handling.

Parameters to the applet are placed in <PARAM> tags between the <APPLET> and </APPLET> tags. Anything else between these tags is ignored. It’s common to include some information here for display by browsers that don’t know about Java, since they’ll ignore the <APPLET> and <PARAM> tags and display whatever else they find there.

OUTPUT

sqrt(1) = 1
sqrt(2) = 1.41421
sqrt(3) = 1.73205
sqrt(4) = 2
sqrt(5) = 2.23607
sqrt(6) = 2.44949
sqrt(7) = 2.64575
sqrt(8) = 2.82843
sqrt(9) = 3
sqrt(10) = 3.16228

TCP CLIENT/SERVER FOR ARITHMETIC OPERATION Sunday, May 4 2008 

TCP CLIENT

//arithtcpclient.java

import java.io.*;

import java.net.*;

public class arithtcpclient

{

public static void main(String[] args) throws IOException

{

System.out.println();

System.out.println(“ARITHMETIC CLIENT”);

System.out.println(“*****************”);

System.out.println(“Enter the host name to connect”);

String str;

DataInputStream inp=new DataInputStream(System.in);

str=inp.readLine();

Socket clientsoc = new Socket(str, 9);

System.out.println(“Enter the inputs”);

PrintWriter out = new PrintWriter(clientsoc.getOutputStream(), true);

BufferedReader in = new BufferedReader(new

InputStreamReader(clientsoc.getInputStream()));

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

String userinput;

try

{

while (true)

{

do

{

userinput = stdin.readLine();

out.println(userinput);

}while(!userinput.equals(“.”));

System.out.println(“Sever Says : ” + in.readLine());

}

}

catch(Exception e)

{

System.exit(0);

}

}

}

TCP SERVER

//arithtcpserver

import java.io.*;

import java.net.*;

public class arithtcpserver

{

public static void main(String arg[]) throws Exception

{

System.out.println();

System.out.println(“ARITHMETIC SERVER”);

System.out.println(“*****************”);

System.out.println(“Server is ready to accept inputs…”);

ServerSocket serversoc=new ServerSocket(9);

Socket clientsoc = serversoc.accept();

PrintWriter out = new PrintWriter(clientsoc.getOutputStream(), true);

BufferedReader in = new BufferedReader(new

InputStreamReader(clientsoc.getInputStream()));

String inputline;

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

try

{

while (true)

{

String s,op=””,st;

int i=0,c=0;

int[] a=new int[2];

while(true)

{

s=in.readLine();

if(s.equals(“+”) || s.equals(“-“) || s.equals(“*”) || s.equals(“/”))

op=s;

else if(s.equals(“.”))

break;

else

{

a[i]=Integer.parseInt(s);

i++;

}

}

if(op.equals(“+”))

c=a[0]+a[1];

else if(op.equals(“-“))

c=a[0]-a[1];

else if(op.equals(“*”))

c=a[0]*a[1];

else if(op.equals(“/”))

c=a[0]/a[1];

s=Integer.toString(c);

out.println(s);

}

}

catch(Exception e)

{

System.exit(0);

}

}

}

Sample Output:

SERVER SIDE

*************

C:\jdk1.5\bin>javac arithtcpserver.java

C:\jdk1.5\bin>java arithtcpserver

ARITHMETIC SERVER

*********************

Server is ready to accept inputs…

CLIENT SIDE

************

C:\JDK1.5\bin>javac arithtcpclient.java

C:\JDK1.5\bin>java arithtcpclient

ARITHMETIC CLIENT

********************

Enter the host name to connect

p4-221

Enter the inputs

8

+

3

Sever Says : 11

9

11

Sever Says : -2

7

*

9

Sever Says : 63

12

/

3

Sever Says : 4

CLIENT/SERVER COMMUNICATION USING TCP SOCKETS Sunday, May 4 2008 

TCP CLIENT

//tcpclient.java

import java.io.*;

import java.net.*;

public class tcpclient

{

public static void main(String[] args) throws IOException

{

System.out.println(“TCP CLIENT”);

System.out.println(“Enter the host name to connect”);

DataInputStream inp=new DataInputStream(System.in);

String str=inp.readLine();

Socket clientsoc = new Socket(str, 9);

PrintWriter out = new PrintWriter(clientsoc.getOutputStream(), true);

BufferedReader in = new BufferedReader(new

InputStreamReader(clientsoc.getInputStream()));

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

String userinput;

try

{

while (true)

{

System.out.println(“Sever Says : ” + in.readLine());

userinput = stdin.readLine();

out.println(userinput);

}

}

catch(Exception e)

{

System.exit(0);

}

}

}

TCP SERVER

//tcpserver.java

import java.io.*;

import java.net.*;

public class tcpserver

{

public static void main(String a[]) throws Exception

{

System.out.println(“TCP SERVER”);

System.out.println(“Server is ready to connect…”);

ServerSocket serversoc=new ServerSocket(9);

Socket clientsoc = serversoc.accept();

PrintWriter out = new PrintWriter(clientsoc.getOutputStream(), true);

BufferedReader in = new BufferedReader(new

InputStreamReader(clientsoc.getInputStream()));

String inputline;

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

try

{

while (true)

{

inputline = stdin.readLine();

out.println(inputline);

System.out.println(“Client Says : “+in.readLine());

}

}

catch(Exception e)

{

System.exit(0);

}

}

}

Sample Output:

CLIENT-SERVER CHATTING USING TCP

*************************************

TCP SERVER

************

Server is ready to connect…

hello

Client Says : hello

How are you

Client Says : I’m doing programs

What programs

Client Says : networking

ok.Go ahead

Client Says : ok.Bye

Bye

TCP CLIENT

***********

Enter the host name to connect

p4-221

Sever Says : hello

hello

Sever Says : how are you

I’m doing programs

Sever Says : What programs

networking

Sever Says : ok.Go ahead

ok.Bye

Sever Says : Bye

Next Page »