Mahendra Multiple Campus
Nepalgunj, Banke
Advance Java Programming (BIT401)
BIT 7th Semester
(Lab Report)
Submitted By: Submitted To:
Name:Manraj Joshi Name: Lokendra B.K.
Symbol No.: 348
List of Experiments
S. No. Experiments Remarks
1 Write a Java program to create an Applet that displays student
information and alsoset foreground and backgrounds.
2 Write a Java Program to create an Applet that scrolls a message from
left to right?
3 Write a java program to draw Lines, ovals, filled ovals and arcs, filled
arcs?
4 Write a java program to draw rectangle, filled rectangle and rounded
rectangle and filled rounded rectangle with an two colors.
5 Write a java program to draw a smiley face ?
6 Write a Java program to create an Applet that displays 4 buttons each
represents different colors. If a user click on particular button then
color is set as back ground to applet.
7 Write a java program to create an applet that displays 2 buttons. If a
user click on one button then change name of another button and
display the button and display the button clicked and vice-versa?
8 Write a java program to create an applet that receives a string and
returns either it Uppercase or Lowercase, Reverse of given string, and
length of given String.
9 Write a java program to demonstrate the mouse event handlers.
10 Write a servlet program o create a simple servlet and test it?
11 Write a java program that implements a simple client/server
application. The client sends data to a server. The server receives the
data, uses it to produce a result, and then sends the result back to the
client. The client displays the result on the console. For example: The
data send from the client is the radius of a circle, and result produced
by the server is the area of circle. (Use [Link])
12 Write a java program to create a sample TCP chat application where
client server can chat with each other?
13 Write a java program to perform a simple conversation with UDP
model?
14 Write a Java Program to create a student table, which includes name,
roll no, branch and age or DOB?
15 Write a java program to create a menu with several menu item by
implementing JMenu
1. Write a Java program to create an Applet that displays student information and
alsoset foreground and backgrounds.
Solution:
import [Link].*;
import [Link].*;
public class StudentInfoApplet extends Applet {
Label nameLabel, ageLabel, gradeLabel;
TextFieldnameField, ageField, gradeField;
Button submitButton;
String name, age, grade;
public void init() {
nameLabel = new Label("Name:");
ageLabel = new Label("Age:");
gradeLabel = new Label("Grade:");
nameField = new TextField(20);
ageField = new TextField(20);
gradeField = new TextField(20);
submitButton = new Button("Submit");
add(nameLabel);
add(nameField);
add(ageLabel);
add(ageField);
add(gradeLabel);
add(gradeField);
add(submitButton);
setBackground([Link]);
setForeground([Link]);}
public void paint(Graphics g) {
[Link]("Student Information:", 20, 100);
[Link]("Name: " + name, 20, 120);
[Link]("Age: " + age, 20, 140);
[Link]("Grade: " + grade, 20, 160);}
public booleanaction(Event e, Object o) {
if ([Link] == submitButton) {
name = [Link]();
age = [Link]();
grade = [Link]();
repaint();
return true;}
return false;}}
2. Write a Java Program to create an Applet that scrolls a message from left to
right?
Solution:
import [Link];
import [Link].*;
public class MessageScrollApplet extends Applet implements Runnable
{
private String message = "Scrolling Message"; // Message to be
scrolled
private int xCoordinate; // X coordinate for the message
private int yCoordinate; // Y coordinate for the message
private Thread thread; // Thread to control the animation
private booleanstopFlag; // Flag to stop the animation
public void init() {
xCoordinate = 0; // Start message from the left
yCoordinate = getHeight() / 2; // Center the message vertically
}
public void start() {
if (thread == null) {
thread = new Thread(this);
stopFlag = false;
[Link](); // Start the thread
}
}
public void run() {
while (!stopFlag) {
xCoordinate += 5; // Move the message 5 pixels to the right
if (xCoordinate>getWidth()) // If message goes beyond the
applet width
xCoordinate = 0; // Reset xCoordinate to restart scrolling
repaint(); // Request redrawing of the applet
try {
[Link](100); // Pause for 100 milliseconds
} catch (InterruptedException e) {
[Link]("Thread interrupted");
}
}
}
public void stop() {
stopFlag = true; // Set the stopFlag to true to stop the thread
thread = null; // Set the thread reference to null
}
public void paint(Graphics g) {
[Link](message, xCoordinate, yCoordinate); // Draw the message
at current coordinates
}
}
3. Write a java program to draw Lines, ovals, filled ovals and arcs, filled arcs?
Solution:
import [Link].*;
import [Link].*;
public class DrawShapes extends Applet {
public void paint(Graphics g) {
// Drawing lines
[Link]([Link]);
[Link](20, 20, 150, 20);
[Link]([Link]);
[Link](20, 40, 150, 40);
// Drawing ovals
[Link]([Link]);
[Link](20, 60, 100, 50);
// Drawing filled ovals
[Link]([Link]);
[Link](150, 60, 100, 50);
// Drawing arcs
[Link]([Link]);
[Link](20, 120, 100, 50, 30, 120);
// Drawing filled arcs
[Link]([Link]);
[Link](150, 120, 100, 50, 45, 90);
}
}
4. Write a java program to draw rectangle, filled rectangle and rounded rectangle
and filled rounded rectangle with an two colors.
Solution:
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class ShapeDrawingExample {
public static void main(String[] args) {
[Link](() -> {
JFrame frame = new JFrame("Shape Drawing Example");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new ShapePanel());
[Link]();
[Link](null);
[Link](true);
});
}
}
class ShapePanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
[Link](g);
Graphics2D g2d = (Graphics2D) g;
// Set colors
Color color1 = [Link];
Color color2 = [Link];
// Draw a rectangle
[Link](color1);
[Link](20, 20, 100, 50);
// Draw a filled rectangle
[Link](color2);
[Link](150, 20, 100, 50);
// Draw a rounded rectangle
[Link](color1);
[Link](20, 100, 100, 50, 20, 20);
// Draw a filled rounded rectangle
[Link](color2);
[Link](150, 100, 100, 50, 20, 20);
}
}
5. Write a java program to draw a smiley face ?
Solution:
import [Link].*;
import [Link].*;
public class SmileyApplet extends JApplet {
@Override
public void init() {
setBackground([Link]);
}
@Override
public void paint(Graphics g) {
[Link](g);
Graphics2D g2d = (Graphics2D) g;
// Draw the face (yellow circle)
[Link]([Link]);
[Link](100, 100, 200, 200);
// Draw the eyes (black circles)
[Link]([Link]);
[Link](150, 160, 30, 30); // Left eye
[Link](220, 160, 30, 30); // Right eye
// Draw the smile (arc)
[Link](150, 200, 100, 60, 180, 180);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Smiley Face");
JApplet applet = new SmileyApplet();
[Link](applet);
[Link](400, 400);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
6. Write a Java program to create an Applet that displays 4 buttons each represents
different colors. If a user click on particular button then color is set as back
ground to applet.
Solution:
import [Link].*;
import [Link].*;
import [Link].*;
public class ColorButtonApplet extends Applet implements
ActionListener {
private Button redButton, greenButton, blueButton,
yellowButton;
public void init() {
// Create buttons
redButton = new Button("Red");
greenButton = new Button("Green");
blueButton = new Button("Blue");
yellowButton = new Button("Yellow");
// Add action listeners
[Link](this);
[Link](this);
[Link](this);
[Link](this);
// Add buttons to the applet
add(redButton);
add(greenButton);
add(blueButton);
add(yellowButton);
}
public void actionPerformed(ActionEvent e) {
if ([Link]() == redButton) {
setBackground([Link]);
} else if ([Link]() == greenButton) {
setBackground([Link]);
} else if ([Link]() == blueButton) {
setBackground([Link]);
} else if ([Link]() == yellowButton) {
setBackground([Link]);
}
}
}
7. Write a java program to create an applet that displays 2 buttons. If a user click on
one button then change name of another button and display the button and
display the button clicked and vice-versa?
Solution:
import [Link].*;
import [Link].*;
import [Link].*;
public class ToggleButtons extends Applet implements
ActionListener {
Button button1, button2;
String buttonText1 = "Button 1";
String buttonText2 = "Button 2";
public void init() {
button1 = new Button(buttonText1);
button2 = new Button(buttonText2);
[Link](this);
[Link](this);
add(button1);
add(button2);
}
public void actionPerformed(ActionEvent e) {
if ([Link]() == button1) {
buttonText2 = "Button 1 Clicked";
buttonText1 = "Button 2";
} else if ([Link]() == button2) {
buttonText1 = "Button 2 Clicked";
buttonText2 = "Button 1";
}
[Link](buttonText1);
[Link](buttonText2);
}
}
8. Write a java program to create an applet that receives a string and returns either
it Uppercase or Lowercase, Reverse of given string, and length of given String.
Solution: import [Link].*;
import [Link].*;
import [Link].*;
public class StringOperations extends Applet implements
ActionListener {
TextFieldinputField;
Button uppercaseButton, lowercaseButton, reverseButton,
lengthButton;
Label resultLabel;
public void init() {
inputField = new TextField(20);
uppercaseButton = new Button("Uppercase");
lowercaseButton = new Button("Lowercase");
reverseButton = new Button("Reverse");
lengthButton = new Button("Length");
resultLabel = new Label();
add(new Label("Enter a string:"));
add(inputField);
add(uppercaseButton);
add(lowercaseButton);
add(reverseButton);
add(lengthButton);
add(resultLabel);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent e) {
String input = [Link]();
if ([Link]() == uppercaseButton) {
[Link]("Uppercase: " + [Link]());
} else if ([Link]() == lowercaseButton) {
[Link]("Lowercase: " + [Link]());
} else if ([Link]() == reverseButton) {
StringBuilder reversed = new
StringBuilder(input).reverse();
[Link]("Reverse: " + [Link]());
} else if ([Link]() == lengthButton) {
[Link]("Length: " + [Link]());
}
9. Write a java program to demonstrate the mouse event handlers.
Solution: import [Link].*;
import [Link].*;
import [Link].*;
public class MouseEventDemo extends Applet implements
MouseListener, MouseMotionListener {
int mouseX = 0, mouseY = 0;
String message = "";
public void init() {
addMouseListener(this);
addMouseMotionListener(this);}
public void mouseClicked(MouseEvent e) {
mouseX = [Link]();
mouseY = [Link]();
message = "Mouse clicked at (" + mouseX + ", " +
mouseY + ")";
repaint();}
public void mouseEntered(MouseEvent e) {
mouseX = [Link]();
mouseY = [Link]();
message = "Mouse entered at (" + mouseX + ", " +
mouseY + ")";
repaint();}
public void mouseExited(MouseEvent e) {
mouseX = [Link]();
mouseY = [Link]();
message = "Mouse exited at (" + mouseX + ", " +
mouseY + ")";
repaint();}
public void mousePressed(MouseEvent e) {
mouseX = [Link]();
mouseY = [Link]();
message = "Mouse pressed at (" + mouseX + ", " +
mouseY + ")";
repaint();}
public void mouseReleased(MouseEvent e) {
mouseX = [Link]();
mouseY = [Link]();
message = "Mouse released at (" + mouseX + ", " +
mouseY + ")";
repaint();}
public void mouseDragged(MouseEvent e) {
mouseX = [Link]();
mouseY = [Link]();
message = "Mouse dragged at (" + mouseX + ", " +
mouseY + ")";
repaint();}
public void mouseMoved(MouseEvent e) {
mouseX = [Link]();
mouseY = [Link]();
message = "Mouse moved at (" + mouseX + ", " +
mouseY + ")";
[Link] a servlet program o create a simple servlet and test it?
Solution: import [Link].*;
import [Link].*;
import [Link].*;
public class SimpleServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Simple Servlet</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<h1>Hello from Simple Servlet!</h1>");
[Link]("</body>");
[Link]("</html>");
}}
[Link] a java program that implements a simple client/server application. The
client sends data to a server. The server receives the data, uses it to produce a
result, and then sends the result back to the client. The client displays the result
on the console. For example: The data send from the client is the radius of a circle,
and result produced by the server is the area of circle. (Use [Link])
Solution:
import [Link].*; (Server Code: "[Link]")
import [Link].*;
public class CircleServer {
public static void main(String[] args) {
try {
ServerSocketserverSocket = new ServerSocket(12345); // Create server
socket
[Link]("Server is running...");
while (true) {
Socket clientSocket = [Link](); // Accept client
connection
[Link]("Client connected.");
BufferedReader in=new BufferedReader(new
InputStreamReader([Link]
m()));
PrintWriter out = new PrintWriter([Link](), true);
double radius = [Link]([Link]());
[Link]("Received radius from client: " + radius);
double area = [Link] * radius * radius;
[Link](area);
[Link]("Sent area to client: " + area);
[Link]();
[Link]();
[Link]();}
} catch (IOException e) {
(Clien Code: "[Link]")
import [Link].*;
import [Link].*;
public class CircleClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 12345); // Connect to
server
[Link]("Connected to server.");
// Create input and output streams for communication with the
server
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter out = new PrintWriter([Link](), true);
// Send radius to the server
double radius = 5.0; // Example radius
[Link](radius);
[Link]("Sent radius to server: " + radius);
// Receive result (area) from the server
double area = [Link]([Link]());
[Link]("Received area from server: " + area);
// Close streams and socket
[Link]();
[Link]();
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
[Link] a java program to create a sample TCP chat application where client server
can chat with each other?
Solution:
(Server Code: "[Link]")
import [Link].*;
import [Link].*;
public class ChatServer {
public static void main(String[] args) {
try {
ServerSocketserverSocket = new ServerSocket(12345); // Create server socket
[Link]("Server is running...");
// Accept client connections
Socket clientSocket = [Link]();
[Link]("Client connected.");
// Create input and output streams for communication with the client
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter out = new PrintWriter([Link](), true);
// Create input stream to read from console
BufferedReaderconsoleInput = new BufferedReader(new
InputStreamReader([Link]));
String receivedMessage;
String sendMessage;
while (true) {
// Read message from client
receivedMessage = [Link]();
if (receivedMessage != null) {
[Link]("Client: " + receivedMessage);}
// Read message from console
[Link]("Server: ");
sendMessage = [Link]();
// Send message to client
[Link](sendMessage);}
} catch (IOException e) {
[Link]();
}}}
(Clien Code: "[Link]")
import [Link].*;
import [Link].*;
public class ChatClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 12345); // Connect to server
[Link]("Connected to server.");
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter out = new PrintWriter([Link](), true);
BufferedReaderconsoleInput = new BufferedReader(new
InputStreamReader([Link]));
String receivedMessage;
String sendMessage;
while (true) {
// Read message from server
receivedMessage = [Link]();
if (receivedMessage != null) {
[Link]("Server: " + receivedMessage);
}
[Link]("Client: ");
sendMessage = [Link]();
[Link](sendMessage);
}
} catch (IOException e) {
[Link]();
}
}}
[Link] a Java Program to create a student table, which includes name, roll no,
branch and age or DOB?
Solution:
public class Student {
private String name;
private int rollNo;
private String branch;
private int age; // or you can use Date of Birth (DOB) as a String or Date object
// Constructor
public Student(String name, int rollNo, String branch, int age) {
[Link] = name;
[Link] = rollNo;
[Link] = branch;
[Link] = age;}
// Getter methods
public String getName() {
return name;}
public int getRollNo() {
return rollNo;}
public String getBranch() {
return branch;}
public int getAge() {
return age;}
// Setter methods
public void setName(String name) {
[Link] = name;
}
public void setRollNo(int rollNo) {
[Link] = rollNo;}
public void setBranch(String branch) {
[Link] = branch;}
public void setAge(int age) {
[Link] = age;}
// toString method to represent student object as a string
@Override
public String toString() {
return "Name: " + name + ", Roll No: " + rollNo + ", Branch: " + branch + ",
Age: " + age;
}
public static void main(String[] args) {
// Creating student objects
Student student1 = new Student("John", 101, "Computer Science", 20);
Student student2 = new Student("Jane", 102, "Electrical Engineering", 22);
// Printing student details
[Link]("Student 1: " + student1);
[Link]("Student 2: " + student2);
}
}
[Link] a java program to create a menu with several menu item by implementing
JMenu
Solution:
import [Link].*;
import [Link].*;
public class MenuDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Menu Demo");
[Link](400, 300);
[Link](JFrame.EXIT_ON_CLOS
E);
// Create menu bar
JMenuBarmenuBar = new JMenuBar();
// Create menus
JMenufileMenu = new JMenu("File");
JMenueditMenu = new JMenu("Edit");
JMenuhelpMenu = new JMenu("Help");
// Create menu items for File menu
JMenuItemnewItem = new JMenuItem("New");
JMenuItemopenItem = new JMenuItem("Open");
JMenuItemsaveItem = new JMenuItem("Save");
JMenuItemexitItem = new JMenuItem("Exit");
// Create menu items for Edit menu
JMenuItemcutItem = new JMenuItem("Cut");
JMenuItemcopyItem = new JMenuItem("Copy");
JMenuItempasteItem = new JMenuItem("Paste");
// Create menu items for Help menu
JMenuItemaboutItem = new JMenuItem("About");
// Add menu items to File menu
[Link](newItem);
[Link](openItem);
[Link](saveItem);
[Link](); // Add a separator
[Link](exitItem);
// Add menu items to Edit menu
[Link](cutItem); [Link](new
[Link](copyItem); ActionListener() {
[Link](pasteItem); public void
// Add menu items to Help menu actionPerformed(ActionEvent e) {
[Link](aboutItem); [Link](frame, "This
// Add menus to the menu bar is a Menu Demo program.", "About",
[Link](fileMenu); JOptionPane.INFORMATION_MESSAGE);
[Link](editMenu); }
[Link](helpMenu); });
// Set the menu bar to the frame // Display the frame
[Link](menuBar); [Link](true);
// Add action listeners for menu items }
[Link](new ActionListener()
} {
public void actionPerformed(ActionEvent e) {
[Link](0);
}});