0% found this document useful (0 votes)
222 views73 pages

List of Experiments: CS8382 Object Oriented Programming Laboratory LTPC 0 0 4 2

The document describes an experiment on developing a Java application to generate electricity bills. It involves creating a class with consumer details as members and methods to calculate the bill amount based on tariff plans for domestic and commercial connections. The bill amount is computed differently based on slab rates for different ranges of units consumed. The program takes input from the user, calls the appropriate calculation method based on connection type, and prints the bill payable.

Uploaded by

Hem Ramesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views73 pages

List of Experiments: CS8382 Object Oriented Programming Laboratory LTPC 0 0 4 2

The document describes an experiment on developing a Java application to generate electricity bills. It involves creating a class with consumer details as members and methods to calculate the bill amount based on tariff plans for domestic and commercial connections. The bill amount is computed differently based on slab rates for different ranges of units consumed. The program takes input from the user, calls the appropriate calculation method based on connection type, and prints the bill payable.

Uploaded by

Hem Ramesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 73

CS8382 OBJECT ORIENTED PROGRAMMING LABORATORY LT P C

0042

COURSE OBJECTIVES

 To build software development skills using java programming for real-world


applications.
 To understand and apply the concepts of classes, packages, interfaces,
arraylist, exception handling and file processing.
 To develop applications using generic programming and event handling.

List of experiments
1. Develop a Java application to generate Electricity bill. Create a class with the
following
members: Consumer no., consumer name, previous month reading, current month reading,
type of EB connection(i.e domestic or commercial). Compute the bill amount using the
following tariff.
If the type of the EB connection is domestic, calculate the amount to be paid as follows:
 First 100 units - Rs. 1 per unit
 101-200 units - Rs. 2.50 per unit
 201 -500 units - Rs. 4 per unit
 >501 units - Rs. 6 per unit
If the type of the EB connection is commercial, calculate the amount to be paid as follows:
 First 100 units - Rs. 2 per unit
 01-200 units - Rs. 4.50 per unit
 201 -500 units - Rs. 6 per unit
 >501 units - Rs. 7 per unit

2. Develop a java application to implement currency converter (Dollar to INR, EURO to


INR,
Yen to INR and vice versa), distance converter (meter to KM, miles to KM and vice versa) ,
time converter (hours to minutes, seconds and vice versa) using packages.

3. Develop a java application with Employee class with Emp_name, Emp_id, Address,
Mail_id,
Mobile_no as members. Inherit the classes, Programmer, Assistant Professor, Associate
Professor and Professor from employee class. Add Basic Pay (BP) as the member of all the

inherited classes with 97% of BP as DA, 10 % of BP as HRA, 12% of BP as PF, 0.1% of BP


for staff club fund. Generate pay slips for the employees with their gross and net salary.

4. Design a Java interface for ADT Stack. Implement this interface using array.
Provide necessary exception handling in both the implementations.

2
5. Write a program to perform string operations using ArrayList. Write functions for
the following
a. Append - add at end
b. Insert – add at particular index
c. Search
d. List all string starts with given letter

6. Write a Java Program to create an abstract class named Shape that contains two integers
and an empty method named print Area(). Provide three classes named Rectangle,
Triangle and Circle such that each one of the classes extends the class Shape. Each one of
the classes contains only the method print Area () that prints the area of the given shape.

7. Write a Java program to implement user defined exception handling.

8. Write a Java program that reads a file name from the user, displays information about
whether the file exists, whether the file is readable, or writable, the type of file and the
length of the file in bytes.

9. Write a java program that implements a multi-threaded application that has three
threads. First thread generates a random integer every 1 second and if the value is even,
second thread computes the square of the number and prints. If the value is odd, the third
thread will print the value of cube of the number.

10. Write a java program to find the maximum value from the given type of elements using
a generic function.

11. Design a calculator using event-driven programming paradigm of Java with the
following options.
a) Decimal manipulations
b) Scientific manipulations

12. Develop a mini project for any application using Java concepts.

TOTAL : 60 PERIODS

COURSE OUTCOMES
Upon completion of the course, the students will be able to

1. Develop and implement Java programs for simple applications that make use of
classes, packages and interfaces.
2. Develop and implement Java programs with arraylist, exception handling
and multithreading
.
3. Design applications using file processing, generic programming and event handling.

3
LIST OF EXPERIMENTS
S. No Experiment Name Signature

1. Simple Java Programs Practice

2. Develop a java application to generate


Electricity bill
Develop a java application to implement
3.
currency converter using packages
Develop a java application to generate pay
4. slips for the employees with their gross and net
salary

5. Design a java interface for ADT Stack and


Array with necessary exception
Develop a java program to perform string
6.
operations using Array List.
Write a java Program to create an abstract
7. class
Write a java program to implement user
8. defined exception handling
Develop a java program to implement file
9.
operation(read & write)

10. Write a java program that implements a multi-


threaded application
Write a java program to find maximum value
11. from given elements using generic function

12. Design a Decimal and Scientific calculator


using event-driven programming in java.
Beyond Syllabus:-
1. Internet Banking using J2EE
2. Restaurant Management System Servlets,
MVC and JDBC Project

4
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
COURSE PLAN
Regulation – 2017

S. No. of CO
Topics
No. hours Mapping
1. Simple Java Programs Practice 8 CO1
2. Develop a java application to generate Electricity bill 8 CO1
Develop a java application to implement currency converter using 4 CO2
3.
packages
Develop a java application to generate pay slips for the employees 4 CO2
4. with their gross and net salary
Design a java interface for ADT Stack and Array with necessary 4 CO3
5.
exception
Develop a java program to perform string operations using Array 4 CO3
6.
List.
7. Write a java Program to create an abstract class 4 CO3
8. Write a java program to implement user defined exception handling 4 CO3
9. Develop a java program to implement file operation(read & write) 4 CO3
10. Write a java program that implements a multi-threaded application 4 CO4
Write a java program to find maximum value from given elements 4 CO4
11.
using generic function
Design a Decimal and Scientific calculator using event-driven 8 CO5
12.
programming in java.
Total Hours 60

Content Beyond Syllabus


1. Internet Banking using J2EE
2. Restaurant Management System Servlets, MVC and JDBC Project

5
EXPT: 1 APPLICATION TO GENERATE ELECTRICITY BILL
DATE:

AIM:

To develop a java application to generate the electricity bill.

ALGORITHM:

Step 1: Start the netbeansIDE8.2


Step 2: Goto FILE new projectjavajava applicationclick next
Step 3: Give the name for the applicationClick Finish.
Step 4: Create a class names EBbillCalculation with the necessary variables.
Step 5: Create getdetails() which gets the basic information form the user
Step 6: Create two more method Domestic_Calculation&
Commerical_Calculation to calculate the bill and print it.
Step 7: Stop the program.

PROGRAM:

import java.io.*;
importjava.util.*;
public class EBbillCalculation
{
intcno;
String cname;
intpm_reading,cm_reading,units;
doublebillpay;
voidgetdetails()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the Consumer No:");
cno=in.nextInt();
System.out.println("Enter the Consumer Name:");
cname=in.next();
System.out.println("Enter the Previous Month Reading:");
pm_reading=in.nextInt();
System.out.println("Enter the Current Month Reading:");
cm_reading=in.nextInt();
units=cm_reading-pm_reading;
System.out.println("Choose the type of EB Connection
1. Domestic connection 2. Commercial
connection");
intch=in.nextInt();
switch(ch)
{

6
case 1:
Domestic_Calculation(units);
break;

7
case 2: Commercial_Calculation(units);
break;
}
}

voidDomestic_Calculation(int units)
{
billpay = 0;
if(units<=100)
{
billpay=units*1.00;
}
else if(units>100 && units<=200)
{
billpay=100*1.00+(units-100)*2.50;
}
else if(units>200 && units<=500)
{
billpay=100*1.00+200*2.50+(units-200)*4.00;
}
else if(units>500)
{
billpay =100*1.00+200*2.50+500*4.00+(units-
500)*6.00;
}
show();
System.out.println("Amount to be Paid : " + billpay);
}
voidCommercial_Calculation(int units)
{
billpay = 0;
if(units<=100)
{
billpay=units*2.00;
}
else if(units>100 && units<=200)
{
billpay=100*2.00+(units-100)*4.50;
}

else if(units>200 && units<=500)


{
billpay=100*2.00+200*4.50+(units-200)*6.00;
}
else if(units>500)
{

8
billpay =100*2.00+200*4.50+500*6.00+(units-500)*7.00;
}
show();
System.out.println("Amount to be Paid : " + billpay);
}

public void show()


{
System.out.println("Customer Number : " + cno);
System.out.println("Customer Number : " + cname);
System.out.println("Units Consumed : " + units);
}

public static void main(String[] args)


{
EBbillCalculationeb=new EBbillCalculation();
eb.getdetails();
}
}

9
OUTPUT:

RESULT:

Thus the java application for generating the electricity bill is created and
executed successfully.

10
EXPT: 2 a) CURRENCY CONVERTER
DATE:
AIM:

To develop a java application to implement the currency converter


(Dollar to INR, Euro to INR, Yen to INR and Vice Versa)

ALGORITHM:

Step 1: Start the netbeansIDE8.2


Step 2: Goto FILE new projectjavajava applicationclick next
Step 3: Give the name for the applicationClick Finish.
Step 4: Create a class called Currencyconvertertakes the type of
conversion from
user using switch case.
Step 5: Display the result and stop the program.

PROGRAM:

packagecurrencyconverter;

import java.io.*;
importjava.util.Scanner;

public class Currencyconverter {

public static void main(String[] args) {

doubleRs;
Scanner in = new Scanner(System.in);
System.out.println("Choose the type of currency conversition");
System.out.println("1.USDollar to INDRupees,2.Euro to
INDRupees,3. JapaneseYen to INDRupees");
intch=in.nextInt();
switch(ch)
{
case 1: System.out.println("Dollar to Rupees
conversion"); System.out.println("Enter the
number of Dollars");
11
double dollar=in.nextInt();
Rs=dollar*67.02;
System.out.println("USD="+dollar+" is INR="+Rs);
break;
case 2: System.out.println("EURO to Rupees
conversion"); System.out.println("Enter the
number of EURO"); double Euro=in.nextInt();
Rs=Euro*78.29;
System.out.println("Euro="+Euro+" is INR="+Rs);
break;
case 3: System.out.println("Japanese Yen to Rupees
conversion"); System.out.println("Enter the number of
Yen");
double Yen=in.nextInt();
Rs=Yen*0.61;
System.out.println("Yen="+Yen+" is INR="+Rs);
break;
}

}
}

12
OUTPUT:

RESULT:

Thus the java application to implement the currency converteris created


and executed successfully.

13
EXPT: 2 b) DISTANCE CONVERTER
DATE:

AIM:

To develop a java application to implement the distance converter( Meter


to Kilometer, Miles to Kilometers)

ALGORITHM:

Step 1: Start the netbeansIDE8.2


Step 2: Goto FILE new projectjavajava applicationclick next
Step 3: Give the name for the applicationClick Finish.
Step 4: Create a class called distanceconvertor takes the type of
conversion from
user using switch case.
Step 5: Case1 chooses the Meters into KiloMeters and Case2 Miles to
KiloMeters
Step 6: Display the result and stop the program.

PROGRAM:

packagecurrencyconverter;
import java.io.*;
importjava.util.Scanner;
public class distanceconvertor
{ public static void main(String
args[])
{
doublemeters,kilometers,miles;
Scanner in = new Scanner(System.in);

System.out.println("Choose the type of Distance conversion");


System.out.println("1.Meters to KiloMeters ,2. Miles to KiloMeters :");
intch=in.nextInt();
switch(ch)
{
case 1: System.out.println("Meters into KiloMeters");
System.out.println("Enter the number of
Meters"); meters=in.nextDouble();
kilometers = meters * 0.001;
14
System.out.println( meters+ " Meters is " +kilometers+ " in
Kilometers");
break;

15
case 2: System.out.println("Miles to Kilometers
conversion"); System.out.println("Enter the number
of Miles"); miles=in.nextDouble();
kilometers =miles * 1.60934;
System.out.println( miles+ " Miles is " +kilometers + " in
Kilometers");
break;
}
}
}

16
OUTPUT:

RESULT:

Thus the java application to implement the distance converteris created


and executed successfully.
EXPT: 2 C) TIME CONVERTER

DATE:
AIM:

To develop a java application to implement the time converter(hours to


minutes, seconds and vice versa)

ALGORITHM:

Step 1: Start the netbeansIDE8.2


Step 2: Goto FILE new projectjavajava applicationclick next
Step 3: Give the name for the applicationClick Finish.
Step 4: Create a class called Timeconverter takes seconds as input form
the user and display the result to user.
Step 5: Stop the program.

PROGRAM:

packagecurrencyconverter;

importjava.util.Scanner;

public class Timeconverter


{
public static void main(String[] args) {
intn,hr,min,sec;
Scanner in = new Scanner(System.in);
System.out.print("Enter the Input in seconds:");
n= in.nextInt();
if(n>3600){
min = n/60;
sec = n%60;
hr =
min/60;
min = min%60;
System.out.println("Converted format:"+hr+ "hour " + min
+"mins" + sec+"secs");
}
else{
min = n/60;
sec = n%60;
System.out.println("Converted format :"+min+" mins " +sec
+"secs");
}
}

OUTPUT:

RESULT:

Thus the java application fortime converteris created and executed


successfully.
EXPT: 3 EMPLOYEE PAYROLL PROCESS
DATE:

AIM:

To develop a java application to calculate the payroll and generate the


pay slip for the employee

ALGORITHM:

Step 1: Start the netbeansIDE8.2


Step 2: Goto FILE new projectjavajava applicationclick next
Step 3: Give the name for the applicationClick Finish.
Step 4: Create a class called Emp and declare the necessary variables.
Step 5: Create the method getEmployeedetails() which collect the basic
information from the employee.
Step 6: Create the method pay_calulation () for calculating the gross and
net salary from the basic pay of the employee.
Step 7: Print the output and stop execution.

PROGRAM:

package employee;
importjava.io.IOException;
importjava.util.Scanner;

classEmp
{
String ename,Address,email;
inteid;
int mobile;

voidgetEmployeedetails()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the Emp_id. :");
eid=in.nextInt();
System.out.println("Enter the Employee Name:");
ename=in.next();
System.out.println("Enter the Employee Address:");
Address=in.next();
System.out.println("Enter the Employee Email id :");
email=in.next();
System.out.println("Enter the Mobile No:");
mobile=in.nextInt();
}
voidpay_calulation(double BasicPay)
{
doubleDA,HRA,PF,Sfund,Gross_Salary,Netsalary;
DA=BasicPay*0.97;
HRA=BasicPay*0.10;
PF=BasicPay*0.12;
Sfund=BasicPay*0.1;
Gross_Salary=BasicPay+DA+HRA;
Netsalary=Gross_Salary-(PF+Sfund);
System.out.println("Gross salary of the Employee"+Gross_Salary);
System.out.println("Net salary of the Employee: "+Netsalary);

}
void display()
{
System.out.println("Emp_id:"+eid);
System.out.println("Employee Name:"+ename);
System.out.println("Employee Address:"+Address);
System.out.println("Employee Email id :"+email);
System.out.println("Employee Mobile No:"+mobile);
}
}
class Programmer extends Emp
{
doubleBasicPay;
voidProgrammerdetails()
{
getEmployeedetails();
Scanner in = new Scanner(System.in);
System.out.println("Enter the Basic Pay of the Programmer:");
BasicPay=in.nextInt();
display();
pay_calulation(BasicPay);
}
}
classAssistantProfessor extends Emp
{
voidAPDetails()
{
doubleBasicPay;
getEmployeedetails();
Scanner in = new Scanner(System.in);
System.out.println("Enter the Basic Pay of the AssistantProfessor:");
BasicPay=in.nextInt();
display();
pay_calulation(BasicPay);
}

}
classAssociateProfessor extends Emp
{
doubleBasicPay;

voidASPDetails()
{
getEmployeedetails();
Scanner in = new Scanner(System.in);
System.out.println("Enter the Basic Pay of the AssociateProfessor:");
BasicPay=in.nextInt();
display();
pay_calulation(BasicPay);
}

}
class Professor extends Emp
{
doubleBasicPay;
voidprofDetails()
{
getEmployeedetails();
Scanner in = new Scanner(System.in);
System.out.println("Enter the Basic Pay of the Professor:");
BasicPay=in.nextInt();
display();
pay_calulation(BasicPay);
}

public class Employee


{

public static void main(String[] args)


{

Scanner in = new Scanner(System.in);


System.out.println("Choose the type Employee");
System.out.println("1.Programmer ,2.Assistant Professor,3.Associate Professor
,4.Professor: ");
intch=in.nextInt();
switch(ch)
{
case 1: System.out.println("PROGRAMMER DETAILS");
Programmer p=new Programmer();
p.Programmerdetails();
break;
case 2: System.out.println("Assistant Professor DETAILS");
AssistantProfessorap=new
AssistantProfessor();
ap.APDetails();
break;
case 3: System.out.println("Associate Professor DETAILS");
AssociateProfessor asp=new
AssociateProfessor();
asp.ASPDetails();
break;
case 4: System.out.println("Professor DETAILS");
Professor pf=new Professor();
pf.profDetails();
break;

}
}}
OUTPUT:

RESULT:

Thus the java program for generating the employee pay slip is created
and executed successfully.
EXPT: 4 ADT STACK
DATE:

AIM:

To develop a program using Java interface for ADT Stack

ALGORITHM:

Step 1: Start the netbeansIDE8.2


Step 2: Goto FILE new projectjavajava applicationclick next
Step 3: Give the name for the applicationClick Finish.
Step 4: Create an interface Mystack with methods push,pop,display.
Step 5: Create a class stack_array which implements the interface
Mystack
Step 6: Create the main class with switch case which gets the input from
the user
to Push / Pop / Display

PROGRAM:
packagestackadt;
import java.io.*;
interfaceMystack
{
public void pop();
public void push();
public void display();
}
classStack_array implements Mystack
{
final static int n=5;
int stack[]=new int[n];
int top=-1;
public void push()
{
try
{
BufferedReaderbr=new BufferedReader(new
InputStreamReader(System.in));
if(top==(n-1))
{
System.out.println(" Stack Overflow");
return;
}
else
{
System.out.println("Enter the element");
intele=Integer.parseInt(br.readLine());
stack[++top]=ele;
}
}
catch(IOException e)
{
System.out.println("e");
}
}
public void pop()
{
if(top<0)
{
System.out.println("Stack underflow");
return;
}
else
{
int
popper=stack[top];
top--;
System.out.println("Popped element:" +popper);
}
}

public void display()


{
if(top<0)
{
System.out.println("Stack is empty");
return;
}
else
{
String str=" ";
for(int i=0; i<=top; i++)
str=str+" "+stack[i]+" <--";
System.out.println("Elements are:"+str);
}
}
}
classStackADT
{
public static void main(String arg[])throws IOException
{
BufferedReaderbr=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Implementation of Stack using Array");
Stack_arraystk=new Stack_array();
intch=0;
do
{
System.out.println("1.Push 2.Pop 3.Display 4.Exit");
System.out.println("Enter your choice:");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
stk.push();
break;
case 2:
stk.pop();
break;
case 3:
stk.display();
break;
case 4:
System.exit(0);
}
}
while(ch<5);
}
}
OUTPUT:

RESULT:

Thus the program for program using Java interface for ADT Stack has
been written and executed successfully.
EXPT: 5 ARRAY LIST

DATE:

AIM:

To write a java program to perform string operations using Array List

ALGORITHM:

Step 1: Start the netbeansIDE8.2

Step 2: Goto FILE new projectjavajava applicationclick next

Step 3: Give the name for the applicationClick Finish.

Step 4: Create an Array list named arrlist, arrlist2 and

add the strings using add()method.

Step 5: Append the arrlist2 to the end of arrlist Using addAll() method .

Step 6: Search the string in Arraylist by using contain() method. if the

string is found , print search is successful otherwise try again.

Step 7: Print all string starts with given letter by using matches() method.

Step 8: Stop the program.

PROGRAM:

importjava.util.ArrayList;
importjava.util.Scanner;

public class ArrayListDemo


{
public static void main(String args[])
{
ArrayList<String>arrlist = new ArrayList<>();
arrlist.add("book");
arrlist.add("cook");
arrlist.add("HTML");
System.out.println("Printing list1:");
System.out.println(arrlist);
arrlist.add(1,"PHP");
System.out.println("(After Insertion)Printing list1:");
System.out.println(arrlist);
ArrayList<String> arrlist2 = new ArrayList<>();
arrlist2.add("cat");
arrlist2.add("bat");
arrlist2.add("hat");
arrlist2.add("Jump");
System.out.println("Printing list2:");
System.out.println(arrlist2);
arrlist.addAll(arrlist2);
System.out.println("( After Appended)Printing all the elements");
System.out.println(arrlist);
System.out.println("SEARCH STRING IN ARRAY LIST");
System.out.println("###########################");
Scanner in = new Scanner(System.in);
System.out.println("ENTER THE STRING TO BE
SEARCH:"); String searchString=in.next();
boolean Found = arrlist.contains(searchString);
if(Found)
System.out.println("SUCCESS!!! String is available in the Arraylist");
else
System.out.println("Failure!!! Try Again");
System.out.println("LIST THE STRING IN ARRAY LIST");
System.out.println("----------------------------------------------");
ArrayList<String>listClone = new ArrayList<>();
for (String string : arrlist)
{
if(string.matches("(?i)(b).*"))
{
listClone.add(string);
}
}
System.out.println(listClone);
}
}
OUTPUT:

RESULT:

Thus the program for Arraylist has been written and executed
successfully.
EXPT NO: 6 ABSTRACT CLASS

DATE:

AIM:

To write a java program to create an abstract class named Shape .

ALGORITHM:

Step 1: Start the netbeansIDE8.2


Step 2: Goto FILE new projectjavajava applicationclick next
Step 3: Give the name for the applicationClick Finish.
Step 4: Create an abstract class named Shape that contains two integers and an
empty method named print Area()
Step 5: Create three classes named Rectangle, Triangle and Circle which
extends Shape
Step 6: Print the area of the given shapes.
Step 7: Stop the program.

PROGRAM:

package javaapplication3;
abstract class shape
{
int a=3,b=4;
abstract public void print_area();
}
class rectangle extends shape
{
publicintarea_rect;
@Override
public void print_area()
{
area_rect=a*b;
System.out.println("The area ofrectangle is:"+area_rect);
}
}
class triangle extends shape
{
intarea_tri;
@Override
public void print_area()
{
area_tri=(int) (0.5*a*b);
System.out.println("The area oftriangle is:"+area_tri);
}
}
class circle extends shape
{
intarea_circle;
@Override
public void print_area()
{
area_circle=(int) (3.14*a*a);
System.out.println("The area ofcircle is:"+area_circle);
}
}
public class JavaApplication3 {
public static void main(String[] args) {
rectangle r=new rectangle();
r.print_area();
triangle t=new triangle();
t.print_area();
circle r1=new circle();
r1.print_area();
}
}
OUTPUT:

RESULT:

Thus the program for abstract class has been written and executed
successfully.
EXPT: 7 USER DEFINED EXCEPTION HANDLING
DATE:

AIM:

To write a Java program to implement user defined exception handling.

ALGORITHM:

Step 1: Start the netbeansIDE8.2


Step 2: Goto FILE new projectjavajava applicationclick next
Step 3: Give the name for the applicationClick Finish.
Step 4: Create a user defined exception called MyException which
extends class Exception.
Step 5: Create try-catch block to handle the exception.
Step 6: Throw an exception of user defined type as an argument in main()
Step 7: Exception is handled using try, catch block
Step 8: Display the user defined exception.

PROGRAM:

package example1;

classMyException extends Exception{


String str1;
MyException(String str2) {
str1=str2;
}
public String toString(){
return ("MyException Occurred: "+str1) ;
}
}
public class Example1 {

public static void main(String[] args)


{
try{
System.out.println("Starting of try block");
// I'm throwing the custom exception using throw
throw new MyException("This is My error Message");
}
catch(MyExceptionexp){
System.out.println("Catch Block") ;
System.out.println(exp) ;
}
}

OUTPUT:

RESULT:

Thus the program user defined exception handling has been written and
executed successfully.
EXPT: 8 FILE OPERATIONS
DATE:
AIM:

To write a Java program to implement the concepts of file.

ALGORITHM:

Step 1: Start the program


Step 2: Open the notepad and type the program and save the program as
Filedemo.java
Step 3: Click on the Cmd Prompt cd C:\Java\jdk1.8\bin
Step 4: Now compile the Filedemo.java using
javac Filedemo.java
Step 5: Before executing the Filedemo.java create another java file using
notepad called Fib.java
Step 6: Execute the program using java Filedemo.java and give the input
file name Fib.java
Step 7: Stop the program.

PROGRAM:

importjava.util.Scanner;
importjava.io.File;

public class Filedemo {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);


String s=input.nextLine();
File f1=new File(s);
System.out.println("File Name:"+f1.getName());
System.out.println("Path:"+f1.getPath());
System.out.println("Abs Path:"+f1.getAbsolutePath());
System.out.println("Parent:"+f1.getParent());
System.out.println("This file is:"+(f1.exists()?"Exists":"Does
not exists"));
System.out.println("Is file:"+f1.isFile());
System.out.println("Is Directory:"+f1.isDirectory());
System.out.println("Is Readable:"+f1.canRead());
System.out.println("IS Writable:"+f1.canWrite());
System.out.println("Is Absolute:"+f1.isAbsolute());
System.out.println("File Last Modified:"+f1.lastModified());
System.out.println("File Size:"+f1.length()+"bytes");
System.out.println("Is Hidden:"+f1.isHidden());

OUTPUT:

Fib.java

public class Fib {

public static void main(String[] args) {


int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);

for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}
}
RESULT:

Thus the program for implementing the file concept has been written and
executed successfully.
EXPT: 9 MULTI THREADING
DATE:

AIM:

To write a Java program to implement the concepts of multithreading.

ALGORITHM:

Step 1: Start the netbeansIDE8.2


Step 2: Goto FILE new projectjavajava applicationclick next
Step 3: Give the name for the applicationClick Finish.
Step 4: Create three threads A, odd, even
Step 5: Thread A generates the random number
Step 6: If random number is even ,Thread even will display the square of
the random number
Step 7: If random number is odd ,Thread odd will display the cube of the
random number

PROGRAM:

packagemtherad;
importjava.util.*;

class even implements Runnable{


publicint x;
public even(int x){
this.x=x;
}

@Override
public void run()
{
System.out.println("Thread Name:Even Thread and square is: " + x * x);

class odd implements Runnable{


publicint x;
public odd(int x){
this.x=x;
}
@Override
public void run()
{
System.out.println("Thread Name:Odd Thread and cube is :"+ x * x * x);

}
class A extends Thread{
public String tname;
public Random r;
public Thread
t1,t2; public
A(String s)
{ tname=s;
}
@Override
public void run()
{
intnum=0;
r=new Random();
try {
for(int i=0;i<50;i++){
num=r.nextInt(100);
System.out.println("main thread and generated number is"+num);
if(num%2==0)
{
t1=new Thread(new even(num));
t1.start();
}else{
t2=new Thread(new odd(num));
t2.start();
}
Thread.sleep(1000);
System.out.println("-----------------------------------");
}
}

catch(InterruptedException ex)
{
System.out.println(ex.getMessage());
}
}
}
public class Mtherad {

public static void main(String[] args) {


A a=new A("one");
a.start();
}

}
OUTPUT:

RESULT:

Thus the program for implementing the concept of multithreading has


been written and executed successfully.
EXPT: 10 GENERIC FUNCTION
DATE:

AIM:

To write a Java program to find the maximum value in a given generic


function.

ALGORITHM:

Step 1: Start the netbeansIDE8.2


Step 2: Goto FILE new projectjavajava applicationclick next
Step 3: Give the name for the applicationClick Finish.
Step 4: Create generic method class called T extends with three objects x,y,z.
Step 5: Initially assume x as max and compare it with y and z.
Step 6: Find the maximum and return it to the main function

PROGRAM:

packagegenericmethodtest;

public class GenericMethodTest {

public static <T extends Comparable<T>> T maximum(T x, T y, T z)


{

T max = x; // assume x is initially the

largest if(y.compareTo(max) > 0) {


max = y; // y is the largest so far
}

if(z.compareTo(max) > 0)
{ max = z;// z is the largest
now
}
return max; // returns the largest object
}

public static void main(String args[])


{
System.out.printf("Max of %d, %d and %d is %d\n\n",
3, 4, 5, maximum( 3, 4, 5 ));
System.out.printf("Max of %.1f,%.1f and %.1f is %.1f\n\n",
6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ));

}
}

OUTPUT:

RESULT:

Thus the program for finding the maximum value using generic function
has been written and executed successfully.
EXPT: 11 CALCULATOR
DATE:

AIM:

To write a Java program to implement the decimal and scientific


calculation using event driven programming .

ALGORITHM:

Step 1: Start the program


Step 2: Open the notepad and type the program and save the program as
ScientificCalculator.java
Step 3:Create the class ScientificCalculator. Define and declare its
variables.
Step 4: Using ScientificCalculator constructor create bottons.
Step 5: Using actionPerformed() method define the function that has to be
done when the corresponding button is pressed.
Step 6:Click on the Cmd Prompt cd C:\Java\jdk1.8\bin
Step 7: Now compile the Filedemo.java using
javacScientificCalculator.java
Step 8: Execute the program using
javaScientificCalculator

PROGRAM:

importjava.awt.*;
importjavax.swing.*;
importjava.awt.event.*;
importjavax.swing.event.*;
public class ScientificCalculator extends JFrame implements
ActionListener
{
JTextFieldtfield;

double temp,temp1,result,a,ml;
static double m1,m2;
int k=1,x=0,y=0,z=0;
charch;
JButton
b1,b2,b3,b4,b5,b6,b7,b8,b9,zero,clr,pow2,pow3,exp,fac,plus,min,div,log,
rec,mul,eq,addsub,dot,mr,mc,mp,mm,sqrt,sin,cos,tan;
Container cont;
JPaneltextpanel,buttonpanel;
ScientificCalculator()
{
cont=getContentPane();
cont.setLayout(new BorderLayout());
JPaneltextpanel=new JPanel();
tfield=new JTextField(25);
tfield.setHorizontalAlignment(SwingConstants.RIGHT);
tfield.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEventkeyevent)
{
char c=keyevent.getKeyChar();
if(c>='0'&&c<='9')
{
}
else
{
keyevent.consume();
}
}
});
textpanel.add(tfield);
buttonpanel=new JPanel();
buttonpanel.setLayout(new GridLayout(8,4,2,2));
boolean t=true;
mr=new JButton("MR");
buttonpanel.add(mr);
mr.addActionListener(this);
mc=new JButton("MC");
buttonpanel.add(mc);
mc.addActionListener(this);
mp=new JButton("M+");
buttonpanel.add(mp);
mp.addActionListener(this);
mm=new JButton("M-");
buttonpanel.add(mm);
mm.addActionListener(this);
b1=new JButton("1");
buttonpanel.add(b1);
b1.addActionListener(this);
b2=new JButton("2");
buttonpanel.add(b2);
b2.addActionListener(this);
b3=new JButton("3");
buttonpanel.add(b3);
b3.addActionListener(this);
b4=new JButton("4");
buttonpanel.add(b4);
b4.addActionListener(this);
b5=new JButton("5");
buttonpanel.add(b5);
b5.addActionListener(this);
b6=new JButton("6");
buttonpanel.add(b6);
b6.addActionListener(this);
b7=new JButton("7");
buttonpanel.add(b7);
b7.addActionListener(this);
b8=new JButton("8");
buttonpanel.add(b8);
b8.addActionListener(this);
b9=new JButton("9");
buttonpanel.add(b9);
b9.addActionListener(this);
zero=new JButton("0");
buttonpanel.add(zero);
zero.addActionListener(this);
plus=new JButton("+");
buttonpanel.add(plus);
plus.addActionListener(this);
min=new JButton("-");
buttonpanel.add(min);
min.addActionListener(this);
mul=new JButton("*");
buttonpanel.add(mul);
mul.addActionListener(this);
div=new JButton("/");
buttonpanel.add(div);
div.addActionListener(this);
addsub=new JButton("+/-");
buttonpanel.add(addsub);
addsub.addActionListener(this);
dot=new JButton(".");
buttonpanel.add(dot);
dot.addActionListener(this);
eq=new JButton("=");
buttonpanel.add(eq);
eq.addActionListener(this);
rec=new JButton("1/x");
buttonpanel.add(rec);
rec.addActionListener(this);
sqrt=new JButton("Sqrt");
buttonpanel.add(sqrt);
sqrt.addActionListener(this);
log=new JButton("log");
buttonpanel.add(log);
log.addActionListener(this);
sin=new JButton("SIN");
buttonpanel.add(sin);
sin.addActionListener(this);
cos=new JButton("COS");
buttonpanel.add(cos);
cos.addActionListener(this);
tan=new JButton("TAN");
buttonpanel.add(tan);
tan.addActionListener(this);
pow2=new JButton("x^2");
buttonpanel.add(pow2);
pow2.addActionListener(this);
pow3=new JButton("X^3");
buttonpanel.add(pow3);
pow3.addActionListener(this);
exp=new JButton("Exp");
buttonpanel.add(exp);
exp.addActionListener(this);
fac=new JButton("n!");
buttonpanel.add(fac);
fac.addActionListener(this);
clr=new JButton("AC");
buttonpanel.add(clr);
clr.addActionListener(this);
cont.add("Center",buttonpanel);
cont.add("North",textpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("1"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"1");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"1");
z=0;
}
}
if(s.equals("2"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"2");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"2");
z=0;
}
}
if(s.equals("3"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"3");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"3");
z=0;
}
}
if(s.equals("4"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"4");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"4");
z=0;
}
}
if(s.equals("5"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"5");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"5");
z=0;
}
}
if(s.equals("6"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"6");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"6");
z=0;
}
}
if(s.equals("7"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"7");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"7");
z=0;
}
}
if(s.equals("8"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"8");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"8");
z=0;
}
}
if(s.equals("9"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"9");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"9");
z=0;
}
}
if(s.equals("0"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"0");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"0");
z=0;
}
}
if(s.equals("AC"))
{
tfield.setText("");
x=0;y=0;
z=0;
}
if(s.equals("log"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{

a=Math.log(Double.parseDouble(tfield.getText()));
tfield.setText(""); tfield.setText(tfield.getText()
+a);

}
}
if(s.equals("1/x"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a=1/(Double.parseDouble(tfield.getText()));
tfield.setText("")
;
tfield.setText(tfield.getText()+a);
}
}
if(s.equals("Exp"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{

a=Math.exp(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText()+a);
}
}
if(s.equals("x^2"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{

a=Math.pow(Double.parseDouble(tfield.getText()),2);
tfield.setText("");
tfield.setText(tfield.getText()+a);
}
}
if(s.equals("X^3"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{

a=Math.pow(Double.parseDouble(tfield.getText()),3);
tfield.setText("");
tfield.setText(tfield.getText()+a);
}
}
if(s.equals("+/-"))
{
if(x==0)
{
tfield.setText("-"+tfield.getText());
x=1;
}
else
{
tfield.setText(tfield.getText());
}
}
if(s.equals("."))
{
if(y==0)
{
tfield.setText(tfield.getText()+".");
y=1;
}
else
{
tfield.setText(tfield.getText());
}
}
if(s.equals("+"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
temp=0;
ch='+';
}
else
{
temp=Double.parseDouble(tfield.getText());
tfield.setText("");
ch='+';
y=0;x=0;
}
tfield.requestFocus();
}
if(s.equals("-"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
temp=0;
ch='-';
}
else
{
y=0;x=0;
temp=Double.parseDouble(tfield.getText());
tfield.setText("");
ch='-';
}
tfield.requestFocus();
}
if(s.equals("/"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
temp=1;
ch='/';
}
else
{
y=0;x=0;
temp=Double.parseDouble(tfield.getText());
ch='/';
tfield.setText("");
}
tfield.requestFocus();
}
if(s.equals("*"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
temp=1;
ch='*';
}
else
{
y=0;x=0;
temp=Double.parseDouble(tfield.getText());
ch='*';
tfield.setText("");
}
tfield.requestFocus();
}
if(s.equals("MC"))
{
ml=0;
tfield.setText("");
}
if(s.equals("MR"))
{
tfield.setText("");
tfield.setText(tfield.getText()+ml);
}
if(s.equals("M+"))
{
if(k==1)
{
ml=Double.parseDouble(tfield.getText());
k++;
}
else
{
ml+=Double.parseDouble(tfield.getText());
tfield.setText(""+ml);
}
}
if(s.equals("M-"))
{
if(k==1)
{
ml=Double.parseDouble(tfield.getText());
k++;
}
else
{
ml-=Double.parseDouble(tfield.getText());
tfield.setText(""+ml);
}
}
if(s.equals("Sqrt"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{

a=Math.sqrt(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText()+a);

}
}
if(s.equals("SIN"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{

a=Math.sin(Double.parseDouble(tfield.getText()));
tfield.setText(""); tfield.setText(tfield.getText()
+a);
}
}
if(s.equals("COS"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{

a=Math.cos(Double.parseDouble(tfield.getText()));
tfield.setText(""); tfield.setText(tfield.getText()
+a);
}
}
if(s.equals("TAN"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{

a=Math.tan(Double.parseDouble(tfield.getText()));
tfield.setText(""); tfield.setText(tfield.getText()
+a);
}
}
if(s.equals("="))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{

temp1=Double.parseDouble(tfield.getText());
switch(ch)
{
case '+':
result=temp+temp1;
break;
case '-':
result=temp-temp1;
break;
case '/':
result=temp/temp1;
break;
case '*':
result=temp*temp1;
break;
}
tfield.setText("");
tfield.setText(tfield.getText()+result);
z=1;
}
}
if(s.equals("n!"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a=fact(Double.parseDouble(tfield.getText()));
tfield.setText("")
;
tfield.setText(tfield.getText()+a);
}
tfield.requestFocus();
}
}
double fact(double x)
{
/*inter=0;
if(x>0)
{
er=20;
return 0;
}*/
doublei,s=1;
for(i=2;i<=x;i+=1.0)
s*=i;
return s;
}
public static void main(String srgs[])
{
/*try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.Window
sLookAndFeel");
}
catch(Exception e)
{
/*/ ScientificCalculator f;
f=new ScientificCalculator();
f.setTitle("Scientific calculator");
f.pack();
f.setVisible(true);
/* } */
}
}
OUTPUT:

RESULT:

Thus the calculator program has been written and executed successfully.
EXPT: 12 MINIPROJECT (ONLINE QUIZ

SYSTEM) DATE:

OBJECTIVE:

It provides a common platform to connect student and teacher online.


The registered teacher can create Quiz and student can take quiz and can
asses himself/herself.

Users of the System

1. Teacher
2. Student

Functional Requirements
1. Teacher

1. Can create quiz after getting logged in!


2. Can enter subjects and enter question with it?s options and answer at
the time of creating quiz.
3. 10 Question for each quiz required to be completed.

2. Student

1. Can search quiz according to their interest.


2. Click on the id of quiz and ready to start it just clicking on a button.
3. After completing all questions, result will be diaplayedAutomatically.
4. Can view the description about each and every question in the
respective quiz.

Non-Functional Requirements

1. Secure access of confidential data (user?s details). SSL can be used.


2. 24 X 7 availability
3. Browser testing and support for IE, NN, Mozila, and Firefox
4. Reports exportable in .XLS, .PDF
5. Create a detailed UML diagram (Component, Sequence, Class) for
the system and its sub-components
User Interface Priorities

1. Professional look and feel


2. Use of AJAX atleast with all registration forms and with every
search option and at the id of each searched result with onmouseover
event.

Tools to be used

1. Use any IDE to develop the project. It may be Myeclipse /


Eclipse / Netbeanse.
2. Oracle 10g for the database.
3. Server: Apache Tomcat/JBoss/Glassfish/Weblogic/Websphere.

Front End and Back End

1. Front End: JSP, JDBC, Javascript, AJAX


2. Back End: Oracle

SOURCE PACKAGE:
packagecom.javatpoint;
importjavax.servlet.*;
importjava.sql.*;

public class MyListener implements

ServletContextListener{ public void

contextInitialized(ServletContextEvent arg0) {
Connection con=null;
try{
ResultSetrs;
Class.forName("oracle.jdbc.driver.OracleDriver");

con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe
","system","oracle");
PreparedStatement ps2= con.prepareStatement("CREATE
SEQUENCE JAVATPOINT MINVALUE 1 MAXVALUE 999999999
INCREMENT BY 1 START WITH 1 NOCACHE NOORDER NOCYCLE");
ps2.executeUpdate();
PreparedStatementps=con.prepareStatement("CREATE TABLE
QUIZCONTACT (NAME VARCHAR2(4000),EMAIL
VARCHAR2(4000),PHONE VARCHAR2(4000),MESSAGE
VARCHAR2(4000))");
ps.executeUpdate();
PreparedStatement ps4=con.prepareStatement("CREATE TABLE
QUIZINFO (SUBJECT VARCHAR2(4000),QUIZNAME
VARCHAR2(4000))");
ps4.executeUpdate();

PreparedStatement ps5=con.prepareStatement("CREATE TABLE


QUIZQ(NAME VARCHAR2(4000),EMAIL VARCHAR2(4000),PHONE
VARCHAR2(4000),QUESTION VARCHAR2(4000))");
ps5.executeUpdate();

ps5= con.prepareStatement("CREATE TABLE


QUIZQUES(QUESTION VARCHAR2(4000),OPTION1
VARCHAR2(4000),OPTION2 VARCHAR2(4000),OPTION3
VARCHAR2(4000),OPTION4 VARCHAR2(4000),ANSWER
VARCHAR2(4000),QUIZNAME VARCHAR2(4000),QID
VARCHAR2(4000),DESCRIPTION VARCHAR2(4000),CONSTRAINT
QUIZQUES_PK PRIMARY KEY (QID) ENABLE)");
ps5.executeUpdate();
ps5= con.prepareStatement("CREATE TABLE QUIZREGISTER
(USERNAME VARCHAR2(4000),USERPASS
VARCHAR2(4000),CATEGORY VARCHAR2(4000),EMAIL
VARCHAR2(4000))");
ps5.executeUpdate();
Statement stmt=con.createStatement();
stmt.executeUpdate("CREATE TRIGGER BI_QUIZINFO before
insert on QUIZINFO for each row begin select JAVATPOINT.nextval into
:NEW.QUIZNAME from dual;end");
stmt.executeUpdate("CREATE TRIGGER BI_QUIZQUES before
insert on QUIZQUES for each row begin select JAVATPOINT.nextval into
:NEW.QID from dual;end");
}
catch(Exception e){
e.printStackTrace();

}
}
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("project undeployed");

}
}
OUTPUT:

RESULT:

Thus the mini project has been executed successfully.


EXPT: 13 CONTENT BEYOND THE SYLLABUS
2-D SHAPES
DATE:

AIM:

To write a Java program to implement 2-D shapes .


ALGORITHM:

Step 1: Start the program


Step 2: Open the notepad and type the program and save the program as
Draw2DObjects.java
Step 3: Import graphics and geometric packages.
Step 4: Create the class Draw2DObjects. Define and declare its variables.
Step 5: Click on the Cmd Prompt cd C:\Java\jdk1.8\bin
Step 6: Now compile the Filedemo.java using
javacDraw2DObjects.java
Step 7: Execute the program using
javaDraw2DObjects

PROGRAM:

importjava.awt.Canvas;
importjava.awt.Graphics;
import java.awt.Graphics2D;
importjava.awt.Shape;
import java.awt.geom.Ellipse2D;
importjava.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;

importjavax.swing.JFrame;

public class Draw2DObjects extends JFrame {


Shape shapes[] = new Shape[5];
public static void main(String args[])
{ Draw2DObjects app = new
Draw2DObjects();
}

public Draw2DObjects() {
add("Center", new MyCanvas());
shapes[0] = new Line2D.Double(0.0, 0.0, 100.0, 100.0);
shapes[1] = new Rectangle2D.Double(10.0, 100.0, 200.0, 200.0);
shapes[2] = new Ellipse2D.Double(20.0, 200.0, 100.0, 100.0);
GeneralPath path = new GeneralPath(new Line2D.Double(300.0, 100.0,
400.0, 150.0));
path.append(new Line2D.Double(25.0, 175.0, 300.0, 100.0), true);
shapes[3] = path;
shapes[4] = new RoundRectangle2D.Double(350.0, 250, 200.0, 100.0,
50.0, 25.0);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

classMyCanvas extends Canvas


{ public void paint(Graphics graphics)
{
Graphics2D g = (Graphics2D) graphics;
for (int i = 0; i <shapes.length; ++i) {
if (shapes[i] != null)
g.draw(shapes[i]);
}
}
}
}
OUTPUT:

RESULT:

Thus the2-D shapes programhas been written and executed successfully.

You might also like