Anda di halaman 1dari 52

PROGRAM CODE:

import java.util.*;

class EBBill
{
public static void main(String args[])
{
String type;
long LastReading, CurrentReading;
long units;
double bill=0;

Scanner sc=new Scanner(System.in);

System.out.println("Last Reading: ");


LastReading=sc.nextLong();
System.out.println("Current Reading: ");
CurrentReading=sc.nextLong();

units=CurrentReading-LastReading;

System.out.println("Enter 'domestic' or 'commercial'


");
type=sc.next();

if(type.compareToIgnoreCase("domestic")==0)
{
if(units<=100)
bill=units;
else if(units<=200)
bill=100 + (units-100)*2.50;
else if(units<=500)
bill=100+(100*2.50)+(units-200)*4;
else
bill=100+(100*2.50)+(300*4)+(units-500)*6;
}
else if(type.compareToIgnoreCase("commercial")==0)
{
if(units<=100)
bill=units*2;
else if(units<=200)
bill=100*2 + (units-100)*4.50;
else if(units<=500)
bill=100*2 +(100*4.50)+(units-200)*6;
else
bill=100*2 +(100*4.50)+(300*6)+(units-
500)*7;
}
else
System.out.println("\nInvalid entry");

System.out.println("The bill amount is Rs."+bill);

}
}
OUTPUT:
PROGRAM CODE:
/*
DolINR.java
Exp2 - Conversion using packages
*/
package convert.currency;

public class DolINR


{
double Dol;
double INR;

public double DoltoINR(double Dol)


{
this.Dol=Dol;
INR=Dol*72.52;
return INR;
}

public double INRtoDol(double INR)


{
this.INR=INR;
Dol=INR/72.52;
return Dol;
}
};
/*
EuroINR.java
Exp2 - Conversion using packages
*/

package convert.currency;

public class EuroINR


{
double Euro;
double INR;

public double EurotoINR(double Euro)


{
this.Euro=Euro;
INR=Euro*84.27;
return INR;
}

public double INRtoEuro(double INR)


{
this.INR=INR;
Euro=INR/84.27;
return Euro;
}
};
/*
YenINR.java
Exp2 - Conversion using packages
*/

package convert.currency;

public class YenINR


{
double Yen;
double INR;

public double YentoINR(double Yen)


{
this.Yen=Yen;
INR=Yen*0.64;
return INR;
}

public double INRtoYen(double INR)


{
this.INR=INR;
Yen=INR/0.64;
return Yen;
}
};
/*
MetKm.java
Exp2 - Conversion using packages
*/

package convert.distance;

public class MetKm


{
double Met;
double Km;

public double MettoKm(double Met)


{
this.Met=Met;
Km=Met/1000;
return Km;
}

public double KmtoMet(double Km)


{
this.Km=Km;
Met=Km*1000;
return Met;
}
};
/*
MileKm.java
Exp2 - Conversion using packages
*/

package convert.distance;

public class MileKm


{
double Mile;
double Km;

public double MiletoKm(double Mile)


{
this.Mile=Mile;
Km=Mile*1.60934;
return Km;
}

public double KmtoMile(double Km)


{
this.Km=Km;
Mile=Km/1.60934;
return Mile;
}
};
/*
HourMinSec.java
Exp2 - Conversion using packages
*/

package convert.time;

public class HourMinSec


{
double Hour;
double Min;
double Sec;

public double HourtoMin(double Hour)


{
this.Hour=Hour;
Min=Hour*60;
return Min;
}

public double HourtoSec(double Hour)


{
this.Hour=Hour;
Sec=Hour*60*60;
return Sec;
}
};
/*
Main.java
Exp2 - Implementation of packages created
*/

import java.util.*;
import convert.time.*;
import convert.distance.*;
import convert.currency.*;

class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
HourMinSec t=new HourMinSec();
MetKm mek=new MetKm();
MileKm mik=new MileKm();
DolINR di=new DolINR();
EuroINR ei=new EuroINR();
YenINR yi=new YenINR();

double hour;
double met,mile,km;
double inr, dol, yen, euro;

System.out.println("TIME CONVERSION");
System.out.println("Hours: ");
hour=sc.nextDouble();
System.out.println("Hours to Minutes:
"+t.HourtoMin(hour));
System.out.println("Hours to Seconds:
"+t.HourtoSec(hour));

System.out.println("\n\nDISTANCE CONVERSION");
System.out.println("Kilometres: ");
km=sc.nextDouble();
System.out.println("Kilometres to metres:
"+mek.KmtoMet(km));
System.out.println("Kilometres to miles:
"+mik.KmtoMile(km));

System.out.println("\nMiles: ");
mile=sc.nextDouble();
System.out.println("Miles to kilometres:
"+mik.MiletoKm(mile));

System.out.println("\nMetres: ");
met=sc.nextDouble();
System.out.println("Metres to kilometres:
"+mek.MettoKm(met));

System.out.println("\n\nCURRENCY CONVERSION");
System.out.println("INR: ");
inr=sc.nextDouble();
System.out.println("INR to Dollar:
"+di.INRtoDol(inr));
System.out.println("INR to Euro:
"+ei.INRtoEuro(inr));
System.out.println("INR to Yen: "+yi.INRtoYen(inr));

System.out.println("Dollar: ");
dol=sc.nextDouble();
System.out.println("Dollar to INR:
"+di.DoltoINR(dol));

System.out.println("Euro: ");
euro=sc.nextDouble();
System.out.println("Euro to INR:
"+ei.EurotoINR(euro));

System.out.println("Yen: ");
yen=sc.nextDouble();
System.out.println("Yen to INR: "+yi.YentoINR(yen));
}
};
OUTPUT:
PROGRAM CODE:
import java.util.*;
import java.io.*;

class Employee
{

String empName;
String empID;
String address;
String mailID;
long mobile;
double DA;
double HRA;
double PF;
double StaffClubFund;

double GrossPay;
double NetPay;

void getDetails()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your details");
System.out.println("Name: ");
empName=sc.nextLine();
System.out.println("Employee ID: ");
empID=sc.nextLine();
System.out.println("Address: ");
address=sc.nextLine();
System.out.println("Mail ID: ");
mailID=sc.nextLine();
System.out.println("Mobile Number: ");
mobile=sc.nextLong();
}

void calc(double BP)


{
DA=0.97*BP;
HRA=0.1*BP;
PF=0.12*BP;
StaffClubFund=(0.01/100)*BP;
GrossPay=BP+DA+HRA;
NetPay=GrossPay-PF-StaffClubFund;
}

void printSlip(double BP)


{
System.out.println("PAY SLIP\n");

System.out.println("Name: "+empName);
System.out.println("ID: "+empID);
System.out.println("Address: "+address);
System.out.println("Mail ID: "+mailID);
System.out.println("Mobile number: "+mobile);

System.out.println("\nBasic pay: "+BP);


System.out.println("DA: "+DA);
System.out.println("HRA: "+HRA);
System.out.println("PF: "+PF);
System.out.println("Staff Club Fund:
"+StaffClubFund);
System.out.println("\nGross Pay: "+GrossPay);
System.out.println("Net Pay: "+NetPay);
}
};

class Programmer extends Employee


{
double BP;

public Programmer()
{
Scanner sc=new Scanner(System.in);
System.out.println("\nEnter the programmer's
basic pay: ");
BP=sc.nextDouble();

getDetails();
calc(BP);
printSlip(BP);
}
};
class AsstProfessor extends Employee
{
double BP;

public AsstProfessor()
{
Scanner sc=new Scanner(System.in);
System.out.println("\nEnter the assistant
professor's basic pay: ");
BP=sc.nextDouble();

getDetails();
calc(BP);
printSlip(BP);
}
};

class AssoProfessor extends Employee


{
double BP;

public AssoProfessor()
{
Scanner sc=new Scanner(System.in);
System.out.println("\nEnter the associate
professor's basic pay: ");
BP=sc.nextDouble();

getDetails();
calc(BP);
printSlip(BP);
}
};

public class PaySlip


{
public static void main(String args[])
{
int ch;
System.out.println("Employee type:\n1.
Programmer\n2.Assistant Professor\n3.Associate
Professor\nEnter your choice ");
Scanner sc=new Scanner(System.in);
ch=sc.nextInt();
switch(ch)
{
case 1:
Programmer prog=new Programmer();
break;

case 2:
AsstProfessor astProf=new
AsstProfessor();
break;

case 3:
AssoProfessor asoProf=new
AssoProfessor();
break;

default:
System.out.println("Invalid
choice");

}
};
OUTPUT:
PROGRAM CODE:
/*
StackOperations.java
*/

interface StackOperations
{
public void push(int val);
public void pop();
public void display();
};

/*
Stack.java
*/

import java.util.*;

class StackOverflow extends Exception


{
String msg;
StackOverflow(String msg)
{
this.msg=msg;
}
public String toString()
{
return "Stack Overflow:\n"+msg;
}
};

class StackEmpty extends Exception


{
String msg;
StackEmpty(String msg)
{
this.msg=msg;
}
public String toString()
{
return "Stack Empty:\n"+msg;
}
};

public class Stack implements StackOperations


{
int s[] = new int[10];
int length;

Stack()
{
length=0;
}

public void display()


{
System.out.println("The stack contains:");
for(int i=0;i<length;i++)
{
System.out.println(s[i]);
}
}

public void push(int val)


{
try
{
if(length>=10)
{
throw new StackOverflow("The stack is
already full");
}
s[length]=val;
length++;
}
catch(Exception e)
{
System.out.println(e);
}
}

public void pop()


{
try
{
if(length==0)
{
throw new StackEmpty("There are no
elements in the stack to be popped");
}
length--;
}
catch(Exception e)
{
System.out.println(e);
}

public static void main(String args[])


{
int val;
Stack st=new Stack();
int ch;
Scanner sc=new Scanner(System.in);

do
{
System.out.println("\n\n1. Push\n2. Pop\n3.
Display\n4. Exit\nEnter your choice ");
ch=sc.nextInt();

switch(ch)
{
case 1:
System.out.println("Enter the element
to be pushed ");
val=sc.nextInt();
st.push(val);
break;
case 2:
st.pop();
break;
case 3:
st.display();
break;
case 4:
break;
default:
System.out.println("Invalid choice");
}
}while(ch>0&&ch<4);
}
};
OUTPUT:
PROGRAM CODE:
import java.util.*;

public class Exp5


{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int ch;
String s;
ArrayList<String> strings=new ArrayList<String>();
do
{
System.out.println("1. Append a string\n2.
Insert a string at a particular index\n3. Search for a
string\n4. List all strings starting with a particular
letter\n5. Exit\nEnter your choice ");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter a string
");
s=sc.next();
strings.add(s);
System.out.println("ArrayList has
been updated");
System.out.println(strings);
break;

case 2:
System.out.println("Enter a string
");
s=sc.next();
System.out.println("Position of
insertion: ");
int pos=sc.nextInt();
strings.add(pos,s);
System.out.println("ArrayList has
been updated");
System.out.println(strings);
break;

case 3:
System.out.println("Enter the string
you are searching for ");
s=sc.next();
if(strings.contains(s))
System.out.println(s+" is
present in the ArrayList");
else
System.out.println("String not
found");
break;

case 4:
System.out.println("Starting letter:
");
char letter=sc.next().charAt(0);
for(int i=0;i<strings.size();i++)
{

if(strings.get(i).charAt(0)==letter)
{

System.out.println(strings.get(i));
}
}
break;
}
}while(ch>=0&&ch<=4);
}
};
OUTPUT:
PROGRAM CODE:
import java.util.*;

abstract class Shape


{
int num1, num2;
public void printArea()
{
}
};

class Rectangle extends Shape


{
Rectangle(int length, int breadth)
{
num1=length;
num2=breadth;
}
public void printArea()
{
System.out.println("Area of rectangle:
"+(num1*num2)+" sq. units");
}
};

class Triangle extends Shape


{
Triangle(int base, int height)
{
num1=base;
num2=height;
}
public void printArea()
{
System.out.println("Area of triangle:
"+(0.5*num1*num2)+" sq. units");
}
};

class Circle extends Shape


{
Circle(int radius)
{
num1=radius;
}
public void printArea()
{
System.out.println("Area of circle:
"+(3.14*num1*num1)+" sq. units");
}
};

class Areas
{
public static void main(String args[])
{
Rectangle r=new Rectangle(2,3);
r.printArea();

Triangle t=new Triangle(4,2);


t.printArea();

Circle c=new Circle(1);


c.printArea();
}
};
OUTPUT:
PROGRAM CODE:
/*
Java program to implement user defined exception handling.
*/

import java.util.*;

class NException extends Exception


{
String msg;

NException(String msg)
{
this.msg=msg;
}

public String toString()


{
return ("Exception name: NException\nMessage:
"+msg);
}
};

public class ExceptionN


{
public static void main(String args[])
{
int i;
Scanner sc=new Scanner(System.in);

try
{
System.out.println("Enter a number ");
i=sc.nextInt();

if(i<0)
{
NException e = new NException("Negative
numbers are not permitted");
throw e;
}

else
{
System.out.println("The number has been
accepted");
}
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println("Exception, if generated,
has been handled.");
}
}
};
OUTPUT:
PROGRAM CODE:
/*
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.
*/

import java.util.*;
import java.io.*;

public class FileHandling


{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the file name along with


directory");
String filename=sc.next();

try
{
File f=new File(filename);

//check for existence


if(f.exists())
{
//get the name of the file associated with
this file object
String name=f.getName();
System.out.println("The name of the file
is "+name);

System.out.println("The file exists");

//check for readability


if(f.canRead())
System.out.println("The file can be
read from");
else
System.out.println("The file cannot
be read from");

//check for writability


if(f.canWrite())
System.out.println("The file can be
written on");
else
System.out.println("The file cannot
be written on");

//type of file - based on extension


int i=name.lastIndexOf('.');
if(i!=-1)
{
String ext=name.substring(i+1);
System.out.println("File type:
"+ext);
}

//length of the file


System.out.println("The length of the file
in bytes is: "+f.length());
}
else
System.out.println("The file does not
exist");

}
catch(Exception e)
{
System.out.println(e);
}
}
};
OUTPUT:
PROGRAM CODE:
/*
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.
*/

import java.util.Random;
import java.util.*;
import java.lang.*;

class RandomGenerate extends Thread


{
int num;

public void run()


{
System.out.println("Number\tSquare/Cube");
try
{
Random rand=new Random();
for(int i=0;i<10;i++)
{
num=rand.nextInt(10);
System.out.print("\n"+num+"\t");
if(num%2==0)
{
EvenSquare e=new EvenSquare(num);
e.start();
}
else
{
OddCube o=new OddCube(num);
o.start();
}
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
};

class EvenSquare extends Thread


{
int n;

EvenSquare(int num)
{
n=num;
}

public void run()


{
int square=n*n;
System.out.print(square);
}
};

class OddCube extends Thread


{
int n;

OddCube(int num)
{
n=num;
}

public void run()


{
int cube=n*n*n;
System.out.print(cube);
}
};
class Multithread
{
public static void main(String args[])
{
RandomGenerate r=new RandomGenerate();
r.start();
}
};
OUTPUT:
PROGRAM CODE:
/*
Find the maximum value from the given type of elements using a
generic function.
*/

import java.util.*;
import java.lang.*;

class Gen
{

public <T extends Comparable<T>> void max(T a, T b)


{
if(a.compareTo(b)>0)
System.out.println(a+" is greater than "+b);
else
System.out.println(b+" is greater than "+a);

public static void main(String args[])


{
Gen obj=new Gen();
obj.max(2,3);
obj.max(5.5,2.3);
obj.max('c','a');
}
};
OUTPUT:
PROGRAM CODE:
/*
Calculator – using event-driven programming paradign of Java
*/

import java.awt.*;
import java.awt.event.*;

class Calculator extends Frame implements ActionListener


{
TextField txt;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bs,bm,bd,be,bc;
String temp, r, n2, num1="0", num2="0";
int op=0, result=0, index;

Calculator()
{
Frame f = new Frame("Calculator");
f.setBounds(500,200,200,300);
f.setLayout(new FlowLayout());

txt=new TextField(20);
f.add(txt);
//txt.setBounds(100,30,20,20);
//txt.setText("It works!");

Panel p=new Panel();


GridLayout g=new GridLayout(7,3,5,5);
p.setLayout(g);

b9=new Button("9");
b9.addActionListener(this);
p.add(b9);

b8=new Button("8");
b8.addActionListener(this);
p.add(b8);

b7=new Button("7");
b7.addActionListener(this);
p.add(b7);

b6=new Button("6");
b6.addActionListener(this);
p.add(b6);

b5=new Button("5");
b5.addActionListener(this);
p.add(b5);

b4=new Button("4");
b4.addActionListener(this);
p.add(b4);

b3=new Button("3");
b3.addActionListener(this);
p.add(b3);

b2=new Button("2");
b2.addActionListener(this);
p.add(b2);

b1=new Button("1");
b1.addActionListener(this);
p.add(b1);

//empty panel added for favourable spacing


Panel p1=new Panel();
p.add(p1);

b0=new Button("0");
b0.addActionListener(this);
p.add(b0);

//empty panel added for favourable spacing


Panel p2=new Panel();
p.add(p2);

Panel s1=new Panel();


p.add(s1);

Panel s2=new Panel();


p.add(s2);

Panel s3=new Panel();


p.add(s3);

ba=new Button("+");
ba.addActionListener(this);
p.add(ba);

bs=new Button("-");
bs.addActionListener(this);
p.add(bs);

bm=new Button("x");
bm.addActionListener(this);
p.add(bm);

bd=new Button("/");
bd.addActionListener(this);
p.add(bd);

be=new Button("=");
be.addActionListener(this);
p.add(be);

bc=new Button("CLR");
bc.addActionListener(this);
p.add(bc);

f.add(p);
f.setVisible(true);
}

public void actionPerformed(ActionEvent e)


{
if(e.getSource()==b0)
{
temp=txt.getText();
temp+="0";
txt.setText(temp);
}

if(e.getSource()==b1)
{
temp=txt.getText();
temp+="1";
txt.setText(temp);
}

if(e.getSource()==b2)
{
temp=txt.getText();
temp+="2";
txt.setText(temp);
}

if(e.getSource()==b3)
{
temp=txt.getText();
temp+="3";
txt.setText(temp);
}

if(e.getSource()==b4)
{
temp=txt.getText();
temp+="4";
txt.setText(temp);
}

if(e.getSource()==b5)
{
temp=txt.getText();
temp+="5";
txt.setText(temp);
}
if(e.getSource()==b6)
{
temp=txt.getText();
temp+="6";
txt.setText(temp);
}

if(e.getSource()==b7)
{
temp=txt.getText();
temp+="7";
txt.setText(temp);
}

if(e.getSource()==b8)
{
temp=txt.getText();
temp+="8";
txt.setText(temp);
}

if(e.getSource()==b9)
{
temp=txt.getText();
temp+="9";
txt.setText(temp);
}

if(e.getSource()==bc)
{
txt.setText("");
num1="0";
}

if(e.getSource()==ba)
{
num1=txt.getText();
op=1;
txt.setText(num1+"+");
}
if(e.getSource()==bs)
{
num1=txt.getText();
op=2;
txt.setText(num1+"-");
}

if(e.getSource()==bm)
{
num1=txt.getText();
op=3;
txt.setText(num1+"x");
}

if(e.getSource()==bd)
{
num1=txt.getText();
op=4;
txt.setText(temp+"/");
}

if(e.getSource()==be)
{
if(op==1)
{
temp=txt.getText();
index=temp.lastIndexOf('+');
num2=temp.substring(index+1);

result=Integer.parseInt(num1)+Integer.parseInt(num2);
r=Integer.toString(result);
txt.setText(r);
}

if(op==2)
{
temp=txt.getText();
index=temp.lastIndexOf('-');
num2=temp.substring(index+1);
result=Integer.parseInt(num1)-
Integer.parseInt(num2);
r=Integer.toString(result);
txt.setText(r);
}

if(op==3)
{
temp=txt.getText();
index=temp.lastIndexOf('x');
num2=temp.substring(index+1);

result=Integer.parseInt(num1)*Integer.parseInt(num2);
r=Integer.toString(result);
txt.setText(r);
}

if(op==4)
{
temp=txt.getText();
index=temp.lastIndexOf('/');
num2=temp.substring(index+1);

result=Integer.parseInt(num1)/Integer.parseInt(num2);
r=Integer.toString(result);
txt.setText(r);
}
}
}

public static void main(String args[])


{
Calculator c=new Calculator();
}
};
OUTPUT:

Anda mungkin juga menyukai