Ex.
No: 01
PRIME NUMBER
Date : 03.07.23
AIM:
To write a program to check whether the given input is prime number or
not.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variable and assigned the value.
Step 3: Check the value with the condition.
Step 4: If the condition satisfy,the output block will be executed.
Step 5: Display the result.
Step 6: Stop the program.
1
CODING
Import java .io.*;
public class Main {
public static void main(String[] args) {
int num = 29;
boolean flag = false;
for (inti = 2; i<= num / 2; ++i) {
// condition for nonprime number
if (num % i == 0) {
flag = true;
break;
}
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
Output:
Cd Desktop
Javac Main.java
Main.java
29 is a prime number.
2
Ex.No : 02
LEAP YEAR
Date : 10.07.23
AIM:
To write a program to check whether the given input is Leap year or not.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variable and assigned the value.
Step 3: Check the value with the condition.
Step 4: If the condition satisfy,the output block will be executed.
Step 5: Display the result.
Step 6: Stop the program.
3
CODING
Import java.io.*;
public class Main {
public static void main(String[] args) {
// year to be checked
int year = 1900;
boolean leap = false;
// if the year is divided by 4
if (year % 4 == 0) {
// if the year is century
if (year % 100 == 0) {
// if year is divided by 400
// then it is a leap year
if (year % 400 == 0)
leap = true;
else
leap = false;
}
// if the year is not century
else
leap = true;
}
else
leap = false;
if (leap)
System.out.println(year + " is a leap year.");
else
System.out.println(year + " is not a leap year.");
}
}
Output:
Cd desktop
Javac Main.java
Main.java
1900 is not a leap year
4
Ex.No : 03
SUM OF THE SERIES
Date : 18.07.23
AIM:
To write a program to find the sum of the series
x+x2/2!+x3/3!+…+xn/n!.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variable and assigned the value.
Step 3: Check the value with the condition.
Step 4: If the condition satisfy,the output block will be executed.
Step 5: Display the result.
Step 6: Stop the program.
5
CODING
import java.io.*;
class GFG {
// Function(recursive) to calculate factorial
public static double factorial(inti)
// Step1: Base case
if (i == 1) {
return 1;
// Step2&3: Recursion execution & call statements
return i * factorial(i - 1);
// Function to calculate sum of series
public static double calculateSum(int N)
// Store total_sum in sum
double sum = 0;
// Iteration by running a loop N times
for (inti = 1; i<= N; i++) {
sum = sum + ((double)i / factorial(i));
// Return calculated final sum
return sum;
6
}
// Main driver method
public static void main(String[] args)
/* No of terms in series
taken inn order to show output */
int N = 5;
// Print sum of series by
// calling function calculating sum of series
System.out.println("The sum of series upto " + N
+ " terms is : "
+ calculateSum(N));
Output:
Cd desktop
Javac GFG.java
GFG.java
The sum of series upto 5 terms is : 2.708333333333333
Time Complexity: O(n)
7
Ex.No: 04
FIND & REPLACE A STRING
Date : 25.07.23
AIM:
To write a program to find and replace a word with the string.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variable and assigned the value.
Step 3: Check the value with the condition.
Step 4: If the condition satisfy,the output block will be executed.
Step 5: Display the result.
Step 6: Stop the program.
8
CODING
import java.io.*;
class Main {
public static void main(String[] args) {
String str1 = "bat ball";
// replace b with c
System.out.println(str1.replace('b', 'c'));
Output:
Cd desktop
Javac Main.java
Main.java
java -cp /tmp/WPERFJs5sy Main
cat call
9
Ex.No : 05
MARK LIST USING INHERITANCE
Date :02.08.23
AIM :
To write a program to prepare the mark list using Inheritance.
ALGORITHM :
Step 1: Start the program.
Step 2: Declare the variable and assigned the value.
Step 3: Check the value with the condition.
Step 4: If the condition satisfy,the output block will be executed.
Step 5: Display the result.
Step 6: Stop the program.
10
CODING
import java.io.*;
class Student {
String name;
int rollNo;
int sub1, sub2;
int total;
float percentage;
void getData() throws IOException {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter Name of Student");
name = br.readLine();
System.out.println("Enter Roll No. of Student");
rollNo = Integer.parseInt(br.readLine());
System.out.println("Enter marks out of 100 of 1st subject");
sub1 = Integer.parseInt(br.readLine());
System.out.println("Enter marks out of 100 of 2nd subject");
sub2 = Integer.parseInt(br.readLine());
}
void calculateTotalAndPercentage() {
total = sub1 + sub2;
percentage = (total * 100) / 200;
}
void show() {
System.out.println("Roll No. = " + rollNo);
System.out.println("Name = " + name);
System.out.println("Marks of 1st Subject = " + sub1);
System.out.println("Marks of 2nd Subject = " + sub2);
11
System.out.println("Total Marks = " + total);
System.out.println("Percentage = " + percentage + "%");
}
}
public class StudentTest {public static void main(String[] args) throws
IOException {
Student s = new Student();
s.getData();
s.calculateTotalAndPercentage();
s.show();
}
}
Output:
Enter Name of Student : Ravi
Enter Roll No. of Student : 121
Enter marks out of 100 of 1st subject : 95
Enter marks out of 100 of 2nd subject : 98
Roll No. = 121
Name = Ravi
Marks of 1st Subject = 95
Marks of 2nd Subject = 98
Total Marks = 193
Percentage = 96.0%
12
Ex.No: 06
CALCULATOR USING APPLET
Date : 12.08.23
AIM :
To create a simple calculator applet that implements the four basic
mathematical function.
ALGORITHM :
Step 1: Start the program.
Step 2: Declare the variable and assigned the value.
Step 3: Check the value with the condition.
Step 4: If the condition satisfy,the output block will be executed.
Step 5: Display the result.
Step 6: Stop the program.
13
CODING
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="Calculator" width="700" height="200">
</applet>*/
public class Calculator extends Applet implements ActionListener {
String msg = "";
TextField t1, t2, t3;
Button b1, b2, b3, b4;
Label l1, l2, l3;
public void init() {
l1 = new Label("First Number");
add(l1);
t1 = new TextField(15);
add(t1);
l2 = new Label("Second Number");
add(l2);
t2 = new TextField(15);
add(t2);
l3 = new Label("Result");
add(l3);
t3 = new TextField(15);
add(t3);
b1 = new Button("ADD");
add(b1);
b1.addActionListener(this);
b2 = new Button("SUB");
add(b2);
14
b2.addActionListener(this);
b3 = new Button("MULT");
add(b3);
b3.addActionListener(this);
b4 = new Button("DIV");
add(b4);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
int x = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
int sum = x + y;
t3.setText(" " + sum);
}
if (e.getSource() == b2) {
int x = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
int sub = x - y;
t3.setText(" " + sub);
}
if (e.getSource() == b3) {
int x = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
int mul = x * y;
t3.setText(" " + mul);
}
if (e.getSource() == b4) {
int x = Integer.parseInt(t1.getText());
15
int y = Integer.parseInt(t2.getText());
int div = x / y;
t3.setText(" " + div);
}
showStatus(" text & button example");
repaint();
}
}
OUTPUT
To compile and run the program use the following commands :
>>>javac Calculator.java
>>>appletviewer Calculator.java
16
Ex.No : 07
PAYROLL OF EMPLOYEES
Date: 19.08.23
AIM
To create a applet to calculate the payroll of employees.
ALGORITHM :
Step 1: Start the program.
Step 2: Declare the variable and assigned the value.
Step 3: Check the value with the condition.
Step 4: If the condition satisfy,the output block will be executed.
Step 5: Display the result.
Step 6: Stop the program.
17
CODING
import java.util.Scanner;
public class EmployeeSalaryCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Basic Salary of the Employee:");
double basic = scanner.nextDouble();
double hra = 0.10 * basic; // 10% of basic
double da = 0.08 * basic; // 8% of basic
double grossSalary = basic + hra + da;
double tax = 0.05 * grossSalary; // 5% tax on gross salary
double netSalary = grossSalary - tax;
System.out.println("Employee Salary Breakdown:");
System.out.println("Basic: " + basic);
System.out.println("HRA: " + hra);
System.out.println("DA: " + da);
System.out.println("Gross Salary: " + grossSalary);
System.out.println("Tax Deduction: " + tax);
System.out.println("Net Salary: " + netSalary);
18
Output:
Enter Basic Salary of the Employee:25000
Employee Salary Breakdown:
Basic: 25000.0
HRA: 2500.0
DA: 2000.0
Gross Salary: 29500.0
Tax Deduction: 1475.0
Net Salary: 28025.0
19
Ex.No : 08
SIMPLE SPREAD SHEET
Date : 26.08.23
AIM:
To create a simple spread sheet in a java applet.
ALGORITHM:
Step 1: Create a Java project in eclipse. We have created a Java project with the
name Create Excel File.
Step 2: Create a class with the name CreateExcelFileExample1 and write the
code that we have written in CreateExcelFileExample1.java file.
Step 3: Download the Apache POI library (poi-3.17.jar).
Step 4: Add the Apache POI to the project. Right-click on the project -> Build
Path -> Configure Build Path. It opens the Properties window for the current
project.
Step 5: In the Properties window, click on the Add External JARs button.
Step 6: Go to the path where the poi-3.17.jar file is located. Select the JAR file
and click on the Open button. It adds the JAR file to the project. After that,
click on the Apply and Close button to apply the changes.
20
CODING
import java.io.File;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class WriteDataToExcel {
// any exceptions need to be caught
public static void main(String[] args) throws Exception
{
// workbook object
XSSFWorkbook workbook = new XSSFWorkbook();
// spreadsheet object
XSSFSheet spreadsheet
= workbook.createSheet(" Student Data ");
// creating a row object
XSSFRow row;
// This data needs to be written (Object[])
Map<String, Object[]> studentData
= new TreeMap<String, Object[]>();
studentData.put(
"1",
new Object[] { "Roll No", "NAME", "Year" });
studentData.put("2", new Object[] { "128", "Aditya", "2nd year" });
studentData.put(
"3",
21
new Object[] { "129", "Narayana", "2nd year" });
studentData.put("4", new Object[] { "130", "Mohan", "2nd year" });
studentData.put("5", new Object[] { "131", "Radha", "2nd year" });
studentData.put("6", new Object[] { "132", "Gopal", "2nd year" });
Set<String> keyid = studentData.keySet();
int rowid = 0;
// writing the data into the sheets...
for (String key : keyid) {
row = spreadsheet.createRow(rowid++);
Object[] objectArr = studentData.get(key);
int cellid = 0;
for (Object obj : objectArr) {
Cell cell = row.createCell(cellid++);
cell.setCellValue((String)obj);
}
}
// .xlsx is the format for Excel Sheets...
// writing the workbook into the file...
FileOutputStream out = new FileOutputStream(
new File("C:/savedexcel/GFGsheet.xlsx"));
workbook.write(out);
out.close();
}
}
OUTPUT:
22
It creates a blank excel file at the specified location.
23
Ex.No: 09
BANKING TRANSACTIONS
Date : 04.09.23
AIM:
To create a program to perform Banking Transactions.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variable and assigned the value.
Step 3: Check the value with the condition.
Step 4: If the condition satisfy,the output block will be executed.
Step 5: Display the result.
Step 6: Stop the program.
24
CODING
// Java Program to illustrate Rookie Approach
// In Banking transaction system
// Class 1
// Bank class
// Defining the banking transaction
class Bank {
// Initial balance $100
int total = 100;
// Money withdrawal method. Withdraw only if
// total money greater than or equal to the money
// requested for withdrawal
// Method
// To withdraw money
void withdrawn(String name, int withdrawal)
{
if (total >= withdrawal) {
System.out.println(name + " withdrawn "
+ withdrawal);
total = total - withdrawal;
System.out.println("Balance after withdrawal: "
+ total);
// Making the thread sleep for 1 second after
// each withdrawal
// Try block to check for exceptions
try {
// Making thread t osleep for 1 second
Thread.sleep(1000);
}
// Catch block to handle the exceptions
catch (InterruptedException e) {
// Display the exception along with line
// number
// using printStacktrace() method
e.printStackTrace();
}
}
25
// If the money requested for withdrawal is greater
// than the balance then deny transaction*/
else {
// Print statements
System.out.println(name
+ " you can not withdraw "
+ withdrawal);
System.out.println("your balance is: " + total);
// Making the thread sleep for 1 second after
// each transaction failure
// Try block to check for exceptions
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// Method - to deposit money
// Accept money whenever deposited
void deposit(String name, int deposit)
{
System.out.println(name + " deposited " + deposit);
total = total + deposit;
System.out.println("Balance after deposit: "
+ total);
// Making the thread sleep for 1 second after
// each deposit
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
26
}
}
}
// Class 2
// main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring an object of Bank class and calling the
// withdarwn and deposit methods with suitable
// parameters
// Creating object of class 1 inside main()
Bank obj = new Bank();
// Custom input - Transactions
obj.withdrawn("Arnab", 20);
obj.withdrawn("Monodwip", 40);
obj.deposit("Mukta", 35);
obj.withdrawn("Rinkel", 80);
obj.withdrawn("Shubham", 40);
}
}
OUTPUT
C:\Users\USER\Desktop\LearnCoding\MultiThreading>javac GFG.java
C:\Users\USER\Desktop\LearnCoding\MultiThreading>java GFG
Arnab withdrawn 20
Balance after withdrawal: 80
Monodwip withdrawn 40
Balance after withdrawal: 40
Mukta deposited 35
Balance after deposit: 75
27
Rinkel you can not withdraw 80
your balance is: 75
Shubham withdrawn 40
Balance after withdrawal: 35
28
Ex.No: 10
RESUME OF EMPLOYEES
Date : 22.09.23
AIM:
To create a program to display the resume of employees.
ALGORITHM;
Step 1: Start the program.
Step 2: Declare the variable and assigned the value.
Step 3: Check the value with the condition.
Step 4: If the condition satisfy,the output block will be executed.
Step 5: Display the result.
Step 6: Stop the program.
29
CODING
import java.util.Scanner;
public class EmployeeResume {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter employee name:");
String name = scanner.nextLine();
System.out.println("Enter employee age:");
int age = scanner.nextInt();
System.out.println("Enter employee position:");
scanner.nextLine(); // Consume the newline character
String position = scanner.nextLine();
System.out.println("Enter employee skills:");
String skills = scanner.nextLine();
System.out.println("\nEmployee Resume\n");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Position: " + position);
System.out.println("Skills: " + skills);
scanner.close();
}
}
30
OUTPUT:
Enter employee name: Raj Kanna
Enter employee age: 29
Enter employee position: System service associate
Enter employee skills: C++, Java, Hadoop, Visual Basic
Employee Resume
Name: Raj Kanna
Age: 29
Position: System service associate
Skills: C++, Java, Hadoop, Visual Basic
31