Java Practical File 062
Java Practical File 062
(BCA-502)
Submitted in partial fulfilment of the requirement for the award of
1
S.No Title Sign
1 Write a program to create a reverse string
program in java
main(String[] args) {
scanner.close();
int start = 0;
charArray[start] = charArray[end];
charArray[end] = temp;
start++;
end--;
}
}
Output:--
2. Write a program to create a concat program in string.
import java.util.Scanner;
public class StringConcatenationExample
{ public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first string: ");
String firstString = scanner.nextLine();
System.out.print("Enter the second string: ");
String secondString = scanner.nextLine();
String concatenatedString = concatenateStrings(firstString, secondString);
System.out.println("Concatenated String: " + concatenatedString);
scanner.close();
}
private static String concatenateStrings(String str1, String str2)
{ return str1 + str2;
}
}
Output:--
3. Write program to create simple inheritance in java.
class Person
{ private String
name; private int
age;
System.out.println("Person Information:");
person.displayInfo();
System.out.println();
interface Sports
{ void playSports();
}
interface Music
{ void playMusic();
}
interface AthleteMusician extends Sports, Music
{ void performActivities();
}
class Person implements AthleteMusician {
@Override public void
playSports() {
System.out.println("Playing a sport");
}
@Override public
void playMusic() {
System.out.println("Playing music");
}
@Override
public void performActivities() {
System.out.println("Performing both sports and music activities"); }
}
public class MultipleInheritanceExample
{ public static void main(String[] args) {
Person person = new Person();
person.playSports();
person.playMusic();
person.performActivities();
}
}
Output:---
5. Write a program to create a package program and include that
package to another package in java.
package ASB; public class
Student { private String
name; public Student(String
name) { this.name = name;
}
}}
Output:---
6. Write a program for exception handling using
try and catch block
public class
JavaExceptionExample{ public static
void main(String args[]){ try{
int data=100/0;
}catch(ArithmeticException e){System.out.println(e);}
System.out.println("rest of the code...");
}
}
Output:--
7. Write a program for exception handling using try , catch , throw
and finally block functions.
import java.util.Scanner; public class
ExceptionHandlingExample {
public static void main(String[] args)
{ Scanner scanner = new
Scanner(System.in);
try {
System.out.print("Enter a dividend:
"); int dividend = scanner.nextInt();
System.out.print("Enter a divisor: ");
int divisor = scanner.nextInt(); int
result = divide(dividend, divisor);
System.out.println("Result of the division: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} catch (Exception e) {
System.out.println("An unexpected error occurred: " + e.getMessage());
} finally {
System.out.println("Finally block: Cleanup or additional actions can be performed
here.");
scanner.close();
}
}
private static int divide(int dividend, int divisor) throws ArithmeticException {
if (divisor == 0) {
throw new ArithmeticException("Cannot divide by zero.");
}
return dividend / divisor;
}, }
Output:--
8. Write a program to create basic thread program in java
thread1.start();
thread2.start();
thread3.start();
}
}
Output:---
thread1.start();
thread2.start();
thread3.start();
}
}
Output:--
10.Create a program to use the TextField program
TextField tf;
ActionListenerExample()
tf=new TextField();
tf.setBounds(50,50, 150,20);
b.setBounds(50,100,60,30);
b.addActionListener(this);
f.add(b);
f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
new ActionListenerExample();
tf.setText("Welcome to Javatpoint.");
Output
11.Create a AWT counter program
import java.awt.*; import java.awt.event.*;
() { setLayout(new FlowLayout());
tfCount.setEditable(false);
add(tfCount);
add(btnCount);
BtnCountListener();
btnCount.addActionListener(listener);
setVisible(true);
actionPerformed(ActionEvent evt) {
++count;
tfCount.setText(count + ""); } }
OutPut:--
12.Create a Java Combox by using the A.W.T
import java.awt.*; import
java.awt.event.*; public
class awtdemo2
awtdemo2(){
label.setAlignment(Label.CENTER);
label.setSize(400,100);
checkBox1.setBounds(100,100, 50,50);
checkBox2.setBounds(100,150, 50,50);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
checkBox1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e)
});
checkBox2.addItemListener(new ItemListener() {
});
new awtdemo2();
}
Output
c.add("C");
c.add("C++");
c.add("Java");
c.add("PHP");
c.add("Android");
f.add(c);
f.add(label);
f.add(b);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "Programming language Selected: "+ c.getItem(c.getSelectedIndex());
label.setText(data);
}
});
}
Output:--
15.Create a Calculator by using the A.W.T in java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class awtdemo2 extends Frame implements ActionListener
{
Label lb1,lb2,lb3;
TextField txt1,txt2,txt3;
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7;
public awtdemo2()
{
lb1 = new Label("Var 1");
lb2 = new Label("Var 2");
lb3 = new Label("Result");
txt1 = new TextField(10);
txt2 = new TextField(10);
txt3 = new TextField(10);
btn1 = new Button("Add");
btn2 = new Button("Sub");
btn3 = new Button("Multi");
btn4 = new Button("Div");
btn5 = new Button("Mod");
btn6 = new Button("Reset");
btn7 = new Button("Close");
add(lb1); add(txt1);
add(lb2); add(txt2);
add(lb3); add(txt3);
add(btn1); add(btn2);
add(btn3); add(btn4);
add(btn5); add(btn6);
add(btn7);
setSize(200,200);
setTitle("Calculator");
setLayout(new FlowLayout());
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{ double a=0,b=0,c=0;
try
{
a = Double.parseDouble(txt1.getText());
}
catch (NumberFormatException e)
{ txt1.setText("Invalid input");
}
try
{
b = Double.parseDouble(txt2.getText());
}
catch (NumberFormatException e)
{ txt2.setText("Invalid input");
}
if(ae.getSource()==btn1)
{ c=
a + b;
txt3.setText(String.valueOf(c));
}
if(ae.getSource()==btn2)
{ c
= a - b;
txt3.setText(String.valueOf(c));
}
if(ae.getSource()==btn3)
{ c = a * b;
txt3.setText(String.valueOf(c));
}
if(ae.getSource()==btn4)
{ c=
a / b;
txt3.setText(String.valueOf(c));
}
if(ae.getSource()==btn5)
{ c = a % b;
txt3.setText(String.valueOf(c));
}
if(ae.getSource()==btn6)
{
txt1.setText("0");
txt2.setText("0");
txt3.setText("0");
}
if(ae.getSource()==btn7)
{
System.exit(0);
}
}
public static void main(String[] args)
{
awtdemo2 calC = new awtdemo2();
calC.setVisible(true);
calC.setLocation(300,300);
}
}
Output:---
Output:----
18.Make A socket server program where client and
can communicate.
11.1 Creating a Simple Server
import java.io.BufferedReader;
import java.io.IOException; import
java.io.InputStreamReader;
import java.io.PrintWriter; import
java.net.ServerSocket; import
java.net.Socket;
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket);
String message;
while ((message = in.readLine()) != null) {
System.out.println("Received from client: " + message);
out.println("Server echoes: " + message);
}
clientSocket.close();
System.out.println("Client disconnected");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
11.2 Client side program
import java.io.BufferedReader;
import java.io.IOException;
import
java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
String userInputString;
do {
System.out.print("Enter a message: ");
userInputString = userInput.readLine();
out.println(userInputString);
} catch (IOException e)
{ e.printStackTrace();
}
}}
Output:---
Initialised Socket server
Client side
Server Side
.login-container
{ position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 300px;
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h2
{
color: #333;
}
label
{ display:
block;
margin-bottom: 8px;
}
input { width:
100%; padding: 8px;
margin-bottom: 16px;
box-sizing: border-box;
}
button {
background-color: #4caf50;
color: #fff;
padding: 10px 15px;
border: none;
border-radius: 3px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="login-container">
<h2>Login</h2>
<form action="#" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<button type="submit">Login</button>
</form>
</div>
</body>
</html>
Output:---
<!DOCTYPE html>
<html>
<head>
<style> .flex-container
{ display: flex;
background-color: DodgerBlue;
}
.flex-container > div
{ background-color:
#f1f1f1; margin: 10px;
padding: 20px; font-size:
30px;
}
</style>
</head>
<body>
<div class="flex-container">
<div>1</div>
<div>2</div> <div>3</div>
<div>4</div>
<div>5</div>
</div>
</body>
</html>
Output:
20.Write a program to create a gridlayout in H.T.M.L
Gridlayout
<!DOCTYPE html>
<html>
<head>
<style>
.item1 { grid-area: header; }
.item2 { grid-area: menu; }
.item3 { grid-area: main; }
.item4 { grid-area: right; }
.item5 { grid-area: footer; }
.grid-container
{ display: grid; grid-
template-areas:
'header header header header header header'
'menu main main main right right'
'menu footer footer footer footer footer';
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
<h1>Grid Layout</h1>
<div class="grid-container">
<div class="item1">Header</div>
<div class="item2">Menu</div>
<div class="item3">Body</div>
<div class="item4">Right</div>
<div class="item5">Footer</div>
</div>
</body>
</html>
Output: