EX.NO:1(A) SIMPLE JAVA PROGRAM DATE:11/07/2012 AIM: To write a simple java program to display the required data. ALGORITHM: 1. Create a class named sample. 2. Write the code to display the content “JAVA LAB”. 3. Save the program. 4. Compile and display the result.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
EX.NO:1(A) SIMPLE JAVA PROGRAM
DATE:11/07/2012
AIM:
To write a simple java program to display the required data.
ALGORITHM:
1. Create a class named sample.2. Write the code to display the content “JAVA LAB”.3. Save the program.4. Compile and display the result.
PROGRAM:
import java.io.*;
class sample
{
public static void main(String s[])
{
System.out.println("JAVA LAB");
}
}
OUTPUT:
RESULT:
Thus a sample java program to display the required data was executed.
EX:NO:1(B) SIMPLE JAVA PROGRAM USING TWO CLASSES
AIM:
To write a simple java program using two classes to find the sum of the given values.
ALGORITHM:
1. Create a class with three integer values.2. Create a method to return the sum of the values.3. Create another class which is the main class.4. Create a class object using this object specify values for the integer.5. Using the object call the method to display the sum of the three integer.6. Compile and display the results.
PROGRAM:
import java.io.*;
class a
{
int l,m,n;
int tot()
{
return l+m+n;}}
class bb
{
public static void main(String args[])
{
int ans;
a obj=new a();
obj.l=60;
obj.m=70;
obj.n=50;
ans=obj.tot();
System.out.println("The total is: "+ans);}
}
OUTPUT:
RESULT:
Thus the sample java program using two classes to find sum of the given values was executed.
EX:NO:1(C) TO DISPLAY DATE AND TIME
AIM:
To write the java program to display current date and time.
ALGORITHM:
1. Create the class with class name.2. To create the package and subpackage from the utility function in java.3. Create the object for a class and call the object to display the date and time.4. Get the input data using the string function.5. Compute and run the program to display the result.
System.out.println("Now the date is:--> "+DateNow);
}
}
OUTPUT:
RESULT:
Thus the Java program to display the current date and time was written and executed.
EX.NO:02 EVENT HANDLING MECHANISM
DATE:12/07/2012
AIM:
To write a java program for the event handling mechanism.
ALGORITHM:
1. Generate a window which can be viewed with an applet viewer.2. Create a subclass ‘test’ inherited from superclass Applet.3. Declare label l1,l2,button b1,textfield tf1,tf2,textarea ta.4. Initialise variable l1,l2,tf1,tf2,ta and b1.5. Create an object bh for class ButtonHandler.6. Using add() method labels,button,textfield and textarea are added to the window.7. The Label of ‘Submit’ button is obtained y calling the getAction command() method on the
ActionEvent object passed to action Performed().8. ActionEvent object is passed as a reference to string ‘s’.9. If ‘submit’ button is pressed contents of tf1 and tf2 are concatenated and are displayed on the
Thus the program for event handling mechanism has been written and executed.
EX.NO:03 FILE HANDLING
DATE:25/07/2012
AIM:
To create a file, read a file, write a file and copy a file using java.
ALGORITHM :
CREATE A FILE:
1. Create a class named createfile.2. Declare a variable f for the file.3. Assign the file name to the variable f.4. If file already exists print ‘File already exists’.5. Else create a file to the current directory by exception.6. Compile and save it.
READ A FILE:
1. Create a class named fileread.2. Open the file first that is required to read.3. Get the object of DataInputStream.4. BufferedReader is used for the Buffered Input character condition stream.5. Read the file line by line using while loop condition.6. Print the content on the console or display.7. Close the input stream.
WRITE A FILE:
1. Create a class named filewriter.2. Use BufferedReader for Buffered Input character.3. Enter the file name to create.4. If not exists create a new file using Boolean.5. BufferedWriter is used to buffered output character stream.6. Close the output stream.
COPY A FILE
1. Create a class name copyfile.2. Open the input stream and output stream for copying the file.3. Mention the length of the stream using by bytelen.4. Write the contents in the file.5. Close the input stream and output stream.6. Display file copied.7. Using exception handling, if file not found throws the exception and exit.8. Catch the exception and display that get message.9. Using switch case statement, to copy the contents of file1 to file2 and also give the default
condition.
PROGRAM:CREATE A FILE
import java.io.*;public class CreateFile1
public static void main(String []args)throws IOException{File f;f=new File("myfile.txt");if(!f.exists()){f.createNewFile();System.out.println("Newfile\"myfile.txt\"has been created to the current directory");}elseSystem.out.println("The specified file is already exist");}}
import java.io.*;public class FileWrite{public static void main(String []args)throws IOException{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));System.out.println("please enter the file name to create");String file_name=in.readLine();File file=new File(file_name);boolean exist=file.createNewFile();if(!exist){System.out.println("File already exists");System.exit(0);}else{FileWriter fstream=new FileWriter(file_name);BufferedWriter out=new BufferedWriter(fstream);out.write(in.readLine());out.close();System.out.println("File created Successfully");}}}COPY A FILE:import java.io.*;public class CopyFile{private static void copyfile(String srFile,String dtFile){try{File f1=new File(srFile);File f2=new File(dtFile);InputStream in=new FileInputStream(f1);OutputStream out=new FileOutputStream(f2,true);OutputStream ut=new FileOutputStream(f2);byte[] buf=new byte[1024];int len;while((len=in.read(buf))>0){out.write(buf,0,len);}in.close();out.close();System.out.println("File copied");}catch(FileNotFoundException ex){System.out.println(ex.getMessage()+"in the specified directory");
System.exit(0);}catch(IOException e){System.out.println(e.getMessage());}}public static void main(String []args){switch(args.length){case 0:System.out.println("File has not mentioned");System.exit(0);case 1:System.out.println("Destination file has not mentioned");System.exit(0);case 2:copyfile(args[0],args[1]);System.exit(0);default: System.out.println("Multiple files are not allow");System.exit(0);}}}
OUTPUT:
CREATE A FILE:
READ A FILE:
WRITE A FILE:
COPY A FILE:
RESULT:
Thus the program to create a file, read a file, write a file and copy a file was written and executed.
EX.NO:04 (a) CLIENT SERVER APPLICATION FOR CHAT USING TCP
DATE:01/08/2012
AIM:
To chat with client and server application using TCP/IP protocols.
ALGORITHM:
SERVER APPLICATION:
1. Create a class TCPserver1.2. Declare an object for socket as s and declare it as null.3. Create the two variables is,is1 and declare it as null.4. Output stream also created and declare it as null.5. Give the address for the socket of server.6. Catch the exception e.7. In the try block, create the object for is,is1 and os.8. Using do while condition read the line by line of the content and then close the is and os.9. Display the exception if caught.
CLIENT APPLICATION:
1. Create the class TCPclient1.2. Declare an object for socket as c and declare it as null.3. Create the variables for input stream is and is1 and declare it as null.4. Create an variable for print stream as os.5. Give the IP address for client c.6. Catch the exception e.7. Create the object for os,is,is1 in try block.8. Using do while condition read the content line by line.9. Close the input and output stream.10. 10.Display the exception if caught.
OUTPUT:ServerC:\Program Files\Java\jdk1.5.0\bin>javac TCPserver1.javaNote: TCPserver1.java uses or overrides a deprecated API.Note: Recompile with -deprecation for details.C:\Program Files\Java\jdk1.5.0\bin>java TCPserver1Client: Hai ServerServer:Hai ClientClient: How are youServer:FineClient: quitServer:quitClientC:\Program Files\Java\jdk1.5.0\bin>javac TCPclient1.javaNote: TCPclient1.java uses or overrides a deprecated API.Note: Recompile with -deprecation for details.C:\Program Files\Java\jdk1.5.0\bin>java TCPclient1Client:Hai ServerServer: Hai ClientClient:How are youServer: FineClient:quit
RESULT:
Thus the program Using TCP by client server application for chat written and executed successfully.
EX.NO:04(b) IMPLEMENTATION OF TCP/IP ECHO
AIM:
To implement TCP/IP Echo application.
ALGORITHM:
SERVER APLLICATION:
1. Create the class EServer.2. Create an object s for socket of server and declare it as null.3. Create an object ps,is for print and input stream.4. Declare the object c for client as null.5. Give the IP address of the server in the try block.6. Catch the exception e.7. Create an object for input stream and print stream in try.8. Using while condition, read the content line by line.9. Display the exception if caught.
CLIENT APPLICATION:
1. Create the class EClient.2. Create an object c for client socket and declare it as null.3. Create an object for input stream is,is1 and printstream os.4. Give the IP address for the client.5. Catch the exception e.6. Create an object for output stream os and input stream is,is1 in the try block.7. Using while read the content line by line.8. Display the exception if caught.9. Display the message socket closed.
Server:C:\Program Files\Java\jdk1.5.0\bin>javac EServer.javaNote: EServer.java uses or overrides a deprecated API.Note: Recompile with -deprecation for details.C:\Program Files\Java\jdk1.5.0\bin>java EServerC:\Program Files\Java\jdk1.5.0\bin>Client:C:\Program Files\Java\jdk1.5.0\bin>javac EClient.javaNote: EClient.java uses or overrides a deprecated API.Note: Recompile with -deprecation for details.C:\Program Files\Java\jdk1.5.0\bin>java EClientClient:Hai ServerServer:Hai ServerClient:HelloServer:HelloClient:endServer:endClient:dsSocket Closed!
RESULT:
Thus the program for implementation of TCP/IP Echo was written and executed successfully.
EX.NO:04 (c) PROGRAMS USING SIMPLE UDP
AIM:
To write a client-server application for chat using UDP
ALGORITHM:
CLIENT
1. Include necessary package in java
2. To create a socket in client to server.
3. The client establishes a connection to the server.
4. The client accept the connection and to send the data from client to server and vice versa
5. The client communicate the server to send the end of the message
6. Stop the program.
SERVER
1. Include necessary package in java
2. To create a socket in server to client
3. The server establishes a connection to the client.
4. The server accept the connection and to send the data from server to client and vice versa
5. The server communicate the client to send the end of the message
6. Stop the program.
PROGRAM :
UDPserver.java
import java.io.*;import java.net.*;class UDPserver{ public static DatagramSocket ds;
public static byte buffer[]=new byte[1024];public static int clientport=789,serverport=790;public static void main(String args[])throws Exception{
ds=new DatagramSocket(clientport);System.out.println("press ctrl+c to quit the program");BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));InetAddress ia=InetAddress.getByName("localhost");while(true){
OutputServerC:\Program Files\Java\jdk1.5.0\bin>javac UDPserver.javaC:\Program Files\Java\jdk1.5.0\bin>java UDPserverpress ctrl+c to quit the programClient:Hai ServerServer:Hello ClientClient:How are YouServer:I am Fine what about youClient
C:\Program Files\Java\jdk1.5.0\bin>java UDPclientserver waitingClient:Hai ServerServer:Hello ClieClient:How are YouServer:I am Fine wClient:end
RESULT:
Thus the above program a client-server application for chat using UDP was executed and
successfully
EX:NO:5 JAVA APPLET PROGRAM FOR CALCULATOR
AIM:
To write a java applet program for calculator.
ALGORITHM:
1. Create the subclass cal.2. Create an interface action listener using the keyword implements.3. Declare the variables t1,v1,v2 result for int text field respectively.4. Create a variable b for button and assign the value.5. Create the button for add,sub,mul,div,mod,clear and EQ.6. Create the variable OP as char type.7. Create the variable k for setting the color and also assign the value for text field and for
gridlayout gl and assign the value.8. Create the variable t1 using ‘this’ keyword.9. Add the button and declare it as t1.
10. Use ‘for’ for interface and add all the commands.
11. In the interface ActionListener, declare str and char and else if condition to perform the action
System.out.println("the first number is"+args[1]);double d1=Double.valueOf(args[1]).doubleValue( );System.out.println("the Second number is"+args[2]);double d2=Double.valueOf(args[2]).doubleValue();System.out.println("the sum is "+addServerIntf.add(d1,d2));