Professional Documents
Culture Documents
OOP Aim and Program
OOP Aim and Program
Aim:
To develop a Java application to generate Electricity bill.
Procedure:
4. If the type of the EB connection is commercial, calculate the amount to be paid as follows:
Input program:
import java.util.*;
class calculateBill
{
int cno;
String cname;
long units,prev,cur;
String type;
double billpay;
void get_det()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter previous month reading");
prev=sc.nextLong();
System.out.println("enter current month reading");
cur=sc.nextLong();
System.out.println("enter type of EB connection(domestic,commercial):");
type=sc.next();
System.out.println("enter customer no:");
cno=sc.nextInt();
System.out.println("enter customer name:");
cname=sc.next();
units=cur-prev;
if(type.equalsIgnoreCase("domestic"))
Bill_dom(units);
else
Bill_com(units);
display();
}
void display()
{
System.out.println("Name:"+cname);
System.out.println("number:"+cno);
System.out.println("Bill to pay:"+billpay);
}
void Bill_dom(long units)
{
if(units<100)
billpay=units*1;
else if(units<200)
billpay=100*1+(units-100)*2.50;
else if(units<500)
billpay=100*1+100*2.50+(units-300)*4;
else
billpay=100*1+100*2.50+300*4+(units-500)*6;
}
void Bill_com(long units)
{
if(units<100)
billpay=units*2;
else if(units<200)
billpay=100*2+(units-100)*4.50;
else if(units<500)
billpay=100*2+100*4.50+(units-300)*6;
else
billpay=100*2+100*4.50+300*6+(units-500)*7;
}
}
class computebill
{
public static void main(String args[])
{
calculateBill obj=new calculateBill();
obj.get_det();
}
}
Result:
Aim:
To develop a Java application to implement currency converter , distance converter and time converter using
packages.
Procedure:
1. Start the program
2. Create three packages for currency converter , distance converter and time converter.
3. Create corresponding code for conversion
4. Print the converted value.
5. Stop the program
Input program :
CURRENCY CONVERTER:
package ConversionDemo;
public class CurrencyConverter
{
double ER=0;
public CurrencyConverter(double CurrentExchange)
{
ER=CurrentExchange;
}
public double DollarToINR(double Dollars)
{
double INR=0;
INR=Dollars*ER;
return INR;
}
public double INRToDollar(double INR)
{
double Dollars=0;
Dollars=INR/ER;
return Dollars;
}
public double EuroToINR(double Euros)
{
double INR=0;
INR=Euros*ER;
return INR;
}
public double INRToEuro(double INR)
{
double Euros=0;
Euros=INR/ER;
return INR;
}
public double YenToINR(double Yens)
{
double INR=0;
INR=Yens*ER;
return INR;
}
public double INRToYen(double INR)
{
double Yens=0;
Yens=INR/ER;
return Yens;
}
}
DISTANCE CONVERTER:
package ConversionDemo;
public class DistanceConverter
{
public double MeterToKM(double Meters)
{
double KM=0;
KM=Meters/1000;
return KM;
}
public double KMToMeter(double KM)
{
double Meters=0;
Meters=KM*1000;
return Meters;
}
public double MileToKM(double Miles)
{
double KM=0;
KM=Miles/0.621371;
return KM;
}
public double KMToMile(double KM)
{
double Miles=0;
Miles=KM*0.621371;
return Miles;
}
}
TIME CONVERTER:
package ConversionDemo;
public class TimeConverter
{
public double HrToMin(double Hours)
{
double Minutes=0;
Minutes=Hours*60;
return Minutes;
}
public double MinToHour(double Minutes)
{
double Hours=0;
Hours=Minutes/60;
return Hours;
}
public double HrToSec(double Hours)
{
double Seconds=0;
Seconds=Hours*3600;
return Seconds;
}
USING PACKAGE:
import ConversionDemo.CurrencyConverter;
import ConversionDemo.DistanceConverter;
import ConversionDemo.TimeConverter;
import java.util.Scanner;
class Converter
{
public static void main(String[] args)throws NoClassDefFoundError
{
double CurrentExchange;
int choice,choice1,choice2,choice3;
double inr;
double km;
double hr;
char ans='y';
do
{
System.out.println("\n Main Menu");
System.out.println("\n1Currency Converter \n2.Distance Converter \n3.Time Converter");
System.out.println("Enter your choice:");
Scanner input =new Scanner(System.in);
choice=input.nextInt();
switch(choice)
{
case 1:
System.out.println("\tCurrency Conversion");
do
{
System.out.println("Menu For Currency Conversion");
System.out.println("1.Dollar to INR");
System.out.println("2.INR to Dollar");
System.out.println("3.Euro to INR");
System.out.println("4.INR to Euro");
System.out.println("5.Yen to INR");
System.out.println("6.INR to Yen");
System.out.println("Enter your choice:");
choice1=input.nextInt();
System.out.println("Please enter the current exchange rate:");
CurrentExchange=input.nextDouble();
CurrencyConverter cc=new CurrencyConverter(CurrentExchange);
switch(choice1)
{
case 1:
System.out.print("Enter Dollars:");
dollar dollar=input.nextDouble();
System.out.println(inr +"Rs.are converted to "+cc.DollarToINR(dollar)+"Rs.");
break;
case 2:
System.out.print("Enter INR:");
inr=input.nextDouble();
System.out.println(inr+"Rs.are converted to "+cc.INRToDollar(inr)+"Dollars");
break;
case 3:
System.out.print("Enter Euro:");
dollar euro=input.nextDouble();
System.out.println(euro +"Euros are converted to "+cc.EuroToINR(euro)+"Rs.");
break;
case 4:
System.out.print("Enter INR:");
inr=input.nextDouble();
System.out.println(inr+"Rs.are converted to "+cc.INRToEuro(inr)+"Euros");
break;
case 5:
System.out.print("Enter Yen:");
dollar yen=input.nextDouble();
System.out.println(yen +"Yens are converted to "+cc.YenToINR(yen)+"Rs.");
break;case 6:
System.out.print("Enter INR:");
inr=input.nextDouble();
System.out.println(inr+"Rs.are converted to "+cc.INRToYen(inr)+"Yens");
break;
}
System.out.println("Do you want to go to Currency Conversion Menu?(y/n)");
ans=input.next().charAt(0);
}while(ans=='y');
break;
case 2:System.out.println("\tDistance Conversion");
do
{
System.out.println(" Menu For Distance Conversion");
System.out.println("1.Meter to Km");
System.out.println("2.Km to Meter");
System.out.println("3.Miles to Km");
System.out.println("4.Km to Miles");
System.out.println("Enter your choice:");
choice2=input.nextInt();
DistanceConverter dc=new DistanceConverter();
switch(choice2)
{
case 1:
System.out.print("Enter meters to Km:");
double meter=input.nextDouble();
System.out.println(meter+"Meters are converted to "+dc.MeterToKm(meter)+"Km.");
break;
case 2:
System.out.print("Enter Km to convert to meters:");
km=input.nextDouble();
System.out.println(km+"Km. are converted meters"+dc.KMToMeters(km)+"Meters");
break;
case 3:
System.out.print("Enter Miles to convert to Km:");
double miles=input.nextDouble();
System.out.println(miles+"Miles are converted to "+dc.MileToKM(miles)+"Km.");
break;
case 4:
System.out.print("Enter Km to convert to miles:");
km=input.nextDouble();
System.out.println(km+"Km. are converted miles"+dc.KMToMile(km)+"Miles");
break;
}
System.out.println("Do you want to go to Distance Conversion Menu?(y/n)");
ans=input.next().charAt(0);
}while(ans=='y');
break;
case 3:
System.out.println("\tTime Conversion");
do
{
System.out.println(" Menu For Time Conversion");
System.out.println("1.Hour to Minutes");
System.out.println("2.Minutes to Hour ");
System.out.println("3.Hour to Seconds");
System.out.println("4.Seconds to Hour");
System.out.println("Enter your choice:");
choice3=input.nextInt();
TimeConverter tc=new TimeConverter();
switch(choice3)
{
case 1:
System.out.print("Enter hours to convert to Minutes:");
hr=input.nextDouble();
System.out.println(hr+"Hours. are converted to"+tc.HrToMin(hr)+"min.");
break;
case 2:
System.out.print("Enter Minutes to convert to hours:");
double minutes=input.nextDouble();
System.out.println(minutes+"Minutes. are converted to "+tc.MinToHour(minutes)+"Hours");
break;
case 3:
System.out.print("Enter Hours to convert to Seconds:");
hr=input.nextDouble();
System.out.println(hr+"Hours. are converted to"+tc.HrToSec(hr)+"Seconds.");
break;
case 4:
System.out.print("Enter Seconds to convert to hours:");
double seconds=input.nextDouble();
System.out.println(seconds+"Seconds. are converted hours"+tc.SecToHour(seconds)+"Hours");
break;
}
System.out.println("Do you want to go to Time Conversion Menu?(y/n)");
ans=input.next().charAt(0);
}
while(ans=='y');
break;
}
System.out.println("Do you want to go back to Main Menu?(y/n)");
ans=input.next().charAt(0);
}while(ans=='y');
}
}
Result:
Thus the Java application has been created for currency conversion, distance conversion and time conversion and
it was successfully executed.
Ex. No.:3
PAYROLL PROCESSING
Aim:
To develop a Java application with employee class and generate pay slips for the employees with their gross and
net salary.
Procedure:
5. Generate pay slips for the employees with their gross and net salary.
6. Stop the program
Program:
import java.util.Scanner;
class Employee
{
int id;
String name,address,mail,mno;
Scanner get=new Scanner(System.in);
Employee()
{
System.out.println("Enter Name of the Employee:");
name=get.nextLine();
System.out.println("Enter mailid of the Employee:");
mail=get.nextLine();
System.out.println("Enter Address of the Employee:");
address=get.nextLine();
System.out.println("Enter mobile no.of the Employee:");
mno=get.nextLine();
System.out.println("Enter id:");
id=get.nextInt();
}
void display()
{
System.out.println("Employee Name:"+name);
System.out.println("ID:"+id);
System.out.println("Mail Id:"+mail);
System.out.println("Address:"+address);
System.out.println("Mobile No.:"+mno);
}
}
class Programmer extends Employee
{
float grosssalary,netsalary;
float bpay;
Programmer()
{
System.out.println("Enter Basic Pay:");
bpay=get.nextFloat();
grosssalary=(0.97f*bpay)+(0.10f*bpay)+(0.12f*bpay)+(0.001f*bpay)+bpay;
netsalary=(0.97f*bpay)+(0.10f*bpay)+bpay;
}
void display()
{
System.out.println("========="+"\n"+"Programmer"+"\n"+"========"+"\n");
super.display();
System.out.println("Gross Salary:"+grosssalary);
System.out.println("Net Salary:"+netsalary);
}
}
class AssistantProfessor extends Employee
{
float grosssalary,netsalary;
float bpay;
AssistantProfessor()
{
System.out.println("Enter Basic Pay:");
bpay=get.nextFloat();
grosssalary=(0.97f*bpay)+(0.10f*bpay)+(0.12f*bpay)+(0.001f*bpay)+bpay;
netsalary=(0.97f*bpay)+(0.10f*bpay)+bpay;
}
void display()
{
System.out.println("========"+"\n"+"AssistantProfessor"+"\n"+"========"+"\n");
super.display();
System.out.println("Gross Salary:"+grosssalary);
System.out.println("Net Salary:"+netsalary);
}
}
class AssociateProfessor extends Employee
{
float grosssalary,netsalary;
float bpay;
AssociateProfessor()
{
System.out.println("Enter Basic pay:");
bpay=get.nextFloat();
grosssalary=(0.97f*bpay)+(0.10f*bpay)+(0.12f*bpay)+(0.001f*bpay)+bpay;
netsalary=(0.97f*bpay)+(0.10f*bpay)+bpay;
}
void display()
{
System.out.println("========"+"\n"+"AssociateProfessor"+"\n"+"========="+"\n");
super.display();
System.out.println("Gross Salary:"+grosssalary);
System.out.println("Net Salary:"+netsalary);
}
}
class Professor extends Employee
{
float grosssalary,netsalary;
float bpay;
Professor()
{
System.out.println("Enter Basic pay:");
bpay=get.nextFloat();
grosssalary=(0.97f*bpay)+(0.10f*bpay)+(0.12f*bpay)+(0.001f*bpay)+bpay;
netsalary=(0.97f*bpay)+(0.10f*bpay)+bpay;
}
void display()
{
System.out.println("==============="+"\n"+"Professor"+"\n"+"===============
====="+"\n");
super.display();
System.out.println("Gross Salary:"+grosssalary);
System.out.println("Net Salary:"+netsalary);
}
}
class Employees
{
public static void main(String args[])
{
System.out.println("=============="+"\n"+"Enter Professor
Details"+"\n"+"============"+"\n");
Professor ob1=new Professor();
ob1.display();
AssociateProfessor ob2=new AssociateProfessor();
System.out.println("==============="+"\n"+"Enter AssociateProfessor
Details"+"\n"+"===================="+"\n");
ob2.display();
AssistantProfessor ob3=new AssistantProfessor();
System.out.println("==============="+"\n"+"Enter AssistantProfessor
Details"+"\n"+"===================="+"\n");
ob3.display();
Programmer ob4=new Programmer();
System.out.println("==============="+"\n"+"Enter Programmer
Details"+"\n"+"===================="+"\n");
ob4.display();
}
}
Result:
Thus the Java application has been created with with employee class and pay slips are generated for the
employees with their gross and net salary.
Ex. No.:4
ADT STACK
Aim
Program:
interface Stack
{
public void push(int x)throws StackException;
public int pop()throws stackExpection;
}
class StackException extends Exception{
public StackException(String message)
{
super(message);
}
}
public class Main implements stack{
public int[] item;
public int stackTop;
public Main(int size){
item=new int[size];
stackTop=0;
}
public void push(int x)throws StackException{
if(stackTop==item.length){
throw new stackException("Stack overflow");
}
item[stackTop]=x;
stackTop++;
}
public int pop () throws StackException{
int returnItem;
if(stackTop==0){
throw new stackException("Stack underflow");
}
returnItem=item[stackTop-1];
StackTop--;
return returnItem;
}
public static void main(String[]args){
int x;
stack s;
s=new Main(6);
try
{
x=4;s.push(x);System.out.println("push("+x+");");
x=7;s.push(x);System.out.println("push("+x+");");
x=8;s.push(x);System.out.println("push("+x+");");
x=9;s.push(x);System.out.println("push("+x+");");
x=s.pop();System.out.println("pop()--->"+x);
x=s.pop();System.out.println("pop()--->"+x);
x=s.pop();System.out.println("pop()--->"+x);
x=s.pop();System.out.println("PoP()--->"+x);
x=s.pop();System.out.println("Pop()--->"+x);
}
catch(stackExpection e){
System.out.println("Error dectected:"+e.getMessage());
System.exit(1);
}
}
}
Result:
Thus the design and implementation of ADT Stack using array has successfully executed.
Ex. No.:5
STRING OPERATIONS
Aim
Result:
Thus the implementation of string operations using array list has been successfully executed.
Ex. No.:6
ABSTRACT CLASS
Aim
To write a Java Program to create an abstract class named Shape and provide three classes named Rectangle,
Triangle and Circle such that each one of the classes extends the class Shape.
Procedure:
Program:
}
}
r.x=5;
r.y=10;
r.printArea();
System.out.println();
t.x=6;
t.y=8;
t.printArea();
System.out.println();
c.x=5;
c.printArea();
System.out.println();
Result:
Thus the design and implementation of Abstract class has been successfully executed.
EX. NO.:7 EXCEPTION HANDLING
Aim
To write a Java program to implement user defined exception handling.
Procedure:
1. Start the program
2. Define the exception for getting a number from the user.
3. If the number is positive print the number as such.
4. If the number is negative throw the exception to the user as ‘Number must be
positive’.
5. Stop the Program.
Program:
import java.lang.*;
import java.io.DataInputSteram;
class MyException extends Exception{
MyException(string MESSAGE)[
super(message);
}
}
class Main{
public static void main(String a[])]
int no;
DataInputSteram ds=new DataInputStream(System.in);
try{
System.out.println("Enter a positive number:");
no=Integer.parseInt(ds.readLine());
if(no<0){
throw new MyException("Number must be positive");
}
System.out.println("Number:"+no);
}
catch(MyException e){
System.out.pirntln("Caught MyException");
System.out.println(e.getMessage());
}
catch(Exception e){}
}
}
Result:
Thus the user defined exception has been successfully implemented.
E x. No.: 8
FILE INFORMATION
Aim
To 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.
Procedure:
1. Start the program
2. Read the filename from the user.
3. Use getName() Method to display the filename.
4. Use getPath() Method to display the path of the file.
5. Use getParent() Method to display its parent’s information.
6. Use exists() Method to display whether the file exist or not
7. Use isFile() and isDirectory() Methods to display whether the file is file or directory. 8.Use
canRead() and canWrite) methods to display whether the file is readable or writable.
9. Use lastModified() Method to display the modified information.
10. Use length() method to display the size of the file.
11. Use isHiddden() Method to display whether the file is hidden or not.
Program:
import java.util.Scanner;
import java.io.File;
class FileInfo
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("Enter the filename:");
String s=input.nextLine();
File f1=new File(s);
System.out.println("DETAILS ABOUT THE FILE");
System.out.println("--------------------");
System.out.println("File Name :"+f1.getName());
System.out.println("Path:"+f1.getPath());
System.out.println("Absolute Path:"+f1.getAbsolutePath());
System.out.println("Parent:"+ f1.getParent());
System.out.println("This file:"+(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());
}
}
Result:
Thus the information of the file has been displayed successfully using various file methods.
Ex. No.: 9
MULTITHREADING
Aim
To write a java program that implements a multi-threaded application.
Procedure:
1. Start the program
2. Design the first thread that generates a random integer for every 1 second .
3. If the first thread value is even, design the second thread as the square of the number and
then print it.
4. If the first thread value is odd, then third thread will print the value of cube of the
number.
5. Stop the program.
Program:.
import java.util.*;
class even implements Runnable
{
public int x;
public even(int x)
{
this.x=x;
}
public void run()
{
System.out.println("New Thread"+x+"is EVEN and Square of"+x+"is:"+x*x);
}
}
class odd implements Runnable
{
public int x;
public odd(int x)
{
this.x=x;
}
public void run()
{
System.out.println("New Thread"+x+"is ODD and Cube of"+x+"is:"+x*x*x);
}
}
class A extends Thread
{
public void run()
{
int num=0;
Random r=new Random();
try
{
for(int i=0;i<5;i++)
{
num=r.nextInt(100);
System.out.println("Main Thread and Generated Number is"+num);
if(num%2==0)
{
Thread t1=new Thread(new even(num));
t1.start();
}
else
{
Thread t2=new Thread(new odd(num));
t2.start();
}
Thread.sleep(1000);
System.out.println("---------------------------");
}
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
public class ThreeThreads
{
public static void main(String[] args)
{
A a=new A();
a.start();
}
}
Result:
Thus the implementation of multithreading has been done using three threads.
Thus the implementation of generic function is achieved for finding the maximum value from the given type of
elements.
Ex. No.: 11
CALCULATOR
Aim
To design a calculator using event-driven programming paradigm of Java for Decimal manipulations and
Scientific manipulations.
Procedure:
1. Start the program
2. Using the swing components design the buttons of the calculator
3. Use key events and key listener to listen the events of the calculator.
4. Do the necessary manipulations.
5. Stop the program.
Program:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class DecimalCalculator extends JFrame implements ActionListener
{
JTextField tfield;
double temp,temp1,result,a,ml;
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,plus,min,div,rec,mul,eq,addsub,dot,mr,mc,mp,mm,sqr
t;
Container cont;
JPanel textpanel,buttonpanel;
DecimalCalculator()
{
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;
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);
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("+/-"))
{
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("="))
{
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;
}
}
}
public static void main(String args[])
{
DecimalCalculator d;
d=new DecimalCalculator();
d.setTitle("Decimal calculator");
d.pack();
d.setVisible(true);
}
}
Program (B):
mport 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,ml;
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,pow3,exp,fac,plus,min,div,log,rec,mul,eq,addsu
b,dot,mr,mc,mp,mm,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;
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("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)
{
double i,s=1;
for(i=2;i<=x;i+=1.0)
s*=i;
return s;
}
public static void main(String args[])
{
ScientificCalculator f;
f=new ScientificCalculator();
f.setTitle("Scientific calculator");
f.pack();
f.setVisible(true);
}
}
Result:
Thus the implementation of generic function is achieved for finding the maximum value from the given type of
elements.
Ex. No.: 12
Aim
Program:
java.sql.Connection;
import java.sql.DriverManager;
if(con==null){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(Exception e){
e.printStackTrace();
return con;
}
//Exception class
package com.myproject.bus.util;
/**
*/
//Bean class
package com.myproject.bus.bean;
String scheduleId;
destination; String
startTime; String
arrivalTime;
return scheduleId;
this.scheduleId = scheduleId;
return source;
}
this.source = source;
return destination;
this.destination = destination;
return startTime;
this.startTime = startTime;
return arrivalTime;
this.arrivalTime = arrivalTime;
//DAO class
package com.myproject.bus.dao;
java.sql.*;
com.wipro.bus.bean.*; import
com.wipro.bus.util.DBUtil; public class
ScheduleDAO {
int i=0;
try{
values(?,?,?,?,?)");
Connection con=DBUtil.getDBConnection();
ps.setString(1,scheduleBean.getScheduleId());
ps.setString(2,scheduleBean.getSource());
ps.setString(3,scheduleBean.getDestination());
ps.setString(4,scheduleBean.getStartTime());
ps.setString(5,scheduleBean.getArrivalTime());
i=ps.executeUpdate();
}catch(Exception e){
e.printStackTrace();
if(i>0)
return "SUCCESS";
else
return "FAIL";
sid="";
int i=0;
try{
ResultSet rs=ps.executeQuery();
while(rs.next())
i=rs.getInt(1);
String s=Integer.toString(i);
sid=new StringBuffer().append(source.charAt(0)).append(source.
charAt(1)).append(destination.charAt(0)).append(destination.charAt(1)).append(s).t o-
String().toUpperCase();
//sid=new StringBuffer().append(source.charAt(0)).append(source.
charAt(1)).append(destination.charAt(0)).append(destination.charAt(1)).toUppercase().to-
String();
}catch(Exception e){
e.printStackTrace();
return sid;
ScheduleBean();
try{
Connection con=DBUtil.getDBConnection();
ResultSet rs=ps.executeQuery();
while(rs.next()){ sb.setScheduleId(rs.getString(1));
sb.setSource(rs.getString(2));
sb.setDestination(rs.getString(3));
sb.setStartTime(rs.getString(4));
sb.setArrivalTime(rs.getString(5));
al.add(sb);
}
}catch(Exception e){
e.printStackTrace();
return al;
//Administrator class
package com.myproject.bus.service;
import java.sql.Connection;
import java.sql.*;
import java.util.ArrayList;
import com.myproject.bus.bean.ScheduleBean;
import com.myproject.bus.dao.ScheduleDAO;
import com.myproject.bus.util.DBUtil;
import com.myproject.bus.util.InvalidInputException;
source=scheduleBean.getSource();
String destination=scheduleBean.getDestination();
try{
length()<2)||(destination.length()<2))
else if(source==destination)
else{
sid=sd.generateID(source,destination);
scheduleBean.setScheduleId(sid);
String s=sd.createSchedule(scheduleBean);
return s;
}catch(Exception e){
return e.toString();
sb=new ScheduleBean();
try{
Connection con=DBUtil.getDBConnection();
source='"+source+"'and destination='"+destination+"'");
ResultSet rs=ps.executeQuery();
while(rs.next()){ sb.setScheduleId(rs.getString(1));
sb.setSource(rs.getString(2));
sb.setDestination(rs.getString(3));
sb.setStartTime(rs.getString(4));
sb.setStartTime(rs.getString(5));
al.add(sb);
//System.out.println("adding");
}catch(Exception e){
e.printStackTrace();
return al;
}
// Main servlet
package com.myproject.bus.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
com.myproject.bus.dao.ScheduleDAO;
import com.myproject.bus.service.Administrator;
/**
*/
a=new Administrator();
dest=request.getParameter("Destination"); String
stime=request.getParameter("StartTime"); String
atime=request.getParameter("ArrivalTime"); sb.setSource(source);
sb.setDestination(dest);
sb.setStartTime(stime);
sb.setArrivalTime(atime); String
cr=a.addSchedule(sb);
return cr;
}
source=request.getParameter("Source"); String
dest=request.getParameter("Destination");
al=a.viewSchedule(source,dest);
return al;
ServletException, IOException{
String res="";
m=new MainServlet();
out=response.getWriter();
if(str.equalsIgnoreCase("newSchedule")){
res=m.addSchedule(request); if(res.equalsIgnoreCase("SUCCESS"))
response.sendRedirect("success.html");
else if(res.equalsIgnoreCase("FAIL"))
response.sendRedirect("errorinserting.html");
}else if(str.equalsIgnoreCase("viewSchedule")){
al=m.viewSchedule(request); if(al.size()==0)
response.sendRedirect("displaySchedule.html");
else{
/*request.setAttribute("busSchedule",al);
RequestDispatcher rd=request.getRequestDispatcher("displaySchedule.html”);
rd.forward(request,,response);*/
}}
}}
//Addschedule.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
</form>
</body>
</html>
//Viewschedule.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<body>
</form>
</body>
</html>
//displayschedule.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
</body>
</html>
//errorinserting.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
<form>
<h1> <font color="blue"> Error in adding the record!Please try again... </font> </h1>
</form>
</body>
</html>
//menu.html(main html)
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
<h2>
a>
ule</a></h2>
</form>
</body>
</html>
//success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<body>
<form>
</form>
</body>
</html>
Result:
Thus the design of vehicle management system using Java has been successfully executed.