Java Lab BSC II Year
Java Lab BSC II Year
: 01
DATE:
TO SORT GIVEN NUMBERS USING ARRAYS
AIM:
To write a program for sorting a given numbers using array.
ALGORITHM:
STEP – 1: To start a java program.
STEP – 2: Open the notepad and import the packages java.util.Arrays.
STEP – 3: To create a class named as ArraySortExample and to call an array
using Array.sort.
STEP – 4: To save the file using ArraySortExample.java
STEP – 5: To open the command prompt of compilation.
STEP – 6: To compile the program using the command javac
ArraySortExample.java
STEP – 8: To run the program using the command java ArraySortExample
STEP – 9: close the program and exit.
1. TO SORT GIVEN NUMBERS USING ARRAYS
// ArraySortExample.java
import java.util.Arrays;
RESULT:
Thus, the above java program is successfully executed.
Ex. No.:02
Date:
TO IMPLEMENT THE FIND AND REPLACE OPERATION IN THE
GIVEN TEXT
AIM:
To write a java program to find and replace operation in the given text.
ALGORITHM:
STEP – 1: To start a java program.
STEP – 2: Open the notepad and import the packages java.util.Scanner.
STEP – 3: To create a class named as FindReplaceProgram.
STEP – 4: To save the file named as FindReplaceProgram.java
STEP – 5: To open the command prompt for compilation.
STEP – 6: To compile the program using the command javac
FindReplaceProgram.java
STEP – 7: To run the program using the command java FindReplaceProgram
STEP – 8: close the program and exit.
2. TO IMPLEMENT THE FIND AND REPLACE OPERATION IN
THE GIVEN TEXT
// FindReplaceProgram.java
import java.util.Scanner;
// Input text
System.out.println("Enter the text:");
String inputText = scanner.nextLine();
RESULT:
Thus, the above java program is successfully executed.
EX.NO.:03
DATE:
TO IMPLEMENT A CALCULATOR TO PERFORM ARTHIMETIC
OPERATION, DOING WITH CONSTRUCTOR
AIM:
To write a java program for to implement a calculator to perform
arithmetic operation working with constructor.
ALGORITHM:
STEP – 1: To start a java program.
STEP – 2: Open the notepad and import the packages java.util.Scanner.
STEP – 3: To create a class named as CalculatorProgram.
STEP – 4: To save the file named as CalculatorProgram.java
STEP – 5: To open the command prompt for compilation.
STEP – 6: To compile the program using the command javac
CalculatorProgram.java
STEP – 7: To run the program using the command java CalculatorProgram
STEP – 8: close the program and exit.
class Calculator {
private double result;
// Constructor
public Calculator() {
result = 0;
}
// Addition method
public void add(double num) {
result += num;
}
// Subtraction method
public void subtract(double num) {
result -= num;
}
// Multiplication method
public void multiply(double num) {
result *= num;
}
// Division method
public void divide(double num) {
if (num != 0) {
result /= num;
} else {
System.out.println("Cannot divide by zero.");
}
}
scanner.close();
}
}
OUTPUT:
RESULT:
Thus, the above java program is successfully executed.
EX.NO.:04
DATE:
TO FIND STUDENT PERCENTAGE AND GRADE USING THE
COMMAND LINE ARGUMENTS
AIM:
To write a java program for to find student percentage and grade using
the command line arguments.
ALGORITHM:
STEP – 1: To start a java program.
STEP – 2: Open the notepad.
STEP – 3: To create a class named as StudentResult.
STEP – 4: To save the file named as StudentResult.java
STEP – 5: To open the command prompt for compilation.
STEP – 6: To compile the program using the command javac
StudentResult.java
STEP – 7: To run the program using the command java StudentResult
STEP – 8: close the program and exit.
OUTPUT:
RESULT:
Thus, the above java program is successfully executed.
EX.NO.:05
DATE:
TO DRAW CIRCLE OR TRIANGLE OR SQUARE USING
POLYMORPHISM AND INHERITANCE
AIM:
To write a java program to draw circle or triangle or square using
polymorphism and inheritance.
ALGORITHM:
STEP – 1: To start a java program.
STEP – 2: Open the notepad and import packages java.util.scanner.
STEP – 3: To create a class named as DrawingProgram.
STEP – 4: To save the file named as DrawingProgram.java
STEP – 5: To open the command prompt for compilation.
STEP – 6: To compile the program using the command javac
DrawingProgram.java
STEP – 7: To run the program using the command java DrawingProgram
STEP – 8: close the program and exit.
Shape shape;
switch (choice) {
case 1:
shape = new Circle();
break;
case 2:
shape = new Triangle();
break;
case 3:
shape = new Square();
break;
default:
System.out.println("Invalid choice");
return;
}
OUTPUT:
RESULT:
Thus, the above program is successfully verified.
EX.NO.:06
DATE:
TO IMPLEMENT MULTIPLE INHERITANCE CONCEPTS IN JAVA
USING INTERFACE
AIM:
To write a java program to implement multiple inheritance concepts in
java using interface, you can choose your own example for a company or an
educational institution or a general concept which requires the use of interface
to solve particular program.
ALGORITHM:
STEP – 1: To start a java program.
STEP – 2: Open the notepad.
STEP – 3: To create a class named as CompanayExample.
STEP – 4: To save the file named as CompanyExample.java
STEP – 5: To open the command prompt for compilation.
STEP – 6: To compile the program using the command javac
CompanyExample.java
STEP – 7: To run the program using the command java CompanyExample
STEP – 8: close the program and exit.
@Override
public void getSalary() {
System.out.println("Getting regular salary");
}
}
@Override
public void getSalary() {
System.out.println("Getting manager's salary");
}
@Override
public void manageTeam() {
System.out.println("Managing a team of employees");
}
}
@Override
public void getSalary() {
System.out.println("Getting programmer's salary");
}
@Override
public void code() {
System.out.println("Coding in Java");
}
}
EX.NO.:07
DATE:
TO CREATE THREADS AND PERFORM OPERATIONS LIKE START,
STOP, SUSPEND, RESUME
AIM:
To write a java program to create threads and perform operations like
start, stop, suspend and resume.
ALGORITHM:
STEP – 1: To start a java program.
STEP – 2: Open the notepad.
STEP – 3: To create a class named as ThreadOperationsExample.
STEP – 4: To save the file named as ThreadOperationsExample.java
STEP – 5: To open the command prompt for compilation.
STEP – 6: To compile the program using the command javac
ThreadOperationsExample.java
STEP – 7: To run the program using the command java
ThreadOperationsExample.
STEP – 8: close the program and exit.
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
try {
System.out.println("Thread: " + i);
Thread.sleep(1000);
synchronized (this) {
while (suspended) {
wait();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
try {
// Let the thread run for a while
Thread.sleep(3000);
OUTPUT:
RESULT:
Thus, the above program is successfully verified.
EX.NO.:08
DATE:
TO DEVELOP AN APPLET TO PLAY MUTLIPLE AUDIO CLIPS
USING MULTITHREADING
AIM:
To write a program to develop an java applet to play multiple audio clips
using multithreading.
ALGORITHM:
STEP - 1: To create a new folder.
STEP – 2: To write the java code and save the file the folder with named as
SoundApplet.java
STEP – 3: To write the html code and save the file in same folder with named
as SoundApplet.html
STEP – 4: To save any audio clip in the folder and rename the wav files as
sound.wav
STEP – 5: To open the folder in the visual studio and test the code.
STEP – 6: To open the terminal by right click on the folder and select the open
terminal options.
STEP - 7: Now to run the code type javac SoundApplet.java
STEP – 8: Then type appletviewer SoundApplet.html
STEP – 9: Now the output will be displayed.
STEP – 10: Stop and exit the program.
playBtn.setFont(buttonFont);
pauseBtn.setFont(buttonFont);
restartBtn.setFont(buttonFont);
playBtn.setBackground(Color.GREEN);
pauseBtn.setBackground(Color.RED);
restartBtn.setBackground(Color.BLUE);
playBtn.setForeground(Color.WHITE);
pauseBtn.setForeground(Color.WHITE);
restartBtn.setForeground(Color.WHITE);
add(status);
}
soundClip.play();
status.setText("Sound Preview: Playing");
}
}
soundClip.stop();
status.setText("Sound Preview: Paused");
}
}
SoundApplet.html
<!DOCTYPE html>
<html>
<head>
<title>Sound Applet</title>
</head>
<body>
<applet code="SoundApplet.class" width="300" height="300">
</applet>
</body>
</html>
OUTPUT:
RESULT:
Thus, the above program is executed successfully.
EX.NO.:09
DATE:
TO RETRIEVE EMPLOYEE DATA FROM A FILE
AIM:
To write a program to retrieve employee data from a file.
ALGORITHM:
STEP - 1: To create a new folder.
STEP – 2: To write the java code and save the file the folder with named as
EmployeeDataReader.java
STEP – 3: To write the html code and save the file in same folder with named
as employee_data.txt
STEP – 4: To open the command prompt for compilation.
STEP – 6: To compile the program using the command javac
EmployeeDataReader.java
STEP – 7: To run the program using the command java EmployeeDataReader.
STEP – 8: close the program and exit.
// EmployeeDataReader.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
reader.close();
} catch (IOException e) {
System.err.println("Error reading from the file: " + e.getMessage());
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
System.err.println("Error parsing employee data: " + e.getMessage());
}
}
}
OUTPUT:
RESULT:
Thus, the above program is executed successfully.
EX.NO:10
DATE:
TO RETRIEVE STUDENT DATA USING JDBC
AIM:
To write a program to retrieve student data using JDBC.
ALGORITHM:
STEP - 1: To Start the program.
STEP - 2: Import the packages sql.
STEP - 3: Create the class as MysqlCon
STEP - 4: Register the JDBC connection.
STEP - 5: Create a connection and give username and password as parameters.
STEP - 6: Create a statement object.
STEP - 7: Create a ResultSet object.
STEP - 8: In the while loop get the row values of table one by one and print.
STEP - 9: Close the Connection
STEP - 10: Stop the program.
OUTPUT:
D:\Javaprog> javac
MysqlCon.java D:\Javaprog>
java MysqlCon
RESULT:
Thus, the above program is successfully verified.