CS3381 Merged
CS3381 Merged
Branch : B.E-CSE
Year & Semester : II year/III semester
DEPARTMENT OF CSE
GOVT COLLEGE OF ENGINEERING-TIRUNELVELI
GOVERNMENT COLLEGE OF ENGINEERING
TIRUNELVELI
2024-2025
Register Number:
CERTIFICATE
This is a Bonafide record of work done by ............
.Government College of Engineering,
Tirunelveli during the year 2024-2025.
Place: Tirunelveli Date:
AIM:
To write a java program for performing sequential search.
ALGORITHM:
Step 1: Start
Step 2: Get number of elements in an array and store it in 'n'
Step 3: Read 'n' element for the array
Step 4: Read the search element
Step 5: Compare each element in the array with the search element. If it matches,
display the position.
Step 6: Display element not found
Step 7: Stop
PROGRAM:
import
java.util.Scanner; class
SequentialSearch(
public static void main(String a[])
{ int n,i,key;
int arr[]=new int[100];
Scanner s=new Scanner(System.in);
System.out.print("Enter the no of elements:");
n=s.nextInt();
System.out.println("Enter the elements");
for(i=0;i<n;i++){
arr[i]=s.nextInt();
}
System.out.print("Enter the search element:");
key=s.nextInt();
for(i=0;i<n;i++)
{ if(arr[i]==key){
System.out.print("Element is found at index "+i);
break;
}
}
if(i>=n)(
System.out.print("Element is not found");
}
}
}
OUTPUT :
RESULT:
Thus the java program for sequential search is written and output was verified
successfully.
EX.NO: 1B BINARY SEARCH PAGE
NO: DATE:
AIM:
ALGORITHM:
Step 1:Start
Step 2:Read the number of elements n and store the elements in the array.
Step 3 : Initialize an array of size n.
Step 4 : Use nested loops to sort the array Outer loop from 0 to n-1 and Inner loop from 0
to n-i-1.
Step 5 : Compare adjacent elements if the first is greater than the second Swap them.
Step 6 : Display the sorted array.
Step 7 : Get the target element from the user.
Step 10 : Calculate middle index middle is equal to target store the index in result and
break.
Step 11 : if the element at middle is lesser, the target moves left to
middle+1.Otherwise middle-1.
Step 12 : If the result is not -1 print the index of the found element otherwise not found.
Step 13 : Stop the program.
PROGRAM:
import java.util.Scanner;
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++)
{ if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
right = middle - 1;
}
}
if (result != -1) {
} else {
System.out.println("Element not found in the array.");
scanner.close();
}
}
OUTPUT :
RESULT:
Thus the java program to search an element in an array using Binary Search was
Written,Executed and the Output was verified Successfully.
EXP NO:1C SELECTION SORTING PAGE
NO: DATE:
AIM:
ALGORITHM:
STEP 1: Start the program
STEP 2: Read the number of elements in an array and store it in
'n' STEP 3: Read the elements of array and store it in a[]
STEP 4: Display the array elements before sorting
STEP 5: Initialize i equals to 0
STEP 6: Check if i is less than n. If true, assign min_index equals to i and go to the
next step. Otherwise, go to step 11.
STEP 7: Initialize j equals to i plus 1
STEP 8: Check if j is less than n. If true, go to the next step. Otherwise, go to step 9.
STEP 9: Check if a[i] is greater than a[j]. If true, min_index equals to j.
STEP 10: Check if min_index is not equal to i. If true, swap a[min_index] and a[i]
using an indirect method.
STEP 11: Increment i by 1 and go to step 6.
STEP 12: Display the array elements after
PROGRAM:
import
java.util.Scanner; class
Selectionsort{
public static void main(String args[])
{ int n,i,j,min_index;
int a[]=new int[100];
Scanner s=new Scanner(System.in);
System.out.print(“Enter the no of elements:”);
n=s.nextInt();
System.out.println(“Enter the elements”);
for(i=0;i<n;i++){
a[i]=s.nextInt();
}
System.out.println(“Before
Sorting”); for(i=0;i<n;i++){
System.out.print(a[i]+” “);
}
System.out.println();
for(i=0;i<n;i++){
min_index=i;
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
min_index=j;
}
}
if(min_index!=i){
int
temp=a[min_index];
a[min_index]=a[i];
a[i]=temp;
}
System.out.println(“After
Sorting”); for(i=0;i<n;i++){
System.out.print(a[i] +” “);
}
System.out.println();
}
}
OUTPUT:
RESULT:
Thus the java program to sort the elements using selection sort was written,executed
and the output was verified successfully.
EX NO: 1D INSERTION SORT PAGE
NO: DATE:
AIM:
To write a java program for performing insertion sort.
ALGORITHM:
PROGRAM:
import
java.util.Scanner; class
Insertionsort
{
public static void main(String args[])
{
int n,i,j,key;
int a[]=new int[100];
Scanner s=new Scanner(System.in);
System.out.print("Enter the no of elements:");
n=s.nextInt();
System.out.println("Enter the elements");
for(i=0;i<n;i++)
{
a[i]=s.nextInt();
}
System.out.println("Before
Sorting"); for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
for(i=1;i<n;i++)
{
key=a[i];
j=i-1;
while(j>=0 && a[j]>key)
{
a[j+1] =
a[j]; j=j-1;
}
a[j+1] = key;
}
System.out.println("After
Sorting"); for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
OUTPUT:
RESULT:
Thus the java program for performing insertion sort was executed and the output was
verified successfully.
EX.NO: 2A STACK PAGE
NO: DATE:
AIM:
To write a java program to perform the operations on stack.
ALGORITHM:
Step 1: Create a Stack class.
Step 2: int top = -1 to represent the top of the stack. int max = 5 to define the maximum stack size as
the array for storing stack elements.
Step 3: Accept the element to push from the user. Check if the stack is full (top>=max-1).If true,
print "Stack Overflow". Otherwise, increment top by 1, add the new element at stack[top],
and print "Element Inserted".
Step 4: Check if the stack is empty. top < 0 If true, print "Stack Underflow". Otherwise, retrieve the
element at stack[top] , decrement top by 1, and print "Element Deleted".
Step 5: Check if the stack is not empty top >= 0 If true, print the top element: stack[top]. Otherwise,
print a message indicating the stack is empty.
Step 6: Print all the elements in the stack from top to 0.
Step 7: In the main function, Present the user with a menu of operations Push,Pop,Peek,Display,Exit
Use a while loop to keep the menu running until the user chooses to exit (option 5). For each user
choice, call the appropriate method. If the choice is invalid, print "Invalid Choice".
Step 8: Exit the program when the user selects option 5.
PROGRAM:
import
java.util.Scanner; class
Stack{
int top=-1,max=5;
int stack[]=new int[max];
void push(){
Scanner s=new Scanner(System.in);
System.out.print("Enter the value to be pushed:");
int value=s.nextInt();
if(top>=max-1){
System.out.print("Stack
OverFlow");
}else{
++top;
stack[top]=value;
System.out.printf("Element Inserted");
}
}
void pop(){
int value;
if(top<0){
System.out.print("Stack UnderFlow");
}else{ value=stack
[top]; top--;
System.out.println("Element Deleted");
}
}
void peek(){
System.out.println("Element at Top "+stack[top]);
}
void display(){
System.out.println("Elements of stack:");
for(int i=top;i>=0;i--){
System.out.printf("%d\n", stack[i]);
}
}
public static void main(String a[])
{ int ch=0;
Stack stc=new Stack();
Scanner input=new Scanner(System.in); System.out.print("Operations:\n1.Push\n2.Pop\
n3.Peek\n4.Display\n5.Exit\n"); while(ch<5){
System.out.print("\nEnter your choice:");
ch=input.nextInt();
if(ch==1)
{ stc.push()
;
}else if(ch==2)
{ stc.pop();
}else if(ch==3){
stc.peek();
}else if(ch==4)
{ stc.display();
}else if(ch==5){
System.out.println("Exiting..");
}else{
System.out.println("Invalid Choice");
}
}
}
}
OUTPUT:
RESULT:
Thus , the above java program to perform an insertion, deletion, displaying the elements in
the stack was executed successfully and the output was verified.
EX.NO: 2B QUEUE PAGE
NO: DATE:
AIM:
To develop queue data structure using classes and objects.
ALGORITHM:
Step 1:Start the program.
Step 2:Initialize front = -1, rear = -1, ch = 0, n, i, d.
Step 3:Get the queue size and store it in n.
Step 4:Display the menu: enqueue, dequeue, peek, display, and exit.
Step 5:Check if the condition choice is not equal to 5. If true, go to the next step.
Otherwise, go to step 17.
Step 6:Get the choice and store it in ch.
Step 7:Check if choice is 1, then check that the rear is equal to n-1 and display "Queue
Overflow" and go to step 5.
Step 8:Check if front == -1 and rear == -1. If true, go to the next step. Otherwise, go
to step 10.
Step 9:Get the element and store it in d. Assign front and rear to 0 and queue[rear] =
d.
Step 10:Get the element and assign queue[++rear] = d and go to step 5.
Step 11:Check if choice is 2, and check if front is equal to -1 and rear is equal to -1,
and display "Underflow", go to step 5. Otherwise, go to the next step.
Step 12:Check if front is equal to rear, if true, display removed element by
queue[front] and front = rear = -1 and go to step 5.
Step 13:If choice is 3, check if the queue is underflow or not. If true, print that.
Otherwise, display the front element and go to step 5.
Step 14:If choice is 4, check if the queue is underflow or not. If true, display that.
Otherwise, display the queue element and go to step 5.
Step 15:If choice is 5, display the end and go to step 17.
Step 16:If the entered choice is out of range, display invalid choice and go to step 5.
Step 17:Stop the program.
PROGRAM:
import java.util.Scanner;
class Queue{
int rear=-
1,front=0,max=5; int
queue[]=new int[max];
void enqueue(){
Scanner s=new Scanner(System.in);
System.out.print("Enter the value to be enqueued:");
int value=s.nextInt();
if(rear>=max-1)
{ System.out.print("Queue
OverFlow");
}else{
++rear;
queue[rear]=value;
System.out.printf("Element Enqueued");
}
}
void dequeue()
{ int value;
if(rear<0){
System.out.print("Stack UnderFlow");
}else{ value=queue[fron
t]; front++;
System.out.println("Element Deleted:"+value);
}
}
void peek(){
System.out.println("Element at Front "+queue[front]);
}
void display()
{ System.out.println("Elements of
queue:"); for(int i=front;i<=rear;i++){
System.out.printf("%d\t",queue[i]);
}
}
public static void main(String a[])
{ int ch=0;
Queue que=new Queue();
Scanner input=new Scanner(System.in); System.out.print("Operantions:\n1.Enqueue\
n2.Dequeue\n3.Peek\n4.Display\n5.Exit\n");
while(ch<5){
System.out.print("\nEnter your choice:");
ch=input.nextInt();
if(ch==1)
{ que.enqueue();
}else if(ch==2){
que.dequeue();
}else if(ch==3)
{ que.peek();
}else if(ch==4)
{ que.display(
);
}else if(ch==5){
System.out.println("Exiting..");
}else{
System.out.println("Invalid Choice");
}
}
}
}
OUTPUT:
RESULT:
Thus , the program to perform queue operation was executed and output was verified
successfully.
EXP NO: 3 EMPLOYEE PAYROLL PAGE
NO: DATE
AIM:
To develop a java application which implements employee payroll by
Getting employee information
ALGORITHM:
Step 1:Start
Step 2:Get the employee name,Id, address,mail Id,mobile No,basic pay for
programmer, assistant professor, associate professor and professor
Step 3: Calculate DA as 97% of BP ,HRA as 10 % of BP, PF as 12% ofBP and 0.1%
of BP for staff club funds
Step 4 : Calculate gross salary as sum of basic pay,DA and HRA
Step 5: Calculate net salary as difference between deduction (PF and staff club fund)
and gross salary
Step 6: Display the name ,Id, address, mail Id, mobile No, basic pay,
DA,HRA,PF,staff club fund , gross salary,net salary for programmer, assistant
professor, associate professor and professor.
Step 7:Stop
PROGRAM:
import
java.util.Scanner; class
EmpPayRoll{
String
empName,empId,address,emailid; long
mobileNo;
int basic_pay;
public EmpPayRoll(String empName,String empId,String address,String emailid,long
mobileNo,int basic_pay){
this.empName=empName;
this.empId=empId;
this.address=address;
this.emailid=emailid;
this.mobileNo=mobileNo;
this.basic_pay=basic_pay;
}
public void display(){
System.out.println("Name:"+empName);
System.out.println("Employer Id:"+empId);
System.out.println("Address:"+address);
System.out.println("Email ID:"+emailid);
System.out.println("Mobile
No:"+mobileNo); System.out.println("Basic
Pay:");
}
public void calculate()
{ double
da=0.07*basic_pay;
double hra=0.10*basic_pay;
double pf=0.12*basic_pay;
double
sfr=0.001*basic_pay;
double gross_salary=basic_pay+da+hra;
double net_salary=gross_salary-(pf+sfr);
System.out.println("Basic
Pay:"+basic_pay);
System.out.println("Gross
Salary:"+gross_salary); System.out.println("Net
Salary:"+net_salary);
}
}
class programmer extends EmpPayRoll{
public programmer(String empName,String empId,String address,String emailid,long
mobileNo,int basic_pay){
super(empName,empId,address,emailid,mobileNo,basic_pay);
}
}
class associativeManager extends EmpPayRoll{
public associativeManager(String empName,String empId,String address,String
emailid,long mobileNo,int basic_pay){
super(empName,empId,address,emailid,mobileNo,basic_pay);
}
}
class assistantManager extends EmpPayRoll{
public assistantManager(String empName,String empId,String address,String
emailid,long mobileNo,int basic_pay){
super(empName,empId,address,emailid,mobileNo,basic_pay);
}
}
class professor extends EmpPayRoll{
public professor(String empName,String empId,String address,String emailid,long
mobileNo,int basic_pay){
super(empName,empId,address,emailid,mobileNo,basic_pay);
}
}
class Main{
public static void main(String[] args)
{ Scanner s=new Scanner(System.in);
System.out.println("Enter the Name:");
String name=s.nextLine();
System.out.println("Enter the Employee ID:");
String empId=s.nextLine();
System.out.println("Enter the Address:");
String address=s.nextLine();
System.out.println("Enter the Email ID:");
String emailid=s.nextLine();
System.out.println("Enter the Mobile No:");
long mobileno=s.nextLong();
System.out.println("Enter the Basic Pay:");
int bp=s.nextInt();
System.out.println("Options:\n1.Programmer\n2.Associative Manager\
n3.Assistance Managaer\n4.Professor");
System.out.println("Enter Your Choice:");
int ch=s.nextInt();
switch(ch){
case 1:
programmer pg=new programmer(name,empId,address,emailid,mobileno,bp);
pg.display();
pg.calculate();
break;
case 2:
associativeManager am1=new
associativeManager(name,empId,address,emailid,mobileno,bp);
am1.display();
am1.calculate();
break;
case 3:
assistantManager am2=new
assistantManager(name,empId,address,emailid,mobileno,bp);
am2.display();
am2.calculate();
break;
case 4:
professor pr=new professor(name,empId,address,emailid,mobileno,bp);
pr.display();
pr.calculate();
break;
default:
System.out.println("Invalid
choice."); break;
}
}
}
OUTPUT:
RESULT:
Thus the java program for implementation of employee payroll was written and
executed successfully and output was verified.
EX.NO: 4 ABSTRACT CLASS PAGE
NO: DATE:
AIM:
Write a Java program to calculate and display areas of different geometric shapes
(Square ,Rectangle ,Triangle ,Circle) by using Abstract class.
ALGORITHM:
Step 2: Display the menu options 1. Square ,2. Rectangle ,3.Triangle , 4.Circle .
Step 5: If the user chooses Square, then Read side length. Calculate area of the
square.Then display area.
Step 6: If the user chooses Rectangle, then Read length and breadth. Calculate area =
length * breadth. Then display area.
Step 7: If the user chooses Triangle, then Read breadth and height.Calculate area of
the triangle.Then display area
Step 8: If the user chooses Circle, then Read radius. Calculate area = π *
radius^2.Then display area. Otherwise, go to Step 9.
Step 9:If the user chooses Exit, then go to Step 10. Otherwise, display "Invalid
Choice" and go back to Step 2.
PROGRAM :
import
java.util.Scanner;
int dim1,dim2;
this.dim2=dim2;
this.dim1=dim1;
super(length,breadth);
void printArea(){
int area=dim1*dim2;
super(breadth,height);
void printArea(){
double area=dim1*dim2*0.5;
}
}
radius){ super(radius);
void printArea(){
double area=dim1*dim1*3.14;
side){ super(side);
void printArea(){
double area=dim1*dim1;
class Area{
{ Scanner scan=new
=5){
ch=scan.nextInt();
switch(ch){
case 1:
int side=scan.nextInt();
s.printArea();
break;
case 2:
int width=scan.nextInt();
r.printArea();
break;
case 3:
int height=scan.nextInt();
Triangle t=new
Triangle(breadth,height); t.printArea();
break;
case 4:
int radius=scan.nextInt();
c.printArea();
break;
case 5:
System.out.println("Exiting..");
break;
default:
System.out.println("Invalid
Choice"); break;
scan.close();
}
OUTPUT:
RESULT:
Thus, The Java program to calculate the area for different geometric shapes by using abstract
class was executed successfully and the output was verified.
EXP NO:5 INTERFACE PAGE
NO: DATE:
AIM:
To implement an interface shape and derive some classes which implement the
interface shapes.
ALGORITHM:
Step-1: Start
Step-2: Define an interface with variables.
Step-3: Also create an abstract method in the interface to print the area.
Step-4: Define a class Rectangle which
implements the interface and printArea method is overridden.
Step-5: Define a class Triangle which implements the interface and printArea method
is overridden
Step-6: Define a class Circle which implements the interface and printArea method is
overridden.
Step-7: In the main function create an object for each class and print the area of the
classes respectively.
Step-8: STOP
PROGRAM:
import
java.util.Scanner;
interface Shapes {
void printArea();
}
class Rectangle implements
Shapes{ double length,breadth;
public Rectangle(double l,double b){
this.length=l;
this.breadth=b;
}
public void printArea()
{ double
area=length*breadth;
System.out.println("Area of rectangle:"+area);
}
}
class Triangle implements
Shapes{ double breadth,height;
public Triangle(double b,double h){
this.breadth=b;
this.height=h;
}
public void printArea(){
double area=0.5*breadth*height;
System.out.println("Area of triangle:"+area);
}
}
class Circle implements
Shapes{ double radius;
public Circle(double r){
this.radius=r;
}
public void printArea(){
double area=3.14*radius*radius;
System.out.println("Area of triangle:"+area);
}
}
class AreaOfShapes{
public static void main(String a[])
{ int ch=0;
Scanner s=new Scanner(System.in); System.out.println("1.Rectangle\
n2.Triangle\n3.Circle\n4.Exit"); while(ch!=4){
System.out.println("Enter your choice:");
ch=s.nextInt();
switch(ch){
case 1:
System.out.println("Enter length and breath:");
double length=s.nextDouble();
double breadth=s.nextDouble();
Rectangle rec=new Rectangle(length,breadth);
rec.printArea();
break;
case 2:
System.out.println("Enter breath and height:");
double breadth_1=s.nextDouble();
double height=s.nextDouble();
Triangle tri=new
Triangle(breadth_1,height); tri.printArea();
break;
case 3:
System.out.println("Enter
radius:"); double
radius=s.nextDouble(); Circle
cir=new Circle(radius);
cir.printArea();
break;
case 4:
System.out.println("Exiting..");
break;
default:
System.out.println("Invalid Choice");
}
}
}
}
OUTPUT:
RESULT:
Thus the java program to calculate the area of various shapes using interface is
written and outputs are verified
EXP NO: 6 EXCEPTION HANDLING IN JAVA PAGE
NO: DATE:
AIM:
To construct a java program to perform exception handling and create user
defined exceptions.
ALGORITHM:
Step 1:start
Step 2:Create a class “AgeValidity” which inherits the exception class and gets
an error message.
Step 3:Create a class “VotedValidity” in which there is a method called
checkAge(). Step 4:In this method.if age is lesser than 18,throw an error message
called “Age is invalid”.
Step 5:In main method try block will be executed.if there is no error,try block will
be Executed completely,otherwise an error message will be thrown.
Step 6:Stop.
PROGRAM:
import
java.util.*;
import java.io.*;
class AgeValidityException extends Exception {
public AgeValidityException(String message) {
super(message);
}
}
class VoterValidity {
public static void checkAge(int age) throws AgeValidityException {
if (age<18) {
throw new AgeValidityException("Error: Age is not valid.");
}
System.out.println("Valid Age!You can vote");
}
public static void main(String[] args)
{ try {
System.out.println("Voter's Validity");
int age;
Scanner s = new Scanner(System.in);
System.out.println("Enter two numbers:");
age = s.nextInt();
checkAge(age);
} catch (AgeValidityException e) {
System.out.println(e);
} catch (InputMismatchException e)
{ System.out.println("Error: Please enter valid
age.");
}finally{
System.out.println("Program Executed");
}
}
}
OUTPUT:
RESULT:
Thus the program to implement exception handling and create user defined exceptions
was written and executed successfully.
EXP NO: 7 MULTI THREADING PAGE
NO: DATE:
AIM:
To create three concurrent threads that work with shared data. One thread generates
random numbers, while the other two calculate the square of even numbers and the cube of
odd numbers, respectively.
ALGORITHM:
Step-1 : START
Step-2 : Define a class SharedData to hold an integer number.
Step-3 : Create a RandomNumberGenerator class extending Thread. In its run
method, generate a random integer and set it in SharedData every second
until stopped.
Step-4 : Create a SquareCalculator class extending Thread. In its run method, retrieve
the number from SharedData. If it's even, calculate its square and print it every
second until stopped.
Step-5 : Create a CubeCalculator class extending Thread. In its run method, retrieve
the number from SharedData If it's odd, calculate its cube and print it every second
until stopped.
Step-6 : In the main method, create an instance of SharedData. Use Thread.sleep() to
pause the main thread for the specified duration.
Step-7 : Stop all three threads after the sleep
period. Step-8 : STOP
PROGRAM:
import java.util.*;
class SharedData{
private int number;
public synchronized void setNumber(int number){
this.number=number;
}
public synchronized int getNumber(){
return number;
}
}
class RandomNumberGenerator extends
Thread{ private final SharedData sharedData;
private boolean running=true;
public RandomNumberGenerator(SharedData sharedData) {
this.sharedData=sharedData;
}
@Override
public void run(){
Random random=new Random();
while (running) {
int number=random.nextInt(10);
sharedData.setNumber(number);
try{
Thread.sleep(1000);
}catch(InterruptedException e){
Thread.currentThread().interrupt();
break;
}
}
}
public void stopGenerator(){
running=false;
}
}
class SquareCalculator extends
Thread{ private final SharedData
sharedData; private boolean
running=true;
OUTPUT:
RESULT:
Thus the Java program to calculate the squares and cubes of a random generated
number using Multi Threading is written and outputs are verified.
EX NO: 8 FILE OPERATIONS PAGE
NO: DATE:
AIM:
To write a Java program for the implementations of file operations.
ALGORITHM:
Step 1.:Start
Step 2:Create a Scanner object for input
Step 3: Define the filename as
user_input.tx
Step 4:Read the data to be written from the user
Step 5:Call the writeFile method to write the content to the file
Step 6:Read the content to append
Step 7:Call the appendToFile method to append
Step 8:Call the readFile method to read and display the file content
Step 9: Call the deleteFile method to delete the file
Step 10:Stop
PROGRAM:
import java.io.*;
import java.util.Scanner;
public class SimpleFileOperations {
public static void writeFile(String filename, String content)
{ try (FileWriter writer = new FileWriter(filename)) {
writer.write(content);
System.out.println("File written successfully!");
} catch (IOException e)
{ System.out.println("Error writing to the
file.");
}
}
public static void appendToFile(String filename, String content) {
try (FileWriter writer = new FileWriter(filename, true)) {
writer.write(content);
System.out.println("Content appended successfully!");
} catch (IOException e)
{ System.out.println("Error appending to the
file.");
}
}
public static void readFile(String filename) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename)))
{ String line;
System.out.println("File content:");
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e)
{ System.out.println("Error reading the
file.");
}
}
public static void deleteFile(String filename)
{ File file = new File(filename);
if (file.delete()) {
System.out.println("File deleted successfully!");
} else {
System.out.println("Error deleting the file or file does not exist.");
}
}
public static void main(String[] args)
{ Scanner scanner = new
Scanner(System.in); String filename =
"user_input_file.txt");
System.out.println("Enter content to write to the file:");
String contentToWrite = scanner.nextLine();
writeFile(filename, contentToWrite);
RESULT:
Thus the Java program for implementations of file operations was written , executed and
the output was verified successfully.
EXP NO: 9 GENERIC CLASS PAGE
NO: DATE:
AIM:
ALGORITHM:
Step1: Start
PROGRAM:
import java.util.Scanner;
class GenericClass<T>
{
private T obj;
public T getObject()
{ return obj;
}
}
}
OUTPUT:
RESULT:
Thus, the above java program for developing applications to demonstrate the
features of generics classes was executed and the output was verified successfully.
EXP NO: 10 JAVAFX PAGE
NO: DATE:
AIM:
To write a program to implement a java fx library and create an application using it.
ALGORITHM:
Step 1:Start.
Step 2:Import the required packages and classes from java fx.
Step 3:Create start() method to implement the java fx application.
Step 4:Create text fields using “TextField” object to get input and text result using
“TextArea” for output.
Step 5: Create buttons called “Add”,”Subtract”,”Multiply” and “Divide” using “Button”
Object.
Step 6:Create a scene using Vbox where all the buttons and textfields act as a nodes of
the scene.
Step 7:Add all the nodes like buttons and text fields to the VBox.
Step 8:Create calculate class.In this class create functions called add,subtract,multiply
And divide where all the arithmetic operations are handled and set it into action
to the respective button by setOnAction() method.
Step 9:Create a menubar and set in on top of the scene.create menu “File” which contain
exit button to exit the scene.
Step 10:In the main method,call launch() method where all the requirements in start
Method will be executed.
Step 11:Stop.
PROGRAM:
package com.example.hellofx;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Simple Calculator");
primaryStage.setScene(scene);
primaryStage.show();
}
menuBar.getMenus().add(fileMenu);
return menuBar;
}
switch (operation)
{ case "add":
result = num1 + num2;
break;
case "subtract":
result = num1 - num2;
break;
case "multiply":
result = num1 * num2;
break;
case "divide":
if (num2 != 0) {
result = num1 / num2;
} else {
resultArea.setText("Error: Division by zero.");
return;
}
break;
}
resultArea.setText("Result: " + result);
} catch (NumberFormatException e)
{ resultArea.setText("Error: Invalid
input.");
}
}
}
OUTPUT:
RESULT:
Thus the given program to implement java fx was written and executed and the output
was verified successfully.