CS8383-OBJECT ORIENTED PROGRAMMING LABORATORY 2017-lab manual
CS8383-OBJECT ORIENTED PROGRAMMING LABORATORY 2017-lab manual
0 04 2
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
101-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.
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.
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
OUTCOMES
Upon completion of the course, the students will be able to
Develop and implement Java programs for simple applications that make use of classes,
packages and interfaces.
Develop and implement Java programs with arraylist, exception handling and
multithreading.
Design applications using file processing, generic programming and event handling.
GENERATING ELECTRICITY BILL
AIM:
ALGORITHM:
PROGRAM:
import java.io.*;
import java.util.*;
class ElectricityBill
{
double bill;
double domesticbillcalc (int units)
{
if(units<100)
bill = units * 1 ;
else if(units <= 200)
bill = 100 * 1 + (units - 100) * 2.50 ;
else if(units <= 500)
bill = 100 * 1 + 200 * 2.50 + (units - 200) * 4 ;
else
bill = 100 * 1 + 200 * 2.50 + 500 * 4 + (units - 500) * 6 ;
return bill;
}
double commercialbillcalc (int units)
{
if(units<100)
bill = units * 2 ;
else if(units <= 200)
bill = 100 * 1 + (units - 100) * 4.50 ;
else if(units <= 500)
bill = 100 * 1 + 200 * 4.50 + (units - 200) * 6 ;
else
bill = 100 * 1 + 200 * 4.50 + 500 * 6 + (units - 500) * 7 ;
return bill;
}
void show(String ptype,String consno,String consname,int pmr,int cmr,int units)
{
System.out.println("Type of Connection : " + ptype);
System.out.println("Consumer Number : " + consno);
System.out.println("Customer Name : " + consname);
System.out.println("Previous Month Reading : " + pmr);
System.out.println("Current Month Reading : " + cmr);
System.out.println("Units Consumed : " + units);
}
}
class Ebbill
{
public static void main(String[] args)
{
Scanner c = new Scanner(System.in);
System.out.println("Enter the Type of Connection :");
String ptype=c.next();
System.out.println("Enter the Consumer Number :");
String consno=c.next();
System.out.println("Enter the Consumer Name :");
String consname=c.next();
System.out.println("Enter the Previous Month Reading :");
int pmr=c.nextInt();
System.out.println("Enter the Current Month Reading :");
int cmr=c.nextInt();
int units = cmr-pmr;
ElectricityBill b = new ElectricityBill();
if(ptype.equalsIgnoreCase("DOMESTIC"))
{
b.show(ptype,consno,consname,pmr,cmr,units);
b.domesticbillcalc(units);
System.out.println("Bill to pay : " + b.bill);
}
else if(ptype.equalsIgnoreCase("COMMERCIAL"))
{
b.show(ptype,consno,consname,pmr,cmr,units);
b.commercialbillcalc(units);
System.out.println("Bill to pay : " + b.bill);
}
}
}
NOTE:
To Compile,
javac Ebbill.java
To Run
java Ebbill
OUTPUT:
RESULT:
Thus the application for generating Electricity bill has been successfully executed.
Viva questions:
1. How to calculate electricity bill?
2. How to calculate the Previous Month Reading?
3. How to calculate the Current Month Reading?
4. How to calculate the domesticbill?
5. How to calculate the commercialbill?
CURRENCY CONVERTER, DISTANCE CONVERTER AND TIME CONVERTER
USING PACKAGES
AIM:
ALGORITHM:
PROGRAM:
package CurrencyConverter;
public class CurrencyConverter
{
public double dollortoinr(double x)
{
double inr=x*67.86;
return inr;
}
public double inrtodollor(double x)
{
double dollor=x/67.86;
return dollor;
}
public double eurotoinr(double x)
{
double inr=x*79.18;
return inr;
}
public double inrtoeuro(double x)
{
double euro=x/79.18;
return euro;
}
public double yentoinr(double x)
{
double inr=x*0.62;
return inr;
}
public double inrtoyen(double x)
{
double yen=x/0.62;
return yen;
}
}
package DistanceConverter;
public class DistanceConverter
{
public double metertokm(double x)
{
double km=x*0.001;
return km;
}
public double kmtometer(double x)
{
double meter=x/0.001;
return meter;
}
public double milestokm(double x)
{
double km=x*1.60934;
return km;
}
public double kmtomiles(double x)
{
double miles=x/1.60394;
return miles;
}
}
package TimeConverter;
public class TimeConverter
{
public double hourstominutes(double x)
{
double minutes=x*60;
return minutes;
}
public double minutestohours(double x)
{
double hours=x/60;
return hours;
}
public double hourstoseconds(double x)
{
double seconds=x*3600;
return seconds;
}
public double secondstohours(double x)
{
double hours=x/3600;
return hours;
}
}
//File Name should be Converter.java separate this file from above 3 folders
import CurrencyConverter.*;
import DistanceConverter.*;
import TimeConverter.*;
import java.io.*;
import java.util.*;
class Converter
{
public static void main(String args[])
{
System.out.println("1.CurrencyConverter");
System.out.println("2.DistanceConverter");
System.out.println("3.TimeConverter");
Converter cr = new Converter();
Scanner c = new Scanner(System.in);
int choice = c.nextInt();
String op = null;
switch(choice)
{
case 1: cr.Currency(); break;
case 2: cr.Distance(); break;
case 3: cr.Time(); break;
default:
System.out.println("Invalid case");
return;
}
}
NOTE:
To Compile,
javac Converter.java
To Run
java Converter
OUTPUT:
RESULT:
Thus the application for currency converter, distance converter and time converter
using packages has been successfully executed.
Viva questions:
1. How to create a package in java?
2. How to Create a class CurrencyConverter inside a package name CurrencyConverter?
3. How to Create a class DistanceConverter inside a package name DistanceConverter?
4. How to Create object for a class in memory?
5. How to Create a class TimeConverter inside a package name TimeConverter
GENERATING EMPLOYEE PAYROLL DETAILS
AIM:
To develop a java application for generating pay slips of employees with their gross
and net salary.
ALGORITHM:
PROGRAM:
package employee;
public class Employee
{
private String name;
private String id;
private String address;
private String mailId;
private String mobileNo;
public Employee(String name, String id, String address, String mailId, String mobileNo)
{
this.name= name;
this.id= id;
this.address= address;
this.mailId= mailId;
this.mobileNo= mobileNo;
}
public void display()
{
System.out.println("Emp_Name : "+ name + "\t" + "Emp_id : "+ id);
System.out.println("Address : " + address);
System.out.println("Mail_id : "+ mailId + "\t" + "Mobile_no : " + mobileNo);
}
public void paySlip()
{
}
}
package employee;
public class Programmer extends Employee
{
private float bPay;
private String des;
public Programmer(String name, String id, String address, String mailId, String mobileNo,
float bPay, String des)
{
super(name, id, address, mailId, mobileNo);
this.bPay= bPay;
this.des= des;
}
public void paySlip()
{
float da=bPay*97/100;
float hra=bPay*10/100;
double grossSalary=bPay + da + hra;
float pf=bPay*12/100;
double scf=bPay*0.1/100;
double netSalary=grossSalary - pf - scf;
System.out.println("------------ Employees Pay Slips ------------");
super.display();
System.out.println("Designation: "+des);
System.out.println("Basic_Pay: "+bPay);
System.out.println("Gross Salary : "+ grossSalary + "\t" + "Net Salary : " + netSalary);
System.out.println("------------ End of the Statements -----------");
}
}
//For Packages, Folder Name should be employee
// File Name should be AssistantProfessor.java
package employee;
public class AssistantProfessor extends Employee
{
private float bPay;
private String des;
public AssistantProfessor(String name, String id, String address, String mailId, String
mobileNo, float bPay, String des)
{
super(name, id, address, mailId, mobileNo);
this.bPay= bPay;
this.des= des;
}
public void paySlip()
{
float da=bPay*97/100;
float hra=bPay*10/100;
double grossSalary=bPay + da + hra;
float pf=bPay*12/100;
double scf=bPay*0.1/100;
double netSalary=grossSalary - pf - scf;
System.out.println("------------ Employees Pay Slips ------------");
super.display();
System.out.println("Designation: "+des);
System.out.println("Basic_Pay: "+bPay);
System.out.println("Gross Salary : "+ grossSalary + "\t" + "Net Salary : " + netSalary);
System.out.println("------------ End of the Statements -----------");
}
}
package employee;
public class AssociateProfessor extends Employee
{
private float bPay;
private String des;
public AssociateProfessor(String name, String id, String address, String mailId, String
mobileNo, float bPay, String des)
{
super(name, id, address, mailId, mobileNo);
this.bPay= bPay;
this.des= des;
}
public void paySlip()
{
float da=bPay*97/100;
float hra=bPay*10/100;
double grossSalary=bPay + da + hra;
float pf=bPay*12/100;
double scf=bPay*0.1/100;
double netSalary=grossSalary - pf - scf;
System.out.println("------------ Employees Pay Slips ------------");
super.display();
System.out.println("Designation: "+des);
System.out.println("Basic_Pay: "+bPay);
System.out.println("Gross Salary : "+ grossSalary + "\t" + "Net Salary : " + netSalary);
System.out.println("------------ End of the Statements -----------");
}
}
package employee;
public class Professor extends Employee
{
private float bPay;
private String des;
public Professor(String name, String id, String address, String mailId, String mobileNo, float
bPay, String des)
{
super(name, id, address, mailId, mobileNo);
this.bPay= bPay;
this.des= des;
}
public void paySlip()
{
float da=bPay*97/100;
float hra=bPay*10/100;
double grossSalary=bPay + da + hra;
float pf=bPay*12/100;
double scf=bPay*0.1/100;
double netSalary=grossSalary - pf - scf;
System.out.println("------------ Employees Pay Slips ------------");
super.display();
System.out.println("Designation: "+des);
System.out.println("Basic_Pay: "+bPay);
System.out.println("Gross Salary : "+ grossSalary + "\t" + "Net Salary : " + netSalary);
System.out.println("------------ End of the Statements -----------");
}
}
//File Name should be Emp.java separate this file from above folder
import employee.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Emp
{
Employee e;
ArrayList<Employee> obj= new ArrayList<>();
Scanner get= new Scanner(System.in);
public void addEmployee()
{
System.out.println("Enter the Emp_Name:");
String name = get.next();
System.out.println("Enter the Emp_id:");
String id = get.next();
System.out.println("Enter the Address:");
String address = get.next();
System.out.println("Enter the Mail_id:");
String mailId = get.next();
System.out.println("Enter the Mobile_no:");
String mobileNo = get.next();
System.out.println("Enter the Designation:");
String des = get.next();
System.out.println("Enter the Basic_Pay:");
float bPay = get.nextFloat();
if(des.equalsIgnoreCase("Programmer"))
{
e= new Programmer(name, id, address, mailId, mobileNo, bPay, des);
obj.add(e);
}
else if(des.equalsIgnoreCase("AssistantProfessor"))
{
e= new AssistantProfessor(name, id, address, mailId, mobileNo, bPay, des);
obj.add(e);
}
else if(des.equalsIgnoreCase("AssociateProfessor"))
{
e= new AssociateProfessor(name, id, address, mailId, mobileNo, bPay, des);
obj.add(e);
}
else if(des.equalsIgnoreCase("Professor"))
{
e= new Professor(name, id, address, mailId, mobileNo, bPay, des);
obj.add(e);
}
}
public void displayEmployee()
{
for(Employee e:obj)
{
e.paySlip();
}
}
public static void main(String args[]) throws IOException
{
Emp x= new Emp();
String check;
do
{
x.addEmployee();
System.out.println("Do you wnat continue press 'y'");
check=x.get.next();
}
while(check.equalsIgnoreCase("y"));
x.displayEmployee();
}
}
NOTE:
D:\>javac Emp.java
D:\>java Emp
Enter the Emp_Name:
Suresh
Enter the Emp_id:
E708
Enter the Address:
cuddalore
Enter the Mail_id:
[email protected]
Enter the Mobile_no:
7894561230
Enter the Designation:
Programmer
Enter the Basic_Pay:
7500
Do you wnat continue press 'y'
y
Enter the Emp_Name:
Rakesh
Enter the Emp_id:
E705
Enter the Address:
pondy
Enter the Mail_id:
[email protected]
Enter the Mobile_no:
4567891230
Enter the Designation:
Professor
Enter the Basic_Pay:
15000
Do you wnat continue press 'y'
y
Enter the Emp_Name:
kumar
Enter the Emp_id:
E405
Enter the Address:
madurai
Enter the Mail_id:
[email protected]
Enter the Mobile_no:
1237894560
Enter the Designation:
AssistantProfessor
Enter the Basic_Pay:
18000
Do you wnat continue press 'y'
y
Enter the Emp_Name:
Naresh
Enter the Emp_id:
E102
Enter the Address:
villupuram
Enter the Mail_id:
[email protected]
Enter the Mobile_no:
9873214560
Enter the Designation:
AssociateProfessor
Enter the Basic_Pay:
20000
Do you wnat continue press 'y'
n
------------ Employees Pay Slips ------------
Emp_Name : Suresh Emp_id : E708
Address : cuddalore
Mail_id : [email protected] Mobile_no : 7894561230
Designation: Programmer
Basic_Pay: 7500.0
Gross Salary : 15525.0 Net Salary : 14617.5
------------ End of the Statements -----------
------------ Employees Pay Slips ------------
Emp_Name : Rakesh Emp_id : E705
Address : pondy
Mail_id : [email protected] Mobile_no : 4567891230
Designation: Professor
Basic_Pay: 15000.0
Gross Salary : 31050.0 Net Salary : 29235.0
------------ End of the Statements -----------
------------ Employees Pay Slips ------------
Emp_Name : kumar Emp_id : E405
Address : madurai
Mail_id : [email protected] Mobile_no : 1237894560
Designation: AssistantProfessor
Basic_Pay: 18000.0
Gross Salary : 37260.0 Net Salary : 35082.0
------------ End of the Statements -----------
------------ Employees Pay Slips ------------
Emp_Name : Naresh Emp_id : E102
Address : villupuram
Mail_id : [email protected] Mobile_no : 9873214560
Designation: AssociateProfessor
Basic_Pay: 20000.0
Gross Salary : 41400.0 Net Salary : 38980.0
------------ End of the Statements -----------
D:\>
RESULT:
Thus the application for generating pay slips of employees with their gross and net
salary has been successfully executed.
Viva questions:
1. How to create a package in java?
2. How to Create a class Employee inside a package name employee?
3. How to Create classes Programmer?
4. How to initialize the instance variable of Employee class?
5. How to Compile the outside package?
DESIGN A JAVA INTERFACE FOR ADT STACK
AIM:
To Design a Java interface for ADT Stack and implement this interface using array,
provide necessary exception handling in the implementation.
ALGORITHM:
PROGRAM:
import java.util.Scanner;
interface MyStack
{
public void pop();
public void push();
public void display();
}
class StackAdt
{
public static void main(String arg[])
{
Scanner in= new Scanner(System.in);
System.out.println("Implementation of Stack using Array");
StackArray stk=new StackArray();
int ch=0;
do
{
System.out.println("1.Push 2.Pop 3.Display 4.Exit");
System.out.println("Enter your choice:");
ch=in.nextInt();
switch(ch)
{
case 1:
stk.push();
break;
case 2:
stk.pop();
break;
case 3:
stk.display();
break;
case 4:
System.exit(0);
}
}
while(ch<4);
}
}
NOTE:
To Compile,
javac StackAdt.java
To Run
java StackAdt
OUTPUT:
D:\>javac StackAdt.java
D:\>java StackAdt
Implementation of Stack using Array
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:
1
Enter the element
10
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:
1
Enter the element
20
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:
1
Enter the element
30
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:
1
Enter the element
45
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:
1
Enter the element
55
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:
1
Stack Overflow
1.Push 2.Pop 3.Display 4.Exit
Enter your choice:
3
Elements are: 10 --> 20 --> 30 --> 45 --> 55 -->
RESULT:
Thus the Implementation for ADTStack interface using array has been successfully
executed.
Viva questions:
1. How will you Import the java packages?
2. How will you a Create a class StackAdt and object for a class StackArray in memory?
3. How will you Create a class StackAdt and object for a class StackArray in memory?
4. How will you define a class StackArray to implement the MyStack using array.?
5. How will you define the functions of the interface accordingly and handle the stack overflow
and underflow exceptions?
STRING OPERATIONS USING ARRAYLIST
AIM:
ALGORITHM:
PROGRAM:
import java.util.*;
public class ArrayList
{
public static void main(String args[])
{
ArrayList<String> obj = new ArrayList<String>();
System.out.println("\n"+"Elements in ArrayList:");
System.out.print("\t"+obj+" "+"\n" + "\n");
/* Insert - add at particular index */
obj.add(0, "Arujun");
obj.add(1, "Barath");
if (ans)
System.out.println("\t"+"ArrayList contains" +searchStr+ "\n");
else
System.out.println("ArrayList does not contains "+searchStr);
To Compile:
javac ArrayList.java
To Run:
java ArrayList
OUTPUT:
RESULT:
Thus the Implementation for string operations using ArrayList has been successfully
executed.
Viva questions:
1. How to Import the java packages.?
2. How will you define a class ArrayList?
3. How will Create an object for ArrayList to add string elements?
4. How will perform the Append - add at end functions?
5. How will perform the Insert - add at particular index?
FINDING THE AREA OF DIFFERENT SHAPES
AIM:
To write a java program to find the area of different shapes by using abstract class.
ALGORITHM:
PROGRAM:
import java.io.*;
import java.util.*;
NOTE:
To Compile:
javac Area.java
To Run:
java Area
OUTPUT:
RESULT:
Thus the Implementation for finding the area of different shapes using abstract class
has been successfully executed.
Viva questions:
1.How will you Import the java packages?
2. How will you Create an abstract class name?
3. How will you Create a class Rectangle, Triangle, Circle?
4. How will you get the input during runtime?
5. . How will you Create object for a class in memory?
CREATING OWN EXCEPTIONS
AIM:
ALGORITHM:
PROGRAM:
import java.io.*;
import java.util.*;
class UserException
{
static void compute(int a) throws MyException
{
System.out.println ("Called Compute(" + a + ")");
if(a>10)
throw new MyException(a);
System.out.println ("Normal Exit");
}
public static void main(String args[])
{
try
{
compute(1);
compute(20);
}
catch(MyException e)
{
System.out.println("Caught " + e);
}
}
}
NOTE:
To Compile:
javac UserException.java
To Run:
java UserException
OUTPUT:
RESULT:
Thus the Implementation for user defined exception handling has been successfully
executed.
Viva questions:
1. How will you Import the java packages.?
2. How to Create a subclass of Exception named as MyException?
3. When the exception is thrown?
4. How the main ( ) method sets up an exception handler?
5. Define exception handling in java?
GETTING FILE INFORMATION
AIM:
To write a java program to implement file information such as 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.
ALGORITHM:
PROGRAM:
import java.io.*;
import java. util.*;
public class FileInfo
{
public static void main(String[] args) throws IOException
{
Scanner in=new Scanner(System.in);
if(f.exists())
{
result = f.canRead() ? "readable." : "not readable.";
System.out.println("\nThe file is " + result);
NOTE:
To Compile:
javac FileInfo.java
To Run:
java FileInfo
OUTPUT:
RESULT:
Thus the Implementation for getting file information has been successfully executed.
Viva questions:
1. How will you Import the java packages.?
2. How to write a java program to implement file information?
3. How to create a File object associated with the file?
4. How the the program uses conditional operator to check different functionalities of
the given file.?
5. How will you get the input during runtime?
MULTI THREADED APPLICATION
AIM:
ALGORITHM:
PROGRAM:
import java.util.*;
NOTE:
To Compile:
javac Multithread.java
To Run:
java Multithread
OUTPUT:
RESULT:
Thus the Implementation for application for multithreading has been successfully
executed.
Viva questions:
1. How will you Import the java packages?
2. How to Create a thread?
3. How the thread generates a random integer?
4. How the thread computes the square of the number?
5. How the thread will print the value of cube of the number?
GENERIC PROGRAMMING
AIM:
To write a java program to find the maximum value from the given type of elements
using a generic function.
ALGORITHM:
PROGRAM:
import java.util.*;
class MyGeneric {
public static <T extends Comparable<T>> T max(T... elements)
{
T max = elements[0];
for (T element : elements) {
if (element.compareTo(max) > 0)
{
max = element;
}
}
return max;
}
NOTE:
To Compile:
javac MyGeneric.java
To Run:
java MyGeneric
OUTPUT:
RESULT:
Thus the Implementation for finding the maximum value from the given type of
elements using a generic function has been successfully executed.
Viva questions:
1. How will you Import the java packages?
2. How will you the compare To() method works?
3. How will you Create a generic method max()?
4. How will you call the generic method?
5. How the compiler handles each method.
EVENT - DRIVEN PROGRAMMING
AIM:
ALGORITHM:
PROGRAM:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class ScientificCalculator extends JFrame implements ActionListener
{
JTextField tfield;
double temp, temp1, result, a;
static double m1, m2;
int k = 1, x = 0, y = 0, z = 0;
char ch;
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, zero, clr, pow2, exp, plus, min, div, log, rec,
mul, eq, dot, sqrt, sin, cos, tan;
Container cont;
JPanel textPanel, buttonpanel;
ScientificCalculator()
{
cont = getContentPane();
cont.setLayout(new BorderLayout());
JPanel textpanel = new JPanel();
tfield = new JTextField(25);
tfield.setHorizontalAlignment(SwingConstants.RIGHT);
tfield.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent keyevent) {
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;
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);
eq = new JButton("=");
buttonpanel.add(eq);
eq.addActionListener(this);
cont.add("Center", buttonpanel);
cont.add("North", textpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
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(".")) {
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 {
x = 0;
y = 0;
temp = Double.parseDouble(tfield.getText());
tfield.setText("");
ch = '-';
}
tfield.requestFocus();
}
if (s.equals("/")) {
if (tfield.getText().equals("")) {
tfield.setText("");
temp = 1;
ch = '/';
} else {
x = 0;
y = 0;
temp = Double.parseDouble(tfield.getText());
ch = '/';
tfield.setText("");
}
tfield.requestFocus();
}
if (s.equals("*")) {
if (tfield.getText().equals("")) {
tfield.setText("");
temp = 1;
ch = '*';
} else {
x = 0;
y = 0;
temp = Double.parseDouble(tfield.getText());
ch = '*';
tfield.setText("");
}
tfield.requestFocus();
}
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;
}
}
tfield.requestFocus();
}
NOTE:
To Compile:
javac ScientificCalculator.java
To Run:
java ScientificCalculator
OUTPUT:
Tan 45 Log 2
RESULT:
Thus the Implementation for designing the scientific calculator has been successfully
executed.
Viva questions:
1. How will you Import the java packages?
2. How will you Create the class calculator?
3. How will you Declare the buttons?
4. How will you Design the layout of the calculator?
5. How will you Pass the parameters for the methods?
MINI PROJECT - OPAC SYSTEM
AIM:
To develop a mini project OPAC system for library using Java concepts.
ALGORITHM:
PROGRAM:
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Data extends JFrame implements ActionListener
{
JTextField id;
JTextField name;
JButton next;
JButton addnew;
JPanel p;
static ResultSet res;
static Connection conn;
static Statement stat;
public Data()
{
super("My Application");
Container c = getContentPane();
c.setLayout(new GridLayout(5,1));
id = new JTextField(20);
name = new JTextField(20);
next = new JButton("Next BOOK");
p = new JPanel();
p.add(next);
next.addActionListener(this);
pack();
setVisible(true);
addWindowListener(new WIN());
}
NOTE:
Create a new Database
1. Create a new Database file in MS ACCESS (our backend) named “books.mdb”.
2. Then create a table named “stu” in it.
3. The table stu contains the following fields and data types
i. ISBN - Text
ii. BookName - Text
4. Enter various records as you wish.
5. Save the database file.
Next step is to add our “books.mdb” to the System DSN. To do that follows the procedure
given below,
i. Go to Start-> Control Panel -> Administrative tools.
ii. In that double click “Data Sources (ODBC)”.
iii. ODBC Data Source Administrator dialog appears.
iv. In that select “System DSN” tab and click the Add Button.
v. Select “Microsoft Access Driver(*.mdb)” and click Finish.
vi. ODBC Microsoft Access Setup appears. In the “Data Source name” type “stu”.
vii. Click on the “Select” button and choose your database file. Then click ok.
To Compile:
javac Data.java
To Run:
java Data
RESULT:
Thus the program to develop the simple OPAC for the libraries is executed
successfully.
Viva questions:
1.How will you Import the awt,swing packages.?
2. How will you Extend the JFrame?
3. How will you Create the textfield?
4. How will you Create object?
5. How will you Assign the length and breadth value for the layout?