Advanced Java Lab
Advanced Java Lab
Advanced Java Lab
TECHNOLOGY
B. Tech in CSIT
2023-2024
1
AJP Lab Manual REVA University
List of Experiments
5 Program Outcome 8
6 Course Details 9
• Course Objectives
• Lab Requirement
• Guidelines to Students
• Course Outcomes 10
7
• Conduction of Practical Examination
• CO-PO-PSO Mapping
Lab Evaluation Process
8 15
Exp1 a. Write a Java program to create an Applet that displays 12
student information and also set foreground and
backgrounds.
2
AJP Lab Manual REVA University
3
AJP Lab Manual REVA University
4
AJP Lab Manual REVA University
Additional question 61
5
AJP Lab Manual REVA University
• To create state of the art computing labs infrastructure and research facilities in
information technology.
• To provide student-centric learning environment in Computing and Information
technology through innovative pedagogy and education reforms.
• To encourage research, innovation and entrepreneurship in computing and
information technology through industry/academia collaborations and extension
activities
• Organize programs through club activities for knowledge enhancement in thrust
areas of information technology.
• To enhance leadership qualities among the youth and enrich personality traits,
promote patriotism, moral and ethical values.
6
AJP Lab Manual REVA University
PEO3: Carry out research in the advanced areas of AI & ML and address the basic
needs of the society.
7
AJP Lab Manual REVA University
8
AJP Lab Manual REVA University
ADVANCEDJAVAPROGRAMMINGLAB
OBJECTIVE:
❖ To create a fully functional window-based applications. To develop GUI
applications like Calculator, Notepad, Simple user forms, and designing and
implementing Component based application like Jelly Beans, Color bean, and
also designing of server-side pages, client server interactions with TCP.
OUTCOMES:
❖ Student can able to Designing of window-based applications.
❖ Studentcanabletocreateaclientandservercommunicationusingnetpackage.
❖ Student can able to design reusable software components using javabeans.
❖ Able to develop server-side programming.
❖ Able to develop the dynamic web pages using JSP.
CO-PO MAPPING:
CO/PO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
CO1 2 1 1 1 2 2 3 2 1
CO2 2 1 2 2 2 1 2 2
CO3 2 3 1 1 1 2 2 1
CO4 2 2 2 1 2 3 2
CO5 2 2 2 2 1 2 1
1. Lab Requirements:
• Simple Editor Note pad or other editor tools like Editplus or notepad++.
• A web browser IE/Firefox/chrome.
• Apache Tomcat webserver.
• JVM (Java virtual machine) must be installed on your system
• BDK (Bean development kit) must also be installed
Following are the required hardware and software for this lab, which is available in the
laboratory.
Minimum System requirements:
• Processors: Intel Atom® processor or Intel® Core™ i3 processor.
• Disk space: 1 GB.
• Operating systems: Windows* 7 or later, macOS, and Linux.
• Python* versions: 2.7.X, 3.6.X.,3.8.X
3. Guidelines to Students
➢ Equipment in the lab for the use of student community. Students need to maintain a
proper decorum in the computer lab. Students must use the equipment with care. Any
damage caused is punishable.
➢ Students are required to carry their observation / programs book with completed exercises
while entering the lab.
➢ Students are supposed to occupy the machines allotted to them and are not supposed to
talk or make noise in the lab. The allocation is put up on the lab noticeboard.
9
AJP Lab Manual REVA University
➢ Labcanbeusedinfreetime/lunchhoursbythestudentswhoneedtousethesystemsshouldtakepri
orpermissionfromthe lab in-charge.
➢ Lab records need to be submitted on or before date of submission.
➢ Students are not supposed to use flash drives.
10
AJP Lab Manual REVA University
Change of experiment is allowed only once and marks allotted to the procedure part to be made
zero.
CO/PO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
CO1 2 1 1 1 2 2 3 2 1
CO2 2 1 2 2 2 1 2 2
CO3 2 3 1 1 1 2 2 1
CO4 2 2 2 1 2 3 2
CO5 2 2 2 2 1 2 1
11
AJP Lab Manual REVA University
1 a. Write a Java program to create an Applet that displays student information and
also set foreground and backgrounds.
SOURCE
import java.awt.*;
import java.applet.*;
</Applet>*/
12
AJP Lab Manual REVA University
Output:
In cmd prompt:
D:\cse>javac MsgApplet.java
D:\cse>appletviewer
MsgApplet.java
13
AJP Lab Manual REVA University
1. b. Write a Java Program to create an applet that scrolls a message from left to right?
import java.awt.*;
import java.applet.*;
14
AJP Lab Manual REVA University
Output:
15
AJP Lab Manual REVA University
SOURCE:
import java.awt.*;
import java.applet.*;
</applet>
*/
16
AJP Lab Manual REVA University
OUTPUT:
17
AJP Lab Manual REVA University
2. a. Write a java program to draw Lines, ovals, filled ovals and arcs, filled arcs?
SOURCE:
import java.awt.*;
import java.applet.*;
// Draw an arc
g.drawArc(40, 240, 50, 50, 0, -180);
18
AJP Lab Manual REVA University
OUTPUT:
19
AJP Lab Manual REVA University
2. b. Write a java program to draw rectangle, filled rectangle, rounded rectangle and
filled rounded rectangle with any two colors?
SOURCE:
import java.awt.*;
import java.applet.*;
20
AJP Lab Manual REVA University
OUTPUT:
21
AJP Lab Manual REVA University
SOURCE:
import java.awt.*;
import java.applet.*;
// Draw a rectangle
g.drawRect(40, 40, 200, 200);
// Draw a circle
g.drawOval(90, 70, 80, 80);
// Draw a line
g.drawLine(130, 95, 130, 115);
// Draw an arc
g.drawArc(113, 115, 35, 20, 0, -180);
}
}
OUTPUT:
22
AJP Lab Manual REVA University
SOURCE:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
String s1 = cbg.getSelectedCheckbox().getLabel();
if (s1 == "Red") {
g.setColor(Color.red);
g.fillOval(10, 50, 30, 30);
23
AJP Lab Manual REVA University
Output:
24
AJP Lab Manual REVA University
3. b. Write a Java Program to create multiple frames, which create Frame2 with a back button,
such that when a user click back button, Frame 2 is closed and we see the Frame1 only?
SOURCE:
Saveas: MyFrame1.java
import java.awt.*;
import java.awt.event.*;
MyFrame1() {
b1 = new Button("Next"); // Create a button with label "Next"
add(b1); // Add the button to the frame
25
AJP Lab Manual REVA University
Saveas: MyFrame2.java
MyFrame2() {
// Creating a new button with the label "Back"
b1 = new Button("Back");
// Adding the button to the frame
add(b1);
// Registering the frame as the ActionListener for the button
b1.addActionListener(this);
}
26
AJP Lab Manual REVA University
4.Write a Java Program a simple user form which reads the name of a user and mail id in
Text fields, select gender with radio buttons, and selects some Known languages using
checkboxes, and also enters an address in a text area. After filling details whenever a user
press the “submit” button, then displays all the information about the user input.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
d.add("");
for (int i = 1; i <= 31; i++) {
d.add("" + i);
}
m = new Choice();
m.add("");
String md[] = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JULY", "AUG",
"SEP", "OCT", "NOV", "DEC" };
for (int j = 0; j <= 11; j++) {
m.add("" + md[j]);
}
y = new Choice();
y.add(" ");
for (int k = 1980; k <= 2999; k++) {
y.add("" + k);
}
ad = new Label("\nADRS:", Label.LEFT);
t = new TextArea(5, 10);
b1 = new Button("SUBMIT");
add(l1);
add(fn);
add(l2);
add(mn);
add(l3);
add(ln);
add(pa);
add(ps);
add(gn);
add(w);
add(lo);
add(l);
add(dob);
add(d);
add(m);
add(y);
add(ad);
add(t);
add(b1);
}
mn.addActionListener(this);
ln.addActionListener(this);
ps.addActionListener(this);
b1.addActionListener(this);
}
29
AJP Lab Manual REVA University
OUTPUT:
30
AJP Lab Manual REVA University
5. Write a Java program that works as a simple calculator. Use a grid layout to arrange
buttons for the digits and for the +, -,*, % operations. Add a text field to display the
resu1lt.
SOURCE:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Cal" width=300 height=300>
</applet>
*/
31
AJP Lab Manual REVA University
t1.addActionListener(this);
add(t1);
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}
if (Character.isDigit(ch))
t1.setText(t1.getText() + str);
else if (str.equals("add")) {
v1 = Integer.parseInt(t1.getText());
OP = '+';
t1.setText("");
} else if (str.equals("sub")) {
v1 = Integer.parseInt(t1.getText());
OP = '-';
32
AJP Lab Manual REVA University
t1.setText("");
} else if (str.equals("mul")) {
v1 = Integer.parseInt(t1.getText());
OP = '*';
t1.setText("");
} else if (str.equals("div")) {
v1 = Integer.parseInt(t1.getText());
OP = '/';
t1.setText("");
} else if (str.equals("mod")) {
v1 = Integer.parseInt(t1.getText());
OP = '%';
t1.setText("");
}
if (str.equals("EQ")) {
v2 = Integer.parseInt(t1.getText());
if (OP == '+')
result = v1 + v2;
else if (OP == '-')
result = v1 - v2;
else if (OP == '*')
result = v1 * v2;
else if (OP == '/')
result = v1 / v2;
else if (OP == '%')
result = v1 % v2;
t1.setText("" + result);
}
if (str.equals("clear")) {
t1.setText("");
}
}
}
33
AJP Lab Manual REVA University
OUTPUT:
34
AJP Lab Manual REVA University
35
AJP Lab Manual REVA University
6 a. Write a Java program to implement JcomboBox where we can select state and
language and display selected items using lables?
SOURCE:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
TenA() {
Container c = getContentPane(); // Get the content pane
c.setLayout(null); // Set the layout to null
36
AJP Lab Manual REVA University
37
AJP Lab Manual REVA University
38
AJP Lab Manual REVA University
6. b. Write a Java program to implement a JList where we can select multiple courses and
display them in Text area.
SOURCE:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
// Constructor
TenB() {
Container c = getContentPane();
c.setLayout(null);
String items[] = {"Java", "C#", "C++", "C"}; // Create an array of items for the JList
jl = new JList(items); // Initialize the JList with the items array
jl.setBounds(100, 50, 100, 100); // Set the position and size of the JList
c.add(jl); // Add the JList to the container
// Main method
public static void main(String args[]) {
TenB t = new TenB(); // Create an instance of the TenB class
t.setTitle("List"); // Set the title of the JFrame
t.setSize(200, 200); // Set the size of the JFrame
39
AJP Lab Manual REVA University
OUTPUT:
40
AJP Lab Manual REVA University
6.C. Write a Java program that creates a user interface to perform integer divisions.
The user enters two numbers in the text fields, Num1 and Num2. The division of Num1
and Num2 is displayedin the Result field when the Divide button is clicked. If Num1 or
Num2 were not an integer, the program would throw a Number Format Exception. If
Num2 were Zero, the program would throw an Arithmetic Exception Display the
exception in a message dialog box.
SOURCE:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
41
AJP Lab Manual REVA University
42
AJP Lab Manual REVA University
43
AJP Lab Manual REVA University
7. a. Write a Java program to retrieve the information from the given URL (Note: Read the
URL from Command Line Arguments).
SOURCE:
import java.net.*;
import java.io.*;
class TwelveA {
public static void main(String args[]) throws Exception {
// Create a new URL object with the specified URL
URL hp = new URL("http://www.yahoo.com/index.html");
// Print the protocol of the URL
System.out.println("Get protocol: " + hp.getProtocol());
// Print the domain name of the URL
System.out.println("Get Domain name: " + hp.getHost());
// Print the port number of the URL
System.out.println("Port number: " + hp.getPort());
// Print the file path of the URL
System.out.println("File: " + hp.getPath());
// Print the external form of the URL
System.out.println("URL is: " + hp.toExternalForm());
// Print the file name of the URL
System.out.println("File name: " + hp.getFile());
}
}
OUTPUT:
44
AJP Lab Manual REVA University
class TwelveB1 {
public static void main(String args[]) throws Exception {
// Create a server socket on port 888
ServerSocket ss = new ServerSocket(888);
45
AJP Lab Manual REVA University
br1.close();
ss.close();
s.close();
}
}
SERVERCODE:
import java.io.*;
import java.net.*;
class TwelveB2 {
public static void main(String args[]) throws Exception {
// Create a socket object and connect to the localhost on port 888
Socket s = new Socket("localhost", 888);
System.out.println("Enter radius:");
int r = Integer.parseInt(br.readLine());
46
AJP Lab Manual REVA University
Output:
47
AJP Lab Manual REVA University
8. a. Write a java program to create a sample TCP chat application where client and server
can chat with each other?
SOURCE:
CLIENTCODE:
import java.net.*;
import java.io.*;
class ThirteenA1 {
public static void main(String args[]) throws Exception {
// Create a server socket on port 888
ServerSocket ss = new ServerSocket(888);
class ThirteenA2 {
public static void main(String args[]) throws Exception {
// Create a socket object and connect to the server
Socket s = new Socket("localhost", 888);
49
AJP Lab Manual REVA University
50
AJP Lab Manual REVA University
// Create a DatagramPacket with the input bytes, the local host address, and port number 8
DatagramPacket out = new DatagramPacket(b, b.length, ia, 8);
SERVER:
import java.net.*;
import java.io.*;
class ThriteenB1 {
public static void main(String args[]) throws Exception {
// Create a DatagramSocket object to listen for incoming datagrams on port 8
DatagramSocket ds = new DatagramSocket(8);
52
AJP Lab Manual REVA University
OUTPUT:
53
AJP Lab Manual REVA University
9. a. Write a java program to create a bean that counts the number of mouse clicks?
SOURCE:
import java.beans.*;
import java.io.Serializable;
import java.awt.*;
import java.awt.event.*;
public class Mouseclick extends Canvas {
private int count = 0;
public Mouseclick() {
// Add a mouse listener to the canvas
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
count++; // Increment the count when the mouse is pressed
repaint(); // Redraw the canvas
}
};
setSize(50, 80); // Set the size of the canvas
setBackground(Color.green); // Set the background color of the canvas
setForeground(Color.red); // Set the foreground color of the canvas
}
public void paint(Graphics g) {
// Draw the number of mouse clicks on the canvas
g.drawString("Number of Mouse Clicks: " + count, 40, 60);
}
}
OUTPUT:
54
AJP Lab Manual REVA University
9. b. Write a java program to create a bean that counts the number of button clicks?
SOURCE:
import java.awt.*;
import java.awt.event.*;
public class fourteenB extends Panel implements ActionListener {
int count;
Button but;
public fourteenB() {
count = 0;
setSize(200, 100);
but = new Button("Click me"); // Creating a button with the label "Click me"
but.addActionListener(this); // Adding an action listener to the button
add(but); // Adding the button to the panel
}
public void actionPerformed(ActionEvent e) {
count++; // Incrementing the count variable
repaint(); // Calling the repaint method to update the panel
}
public void paint(Graphics g) {
Dimension d = getSize();
Font f = new Font("Courier", Font.BOLD, 30);
int h = d.height;
int w = d.width;
g.setColor(Color.pink);
g.fillRect(0, 0, w - 1, h - 1);
g.setFont(f);
g.setColor(new Color(0, 0, 0));
g.drawString("Click count = " + count, 50, 50); // Displaying the click count on the panel
}}
Output:
55
AJP Lab Manual REVA University
9. c. Write a Java program to create a bean that display employe ename, salary, designation and
company?
SOURCE:
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
public Emp() {
ename = "Hemanth";
designation = "DataBaseManager";
address = "kurnool";
salary = 50000;
age = 24;
}
public void setEname(String name) {
ename = name;
}
public void setDesignation(String desg) {
designation = desg;
}
public void setAddress(String adr) {
address = adr;
}
public void setSalary(double sal) {
salary = sal;
}
public void setAge(int ag) {
age = ag;
}
public String getEname() {
return ename;
}
public String getDesignation() {
return designation;
}
public double getSalary() {
return salary;
}
public int getAge() {
return age;
56
AJP Lab Manual REVA University
}
public void paint(Graphics g) {
g.drawString("Name:" + ename, 20, 20);
g.drawString("Address:" + address, 20, 40);
g.drawString("Designation:" + designation, 20, 70);
g.drawString("Salary:" + salary, 20, 80);
}
}
OUTPUT:
57
AJP Lab Manual REVA University
58
AJP Lab Manual REVA University
10. b. Write a Java Program to create a Servlet that Reads Request Parameters and
display their values?
<html>
<head>
<title>ServletParameters</title>
</head>
<body>
<center>
<formname="form1"method="POST"action="http://localhost:9401/rpar/rparam">
<table>
<tr>
<td><b>Employee</td>
<td><inputtype="text"name="ename"value=""></td>
</tr>
<tr>
<td><b>Id</td>
<td><inputtype="text"name="id"value=""></td>
</tr>
<tr>
<td><inputtype="submit"value="submit"></td>
<td><inputtype="reset"value="clear"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
Rparam.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
59
AJP Lab Manual REVA University
Web.xml:
<web-app>
<servlet>
<servlet-name>rp</servlet-name>
<servlet-class>Rparam</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>rp</servlet-name>
<url-pattern>/rparam</url-pattern>
</servlet-mapping>
</web-app>
OUTPUT:
60
Additional question and Answers
61
A8- Primitive data types are 8 types and they are: byte, short, int, long, float, double,
boolean, char.Q9-Whatisthemethodoverloading.?
A9- Function with same name but different argument perform different task known as
methodoverloading.Q10-Whatareinnerclass and anonymousclass?
A10- Inner class : classes defined in other classes, including those defined in methods are called
innerclasses. An inner class can have any accessibility including private. Anonymous class
:Anonymous class is aclass defined inside a method without a name and is instantiated and declared
in the same place and cannothaveexplicitconstructors.
62