0% found this document useful (0 votes)
22 views47 pages

Java Practical File 062

The document discusses various Java programming assignments including creating programs for reverse string, string concatenation, inheritance, interfaces, packages, exception handling, threads, AWT components, and HTML/CSS layouts. Students are required to submit programs implementing the listed tasks as part of a practical exam.

Uploaded by

Prashant Bairwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views47 pages

Java Practical File 062

The document discusses various Java programming assignments including creating programs for reverse string, string concatenation, inheritance, interfaces, packages, exception handling, threads, AWT components, and HTML/CSS layouts. Students are required to submit programs implementing the listed tasks as part of a practical exam.

Uploaded by

Prashant Bairwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

PRACTICAL FILE

PRACTICAL- JAVA PROGRAMMING AND DYNAMIC.


WEBPAGE DESIGN

(BCA-502)
Submitted in partial fulfilment of the requirement for the award of

Bachelor of Computer Application (BCA)


To

ASIAN SCHOOL OF BUSINESS, NOIDA


Affiliated to Ch. Charan Singh University, Meerut

Submitted to: - Submitted by: -


Prof. Suvidha Aggarwal Prashant
Roll no:- ASB/BCA/21/043

Batch: BCA 2021-24

1
S.No Title Sign
1 Write a program to create a reverse string
program in java

2 Write a program to create a concat program


in string.

3 Write program to create simple


inheritance in java.
4 Write a program to show multiple
inheritance using interface in java
5 Write a program to create a package
program and include that package to
another package in java.
6 Write a program for exception handling
using try and catch block

7 Write a program for exception handling using try,


catch, throw and finally block function.
8 Write a program to create basic thread program in
java
9 Write a program to multi threading program in
java
10 Write a program using AWT to create textfield in
java.
11 Write a program to create awt counter
12 Write a program to create java combox using awt
13 Write a program to create java selectbox using
awt
14 Write a program to show border-layout in java
15 Write a program to create a calculator by
implementing actionlistenr using AWT in java
16 Write a program to create card-layout using java
17 Write a program to show socket program client
and server side communication
18 Write a program to create a simple log-in page in
HTML
19 Write a program to create a flexlayout using the
HTML and CSS.
20 Write a program to create a grid-layout using
HTML and CSS.
1. Write a program to create a reverse string program in java
import java.util.Scanner; public class

ReverseString { public static void

main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a string: ");

String inputString = scanner.nextLine();

String reversedString = reverseString(inputString);

System.out.println("Reversed String: " + reversedString);

scanner.close();

private static String reverseString(String input) {

char[] charArray = input.toCharArray();

int start = 0;

int end = charArray.length - 1;

while (start < end) {

char temp = charArray[start];

charArray[start] = charArray[end];

charArray[end] = temp;

start++;

end--;

return new String(charArray);

}
}

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;

public Person(String name, int age)


{ this.name = name; this.age
= age;
}

public String getName()


{ return name;
}
public int getAge()
{ return age;
}
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
class Student extends Person
{ private int studentId;

public Student(String name, int age, int studentId)


{ super(name, age); this.studentId =
studentId;
}
public int getStudentId()
{ return studentId;
}

public void displayStudentInfo()


{ displayInfo();
System.out.println("Student ID: " + studentId);
}
}

public class InheritanceExample


{ public static void main(String[] args)
{
Person person = new Person("Ravi", 22);

System.out.println("Person Information:");
person.displayInfo();
System.out.println();

Student student = new Student("Noor", 21, 12345);


System.out.println("Student Information:"); student.displayStudentInfo();
}
}
Output:--
4.Write a program to show multiple inheritance using
interface in java

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;
}

public void display() {


System.out.println("Student Name: " + name);
}
}

Importing Package ASB


import ASB.*; public
class student {
public static void main(String[] args)
{ Student s1= new Student("Ravi");
s1.display();

}}

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

class MyRunnable implements Runnable {


public void run() { for
(int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getId() + " Value " + i);
}
}
}

public class ThreadInterfaceExample


{ public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();

Thread thread1 = new Thread(myRunnable);


Thread thread2 = new Thread(myRunnable);
Thread thread3 = new Thread(myRunnable);

thread1.start();
thread2.start();
thread3.start();
}
}
Output:---

9.Create a multithread program in java.


class MyThread extends Thread
{ public void run() { for
(int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getId() + " Value " + i);
}
}
}
public class MultiThreadExample
{ public static void main(String[] args)
{
// Create multiple instances of MyThread
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
MyThread thread3 = new MyThread();

thread1.start();
thread2.start();
thread3.start();
}
}

Output:--
10.Create a program to use the TextField program

import java.awt.*; import java.awt.event.*; public class

ActionListenerExample implements ActionListener{ static

TextField tf;

ActionListenerExample()

Frame f=new Frame("ActionListener Example");

tf=new TextField();

tf.setBounds(50,50, 150,20);

Button b=new Button("Click Here");

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);

public static void main(String[] args) {

new ActionListenerExample();

public void actionPerformed(ActionEvent e){

tf.setText("Welcome to Javatpoint.");

Output
11.Create a AWT counter program
import java.awt.*; import java.awt.event.*;

public class AWTCounter extends Frame {

private Label lblCount; private TextField

tfCount; private Button btnCount;

private int count = 0; public AWTCounter

() { setLayout(new FlowLayout());

lblCount = new Label("Counter");


add(lblCount); tfCount = new

TextField(count + "", 10);

tfCount.setEditable(false);

add(tfCount);

btnCount = new Button("Count");

add(btnCount);

BtnCountListener listener = new

BtnCountListener();

btnCount.addActionListener(listener);

setTitle("AWT Counter"); setSize(300, 100);

setVisible(true);

public static void main(String[] args) {

AWTCounter app = new AWTCounter();

private class BtnCountListener implements ActionListener {

@Override public void

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(){

Frame f= new Frame("CheckboxGroup Example");

final Label label = new Label();

label.setAlignment(Label.CENTER);

label.setSize(400,100);

CheckboxGroup cbg = new CheckboxGroup();

Checkbox checkBox1 = new Checkbox("C++", cbg, false);

checkBox1.setBounds(100,100, 50,50);

Checkbox checkBox2 = new Checkbox("Java", cbg, false);

checkBox2.setBounds(100,150, 50,50);

f.add(checkBox1); f.add(checkBox2); f.add(label);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

checkBox1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e)

{ label.setText("C++ checkbox: Checked");

});

checkBox2.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e)

{ label.setText("Java checkbox: Checked");

});

public static void main(String args[])

new awtdemo2();

}
Output

13.Create a Java SelectBox by using the A.W.T


import java.awt.*; import
java.awt.event.*; class
awtdemo2
{ awtdemo2() {
Frame f = new Frame(); final
Label label = new Label();
label.setAlignment(Label.CENTER);
label.setSize(400, 100); Button b =
new Button("Show");
b.setBounds(200, 100, 50, 20);
final Choice c = new Choice();

c.setBounds(100, 100, 75, 75);

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);
}
});
}

public static void main(String args[])


{
new awtdemo2();
}
}
Output:--

14.Create a program to Show the Border-Layout .


import java.awt.*; public
class awtdemo2
{
Frame frame;
awtdemo2()
{
frame=new Frame();
Button box1=new Button("**NORTH**");;
Button box2=new Button("**SOUTH**");;
Button box3=new Button("**EAST**");;
Button box4=new Button("**WEST**");;
Button box5=new Button("**CENTER**");;
frame.add(box1,BorderLayout.NORTH);
frame.add(box2,BorderLayout.SOUTH);
frame.add(box3,BorderLayout.EAST);
frame.add(box4,BorderLayout.WEST);
frame.add(box5,BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
}
public static void main(String[] args)
{
new awtdemo2();
}
}

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:---

16.Create a Card layout in A.W.T


import java.awt.*; import
java.awt.event.*; import
javax.swing.*;
public class CardDemo extends Frame implements
ActionListener
{
Button b1, b2, b3, b4, b5;
CardLayout cl;
Container c;
CardDemo ()
{
b1 = new Button ("Button1");
b2 = new Button ("Button2");
b3 = new Button ("Button3");
b4 = new Button ("Button4");
b5 = new Button ("Button5");
c = getContentPane(); cl =
new CardLayout (10, 20);
c.setLayout (cl);
c.add ("Card1", b1);
c.add ("Card2", b2);
c.add ("Card3", b3);
b1.addActionListener (this);
b2.addActionListener (this);
b3.addActionListener (this);
setVisible (true); setSize
(400, 400); setTitle ("Card
Layout");
}
public void actionPerformed (ActionEvent ae)
{ cl.next
(c);
}
public static void main (String[]args)
{
new CardDemo ();
}
}
17.Create a Card layout in java

import java.awt.*; import


javax.swing.*; import
javax.swing.JButton;
import java.awt.event.*;
public class CardLayoutDemo extends JFrame implements
ActionListener
{
JButton b1, b2, b3, b4, b5;
CardLayout cl;
Container c;
CardLayoutDemo ()
{
b1 = new JButton ("Button1");
b2 = new JButton ("Button2");
b3 = new JButton ("Button3");
b4 = new JButton ("Button4");
b5 = new JButton ("Button5");
c = this.getContentPane (); cl
= new CardLayout (10, 20);
c.setLayout (cl);
c.add ("Card1", b1);
c.add ("Card2", b2);
c.add ("Card3", b3);
b1.addActionListener (this);
b2.addActionListener (this);
b3.addActionListener (this);
setVisible (true); setSize
(400, 400); setTitle ("Card
Layout");
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed (ActionEvent ae)
{
cl.next (c);
}
public static void main (String[]args)
{
new CardLayoutDemo ();
}

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;

public class SocketServer


{
public static void main(String[] args) {
final int PORT_NUMBER = 8080;
try (ServerSocket serverSocket = new ServerSocket(PORT_NUMBER)) {
System.out.println("Server is listening on port " + PORT_NUMBER);

while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket);

BufferedReader in = new BufferedReader(new


InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);

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;

public class SimpleSocketClient { public static


void main(String[] args) { final String
SERVER_ADDRESS = "localhost"; final int
PORT_NUMBER = 8080;

try (Socket socket = new Socket(SERVER_ADDRESS, PORT_NUMBER);


BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader userInput = new BufferedReader(new
InputStreamReader(System.in))) {

System.out.println("Connected to the server. Type 'exit' to


disconnect.");

String userInputString;
do {
System.out.print("Enter a message: ");
userInputString = userInput.readLine();
out.println(userInputString);

String serverResponse = in.readLine();


System.out.println("Server response: " + serverResponse);
} while (!"exit".equalsIgnoreCase(userInputString));

} catch (IOException e)
{ e.printStackTrace();
}
}}
Output:---
Initialised Socket server

Client side

Server Side

19. Write program to create a simple html login page.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Page</title>
<style>
body {
font-family: Arial, sans-
serif; background-color:
#f4f4f4; text-align: center;
margin: 0;
padding: 0;
}

.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:---

20. Write a program to create a FlexLayout in H.T.M.L

<!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>

<h1>Create a Flex Container</h1>

<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;
}

.grid-container > div {


background-color: rgba(255, 255, 255, 0.8);
text-align: center; padding: 20px 0; font-
size: 30px;
}
</style>
</head>
<body>

<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:

You might also like