You are on page 1of 26

Date

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-

public Static void main(String args[])


{
System.out.println("Hello");
}
}
Output-

Conclusion- We have learned how Java programs are created, saved, compiled and run.

1813344

Date

Practical-

1813344

Aim: Write a program to find the factorial of a number using

Command line arguments

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:

Command line argument


class factorial1
{public static void main(String args[])
{inti,s=1;
int n=Integer.parseInt(args[0]);
for(i=1;i<=n;i++)
{s=s*i;
}
System.out.println("Factorial of a number is "+s);
}}

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

Aim- Introduction to Applets.


Objective- To know the basics of Applet and print text on the Applet window.
System Requirements- Java installed system with Java development kit (JDK).
Theory- Applets are extra add-ons provided by Java.An Applet is a small application that
performs one specific task which runs within the scope of a large program. Applet can provide
web applications with interactive features that can't be provided by HTML.Since,Java byte code
is platform independent,Java Applets can be executed by browsers running under many
Platforms. When a Java Technology enabled Web Browser Processes a page that contains an
Applet,the Applet code is transferred to the client system and executed by the browser Java
Virtual Machine(JVM).
Advantages Of Java Applets:
1. Applets are supported by most Web Browsers.
2. Java Applets are fast.
3. The same applets can work on all installed versions of java.
Disadvantags of Applets:
1. It requires the java plugins.
2. Somer browsers mostly mobile browsers do not run java applets.
3. Some applets requires a specific java runtime environment.
Algorithm1. Import the java packages.
2. Write the applet code.
3. Defining the class that inherits the applet class.
4. Create objects if needed by applets.
Program Codeimport java.applet.*;
import java.awt.*;
/*<applet code="applet1" height=200 width=200></applet>*/
public class applet1 extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello",70,100);
}
}

Output-

Date

Conclusion- Applets have been studied.

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

Aim:To insert a record in the database of mysql using java.


Objective:In this practical, we will learnto insert records in database of mysql in java.
System Requirement:Hardware: Desktop Pc
Software: Windows Notepad, Command Prompt, Java 1.7, My Sql Connector 5.01.
Theory:MySQL is an open source relational database management system (RDBMS). In july
2013, it was the worlds second most widely used RDBMS and the most widely used client
server model RDBMS. It is named after co-founder michael Wideniudss daughter, My. The SQl
acronym stands for Structured Query Language.
Program Code:
import java.util.Scanner;
import java.sql.*;
import javax.sql.*;
public class insert
{
public static void main(String arg[])
{
String mecode,username,password,dburl,query,mename,mcity;
int msal;
username="root";
password="123";
dburl="jdbc:mysql://localhost/mysql";
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection(dburl,username,password);
Statement stmt=con.createStatement();
Scanner input=new Scanner(System.in);
Scanner input1=new Scanner(System.in);
System.out.println("Enter the employee code:");
mecode=input.nextLine();
System.out.println("Enter the employee name:");
mename=input.nextLine();
System.out.println("Enter the salary:");
msal=input1.nextInt();
System.out.println("Enter the city:");
mcity=input.nextLine();
query="insert into emp1
values('"+mecode+"','"+mename+"',"+msal+",'"+mcity+"')";
stmt.executeUpdate(query);
con.close();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}

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

Aim:Write a program to view the database of mysql using java program.


Objective:In this practical, we will learn to view the entries of database of mysql in java.
System Requirement:Hardware: Desktop Pc
Software: Windows Notepad, Command Prompt, Java 1.7, My Sql Connector 5.01.
Theory: MySQL is an open source relational database management system (RDBMS). In july
2013, it was the worlds second most widely used RDBMS and the most widely used client
server model RDBMS. It is named after co-founder michael Wideniudss daughter, My. The SQl
acronym stands for Structured Query Language.
Coding:
import java.sql.*;
import javax.sql.*;
public class select
{
public static void main(String arg[])
{
String username,password,dburl,query,mcode,mname,mcity;
int msal;
username="root";
password="123";
dburl="jdbc:mysql://localhost/mysql";
query="select * from emp1";
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection(dburl,username,password);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(query);
while(rs.next())
{
mcode=rs.getString(1);
mname=rs.getString(2);
msal=rs.getInt(3);
mcity=rs.getString(4);
System.out.println(mcode+","+mname+","+msal+","+mcity);
}
con.close();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();

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

Aim:Introduction to the Servlets.


Objective: In this practical, we will learn about servlets.
System Requirement: Hardware: Desktop Pc
Software: Windows Notepad, Command Prompt, Java 1.7, My Sql Connector 5.01.
Theory:
Servlets:
A Servlet is a java programming language class used to extend the capabilities of servers that lost
applications accessed via a request-response programming model. Although servlets can respond
to any type of request, they are commonly used to extend the applications hosted by Web servers.
Java Servlet technology defines HTTP-specific Servlet classes for such applications.
For writing Servlets, the javax.servlet and javax.servlet.http packages provide interfaces and
classes. All Servlets must implement the Servlet interface,which defines life-cycle methods. You
can use or extend the GenericServlet class provided with the java Servlet API, while
implementing a generic service. For handling HTTP specific services, the Http Servlet class
provides methods, such as doget() and dopost().
Servlets are protocol and platform-independent server-side components,written in java,which
dynamically extend java enabled servers. Using the request-response paradigm,they provide a
general framework for services built. HTML web pages provide secure web-based access to data
and you can interactively view or modify that data using dynamic web page generation
techniques.
Servlet donot need a graphical user interface,as they run inside servers. Otherwise,they are the
server-side counterpart to applets:they are java application components,which are downloaded,on
demand,to the part of the system which need them.
CLIENT-SERVER COMMUNICATION

PUT

Java Server
Call Servlet

Client 2
POST
HTML

Step 1. Write HelloWorld.java in bin folder


//HelloWorld.java
import java.io.*;
import java.util.*;

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

Aim: Write a program to perform java strings.


Objective: In this practical, we will learn about strings in java.
System Required: Hardware required :
Standard PC
Software required
:
Notepad, jdk1.7
Theory:Swing is a GUIwidget toolkit for Java. It is part of Oracle's Java Foundation
Classes(JFC) an API for providing a graphical user interface (GUI) for Java programs.
Swing was developed to provide a more sophisticated set of GUI components than the earlier
Abstract Window Toolkit (AWT). Swing provides a native look and feel that emulates the look
and feel of several platforms, and also supports a pluggable look and feel that allows applications
to have a look and feel unrelated to the underlying platform. It has more powerful and flexible
components than AWT. In addition to familiar components such as buttons, check boxes and
labels, Swing provides several advanced components such as tabbed panel, scroll panes, trees,
tables, and lists.
Unlike AWT components, Swing components are not implemented by platform-specific code.
Instead, they are written entirely in Java and therefore are platform-independent. The term
"lightweight" is used to describe such an element
Program Code:
import java.awt.*;
import javax.swing.*;
public class Calculator1
{
JFrame f;
JPanel p1,p2,p3,p4,p5;
private JTextFieldtField;
private JMenuEditMenu;
private JMenuBarMenuBar;
private JMenuItem fmi1, fmi2, fmi3;
private JButton num0, num1, num2, num3, num4, num5, num6, num7, num8,num9,num00;
private JButtonbAdd,bSub,bMul,bDiv,equals;
Calculator1()
{
f = new JFrame("Calculator");
MenuBar = new JMenuBar();
EditMenu = new JMenu ("Edit");
fmi1 = new JMenuItem(" Copy ");
fmi2 = new JMenuItem(" Paste ");
fmi3 = new JMenuItem(" Quit ");
EditMenu.add(fmi1);
EditMenu.add(fmi2);
EditMenu.addSeparator();
EditMenu.add(fmi3);

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

You might also like