EX NO: 1
DATE: SORTING THE NUMBERS USING ARRAY
AIM:
To write a Java program using for loop for sorting the numbers using array.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables.
STEP 3: Read the elements using BufferedReader.
STEP 4: Read the elements one by one using loop.
STEP 5: Arrange the elements in sorting order.
STEP 6: Print the elements for ascending order using swapping technique.
STEP 7: Stop the program.
PROGRAM:
package ascend1;
import [Link];
public class Ascend1 {
public static void main(String args[]) {
int i, j, n, temp;
int t[] = new int[8];
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of elements:");
n = [Link]();
[Link]("Enter the elements one by one:");
for (i = 0; i < n; i++) {
t[i] = [Link]();
}
// Ascending order
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++)
{
if (t[i] > t[j]) {
temp = t[i];
t[i] = t[j];
t[j] = temp;
}
}
}
[Link]("Ascending order");
[Link]("----------------");
for (i = 0; i < n; i++) {
[Link](t[i]);
}
// Descending order
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (t[i] < t[j]) {
temp = t[i];
t[i] = t[j];
t[j] = temp;
}
}
}
[Link]("Descending order");
[Link]("----------------");
for (i = 0; i < n; i++) {
[Link](t[i]);
}
}
}
OUTPUT:
Enter the number of Elements
5
Enter the elements one by one
5
9
2
8
3
Ascending Order
----------------------
2
3
5
8
9
Descending Order
------------------------
9
8
5
3
2
RESULT:
Thus, the program was created and executed successfully, and the output was verified.
EX NO: 2
DATE: FIND AND REPLACE OPERATIONS
AIM:
To develop a Java program to find and replace the given text.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables S, f, r and k.
STEP 3: Read the values of S, f, r.
STEP 4: Calculate replace option by using replace function.
STEP 5: Write the original text and replaced text.
STEP 6: Stop the program.
PROGRAM:
package find;
import [Link];
public class Find {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
String text, find, replace;
[Link]("Find & Replace");
[Link]("--------------");
[Link]("Enter the text:");
text = [Link]();
[Link]("Enter the string to find:");
find = [Link]();
if ([Link](find)) {
[Link]("String found");
[Link]("Do you want to replace? (y/n)");
char ch = [Link]().charAt(0);
[Link](); // buffer clear
if (ch == 'y' || ch == 'Y') {
[Link]("Enter the replacement string:");
replace = [Link]();
text = [Link](find, replace);
[Link]("After replacement:");
[Link](text);
}
}
else {
[Link]("String not found");
}
}
}
OUTPUT:
Find and Replace
-----------------------
Enter the text * to Finish
She is a doctor
*
Enter the string to find
Doctor
Doctor is found at 9-Location 1-Line
Do u want to replace? Say y/n.
Y
Enter the string to replace
Teacher
She is a teacher
Do u want to continue? Say y/n.
RESULT:
Thus, the program was created and executed successfully, and the output was verified.
EX NO: 3
DATE: BASIC ARITHMETIC OPERATIONS
AIM:
To develop a Java program to perform basic arithmetic operations.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables.
STEP 3: Read the two values.
STEP 4: Perform addition, subtraction, multiplication, division, and modulus
operation using do-while loop.
STEP 5: Use switch case to perform operations.
STEP 6: Stop the program.
PROGRAM
package arithm;
import [Link];
public class Arithm {
public static void main(String args[]) {
int a, b, c, op;
Scanner sc = new Scanner([Link]);
[Link]("Enter the value of a:");
a = [Link]();
[Link]("Enter the value of b:");
b = [Link]();
do {
[Link]("\n1. Addition");
[Link]("2. Subtraction");
[Link]("3. Multiplication");
[Link]("4. Division");
[Link]("5. Modulo Division");
[Link]("6. Exit");
[Link]("\nEnter your choice:");
op = [Link]();
switch (op) {
case 1:
c = a + b;
[Link]("Addition = " + c);
break;
case 2:
c = a - b;
[Link]("Subtraction = " + c);
break;
case 3:
c = a * b;
[Link]("Multiplication = " + c);
break;
case 4:
if (b != 0)
[Link]("Division = " + (a / (float) b));
else
[Link]("Division by zero not possible");
break;
case 5:
c = a % b;
[Link]("Modulo Division = " + c);
break;
case 6:
[Link]("Exiting program...");
break;
default:
[Link]("Enter valid choice (1 to 6)");
}
} while (op != 6);
}
}
OUTPUT:
Enter the value of a:
42
Enter the value of b:
20
[Link]
[Link]
[Link]
[Link]
[Link] Division
[Link]
Enter your choice
1
ADDITION
---------------
Addition of a and b: 62
[Link]
[Link]
[Link]
[Link]
[Link] Division
RESULT:
Thus, the program was created and executed successfully, and the output was verified.
EX NO: 4
DATE: AREA OF RECTANGLE USING CONSTRUCTOR
AIM:
To write a Java program to find the area of a rectangle using constructor.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare variables for length and breadth.
STEP 3: Create a constructor to initialize the variables.
STEP 4: Create an object for the Rectangle class.
STEP 5: Calculate the area of the rectangle.
STEP 6: Display the result.
STEP 7: Stop the program.
PROGRAM:
package rectangle;
public class Rectangle {
int length, breadth;
Rectangle(int l, int b) {
length = l;
breadth = b;
}
int area() {
return length * breadth;
}
public static void main(String args[]) {
Rectangle ob = new Rectangle(6, 5);
[Link]("--------------------");
[Link]("Area of Rectangle = " + [Link]());
[Link]("--------------------");
}
}
OUTPUT:
-------------------------------
Area of Rectangle=32
-------------------------------
RESULT:
Thus, the program was created and executed successfully, and the output was verified.
------------------------------------------------------------
EX NO: 5
DATE COMMAND LINE ARGUMENTS
AIM:
To develop a Java program using command line arguments.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Read student name, roll number, class and marks using command line arguments.
STEP 3: Calculate total and average marks.
STEP 4: Find the student grade using if-else statement.
STEP 5: Display the result.
STEP 6: Stop the program.
PROGRAM
class student
{
public static void main(String args[])
{
int rno, mark1, mark2, mark3, total, avg;
String name, res;
char grade;
rno = [Link](args[0]);
name = args[1];
mark1 = [Link](args[2]);
mark2 = [Link](args[3]);
mark3 = [Link](args[4]);
[Link]("Roll no\t\t:" + rno);
[Link]("Student name\t:" + name);
[Link]("Mark1\t\t:" + mark1);
[Link]("Mark2\t\t:" + mark2);
[Link]("Mark3\t\t:" + mark3);
total = mark1 + mark2 + mark3;
[Link]("Total\t\t:" + total);
avg = total / 3;
[Link]("Average\t\t:" + avg);
if ((mark1 >= 40) && (mark2 >= 40) && (mark3 >= 40))
{
res = "Pass";
[Link]("Result\t\t:" + res);
if (avg >= 75)
grade = 'A';
else if (avg >= 60)
grade = 'B';
else
grade = 'C';
[Link]("Grade\t\t:" + grade);
}
else
{
res = "Fail";
[Link]("Result\t\t:" + res);
}
}
}
OUTPUT:
Roll no : 101
Student name : Raj
Mark1 : 60
Mark2 : 50
Mark3 : 70
Total : 180
Average : 60
Result : pass
Grade :B
RESULT:
Thus, the program was created and executed successfully, and the output was verified.
EX NO: 6
DATE: DRAW A CIRCLE USING POLYMORPHISM AND INHERITANCE
AIM:
To develop a Java program to draw a circle using polymorphism and inheritance.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create a base class and a derived class using inheritance.
STEP 3: Use polymorphism by overriding the draw() method.
STEP 4: Use the paint() method to draw a circle.
STEP 5: Set the required size and colour.
STEP 6: Display the output.
STEP 7: Stop the program.
PROGRAM:
import [Link].*;
import [Link].*;
class ShapeFrame extends JFrame // Inheritance
{
ShapeFrame()
{
setTitle("Draw Circle");
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// Polymorphism (Method Overriding)
public void paint(Graphics g)
{
[Link](g);
[Link]([Link]);
[Link](200, 200, 100, 100);
[Link](200, 200, 100, 100);
[Link]([Link]);
[Link](100, 100, 50, 50);
[Link](100, 100, 50, 50);
}
}
public class MyOval
{
public static void main(String args[])
{
new ShapeFrame();
}
}
OUTPUT:
RESULT:
Thus, the program was created and executed successfully, and the output was verified.
EX NO: 7
DATE: EXCEPTION HANDLING
AIM:
To demonstrate handling of Arithmetic Exception in Java when dividing a number by zero.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create a method divide_m() that performs division and declares throws
ArithmeticException.
STEP 3: Divide a number by zero inside divide_m().
STEP 4: In main(), call divide_m() inside a try block.
STEP 5: Catch the ArithmeticException using a catch block.
STEP 6: Print the exception message.
STEP 7: Stop the program.
PROGRAM
class examplethrows
{
static void divide_m()throws ArithmeticException
{
int x=22,y=0,z;
z=x/y;
}
public static void main(String args[])
{
try
{
divide_m();
}
catch(ArithmeticException e)
{
[Link]("caught the exception"+e);
}
}
}
OUTPUT:
caught the [Link]:/by zero
RESULT:
Thus, the program was created and executed successfully, and the output was verified.
------------------------------------------------------------
EX NO: 8
DATE: MULTIPLE INHERITANCE USING INTERFACE
AIM:
To calculate the area of a Circle and a Sphere using multiple inheritance through interfaces
in Java.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create an interface area containing:
- A constant pi = 3.1416
- An abstract method area(double radius)
STEP 3: Create class Circle that implements area interface.
STEP 4: Define area(double radius) to return π * radius * radius.
STEP 5: Create class Sphere that implements area interface.
STEP 6: Define area(double radius) to return 4 * π * radius * radius.
STEP 7: Create the main class interface1.
STEP 8: Create objects of Circle and Sphere.
STEP 9: Call the area() method with appropriate radius values.
STEP 10: Display the results.
STEP 11: Stop the program.
PROGRAM:
interface Area
{
double pi = 3.1416; // public static final by default
double area(double radius);
}
class Circle implements Area
{
public double area(double radius)
{
return (pi * radius * radius);
}
}
class Sphere implements Area
{
public double area(double radius)
{
return (4 * pi * radius * radius);
}
}
public class InterfaceDemo
{
public static void main(String args[])
{
double areaCircle, areaSphere;
Circle c = new Circle();
Sphere s = new Sphere();
areaCircle = [Link](2.5);
areaSphere = [Link](3.7);
[Link]("Area of Circle : " + areaCircle);
[Link]("Area of Sphere : " + areaSphere);
}
}
OUTPUT:
Area of circle :19.635
Area of Sphere : 172.034016
RESULT:
Thus, the program was created and executed successfully, and the output was verified.
------------------------------------------------------------
EX NO: 9
DATE: CREATE THREADS AND ASSIGN PRIORITY
AIM:
To demonstrate the use of the main thread in Java by changing its name, priority, and using
the sleep method.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Obtain the current main thread.
STEP 3: Display the current thread information.
STEP 4: Change the thread name and priority.
STEP 5: Display the updated thread information.
STEP 6: Pause the thread execution using sleep method.
STEP 7: Resume execution and display the output message.
STEP 8: Stop the program.
class MainThread
{
public static void main(String args[])
{
Thread thrdMain = [Link]();
[Link]("Current Thread Info : " + thrdMain);
[Link]("nylon");
[Link](7);
[Link]();
[Link]("New Current Thread Info : " + thrdMain);
[Link]();
[Link]("Time to go to bed : sleep for 6 seconds");
[Link]();
try
{
[Link](6000);
}
catch (InterruptedException e)
{
[Link]("Caught : " + e);
}
[Link]("------------------------");
[Link]("Good Morning");
[Link]("------------------------");
}
}
OUTPUT:
Current thread info : Thread[main,5,main]
New Current thread info : Thread[nylon,7,main]
Time to go to bed : sleep for 6 seconds
------------------------
Good Morning
------------------------
RESULT:
Thus, the program was created and executed successfully, and the output was verified.
EX NO: 10
DATE: PLAY MULTIPLE AUDIO CLIPS USING APPLETS
AIM:
To develop a Java applet that plays multiple audio clips using buttons and choice selection.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create an applet class implementing ItemListener and ActionListener.
STEP 3: Add a Choice menu to select different audio clips.
STEP 4: Create Play, Loop, and Stop buttons.
STEP 5: Load the selected audio clip.
STEP 6: Play, loop, or stop the audio based on button click.
STEP 7: Stop the program.
PROGRAM:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class SoundDemo extends JFrame implements ActionListener, ItemListener
{
JComboBox<String> choice;
JButton play, loop, stop;
Clip clip;
public SoundDemo()
{
setTitle("Play Audio Clips");
setSize(400, 200);
setLayout(new FlowLayout());
choice = new JComboBox<>();
[Link]("Ding");
[Link]("Beep");
[Link](this);
play = new JButton("Play");
loop = new JButton("Loop");
stop = new JButton("Stop");
[Link](this);
[Link](this);
[Link](this);
add(choice);
add(play);
add(loop);
add(stop);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void loadSound(String fileName)
{
try
{
if (clip != null)
[Link]();
AudioInputStream ais =
[Link](new File("audio/" + fileName));
clip = [Link]();
[Link](ais);
}
catch (Exception e)
{
[Link](e);
}
}
public void itemStateChanged(ItemEvent e)
{
if ([Link]() == 0)
loadSound("[Link]");
else
loadSound("[Link]");
}
public void actionPerformed(ActionEvent e)
{
if (clip == null) return;
if ([Link]() == play)
[Link]();
else if ([Link]() == loop)
[Link](Clip.LOOP_CONTINUOUSLY);
else if ([Link]() == stop)
[Link]();
}
public static void main(String args[])
{
new SoundDemo();
}
}
OUTPUT:
RESULT:
Thus, the program was created and executed successfully, and the output was verified.
EX NO: 11
DATE: IMPLEMENTING A CALCULATOR USING JAVA SWING
AIM:
To create a simple calculator using Java Swing that performs basic arithmetic operations
using a graphical user interface.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Create a Calculator class implementing ActionListener.
STEP 3: Initialize text field, number buttons (0–9), and operator buttons.
STEP 4: Arrange components using panels and layout managers.
STEP 5: Display the calculator interface.
STEP 6: Read number input from button clicks.
STEP 7: Store the first number when an operator button is pressed.
STEP 8: Perform the selected operation when '=' button is pressed.
STEP 9: Display the result in the text field.
STEP 10: Clear the display when the clear button is pressed.
STEP 11: Stop the program.
PROGRAM
import [Link].*;
import [Link].*;
import [Link].*;
public class MyCalculator extends JFrame implements ActionListener
{
int num1, num2, result;
JTextField T1;
JButton NumButtons[] = new JButton[10];
JButton Add, Sub, Mul, Div, Clear, EQ;
char Operation;
JPanel nPanel, CPanel, SPanel;
public MyCalculator()
{
// North panel
nPanel = new JPanel();
T1 = new JTextField(20);
[Link](T1);
// Center panel
CPanel = new JPanel();
[Link](new GridLayout(4, 4, 5, 5));
for (int i = 0; i < 10; i++)
{
NumButtons[i] = new JButton("" + i);
NumButtons[i].addActionListener(this);
}
Add = new JButton("+");
Sub = new JButton("-");
Mul = new JButton("*");
Div = new JButton("/");
EQ = new JButton("=");
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
for (int i = 0; i < 10; i++)
[Link](NumButtons[i]);
[Link](Add);
[Link](Sub);
[Link](Mul);
[Link](Div);
[Link](EQ);
// South panel
SPanel = new JPanel();
Clear = new JButton("Clear");
[Link](this);
[Link](Clear);
// Frame settings
setLayout(new BorderLayout());
add(nPanel, [Link]);
add(CPanel, [Link]);
add(SPanel, [Link]);
setTitle("Calculator");
setSize(300, 350);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
String str = [Link]();
if ([Link]([Link](0)))
{
[Link]([Link]() + str);
}
else if ([Link]("+") || [Link]("-") || [Link]("*") || [Link]("/"))
{
num1 = [Link]([Link]());
Operation = [Link](0);
[Link]("");
}
else if ([Link]("="))
{
num2 = [Link]([Link]());
switch (Operation)
{
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/':
try {
result = num1 / num2;
} catch (ArithmeticException e) {
[Link](this, "Divide by Zero Error");
return;
}
}
[Link]("" + result);
}
else if ([Link]("Clear"))
{
[Link]("");
}
}
public static void main(String args[])
{
new MyCalculator();
}
}
OUTPUT:
RESULT:
Thus, the program was created and executed successfully, and the output was verified.