Java Practical
Java Practical
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:
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:
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
4
Ex.No : 03
SUM OF THE SERIES
Date : 18.07.23
AIM:
ALGORITHM:
5
CODING
import java.io.*;
class GFG {
if (i == 1) {
return 1;
double sum = 0;
return sum;
6
}
/* No of terms in series
int N = 5;
+ calculateSum(N));
Output:
Cd desktop
Javac GFG.java
GFG.java
7
Ex.No: 04
FIND & REPLACE A STRING
Date : 25.07.23
AIM:
ALGORITHM:
8
CODING
import java.io.*;
class Main {
// replace b with c
System.out.println(str1.replace('b', 'c'));
Output:
Cd desktop
Javac Main.java
Main.java
cat call
9
Ex.No : 05
MARK LIST USING INHERITANCE
Date :02.08.23
AIM :
ALGORITHM :
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 {
Output:
Name = Ravi
Percentage = 96.0%
12
Ex.No: 06
CALCULATOR USING APPLET
Date : 12.08.23
AIM :
ALGORITHM :
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
>>>javac Calculator.java
>>>appletviewer Calculator.java
16
Ex.No : 07
PAYROLL OF EMPLOYEES
Date: 19.08.23
AIM
ALGORITHM :
17
CODING
import java.util.Scanner;
18
Output:
Basic: 25000.0
HRA: 2500.0
DA: 2000.0
19
Ex.No : 08
SIMPLE SPREAD SHEET
Date : 26.08.23
AIM:
ALGORITHM:
Step 1: Create a Java project in eclipse. We have created a Java project with the
Step 2: Create a class with the name CreateExcelFileExample1 and write the
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,
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 {
// spreadsheet object
XSSFSheet spreadsheet
= workbook.createSheet(" Student Data ");
studentData.put(
"1",
new Object[] { "Roll No", "NAME", "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);
}
}
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:
ALGORITHM:
24
CODING
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();
}
}
}
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:
ALGORITHM;
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:
Employee Resume
Age: 29
31