Académique Documents
Professionnel Documents
Culture Documents
Practical-
1813344
Aim-Introduction to Java.
Objective- To know the basics of Java and print text on the console.
System Requirements- Java installed system with Java development kit (JDK).
Theory-Java is a general purpose, object oriented programming language developed by Sun
Microsoft Systems in 1991.Originally called OAK by James Gosling. Java Virtual Machine
gives Java its platform independence. In Java the javac command is the Java compiler that is
used to compile the program. The Java command is used to run the program.
There are many features in Java, which are as follows :
1. Compiled And Interpreted : Java combines both approaches thus making Java two-Stage
system. Java compiler converts the source code in to the byte code instruction. Byte codes
are not just the machine instruction and therefore, in the second stage, Java interpreter
generate the machine code that can be directly executed by the machine that is running the
java program.
2. Platform Independent And Portable : Java program can be easily moved from one
computer system to another, anywhere and anytime. Java program also use the concept of
Write Once And Run Anywhere.
3. Object Oriented :Java is a true Object Oriented Language. Almost everything in Java is
the object. All program code and data resides within objects and classes.
4. Robust And Secure :Java is a robust language as is provides so many safeguards to
ensure the reliable code. It also incorporated the concept of exception handling which
capture series errors and eliminates any risk of crashing the system. The absence of
pointers in Java ensures program can not gain access to memory location without
authentication.
5. Multithreaded :Multithread means handling multiple tasks simultaneously. Java supports
multithreaded program. This means that we need not wait for the application to finish one
task before beginning the another.
Algorithm1. Firstly we write source code of our program in note pad.
2. Save the program with .java extension with same name as class name.
3. Open console.
4. Go to directory where program is saved through cd command (Change Directory
command).
5. Compile the program through javac command.
Example- javac program_name.java
6. After compiling the program the byte code is created ,i.e,
.class file.
7.After that we will run our program.
Example- java class_name
Program Codeclass Example
{
Date
Practical-
Conclusion- We have learned how Java programs are created, saved, compiled and run.
1813344
Date
Practical-
1813344
Scanner class
Objectives: In this practical we have to find the factorial of a number using command inline
argument and Scanner class.
Requirements: Hardware: Standard Pc
Software: Notepad, jdk
Theory:
Command line argument in java: We can pass values to our program at runtime using
command line argument. If we want to pass some values to the java program we can supply it as
command line arguments.
Scanner class: A simple text Scanner which can parse primitive types and strings using regular
expression. A scanner breaks its input tokens using a delimiter pattern , which by default
matches whitespace the resulting tokens may be converted into values of different types using
the various next methods.
Program Code:
Scanner class
import java.util.Scanner;
class factorial
{public static void main(String args[])
{int f=1,n,i;
System.out.println("Enter value of n");
Scanner sc= new Scanner(System.in);
n=sc.nextInt();
for(i=1;i<=n;i++)
{f=f*i;
}
System.out.println("factorial is"+f);
}}
Date
Practical-
1813344
Output:
Command line argument
Scanner classes
Conclusion: We have successfully calculated the factorial of a number using command line
argument and using Scanner class.
Date
Practical-
1813344
Output-
Date
Practical-
1813344
Date
Practical-
1813344
Aim:Write a program to find the sum of two numbers and display the sum as a message.
Objective-In this program, we will learn to use the button and how to perform eevent handling
on buttons.
System Requirements- Java installed system with Java development kit (JDK).
Theory:
The Abstract Window Toolkit (AWT) is Java's original platform-dependent
windowinggraphics, and user-interfacewidget toolkit preceding Swing. The AWT is part of the
Java Foundation Classes(JFC) the standard API for providing a graphical user interface (GUI)
for a Java program. AWT is also the GUI toolkit for a number of Java ME profiles.
Algorithm1. Firstly we write source code of our program in note pad.
2. Save the program with .java extension with same name as class name.
3. Open console.
4. Go to directory where program is saved through cd command (Change Directory
command).
5. Compile the program through javac command.
Example- javac program_name.java
6. After compiling the program the byte code is created ,i.e,
.class file.
7.After that we will run our program.
Example- java class_name
Program Codeimport java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="hello" width=500 height=300>
</applet>*/
public class hello extends Applet implements ActionListener
{
TextField t1,t2;
Label l1,l2;
Button b;
String msg="";
public void init()
{
l1=new Label("Number 1");
l2=new Label("number 2");
Date
Practical-
t1=new TextField(30);
t2=new TextField(30);
b=new Button("Add");
add(l1);
add(t1);
add(l2);
add(t2);
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String str;
str=e.getActionCommand();
if(str.equals("Add"))
{inta,b,c;
a=Integer.parseInt(t1.getText());
b=Integer.parseInt(t2.getText());
c=a+b;
msg="Sum is "+c;
repaint();
}
}
public void paint(Graphics g)
{
g.drawString(msg,100,100);
}
}
Output-
1813344
Date
Practical-
1813344
Conclusion- We have learned how to create buttons in java and perform event handling on them.
Date
Practical-
10
1813344
Aim: Write a program in java to create buttons and perform event handling on these buttons.
Objective-In this practical we will learn how to create buttons and perform event handling on
these buttons.
System Requirements- Java installed system with Java development kit (JDK).
Theory:
Change in the state of an object is known as event i:e,event describes the change in the state of
source. Events are generated as result of user Interaction with the graphical user interface
components. For example clicking on a button, moving the mouse, entering a character through
keyboard etc. are the activities that a causes an event to happen.
Event handling is the mechanism that controls the event and decides what happen when an event
occurs. Java uses the deligation Event Model to handle the events. This model defines the
standard mechanism to generate and handle the events.
Algorithm1. Firstly we write source code of our program in note pad.
2. Save the program with .java extension with same name as class name.
3. Open console.
4. Go to directory where program is saved through cd command (Change Directory
command).
5. Compile the program through javac command.
Example- javac program_name.java
6. After compiling the program the byte code is created ,i.e,
.class file.
7.After that we will run our program.
Example- java class_name
Program Codeimport java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="applet3" height=500 width=300></applet>*/
public class applet3 extends Applet implements ActionListener
{
Button b1,b2,b3;
String msg="";
public void init()
{
b1=new Button("let");
b2=new Button("surprise");
b3=new Button("laugh");
add(b1);
add(b2);
Date
Practical-
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void actionPerformed(ActionEventae)
{
String str=ae.getActionCommand();
if(str.equals("let"))
{
msg="Then y did u enter in the first place ";
}
else if(str.equals("surprise"))
{
msg= "not for you..booyah";
}
else
{
msg="hehahah";
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,50,100);
}
}
Output-
11
1813344
Date
Practical-
12
Conclusion- We have learned how Java programs are created, saved, compiled and run.
1813344
Date
Practical-
13
1813344
Date
catch(SQLException e)
{
e.printStackTrace();
}
}}
Output:
Practical-
14
1813344
Date
Practical-
15
Conclusion: In this exp. we have insert a record in the database of mysql using java.
1813344
Date
Practical-
16
1813344
Date
Practical-
17
1813344
}
}
}
Output:
Conclusion: In this exp. we have studied how to view the database of mysql using java program.
Date
Practical-
18
1813344
PUT
Java Server
Call Servlet
Client 2
POST
HTML
Date
Practical-
19
1813344
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet
{
public void doGet(HttpServletRequestrequest,HttpServletResponse response)
throws IOException, ServletException
{ response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Welcome To CAT Lab");}}
Step 2. Compile the program to generate Helloworld.class (CLASSSPATH C:\apache-tomcat6.0.26\lib\servlet-api.jar)
Step 3. create a folder(Say hello) under C:\apache-tomcat-6.0.26\webapps
Step 4. create a folder(say WEB-INF) and a file index.html under hello folder
index.html
<html>
<head>
<title>Sample "Hello, World" Application</title>
</head>
<ul>
To a <a href="shello">servlet
</a>
</ul>
</html>
Step 5. create a folder classes and a file web.xml under WEB-INF foldernternet
web.xml
<web-app>
<servlet>
<servlet-name>Hell</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hell</servlet-name>
<url-pattern>/shello</url-pattern>
</servlet-mapping>
</web-app>
Step 6. Copy HelloWorld.class to C:\apache-tomcat-6.0.26\webapps\hello\WEB-INF\classes
Step 7. Run the servlet in internet explorer as http://localhost:8080/hello/
Conclusion: In this exp. we have studied the introduction to the servlets.
Date
Practical-
20
1813344
Aim: Write a java program to show how the client server model works.
Objective:In this practical, we will learn the working of client server model.
System Requirement:Hardware: Desktop Pc
Software: Windows Notepad, Command Prompt, Java 1.7, My Sql Connector 5.01.
Theory:
Clientserver model
A computer network diagram of clients communicating with a server via the Internet. The client
server model of computing is a distributed application structure that partitions tasks or workloads
between the providers of a resource or service, called servers, and service requesters, called
clients. Often clients and servers communicate over a computer network on separate hardware,
but both client and server may reside in the same system. A server host runs one or more server
programs which share their resources with clients. A client does not share any of its resources,
but requests a server's content or service function. Clients therefore initiate communication
sessions with servers which await incoming requests.
Program Code:
a) SERVER
import java.io.*;
import java.net.*;
class server
{
public static void main(String arg[])throws IOException
{
ServerSocketss=new ServerSocket(555);
Socket s=ss.accept();
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
BufferedReader br1=new BufferedReader(new
InputStreamReader(s.getInputStream()));
BufferedReaderbr=new BufferedReader(new
InputStreamReader(System.in));
String str;
while(true)
{
str=br1.readLine();
System.out.println(str);
if(str.equalsIgnoreCase("exit"))
{
System.exit(0);
}
String str1=br.readLine();
out.println(str1);
}
}
}
Date
Practical-
21
1813344
b) CLIENT
import java.io.*;
import java.net.*;
class client
{
public static void main(String arg[])throws IOException
{
Socket s=new Socket("Localhost",555);
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
String str=br.readLine();
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
out.println(str);
if(str.equalsIgnoreCase("exit"))
{
System.exit(0);
}
BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str1=br1.readLine();
System.out.println(str1);
}
}
}
Output:
SERVER
Date
Practical-
22
1813344
CLIENT
Conclusion: In this exp. we have studied a java program to show how the client server model
works.
Date
Practical-
23
1813344
Date
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p5 = new JPanel();
tField = new JTextField(35);
num0 = new JButton("0");
num1 = new JButton("1");
num2 = new JButton("2");
num3 = new JButton("3");
num4 = new JButton("4");
num5 = new JButton("5");
num6 = new JButton("6");
num7 = new JButton("7");
num8 = new JButton("8");
num9 = new JButton("9");
num00=new JButton("00");
bAdd = new JButton("+");
bSub = new JButton("-");
bMul = new JButton("x");
bDiv = new JButton("/");
equals = new JButton("=");
}
public void launchFrame()
{
tField.setText("0.");
tField.setEnabled(false);
MenuBar.add(EditMenu);
p2.add(num7);
p2.add(num8);
p2.add(num9);
p2.add(bDiv);
p3.add(num4);
p3.add(num5);
p3.add(num6);
p3.add(bMul);
p4.add(num1);
p4.add(num2);
p4.add(num3);
p4.add(bSub);
Practical-
24
1813344
Date
p5.add(num0);
p5.add(bAdd);
p5.add(equals);
p2.setLayout(new GridLayout (1, 3, 2, 2) );
p3.setLayout(new GridLayout (1, 3, 2, 2) );
p4.setLayout(new GridLayout (1, 3, 2, 2) );
p5.setLayout(new GridLayout (1, 3, 2, 2) );
f.setLayout(new GridLayout (6, 1) );
f.setResizable(false);
f.setSize(300,250);
f.add(tField);
f.add(p1);
f.add(p2);
f.add(p3);
f.add(p4);
f.add(p5);
f.setVisible(true);
f.setJMenuBar(MenuBar);
f.pack();
}
public static void main (String args[]){
Calculator1 s = new Calculator1();
s.launchFrame();
}
}
Output:
Practical-
25
1813344
Date
Practical-
26
Conclusion: We have studied about Strings in Java and performed their use.
1813344