Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

JAVA RECORD (AutoRecovered)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 87

BASIC PROGRAMMS

CALCULATOR USING SWITCH STATEMENT


PROGRAM:
import java.util.*;
class Calci
{
public static void main(String args[]){
int a,b,c;
//char op;
Scanner s=new Scanner(System.in);
System.out.print("Enter the first value:");
a=s.nextInt();
System.out.println();
System.out.print("Enter the second value:");
b=s.nextInt();
System.out.println();
System.out.print("Enter the anyone operation(+,-,*,/):");
char op=s.next().charAt(0);
switch(op){
case '+':
System.out.println("Addition of numbers is "+(a+b));
break;
case '-':
System.out.println("Subration of numbers is "+(a-b));
break;
case '*':
System.out.println("Multiplication of numbers is "+(a*b));
break;
case '/':
System.out.println("Divison of numbers is "+(a/b));
break;
}}}

OUTPUT:
ARMSTRONG NUMBER

PROGRAM

import java.util.*;
class Armstrong
{
public static void main(String args[])
{
int n,temp,sum=0,rem=0;
Scanner s=new Scanner(System.in);
System.out.print("Enter the number:");
n=s.nextInt();
temp=n;
while(temp!=0)
{
rem=temp%10;
sum=sum+rem*rem*rem;
temp=temp/10;
}
if(sum==n)
System.out.println("This is armstrong number");
else
System.out.println("This is not armstrong number");
}
}

OUTPUT:
FIBNOCCI SERIES

PROGRAM

import java.util.*;
class Fibbnooci
{
public static void main(String args[])
{
int n,a=-1,b=1,c,i;
Scanner s=new Scanner(System.in);
System.out.print("Enter the number:");
n=s.nextInt();
i=1;
while(i<=n)
{
c=a+b;
System.out.print(c+" ");
a=b;
b=c;
i++;
}
System.out.println();
}
}

OUTPUT:
LEAP YEAR DEMONSTRATE AR USING IF ELSE IF LADDER

PROGRAM

import java.util.*;
class Leap
{
public static void main(String args[])
{
int year;
Scanner s=new Scanner(System.in);
System.out.print("Enter the year:");
year=s.nextInt();
if(year%400==0)
System.out.println("Leap Year");
else if(year%100==0)
System.out.println("Not Leap year");
else if(year%4==0)
System.out.println("Leap Year");
else
System.out.println("Not leap Year");
}
}

OUTPUT:
MATRIX MULTIPLICATION

PROGRAM

import java.util.*;
class MatrixAdd
{
public static void main(String args[])
{
int a[][],b[][],i,j,n,sum;
Scanner s=new Scanner(System.in);
System.out.print("Enter the square matrix number:");
n=s.nextInt();
a=new int[n][n];
b=new int[n][n];
System.out.println("Enter the values for first matrix ...");
for(i=0;i<n;i++){
for(j=0;j<n;j++)
a[i][j]=s.nextInt();
}
System.out.println("Enter the values for second matrix... ");
for(i=0;i<n;i++){
for(j=0;j<n;j++)
b[i][j]=s.nextInt();
}
System.out.println("Addition of two matrices is...");
for(i=0;i<n;i++){
for(j=0;j<n;j++)
{
sum=a[i][j]+b[i][j];
System.out.print(sum+" ");
}
System.out.println(); } }}

OUTPUT:
MATRIX MULTIPLICATION

PROGRAM
import java.util.*;
class MatrixMul
{
public static void main(String args[])
{
int a[][],b[][],n,i,j,k;
Scanner s=new Scanner(System.in);
System.out.print("Enter the number:");
n=s.nextInt();
int c[][]=new int[n][n];
a=new int[n][n];
b=new int[n][n];
System.out.println("Enter the value 1..");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
a[i][j]=s.nextInt();
}
System.out.println("Enter the values of 2..");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
b[i][j]=s.nextInt();
}
System.out.println("Mutiplication of the matrices..");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=a[i][k]*b[k][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
System.out.print(c[i][j]+" ");
System.out.println();
}
}
}
OUTPUT:
MATRIX SUBRACTION
PROGRAM

import java.util.*;
class MatrixSub
{
public static void main(String args[]) {
int a[][],b[][],n,i,j,diff;
Scanner s=new Scanner(System.in);
System.out.print("Enter the number:");
n=s.nextInt();
a=new int[n][n];
b=new int[n][n];
System.out.println("Enter the value of 1'st matrix...");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
a[i][j]=s.nextInt();
}
System.out.println("Enter the value of second matrices...");
for(i=0;i<n;i++){
for(j=0;j<n;j++)
b[i][j]=s.nextInt();
}
System.out.println("Subraction of two matrices...");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
diff=a[i][j]-b[i][j];
System.out.print(diff+" ");
}
System.out.println();
}
}
}

OUTPUT:
PALLINDROME NUMBER

PROGRAM

import java.util.*;
class Pallindrome
{
public static void main(String args[])
{
int temp,n,rem=0,sum=0;
Scanner s=new Scanner(System.in);
System.out.print("Enter the number:");
n=s.nextInt();
temp=n;
while(temp!=0)
{
rem=temp%10;
sum=(sum*10)+rem;
temp=temp/10;
}
if(sum==n)
System.out.println(n+" is pallindrome number");
else
System.out.println(n+" is not pallindrome number");
}
}

OUTPUT:
PERFECT NUMBER

PROGRAM

import java.util.*;
class Perfect
{
public static void main(String args[])
{
int n,i,sum=0;
Scanner s=new Scanner(System.in);
System.out.print("Enter the number:");
n=s.nextInt();
for(i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
System.out.println(n+" is perfect number");
else
System.out.println(n+" is not perfect number");
}
}

OUTPUT:
PRIME NUMBER

PROGRAM

import java.util.*;
class Prime
{
public static void main(String args[])
{
int n,i,count=0;
Scanner s=new Scanner(System.in);
System.out.print("Enter the number:");
n=s.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
System.out.println(n+" is prime number");
else
System.out.println(n+" is not prime number");
}
}

OUTPUT:
TOTAL AND AVERAGE OF THE STUDENT

PROGRAM

import java.util.*;
class TotAvg
{
public static void main(String args[])
{
int m1,m2,m3,m4,m5,total;
float avg;
Scanner s=new Scanner(System.in);
System.out.print("Enter the mark:");
m1=s.nextInt();
m2=s.nextInt();
m3=s.nextInt();
m4=s.nextInt();
m5=s.nextInt();
total=m1+m2+m3+m4+m5;
System.out.println("Total is "+total);
avg=total/5;
System.out.println("Average is "+avg);
}
}

OUTPUT:
ADDITION TWO NUMBERS

PROGRAM

import java.util.Scanner;
class AddTwoNumbers {
public static void main(String[] args) {
int num1, num2, sum;
Scanner sc = new Scanner(System.in);
System.out.print("Enter First Number: ");
num1 = sc.nextInt();
System.out.print("Enter Second Number: ");
num2 = sc.nextInt();
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}

OUTPUT:
EVEN OR ODD PROGRAM
PROGRAM

import java.util.Scanner;
class CheckEvenOdd
{
public static void main(String args[])
{
int num;
System.out.print("Enter an Integer number:");
Scanner input = new Scanner(System.in);
num = input.nextInt();
if ( num % 2 == 0 )
System.out.println("Entered number is even");
else
System.out.println("Entered number is odd");
}
}

OUTPUT
BASIC ARIRHMETIC OPERATION
PROGRAM
import java.util.*;
class ArithOper
{
public static void main(String args[])
{
int a,b;
Scanner s=new Scanner(System.in);
System.out.print("Enter the two values: ");
a=s.nextInt();
b=s.nextInt();
System.out.println("Addition is "+(a+b));
System.out.println("Subraction is "+(a-b));
System.out.println("Multiplication is "+(a*b));
System.out.println("Division is "+(a/b));

}
}

OUTPUT:
LOOP STATEMENTS

PROGRAM

import java.util.*;
class Loop
{
public static void main(String args[])
{
int i=1;
System.out.println("While loop is processing...");
while(i<=5)
{
System.out.println(i);
i++;
}
System.out.println("Do While loop is processing...");
i=1;
do{
System.out.println(i);
i++;
}while(i<=5);
System.out.println("For loop is processing...");
for(i=1;i<=5;i++)
System.out.println(i);
System.out.println("***Loops are ended***");
}
}

OUTPUT:
SUM OF ODD AND EVEN NUMBERS IN N SERIES

PROGRAM

import java.util.*;
class SumOddEven
{
public static void main(String args[])
{
int n,i,sum1=0,sum2=0;
Scanner s=new Scanner(System.in);
System.out.print("Enter the value of n:");
n=s.nextInt();
for(i=1;i<=n;i++)
{
if(i%2==0)
sum1=sum1+i;
else
sum2=sum2+i;
}
System.out.println("Addition of even numbers is "+sum1);
System.out.println("Addition of odd numbers is "+sum2);
}
}

OUTPUT:
FACTORIAL NUMBER
PROGRAM

import java.util.*;
class Factorial
{
public static void main(String args[])
{
int n,i,fact=1;
Scanner s=new Scanner(System.in);
System.out.print("Enter the number:");
n=s.nextInt();
for(i=1;i<=n;i++)
fact=fact*i;
System.out.println("Factorial of "+n+" is "+fact);
}
}

OUTPUT:
SIMPLE IF-ELSE PROGRAM
PROGRAM

import java.util.*;
class IfElse
{
public static void main(String args[])
{
int age;
Scanner s=new Scanner(System.in);
System.out.print("Enter the age:");
age=s.nextInt();
if(age>=18)
System.out.println("You are elligible to vote and Licence");
else
System.out.println("You are not elligible to vote and Licence");

}
}

OUTPUT:
GREATEST AMOUNG THREE NUMBERS

PROGRAM

import java.util.*;
class Greater3
{
public static void main(String args[])
{
int a,b,c;
Scanner s=new Scanner(System.in);
System.out.print("Enter the three numbers:");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
if(a>b && a>c)
System.out.println(a+" is greater");
else if(b>a && b>c)
System.out.println(b+" is greater");
else
System.out.println(c+" is greater");

}
}

OUTPUT:
SIMPLE INTEREST
PROGRAM
import java.util.Scanner;
public class SimpleInterest
{
public static void main(String args[])
{
float p, r, t, simple_interest;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the Principal : ");
p = scan.nextFloat();
System.out.print("Enter the Rate of interest : ");
r = scan.nextFloat();
System.out.print("Enter the Time period : ");
t = scan.nextFloat();
simple_interest = (p * r * t) / 100;
System.out.print("Simple Interest is: " +simple_interest);
}
}

OUTPUT:
FUNCTION PROGRAM FOR ALL BASIC PROGRAMM

PROGRAM
import java.util.*;
class Function
{
public static void main(String args[])
{
int choice;
char op;
Scanner s=new Scanner(System.in);
System.out.println("This Program are there...");
do{
System.out.println("1.Armstrong Number\n2.Pallindrome Number\n3.Perfect
Number");
System.out.println("4.Prime Number\n5.Leap Year\n6.Simple Interest\n");
System.out.print("Enter your choice:");
choice=s.nextInt();
switch(choice)
{
case 1:
armstrong();
break;
case 2:
pallindrome();
break;
case 3:
perfect();
break;
case 4:
prime();
break;
case 5:
leap();
break;
case 6:
simple_interest();
break;
default:
System.out.println("Enter the valid operation");
}
System.out.print("Do you want to continue(y/n)");
op=s.next().charAt(0);
}while(op=='y');
}

static void armstrong()


{
int n,temp,sum=0,rem=0;
Scanner s=new Scanner(System.in);
System.out.print("Enter the number:");
n=s.nextInt();
temp=n;
while(temp!=0)
{
rem=temp%10;
sum=sum+rem*rem*rem;
temp=temp/10;
}
if(sum==n)
System.out.println("This is armstrong number");
else
System.out.println("This is not armstrong number");

static void pallindrome()


{
int temp,n,rem=0,sum=0;
Scanner s=new Scanner(System.in);
System.out.print("Enter the number:");
n=s.nextInt();
temp=n;
while(temp!=0)
{
rem=temp%10;
sum=(sum*10)+rem;
temp=temp/10;
}
if(sum==n)
System.out.println(n+" is pallindrome number");
else
System.out.println(n+" is not pallindrome number");
}

static void perfect()


{
int n,i,sum=0;
Scanner s=new Scanner(System.in);
System.out.print("Enter the number:");
n=s.nextInt();
for(i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
System.out.println(n+" is perfect number");
else
System.out.println(n+" is not perfect number");

static void prime()


{
int n,i,count=0;
Scanner s=new Scanner(System.in);
System.out.print("Enter the number:");
n=s.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
System.out.println(n+" is prime number");
else
System.out.println(n+" is not prime number");

}
static void leap()
{
int year;
Scanner s=new Scanner(System.in);
System.out.print("Enter the year:");
year=s.nextInt();
if(year%400==0)
System.out.println("Leap Year");
else if(year%100==0)
System.out.println("Not Leap year");
else if(year%4==0)
System.out.println("Leap Year");
else
System.out.println("Not leap Year");
}
static void simple_interest()
{
float p, r, t, simple_interest;
Scanner s=new Scanner(System.in);
System.out.print("Enter the Principal : ");
p = s.nextFloat();
System.out.print("Enter the Rate of interest : ");
r = s.nextFloat();
System.out.print("Enter the Time period : ");
t = s.nextFloat();
simple_interest = (p * r * t) / 100;
System.out.println("Simple Interest is: " +simple_interest);
}
}

OUTPUT:
CALL BY VALUE
PROGRAM

class CBV{
public static void main(String[] args){
int a = 30;
int b = 45;
System.out.println("Before swapping, a = " + a + " and b = " + b);
swapFunction(a, b);
System.out.println("\n**Now, Before and After swapping values will be same
here**:");
System.out.println("After swapping, a = " + a + " and b is " + b);
}
public static void swapFunction(int a, int b) {
System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
int c = a;
a = b;
b = c;
System.out.println("After swapping(Inside), a = " + a + " b = " + b);
}
}

OUTPUT:
CALL BY REFERENCE
PROGRAM

class CBR{
int data=50;

void change(CBR op){


op.data=op.data+100;//changes will be in the instance variable
}

public static void main(String args[]){


CBR op=new CBR();

System.out.println("before change "+op.data);


op.change(op);//passing object
System.out.println("after change "+op.data);

}
}

OUTPUT:
FACTORIAL USING RECURSION
PROGRAM
class FactorialRecursion{
static int factorial(int n){
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
public static void main(String args[]){
int i,fact=1;
int number=4;
fact = factorial(number);
System.out.println("Factorial of "+number+" is: "+fact);
}
}

OUTPUT:
FIBONACCI USING RECURSION

PROGRAM
class FibonacciRecursion{
static int n1=0,n2=1,n3=0;
static void printFibonacci(int count){
if(count>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print(" "+n3);
printFibonacci(count-1);
}
}
public static void main(String args[]){
int count=10;
System.out.print(n1+" "+n2);
printFibonacci(count-2);
}
}

OUTPUT:
INTERFACE CONCEPT IN DIFFERENT PROGRAM

PROGRAM

import java.util.*;
interface OneNumber
{
int Armstrong(int a);
int Pallindrome(int a);
int Perfect(int a);
int Prime(int a);
void SumOfN(int a);
void Reverse(int a);
void SumDigit(int a);
}
interface TwoNumber
{
void NestedArmstrong(int a,int b);
void NestedPallindrome(int a,int b);
void NestedPerfect(int a,int b);
void NestedPrime(int a,int b);
}
class A implements OneNumber,TwoNumber
{
int temp,i,rem,sum,flag;
public int Armstrong(int a)
{
sum=0;
rem=0;
temp=a;
while(temp!=0)
{
rem=temp%10;
sum=sum+(rem*rem*rem);
temp/=10;
}
if(sum==a)
return 1;
else
return 0;
}
public int Pallindrome(int a)
{
sum=0;
temp=a;
rem=0;
while(temp!=0)
{
rem=temp%10;
sum=(sum*10)+rem;
temp/=10;
}
if(sum==a)
return 1;
else
return 0;
}
public int Perfect(int a)
{
sum=0;
for(i=1;i<a;i++)
{
if(a%i==0)
sum=sum+i;
}
if(sum==a)
return 1;
else
return 0;
}
public int Prime(int a)
{
flag=0;
for(i=1;i<=a;i++)
{
if(a%i==0)
flag++;
}
if(flag==2)
return 1;
else
return 0;
}
public void SumOfN(int a)
{
System.out.println("Sum of n numbers...");
sum=0;
for(i=1;i<=a;i++)
sum=sum+i;
System.out.println(sum);
}
public void Reverse(int a)
{
System.out.println("Reverse of the number...");
while(a!=0)
{
System.out.print(a%10);
a/=10;
}
System.out.println();
}
public void SumDigit(int a)
{
System.out.println("Sum of the digits...");
sum=0;
rem=0;
while(a!=0)
{
rem=a%10;
sum=sum+rem;
a/=10;
}
System.out.println(sum);
}
public void NestedArmstrong(int a,int b){}
public void NestedPallindrome(int a,int b){}
public void NestedPerfect(int a,int b){}
public void NestedPrime(int a,int b){}
}
class B extends A implements OneNumber,TwoNumber
{
int i,check=0;
public void NestedArmstrong(int a,int b)
{
System.out.println("Armstrong Numbers are");
for(i=a;i<=b;i++){
check=super.Armstrong(i);
if(check==1)
System.out.println(i);}
}
public void NestedPallindrome(int a,int b)
{
System.out.println("Pallindrome Numbers are");
for(i=a;i<=b;i++)
{
//System.out.println("Loops enter"+i);
check=super.Pallindrome(i);
if(check==1)
System.out.println(i);
}
}
public void NestedPerfect(int a,int b)
{
System.out.println("Perfect Numbers are");
for(i=a;i<=b;i++){
check=super.Perfect(i);
if(check==1)
System.out.println(i);}
}
public void NestedPrime(int a,int b)
{
System.out.println("Prime Numbers are");
for(i=a;i<=b;i++){
check=super.Prime(i);
if(check==1)
System.out.println(i);}
}
}
class Testing
{
public static void main(String args[])
{
int a,b,c,ch=0;
Scanner s=new Scanner(System.in);
System.out.print("Enter One Number:");
a=s.nextInt();
System.out.print("Enter Two Number:");
b=s.nextInt();
c=s.nextInt();
B ob=new B();
ch=ob.Armstrong(a);
if(ch==1)
System.out.println(a+" is armstrong number");
else
System.out.println(a+" is not armstrong number");
ch=ob.Pallindrome(a);
if(ch==1)
System.out.println(a+" is pallindrome number");
else
System.out.println(a+" is not pallindrome number");
ch=ob.Perfect(a);
if(ch==1)
System.out.println(a+" is Perfect number");
else
System.out.println(a+" is not Perfect number");
ch=ob.Prime(a);
if(ch==1)
System.out.println(a+" is Prime number");
else
System.out.println(a+"is not Prime number");
ob.SumOfN(a);
ob.Reverse(a);
ob.SumDigit(a);
ob.NestedArmstrong(b,c);
ob.NestedPallindrome(b,c);
ob.NestedPerfect(b,c);
ob.NestedPrime(b,c);
}
}
OUTPUT
EXCEPTION HANDLING

PROGRAM

// Atithmetic Exception takes place


class ArithExp{
public static void main(String args[]){
try{
int data=100/0;
}
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("rest of the code...");
}
}

OUTPUT:
MULTIPLE CATCH IN EXCEPTION HANDLING

PROGRAM

public class MultipleCatch {


public static void main(String[] args) {
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}

OUTPUT:
FINALLY IN EXCEPTION HANDLING

PROGRAM

class Finally
{
public static void main(String[] args)
{
try{
int x=300;
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println("finally block is executed");
}
}
}

OUTPUT:
PACKAGE
PROGRAM

//Save A.java
package mypack;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}

//Saved by B.java
package mypack;
import mypack.*;

class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}

OUTPUT:
ADDITION UDING PACKAGE

PROGRAM
package letmecalculate;

public class Calculator {


public int add(int a, int b){
return a+b;
}
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(10, 20));
}
}

Package letmecalculate;
import letmecalculate.Calculator;
public class Demo{
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(100, 200));
}
}

OUTPUT:
SYLLABUS PROGRAMS

PROGRAM TO GENERATE ELECTRICITY BILL

PROGRAM

import java.util.*;
class ebill
{
public static void main (String args[])
{
customerdata ob = new customerdata();
ob.getdata();
ob.calc();
ob.display();
}
}
class customerdata
{
Scanner in = new Scanner(System.in);
Scanner ins = new Scanner(System.in);
String cname,type;
int bn;
double current,previous,tbill,units;
void getdata()
{
System.out.print ("\n\t Enter consumer number ");
bn = in.nextInt();
System.out.print ("\n\t Enter Type of connection (D for Domestic or C for
Commercial) ");
type = ins.nextLine();
System.out.print ("\n\t Enter consumer name ");
cname = ins.nextLine();
System.out.print ("\n\t Enter previous month reading ");
previous= in.nextDouble();
System.out.print ("\n\t Enter current month reading ");
current= in.nextDouble();
}
void calc()
{
units=current-previous;
if(type.equals("D"))
{
if (units<=100)
tbill=1 * units;
else if (units>100 && units<=200)
tbill=2.50*units;
else if(units>200 && units<=500)
tbill= 4*units;
else
tbill= 6*units;
}
else
{
if (units<=100)
tbill= 2 * units;
else if(units>100 && units<=200)
tbill=4.50*units;
else if(units>200 && units<=500)
tbill= 6*units;
else
tbill= 7*units;
}
}
void display()
{
System.out.println("\n\t Consumer number = "+bn);
System.out.println ("\n\t Consumer name = "+cname);
if(type.equals("D"))
System.out.println ("\n\t type of connection = DOMESTIC ");
else
System.out.println ("\n\t type of connection = COMMERCIAL ");
System.out.println ("\n\t Current Month Reading = "+current);
System.out.println ("\n\t Previous Month Reading = "+previous);
System.out.println ("\n\t Total units = "+units);
System.out.println ("\n\t Total bill = RS "+tbill);
}
}
OUTPUT
PROGRAM TO IMPLEMENT CURRENCY CONVERTER, DISTANCE
CONVERTER AND TIME CONVERTER USING PACKAGES

PROGRAM

Currencyconversion.java
package currencyconversion;
import java.util.*;
public class currency
{
double inr,usd;
double euro,yen;
Scanner in=new Scanner(System.in);
public void dollartorupee()
{
System.out.println("Enter dollars to convert into Rupees:");
usd=in.nextInt();
inr=usd*67;
System.out.println("Dollar ="+usd+"equal to INR="+inr);
}
public void rupeetodollar()
{
System.out.println("Enter Rupee to convert into Dollars:");
inr=in.nextInt();
usd=inr/67;
System.out.println("Rupee ="+inr+"equal to Dollars="+usd);
}
public void eurotorupee()
{
System.out.println("Enter euro to convert into Rupees:");
euro=in.nextInt();
inr=euro*79.50;
System.out.println("Euro ="+euro +"equal to INR="+inr);
}
public void rupeetoeuro()
{
System.out.println("Enter Rupees to convert into Euro:");
inr=in.nextInt();
euro=(inr/79.50);
System.out.println("Rupee ="+inr +"equal to Euro="+euro);
}
public void yentorupee()
{
System.out.println("Enter yen to convert into Rupees:");
yen=in.nextInt();
inr=yen*0.61;
System.out.println("YEN="+yen +"equal to INR="+inr);
}
public void rupeetoyen()
{
System.out.println("Enter Rupees to convert into Yen:");
inr=in.nextInt();
yen=(inr/0.61);
System.out.println("INR="+inr +"equal to YEN"+yen);
}
}
distance.java
package distanceconversion;
import java.util.*;
public class distance
{
double km,m,miles;
Scanner sc = new Scanner(System.in);
public void kmtom()
{
System.out.print("Enter in km ");
km=sc.nextDouble();
m=(km*1000);
System.out.println(km+"km" +"equal to"+m+"metres");
}
public void mtokm()
{
System.out.print("Enter in meter ");
m=sc.nextDouble();
km=(m/1000);
System.out.println(m+"m" +"equal to"+km+"kilometres");
}
public void milestokm()
{
System.out.print("Enter in miles");
miles=sc.nextDouble();
km=(miles*1.60934);
System.out.println(miles+"miles" +"equal to"+km+"kilometres");
}
public void kmtomiles()
{
System.out.print("Enter in km");
km=sc.nextDouble();
miles=(km*0.621371);
System.out.println(km+"km" +"equal to"+miles+"miles");
}
}
timer.java
package timeconversion;
import java.util.*;
public class timer
{
int hours,seconds,minutes;
int input;
Scanner sc = new Scanner(System.in);
public void secondstohours()
{
System.out.print("Enter the number of seconds: ");
input = sc.nextInt();
hours = input / 3600;
minutes = (input % 3600) / 60;
seconds = (input % 3600) % 60;
System.out.println("Hours: " + hours);
System.out.println("Minutes: " + minutes);
System.out.println("Seconds: " + seconds);
}
public void minutestohours()
{
System.out.print("Enter the number of minutes: ");
minutes=sc.nextInt();
hours=minutes/60;
minutes=minutes%60;
System.out.println("Hours: " + hours);
System.out.println("Minutes: " + minutes);
}
public void hourstominutes()
{
System.out.println("enter the no of hours");
hours=sc.nextInt();
minutes=(hours*60);
System.out.println("Minutes: " + minutes);
}
public void hourstoseconds()
{
System.out.println("enter the no of hours");
hours=sc.nextInt();
seconds=(hours*3600);
System.out.println("Minutes: " + seconds);
}
}
converter.java
import java.util.*;
import java.io.*;
import currencyconversion.*;
import distanceconversion.*;
import timeconversion.*;
class converter
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int choice,ch;
currency c=new currency();
distance d=new distance();
timer t=new timer();
do
{
System.out.println("1.dollar to rupee ");
System.out.println("2.rupee to dollar ");
System.out.println("3.Euro to rupee ");
System.out.println("4..rupee to Euro ");
System.out.println("5.Yen to rupee ");
System.out.println("6.Rupee to Yen ");
System.out.println("7.Meter to kilometer ");
System.out.println("8.kilometer to meter ");
System.out.println("9.Miles to kilometer ");
System.out.println("10.kilometer to miles");
System.out.println("11.Hours to Minutes");
System.out.println("12.Hours to Seconds");
System.out.println("13.Seconds to Hours");
System.out.println("14.Minutes to Hours");
System.out.println("Enter ur choice");
choice=s.nextInt();
switch(choice)
{
case 1:
{
c.dollartorupee();
break;
}
case 2:
{
c.rupeetodollar();
break;
}
case 3:
{
c.eurotorupee();
break;
}
case 4:
{
c.rupeetoeuro();
break;
}
case 5:
{c.yentorupee();
break;}
case 6 :
{
c.rupeetoyen();
break;
}
case 7 :
{
d.mtokm();
break;
}
case 8 :
{
d.kmtom();
break;
}
case 9 :
{
d.milestokm();
break;
}
case 10 :
{
d.kmtomiles();
break;
}
case 11 :
{
t.hourstominutes();
break;
}
case 12 :
{
t.hourstoseconds();
break;
}
case 13 :
{
t.secondstohours();
break;
}
case 14 :
{
t.minutestohours();
break;
}}
System.out.println("Enter 0 to quit and 1 to continue ");
ch=s.nextInt();
}while(ch==1);
}
}

OUTPUT
PROGRAM TO GENERATE PAYSLIP USING INHERITANCE
PROGRAM

import java.util.*;
class employee
{
int empid;
long mobile;
String name, address, mailid;
Scanner get = new Scanner(System.in);
void getdata()
{
System.out.println("Enter Name of the Employee");
name = get.nextLine();
System.out.println("Enter Mail id");
mailid = get.nextLine();
System.out.println("Enter Address of the Employee:");
address = get.nextLine();
System.out.println("Enter employee id ");
empid = get.nextInt();
System.out.println("Enter Mobile Number");
mobile = get.nextLong();
}
void display()
{
System.out.println("Employee Name: "+name);
System.out.println("Employee id : "+empid);
System.out.println("Mail id : "+mailid);
System.out.println("Address: "+address);
System.out.println("Mobile Number: "+mobile);
}}
class programmer extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getprogrammer()
{
System.out.println("Enter basic pay");
bp = get.nextDouble();
}
void calculateprog()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("PAY SLIP FOR PROGRAMMER");
System.out.println("************************************************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("PF:Rs"+pf);
System.out.println("HRA:Rs"+hra);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}}
class asstprofessor extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getasst()
{
System.out.println("Enter basic pay");
bp = get.nextDouble();
}
void calculateasst()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("PAY SLIP FOR ASSISTANT PROFESSOR");
System.out.println("************************************************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
System.out.println("PF:Rs"+pf);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}}
class associateprofessor extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getassociate()
{
System.out.println("Enter basic pay");
bp = get.nextDouble();
}
void calculateassociate()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("PAY SLIP FOR ASSOCIATE PROFESSOR");
System.out.println("************************************************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
System.out.println("PF:Rs"+pf);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}}
class professor extends employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getprofessor()
{
System.out.println("Enter basic pay");
bp = get.nextDouble();
}
void calculateprofessor()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("************************************************");
System.out.println("PAY SLIP FOR PROFESSOR");
System.out.println("************************************************");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
System.out.println("PF:Rs"+pf);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}}
class salary
{
public static void main(String args[])
{
int choice,cont;
do
{
System.out.println("PAYROLL");
System.out.println(" 1.PROGRAMMER \t 2.ASSISTANT PROFESSOR \t
3.ASSOCIATE
PROFESSOR \t 4.PROFESSOR ");
Scanner c = new Scanner(System.in);
choice=c.nextInt();
switch(choice)
{
case 1:
{
programmer p=new programmer();
p.getdata();
p.getprogrammer();
p.display();
p.calculateprog();break;
}
case 2:
{
asstprofessor asst=new asstprofessor();
asst.getdata();
asst.getasst();
asst.display();
asst.calculateasst();break;
}
case 3:
{
associateprofessor asso=new associateprofessor();
asso.getdata();
asso.getassociate();
asso.display();
asso.calculateassociate();break;
}
case 4:
{
professor prof=new professor();
prof.getdata();
prof.getprofessor();
prof.display();
prof.calculateprofessor();break;
}}
System.out.println("Do u want to continue 0 to quit and 1 to continue ");
cont=c.nextInt();
}while(cont==1);
}
}

OUTPUT
PROGRAM FOR STACK ADT USING INTERFACE

PROGRAM
import java.io.*;
interface stackoperation
{
public void push(int i);
public void pop();
}
class Astack implements stackoperation
{
int stack[]=new int[5];
int top=-1;
int i;
public void push(int item)
{
if(top>=4)
{
System.out.println("overflow");
}
else
{
top=top+1;
stack[top]=item;
System.out.println("item pushed"+stack[top]);
}}
public void pop()
{
if(top<0)
System.out.println("underflow");
else
{
System.out.println("item popped"+stack[top]);
top=top-1;
}}
public void display()
{
if(top<0)
System.out.println("No Element in stack");
else
{
for(i=0;i<=top;i++)
System.out.println("element:"+stack[i]);
}}}
class teststack
{
public static void main(String args[])throws IOException
{
int ch,c;
int i;
Astack s=new Astack();
DataInputStream in=new DataInputStream(System.in);
do
{
try {
System.out.println("ARRAY STACK");
System.out.println("1.push 2.pop 3.display 4.exit");
System.out.println("enter ur choice:");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
System.out.println("enter the value to push:");
i=Integer.parseInt(in.readLine());
s.push(i);break;
case 2:
s.pop();break;
case 3:
System.out.println("the elements are:");
s.display();break;
case 4:break;
}}
catch(IOException e)
{
System.out.println("io error");
}
System.out.println("Do u want to continue 0 to quit and 1 to continue ");
c=Integer.parseInt(in.readLine());
}while(c==1);
}
}
OUTPUT
PROGRAM TO PERFORM STRING OPERATIONS USING
ARRAYLIST
PROGRAM
import java.util.*;
import java.io.*;
public class arraylistexample
{
public static void main(String args[])throws IOException
{
ArrayList<String> obj = new ArrayList<String>();
DataInputStream in=new DataInputStream(System.in);
int c,ch;
int i,j;
String str,str1;
do
{
System.out.println("STRING MANIPULATION");
System.out.println("******************************");
System.out.println(" 1. Append at end \t 2.Insert at particular index \t 3.Search \t");
System.out.println( "4.List string that starting with letter \t");
System.out.println("5.Size \t 6.Remove \t 7.Sort\t 8.Display\t" );
System.out.println("Enter the choice ");
c=Integer.parseInt(in.readLine());
switch(c)
{
case 1:
{
System.out.println("Enter the string ");
str=in.readLine();
obj.add(str);
break;
}
case 2:
{
System.out.println("Enter the string ");
str=in.readLine();
System.out.println("Specify the index/position to insert");
i=Integer.parseInt(in.readLine());
obj.add(i-1,str);
System.out.println("The array list has following elements:"+obj);
break;
}
case 3:
{
System.out.println("Enter the string to search ");
str=in.readLine();
j=obj.indexOf(str);
if(j==-1)
System.out.println("Element not found");
else
System.out.println("Index of:"+str+"is"+j);
break;
}
case 4:
{
System.out.println("Enter the character to List string that starts with specified
character");
str=in.readLine();
for(i=0;i<(obj.size()-1);i++)
{
str1=obj.get(i);
if(str1.startsWith(str))
{
System.out.println(str1);
}
}
break;
}
case 5:
{
System.out.println("Size of the list "+obj.size());
break;
}
case 6:
{
System.out.println("Enter the element to remove");
str=in.readLine();
if(obj.remove(str))
{
System.out.println("Element Removed"+str);
}
else
{
System.out.println("Element not present");
}
break;
}
case 7:
{
Collections.sort(obj);
System.out.println("The array list has following elements:"+obj);
break;
}
case 8:
{
System.out.println("The array list has following elements:"+obj);
break;
}
}
System.out.println("enter 0 to break and 1 to continue");
ch=Integer.parseInt(in.readLine());
}while(ch==1);
}
}

OUTPUT:
PROGRAM TO CALCULATE AREA USING ABSTRACT CLASS

PROGRAM

import java.util.*;
abstract class shape
{
int a,b;
abstract public void printarea();
}
class rectangle extends shape
{
public int area_rect;
public void printarea()
{
Scanner s=new Scanner(System.in);
System.out.println("enter the length and breadth of rectangle");
a=s.nextInt(); b=s.nextInt();
area_rect=a*b;
System.out.println("Length of rectangle "+a +"breadth of rectangle "+b);
System.out.println("The area ofrectangle is:"+area_rect);
}}
class triangle extends shape
{
double area_tri;
public void printarea()
{
Scanner s=new Scanner(System.in);
System.out.println("enter the base and height of triangle");
a=s.nextInt(); b=s.nextInt();
System.out.println("Base of triangle "+a +"height of triangle "+b);
area_tri=(0.5*a*b);
System.out.println("The area of triangle is:"+area_tri);
}}
class circle extends shape
{
double area_circle;
public void printarea()
{
Scanner s=new Scanner(System.in);
System.out.println("enter the radius of circle");
a=s.nextInt();
area_circle=(3.14*a*a);
System.out.println("Radius of circle"+a);
System.out.println("The area of circle is:"+area_circle);
}}
public class shapeclass
{
public static void main(String[] args)
{
rectangle r=new rectangle();r.printarea();
triangle t=new triangle(); t.printarea();
circle r1=new circle();
r1.printarea();
}}

OUTPUT
PROGRAM TO IMPLEMENT USER DEFINED EXCEPTION
HANDLING
A)PROGRAM:

import java.util.Scanner;
class NegativeAmtException extends Exception
{
String msg;
NegativeAmtException(String msg)
{
this.msg=msg;
}
public String toString()
{
return msg;
}
}
public class userdefined
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter Amount:");
int a=s.nextInt();
try
{
if(a<0)
{
throw new NegativeAmtException("Invalid Amount");
}
System.out.println("Amount Deposited");
}
catch(NegativeAmtException e)
{
System.out.println(e);
}
}
}
OUTPUT
B) PROGRAM

class MyException extends Exception{


String str1;
MyException(String str2)
{
str1=str2;
}
public String toString()
{
return ("MyException Occurred: "+str1) ;
}
}
class example
{
public static void main(String args[])
{
try
{
System.out.println("Starting of try block");
throw new MyException("This is My error Message");
}
catch(MyException exp)
{
System.out.println("Catch Block") ;
System.out.println(exp) ;
}
}}
OUTPUT
PROGRAM FOR DISPLAYING FILE INFORMATION

PROGRAM
import java.io.*;
import java.util.*;
class filedemo
{
public static void main(String args[])
{
String filename;
Scanner s=new Scanner(System.in);
System.out.println("Enter the file name ");
filename=s.nextLine();
File f1=new File(filename);
System.out.println("*****************");
System.out.println("FILE INFORMATION");
System.out.println("*****************");
System.out.println("NAME OF THE FILE "+f1.getName());
System.out.println("PATH OF THE FILE "+f1.getPath());
System.out.println("PARENT"+f1.getParent());
if(f1.exists())
System.out.println("THE FILE EXISTS ");
else
System.out.println("THE FILE DOES NOT ExISTS ");
if(f1.canRead())
System.out.println("THE FILE CAN BE READ ");
else
System.out.println("THE FILE CANNOT BE READ ");
if(f1.canWrite())
System.out.println("WRITE OPERATION IS PERMITTED");
else
System.out.println("WRITE OPERATION IS NOT PERMITTED");
if(f1.isDirectory())
System.out.println("IT IS A DIRECTORY ");
else
System.out.println("NOT A DIRECTORY");
if(f1.isFile())
System.out.println("IT IS A FILE ");
else
System.out.println("NOT A FILE");
System.out.println("File last modified "+ f1.lastModified());
System.out.println("LENGTH OF THE FILE "+f1.length());
System.out.println("FILE DELETED "+f1.delete());
}
}
OUTPUT
PROGRAM TO IMPLEMENT MULTITHREADED APPLICATION

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 multithreadprog
{
public static void main(String[] args)
{
A a = new A();
a.start();
}
}
OUTPUT
PROGRAM TO FIND THE MAXIMUM VALUE FROM THE GIVEN
TYPE OF ELEMENTS USING GENERICS
PROGRAM

class MyClass<T extends Comparable<T>>


{
T[] vals;
MyClass(T[] o)
{
vals = o;
}
public T min()
{
T v = vals[0];
for(int i=1; i < vals.length; i++)
if(vals[i].compareTo(v) < 0)
v = vals[i];
return v;
}
public T max()
{
T v = vals[0];
for(int i=1; i < vals.length;i++)
if(vals[i].compareTo(v) > 0)
v = vals[i];
return v;
}}
class gendemo
{
public static void main(String args[])
{
int i;
Integer inums[]={10,2,5,4,6,1};
Character chs[]={'v','p','s','a','n','h'};
Double d[]={20.2,45.4,71.6,88.3,54.6,10.4};
MyClass<Integer> iob = new MyClass<Integer>(inums);
MyClass<Character> cob = new MyClass<Character>(chs);
MyClass<Double>dob = new MyClass<Double>(d);
System.out.println("Max value in inums: " + iob.max());
System.out.println("Min value in inums: " + iob.min());
System.out.println("Max value in chs: " + cob.max());
System.out.println("Min value in chs: " + cob.min());
System.out.println("Max value in chs: " + dob.max());
System.out.println("Min value in chs: " + dob.min());
}
}
OUTPUT
PROGRAM TO DESIGN A CALCULATOR USING EVENT DRIVEN
PROGRAMMING PARADIGM OF JAVA
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, pow3, exp,
fac, plus, min, div, log, rec, mul, eq, addSub, 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("/");
div.addActionListener(this);
buttonpanel.add(div);
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");
exp.addActionListener(this);
buttonpanel.add(exp);
fac = new JButton("n!");
fac.addActionListener(this);
buttonpanel.add(fac);
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
{
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("MC"))
{
m1 = 0;
tfield.setText("");
}
if (s.equals("MR"))
{
tfield.setText("");
tfield.setText(tfield.getText() + m1);
}
if (s.equals("M+"))
{
if (k == 1) {
m1 = Double.parseDouble(tfield.getText());
k++;
}
else
{
m1 += Double.parseDouble(tfield.getText());
tfield.setText("" + m1);
}
}
if (s.equals("M-"))
{
if (k == 1) {
m1 = Double.parseDouble(tfield.getText());
k++;
}
else
{
m1 -= Double.parseDouble(tfield.getText());
tfield.setText("" + m1);
}
}
if (s.equals("Sqrt"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = Math.sqrt(Double.parseDouble(tfield.getText()));
tfield.setText("");
field.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)
{
int er = 0;
if (x < 0)
{
er = 20;
return 0;
}
double i, s = 1;
for (i = 2; i <= x; i += 1.0)
s *= i;
return s;
}
public static void main(String args[])
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndF
eel");
}
catch (Exception e)
{
}
ScientificCalculator f = new ScientificCalculator();
f.setTitle("ScientificCalculator");
f.pack();
f.setVisible(true);
}
}

OUTPUT

You might also like