2. E:java programing>java BookDemo
BOOK NAME :the yogi
BOOK ID :111
BOOK AUTHOR :baba
Updated details are :
BOOK NAME :titanic
BOOK ID :222
BOOK AUTHOR :cameroon james
3. EXP.NO:-01 LAB EXERCISE:-
import java.lang.*;
import java.util.*;
class Point
{
double x,y;
Point()
{
x=y=0;
}
Point(double a,double b)
{
x=a;y=b;
}
Point(Point l)
{
x=l.x;
y=l.y;
}
double Finddistance(double a,double b)
{
return (Math.sqrt((x-a)*(x-a)+(y-b)*(y-b)));
}
double Finddistance(Point l)
{
return (Math.sqrt((x-l.x)*(x-l.x)+(y-l.y)*(y-l.y)));
}
void display()
{
System.out.println("("+x+ ","+y+ ")");
}
public static void main(String args[])
{
Point p1=new Point(3.25,7.89);
Point p2=new Point(5.37,18.12);
Point p3=new Point(p2);
p1.display();
p2.display();
4. p3.display();
double c=p1.Finddistance(7.9,16.25);
System.out.println("distance between given points and p1 is:"+c);
c=p1.Finddistance(p3);
System.out.println("distance between p1 and p3 is:"+c);
}
}
E:java programing>javac Point.java
E:java programing>java Point
(3.25,7.89)
(5.37,18.12)
(5.37,18.12)
distance between given points and p1 is:9.566195691078036
distance between p1 and p3 is:10.447358517826409
5. EXP.NO:-02 PRELAB EXERCISE:-
SHAPE2D:-
import java.lang.*;
import java.util.*;
abstract class Shape2d
{
double x,y;
Shape2d(double a,double b)
{
x=a;y=b;
}
abstract double area();
abstract void display();
}
class Rectangle extends Shape2d
{
Rectangle(double a,double b)
{
super(a,b);
}
double area()
{
return(x*y);
}
void display()
{
System.out.println("length="+x+"breadth="+y+"Area of rectangle="+area());
}
}
class Triangle extends Shape2d
{
Triangle(double a,double b)
{
super(a,b);
}
double area()
{
return(0.5*x*y);
}
void display()
6. {
System.out.println("length="+x+"breadth="+y+"Area of triangle="+area());
}
}
class Shape2dDemo
{
public static void main(String args[])
{
Shape2d s=new Rectangle(1,2);
s.display();
s=new Triangle(1,2);
s.display();
}
}
E:java programing>javac Shape2dDemo.java
E:java programing>java Shape2dDemo
length=1.0breadth=2.0Area of rectangle=2.0
length=1.0breadth=2.0Area of triangle=1.0
8. void calc_interest()
{
balance+=(0.04*balance);
System.out.println("current balance with one year interest="+balance);
}
}
class Customer
{
int custid;String name,address;
SBAccount m;
Customer(int id,String cname,String caddress,int accno,double initialbalance)
{
custid=id;
name=cname;
address=caddress;
m=new SBAccount(accno,initialbalance);
System.out.println("custid="+id+"n name="+cname+"n address="+caddress + "n
accountno=" +accno +"n balance="+initialbalance);
}
void transaction(int a)
{
Scanner s= new Scanner(System.in);
switch(a)
{
case 1:
System.out.println("enter the amount=");
a=s.nextInt();
m.deposit(a);
break;
case 2:
System.out.println("enter the amount=");
a=s.nextInt();
m.withdraw(a);
break;
case 3:
m.calc_interest();
break;
}
}
9. }
class BankDemo
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
int i=0;
Customer c=new Customer(1,"harry","12,herbert town,mars",221003116,50000.00);
while(i!=1)
{
System.out.println("enter 1 for deposit ,n enter 2 for withdraw,n enter 3 for calculating
interest ,n enter 4 for exit :");
int b=s.nextInt();
if(b==4)
{
i=1;
System.out.println("visit again");
}
else
c.transaction(b);
}
}
}
E:java programing>javac BankDemo.java
E:java programing>java BankDemo
custid=1
name=harry
address=12,herbert town,mars
accountno=221003116
balance=50000.0
enter 1 for deposit ,
enter 2 for withdraw,
enter 3 for calculating interest ,
enter 4 for exit :
1
enter the amount=
500
10. current balance=50500.0amount deposited=500.0
enter 1 for deposit ,
enter 2 for withdraw,
enter 3 for calculating interest ,
enter 4 for exit :
2
enter the amount=
300
current balance=50200.0amount withdrawed=300.0
enter 1 for deposit ,
enter 2 for withdraw,
enter 3 for calculating interest ,
enter 4 for exit :
3
current balance with one year interest=52208.0
enter 1 for deposit ,
enter 2 for withdraw,
enter 3 for calculating interest ,
enter 4 for exit :
4
visit again
11. EXP.NO:-03 PRELAB EXERCISE:-
package shape2d;
import java.lang.*;
import java.util.*;
public interface twod
{
double area();
double perimeter();
}
package shape2d;
import java.lang.*;
import java.util.*;
public class Circle implements twod
{
double r;
public Circle(double a)
{
r=a;
}
public double area()
{
return (3.14*r*r);
}
public double perimeter()
{
return (2*3.14*r);
}
}
package shape2d;
import java.lang.*;
import java.util.*;
public class Rectangle implements twod
{
double l,br;
public Rectangle(double a,double b)
{
12. l=a;br=b;
}
public double area()
{
return (l*br);
}
public double perimeter()
{
return (2*(l+br));
}
}
package shape2d;
import java.lang.*;
import java.util.*;
public class Square implements twod
{
double side;
public Square(double x)
{
side=x;
}
public double area()
{
return (side*side);
}
public double perimeter()
{
return (4*side);
}
}
import java.lang.*;
import java.util.*;
import shape2d.*;
class ShapeDemo
{
public static void main(String args[])
{
Circle f=new Circle(1.0);
13. System.out.println("Area of Circle="+f.area());
System.out.println("Perimeter of Circle="+f.perimeter());
Square g=new Square(2.0);
System.out.println("Area of Square="+g.area());
System.out.println("Perimeter of Square ="+g.perimeter());
Rectangle c=new Rectangle(1.0,2.0);
System.out.println("Area of Rectangle="+c.area());
System.out.println("Perimeter of Rectangle="+c.perimeter());
}
}
E:java programing>javac shape2dtwod.java
E:java programing>javac shape2dSquare.java
E:java programing>javac shape2dRectangle.java
E:java programing>javac shape2dCircle.java
E:java programing>javac ShapeDemo.java
E:java programing>java ShapeDemo
Area of Circle=3.14
Perimeter of Circle=6.28
Area of Square=4.0
Perimeter of Square =8.0
Area of Rectangle=2.0
Perimeter of Rectangle=6.0
14. EXP.NO:-03 LAB EXERCISE:-
package pkbanking.pkinterface;
import java.lang.*;
import java.util.*;
public interface InterestRate
{
double sbrate=0.04;
}
package pkbanking.pkinterface;
import java.lang.*;
import java.util.*;
public interface Transaction
{
double min_balance=500.0;
void withdraw(double a);
public void deposit(double a);
}
package pkaccount;
import java.lang.*;
import java.util.*;
abstract public class Account
{
protected int accnumber;
protected double balance;
public Account(int a,double b)
{
accnumber=a;
balance=b;
}
}
package pkaccount.sb;
import java.lang.*;
import pkaccount.*;
import pkbanking.pkinterface.*;
import java.util.*;
public class SBAccount extends Account implements Transaction,InterestRate
15. {
public SBAccount(int an,double ib)
{
super(an,ib);
}
public void deposit(double amount)
{
if(amount>0)
{
balance+=amount;
System.out.println("current balance="+balance+"amount deposited="+amount);
}
}
public void withdraw(double amount)
{
if((balance-amount)>min_balance)
{
balance-=amount;
System.out.println("current balance="+balance+"amount withdrawed="+amount);
}
else
System.out.println("insufficient balance found "+"current balance="+balance+"amount
withdraw request="+amount);
}
public void calc_interest()
{
balance+=(sbrate*balance);
System.out.println("current balance with one year interest="+balance);
}
}
package pkcustomer;
import pkaccount.sb.*;
import java.lang.*;
import java.util.*;
public class Customer
{
int custid;
String name,address;
16. SBAccount m;
public Customer(int id,String cname,String caddress,int accno,double initialbalance)
{
custid=id;
name=cname;
address=caddress;
m=new SBAccount(accno,initialbalance);
System.out.println("custid="+id+"n name="+cname+"n address="+caddress + "n
accountno=" +accno +"n balance="+initialbalance);
}
public void transaction(int a)
{
Scanner s= new Scanner(System.in);
switch(a)
{
case 1:
System.out.println("enter the amount=");
a=s.nextInt();
m.deposit(a);
break;
case 2:
System.out.println("enter the amount=");
a=s.nextInt();
m.withdraw(a);
break;
case 3:
m.calc_interest();
break;
}
}
}
import java.lang.*;
import pkcustomer.*;
import java.util.*;
class BankDemo
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
17. int i=0;
Customer c=new Customer(1,"harry","12,herbert town,mars",221003116,50000.00);
while(i!=1)
{
System.out.println("enter 1 for deposit ,n enter 2 for withdraw,n enter 3 for calculating
interest ,n enter 4 for exit :");
int b=s.nextInt();
if(b==4)
{
i=1;
System.out.println("visit again");
}
else
c.transaction(b);
}
}
}
E:java programing>javac pkbankingpkinterfaceInterestRate.java
E:java programing>javac pkbankingpkinterfaceTransaction.java
E:java programing>javac pkaccountAccount.java
E:java programing>javac pkaccountsbSBAccount.java
E:java programing>javac pkcustomerCustomer.java
E:java programing>javac BankDemo.java
E:java programing>java BankDemo
custid=1
name=harry
address=12,herbert town,mars
accountno=221003116
balance=50000.0
enter 1 for deposit ,
enter 2 for withdraw,
enter 3 for calculating interest ,
enter 4 for exit :
1
enter the amount=
500
current balance=50500.0amount deposited=500.0
enter 1 for deposit ,
enter 2 for withdraw,
18. enter 3 for calculating interest ,
enter 4 for exit :
2
enter the amount=
300
current balance=50200.0amount withdrawed=300.0
enter 1 for deposit ,
enter 2 for withdraw,
enter 3 for calculating interest ,
enter 4 for exit :
3
current balance with one year interest=52208.0
enter 1 for deposit ,
enter 2 for withdraw,
enter 3 for calculating interest ,
enter 4 for exit :
4
visit again
20. public static void main(String arg[])
{
try {
int i,y=0,z;Book ob[]=new Book[5];char c;String m,n;
DataInputStream obe=new DataInputStream(System.in);Scanner s=new Scanner(System.in);
ob[0]=new Book("Stephen Hawking","The Nature of Space and Time",1500.00,"Princeton
University Press",1);
ob[1]=new Book("Mazidi","8051microcontroller",600.00,"Dorling Kindersley",4);
ob[2]=new Book("Ali Bahrami","OOSD",300.00,"Tata McGraw Hill",10);
ob[3]=new Book("Rabindranath Tagore","Gitanjali",800.00,"Scribner",8);
ob[4]=new Book("Rabindranath Tagore","The Heart of God",1000.00,"Tuttle Publishing",3);
System.out.println("tDetails of the books are :n ");
for(i=0;i<5;i++)
{
ob[i].display();
}
do{
System.out.println("Enter the Author and Title of the Book : ");
m=obe.readLine();
n=obe.readLine();
for(i=0;i<5;i++)
{
if(ob[i].Author().equals(m))
{
if(ob[i].Title().equals(n))
{
y=1;ob[i].display();
System.out.println("ntEnter the number of copies required :");
z=Integer.parseInt(obe.readLine());
if(z<=ob[i].StockPos()&&z>0)
{System.out.println("Total amount for "+z+" copies of "+n+"Book is="+(z*ob[i].Price()));}
else if(z>ob[i].StockPos())
{ System.out.println("Required copies not in stock");}
else if(z<0)
{throw new IllegalArgumentException("Negative Number is Entered"); }
else if(z==0)
{System.out.println("Have you wont need the Book!");}
}
else
y=0;
21. }
}
if(y==0)System.out.println("Requested Book is not available in the BookShop");
System.out.println("Do You Wish To Buy The Book! [ Press 'n' to come out to the Book Shop
]");
c=s.next().charAt(0);
}while(c!='n');
System.out.println("tVisit Again");
}
catch(Exception e){System.out.println(e.getMessage());}
}
}
C:app>javac BookDemo.java
C:app>java BookDemo
Details of the books are :
BOOK AUTHOR :Stephen Hawking
BOOK TITLE :The Nature of Space and Time
BOOK PRICE :1500.0rupees
BOOK PUBLISHER :Princeton University Press
BOOK STOCK POSITION :1 no.s
BOOK AUTHOR :Mazidi
BOOK TITLE :8051microcontroller
BOOK PRICE :600.0rupees
BOOK PUBLISHER :Dorling Kindersley
BOOK STOCK POSITION :4 no.s
BOOK AUTHOR :Ali Bahrami
BOOK TITLE :OOSD
BOOK PRICE :300.0rupees
BOOK PUBLISHER :Tata McGraw Hill
BOOK STOCK POSITION :10 no.s
BOOK AUTHOR :Rabindranath Tagore
22. BOOK TITLE :Gitanjali
BOOK PRICE :800.0rupees
BOOK PUBLISHER :Scribner
BOOK STOCK POSITION :8 no.s
BOOK AUTHOR :Rabindranath Tagore
BOOK TITLE :The Heart of God
BOOK PRICE :1000.0rupees
BOOK PUBLISHER :Tuttle Publishing
BOOK STOCK POSITION :3 no.s
Enter the Author and Title of the Book :
Mazidi
8051microcontroller
BOOK AUTHOR :Mazidi
BOOK TITLE :8051microcontroller
BOOK PRICE :600.0rupees
BOOK PUBLISHER :Dorling Kindersley
BOOK STOCK POSITION :4 no.s
Enter the number of copies required :
3
Total amount for 3 copies of 8051microcontrollerBook is=1800.0
Do You Wish To Buy The Book! [ Press 'n' to come out to the Book Shop ]
k
Enter the Author and Title of the Book :
Mazidi
8051microcontroller
BOOK AUTHOR :Mazidi
BOOK TITLE :8051microcontroller
BOOK PRICE :600.0rupees
BOOK PUBLISHER :Dorling Kindersley
BOOK STOCK POSITION :4 no.s
Enter the number of copies required :
23. 5
Required copies not in stock
Do You Wish To Buy The Book! [ Press 'n' to come out to the Book Shop ]
l
Enter the Author and Title of the Book :
Mazidi
8051microcontroller
BOOK AUTHOR :Mazidi
BOOK TITLE :8051microcontroller
BOOK PRICE :600.0rupees
BOOK PUBLISHER :Dorling Kindersley
BOOK STOCK POSITION :4 no.s
Enter the number of copies required :
-1
Negative Number is Entered
24. EXP.NO:-04 LAB EXERCISE:-
import java.util.*;
class OperationFailedException extends Exception{
String des;
public OperationFailedException(String d) {
des=d;
}
public OperationFailedException(String d,Throwable e) {
des=d;
System.out.println(" " + ((OperationFailedException)e).des);
}
public String toString(){
return ("Output character = "+des ) ;
}
}
class BadOperandException extends Exception{
int op;
public BadOperandException(int op1) {
op=op1;
}
public String toString(){
return ("Output character = "+op) ;
}
}
class BadOperatorException extends Exception{
char opr;
public BadOperatorException(char opr1) {
opr=opr1;
}
public String toString(){
return ("Output character = "+opr) ;
}
}
class main1
{
26. OperationFailedException e2=new OperationFailedException("INVALID
SECOND OPERAND");
OperationFailedException e1=new OperationFailedException("INVALID
OPERAND",e2);
e1.initCause(new BadOperandException(j));
throw e1;}
}
catch(OperationFailedException f)
{
System.out.println("Caught:"+f);
System.out.println("Caught:"+f.getCause());
}
}
else{
OperationFailedException e2=new OperationFailedException("INVALID FIRST
OPERAND");
OperationFailedException e1=new OperationFailedException("INVALID
OPERAND",e2);
e1.initCause(new BadOperandException(i));
throw e1;}
}
catch(OperationFailedException f)
{
System.out.println("Caught:"+f);
System.out.println("Caught:"+f.getCause());
}
}
}
C:app>javac main1.java
C:app>java main1
Enter two operands and operator as input:
20000
1000
*
20000000
27. C:app>java main1
Enter two operands and operator as input:
1000
1000
*
INVALID FIRST OPERAND
Caught:Output character = INVALID OPERAND
Caught:Output character = 1000
C:app>java main1
Enter two operands and operator as input:
20000
1000
$
Caught:Output character = INVALID OPERATOR
Caught:Output character = $
C:app>java main1
Enter two operands and operator as input:
20000
100
/
INVALID SECOND OPERAND
Caught:Output character = INVALID OPERAND
Caught:Output character = 100
28. EXP.NO:-05 PRELAB EXERCISE:-
import java.lang.*;
import java.util.*;
class factorial extends Thread
{
public void run()
{
try
{
System.out.println("FACTORIAL THREAD PRIORITY = "+this.getPriority());
Scanner s= new Scanner(System.in);
System.out.println("status of multab while completing task is:"+this.isAlive());
System.out.println("Enter the limit of factorial: ");
int n=s.nextInt();
int j=1;
for(int i=1;i<=n;i++)
{
j=j*i;
}
System.out.println("factorial of "+n+"is"+j);
Thread.sleep(500);
}
catch(InterruptedException e){}
}
}
class sum extends Thread
{
public void run()
{
System.out.println("SUM THREAD PRIORITY = "+this.getPriority());
try
{
Scanner s= new Scanner(System.in);
System.out.println("Enter the limit of sum of the series: ");
int n=s.nextInt();
int j=0;
for(int i=1;i<=n;i++)
{
29. j=j+i;
}
System.out.println("sum of the 1 to "+n+" numbers is"+j);
Thread.sleep(500);
}
catch(InterruptedException e){}
}
}
class multab extends Thread
{
public void run()
{
System.out.println("MULTAB THREAD PRIORITY = "+this.getPriority());
try
{
Scanner s= new Scanner(System.in);
System.out.println("Enter the limit of multiplication table: ");
int n=s.nextInt();
System.out.println("Enter the number which has to be multiplied: ");
int k=s.nextInt();
for(int i=1;i<=n;i++)
{
System.out.println(i+"*"+k+"="+(k*i));
Thread.sleep(500);
}
}
catch(InterruptedException e){}
}
}
class ThreadBasic
{
public static void main(String args[]) throws Exception
{
factorial t1=new factorial();
sum t2=new sum();
multab t3=new multab();
t1.setPriority(10);
t2.setPriority(5);
30. t3.setPriority(2);
t1.start();
t1.join();
System.out.println("status of multab after completing task is:"+t1.isAlive());
t2.start();
t2.join();
t3.start();
}
}
E:java programing>javac ThreadBasic.java
E:java programing>java ThreadBasic
FACTORIAL THREAD PRIORITY = 10
status of multab while completing task is:true
Enter the limit of factorial:
5
factorial of 5 is 120
status of multab after completing task is:false
SUM THREAD PRIORITY = 5
Enter the limit of sum of the series:
3
sum of the 1 to 3 numbers is 6
MULTAB THREAD PRIORITY = 2
Enter the limit of multiplication table:
2
Enter the number which has to be multiplied:
3
1*3=3
2*3=6
31. EXP.NO:-05 LAB EXERCISE:-
import java.util.*;
import java.lang.*;
class Queue
{
int q[],front, rear, size, len,m;
public Queue(int n)
{
size=n;
len = 0;
q = new int[size];
front =rear = -1;
}
public boolean isEmpty()
{
return front == -1;
}
public int size()
{
return len ;
}
public void add(int i)
{
if (rear == -1)
{
front = rear = 0;
q[rear] = i;
}
else if ( rear + 1 < size)
q[++rear] = i;
len++ ;
}
public int remove()
{
if (!isEmpty())
{
len-- ;
m = q[front];
if ( front == rear)
32. {
front = rear = -1;
}
else
front++;
} return m;
}
synchronized void get() throws InterruptedException
{
while (isEmpty())
{
Thread.sleep(1000);
System.out.println("Queue is empty " + Thread.currentThread().getName() + " is waiting
, size: " + size());
wait();
}
Thread.sleep(1000);
int i = (Integer) remove();
System.out.println("Consumed: " + i);
notify();
}
synchronized void put(int i) throws InterruptedException
{
while (size() == size)
{
Thread.sleep(1000);
System.out.println("Queue is full " + Thread.currentThread().getName() + " is waiting ,
size: " +size());
wait();
}
Thread.sleep(1000);
add(i);
System.out.println("Produced: " + i);
notify();
}
}
class Producer implements Runnable
{
Queue Qobj;
33. public Producer(Queue obj)
{
this.Qobj = obj;
}
public void run()
{
int counter = 0;
while (true)
{
try{ Qobj.put(counter++); }catch(Exception e){}
}
}
}
class Consumer implements Runnable
{
Queue Qobj;
public Consumer(Queue obj)
{
this.Qobj = obj;
}
public void run()
{
while (true)
{
try{ Qobj.get(); }catch(Exception e){}
}
}
}
public class ProducerConsumer
{
public static void main(String[] args)
{
int QSize = 5;
Queue q=new Queue(QSize);
new Thread(new Producer(q), "Producer").start();
new Thread(new Consumer(q), "Consumer").start();
}
}
34. E:java programing>javac ProducerConsumer.java
E:java programing>java ProducerConsumer
Produced: 0
Produced: 1
Produced: 2
Produced: 3
Produced: 4
Queue is full Producer is waiting , size: 5
Consumed: 0
Consumed: 1
Consumed: 2
Consumed: 3
Consumed: 4
Queue is empty Consumer is waiting , size: 0
Produced: 5
Produced: 6
Produced: 7
Produced: 8
Produced: 9
Queue is full Producer is waiting , size: 5
Consumed: 5
Consumed: 6
Consumed: 7
Consumed: 8
Consumed: 9
Queue is empty Consumer is waiting , size: 0
Produced: 10
Produced: 11
Produced: 12
Produced: 13
Produced: 14
Queue is full Producer is waiting , size: 5
Consumed: 10
.
.
.
36. k.capitalizePlurals(a1);
System.out.println("ArrayList after capitalizePlurals=t"+a1);
k.removePlurals(a1);
System.out.println("ArrayList after removing Plural words=t"+a1);
k.reverse(a1);
System.out.println("ArrayList after reversing the order of the elements in words=t"+a1);
}
public void reverse(ArrayList<String> a1)
{
Collections.reverse(a1);
}
public void capitalizePlurals(ArrayList<String> a1)
{
for(int i=0;i<a1.size();i++)
{
String s=a1.get(i);
if(s.charAt(s.length()-1)=='s')
{
a1.set(i,s.toUpperCase());
}
}
}
public void removePlurals(ArrayList<String> a1)
{
for(int i=0;i<a1.size();i++)
{
String s=a1.get(i);
if((s.charAt(s.length()-1)=='s' )||(s.charAt(s.length()-1)=='S'))
a1.remove(i);
}
}
}
E:java programing>javac ArrayList11.java
E:java programing>java ArrayList11
Original String ArrayList= [hello, photons, sorry, space, comics]
ArrayList after capitalizePlurals= [hello, PHOTONS, sorry, space, COMICS]
ArrayList after removing Plural words= [hello, sorry, space]
ArrayList after reversing the order of the elements in words= [space, sorry, hello]
37. EXP.NO:-06 LAB EXERCISE:-
import java.lang.*;
import java.util.*;
class Point
{
double x,y;
Point()
{
x=y=0;
}
Point(double a,double b)
{
x=a;y=b;
}
Point(Point l)
{
39. double d=t1.Finddistance(t2);
System.out.println("distance between p[" +i+"]"+" and p["+(i+1)+"]"+"points are:"+d);
i++;
}
double d=p2.Finddistance(7.9,16.25);
System.out.println("given points are: ");
p2.display();
System.out.println("distance between given points and p2 is:"+d);
}
}
E:java programing>javac Point.java
E:java programing>java Point
p[1]= (3.25,7.89)
p[2]= (5.37,18.12)
distance between p[1] and p[2]points are:10.447358517826409
p[3]= (5.37,18.12)
p[4]= (0.0,0.0)
distance between p[3] and p[4]points are:18.8989761627449
given points are: (5.37,18.12)
distance between given points and p2 is: 3.146076922136521
EXP.NO:-07 PRELAB EXERCISE:-
import java.util.*;
import java.lang.*;
class TREESET
{
public static void main(String a[])
{
TreeSet<String> a1=new TreeSet<String>();
a1.add("hello");
a1.add("photons");
a1.add("sorry");
a1.add("space");
a1.add("comics");
TREESET k=new TREESET();
40. System.out.println("Original String TREESET=t"+a1);
k.capitalizePlurals(a1);
k.removePlurals(a1);
k.reverse(a1);
}
public void reverse(TreeSet<String> a1)
{
TreeSet <String>treereverse = new TreeSet<String>();
treereverse = (TreeSet)a1.descendingSet();
System.out.println("TREESET after reversing the order of the elements in
words=t"+treereverse);
}
public void capitalizePlurals(TreeSet<String> a1)
{
List<String> list = new ArrayList<String>(a1);
for(int i=0;i<list.size();i++)
{
String s=list.get(i);
if(s.charAt(s.length()-1)=='s')
{
list.set(i,s.toUpperCase());
}
}
a1 = new TreeSet<String>(list);
System.out.println("TREESET after capitalizePlurals = "+a1);
}
public void removePlurals(TreeSet<String> a1)
{
List<String> list = new ArrayList<String>(a1);
for(int i=0;i<list.size();i++)
{
String str=list.get(i);
if(str.charAt(str.length()-1)=='s'|| str.charAt(str.length()-1)=='S')
{
a1.remove(str);
}
}
System.out.println("TREESET after removing plurals = "+a1);
}
}
41. E:java programing>javac TREESET.java
Note: TREESET.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
E:java programing>java KOPT
Original String TREESET= [comics, hello, photons, sorry, space]
TREESET after capitalizePlurals = [COMICS, PHOTONS, hello, sorry, space]
TREESET after removing plurals = [hello, sorry, space]
TREESET after reversing the order of the elements in words= [space, sorry, hello]
EXP.NO:-07 LAB EXERCISE:-
import java.util.*;
import java.lang.*;
class point
{
double x,y;
point(double a,double b)
{
x=a;
y=b;
}
double find_distance(point p)
{
double dis,xd,yd;
xd=x-p.x;
42. yd=y-p.y;
dis=Math.sqrt(xd*xd+yd*yd);
return(dis);
}
void display()
{
System.out.print("("+x+","+y+") ");
}
}
class ComparePts implements Comparator<point>
{
public int compare(point p1,point p2)
{
point org=new point(0,0);
double d1=p1.find_distance(org);
double d2=p2.find_distance(org);
if(d1<d2)return(-1);
if(d1>d2)return(1);
else return(0);
}
}
class HPoint
{
public static void main(String a[])
{
int i;
double x,y,d1;
Scanner s=new Scanner(System.in);
point p[]=new point[10];
HashSet<point> hs=new HashSet<point>();
for(i=0;i<10;i++)
{
System.out.println("Enter x & y for point "+(i+1));
x=s.nextDouble();
y=s.nextDouble();
p[i]=new point(x,y);
hs.add(p[i]);
}
43. TreeSet<point> ts=new TreeSet<point>(new ComparePts());
Iterator t=hs.iterator();
while(t.hasNext())
{
point pt=(point)t.next();
ts.add(pt);
}
System.out.println("Points in ascending Order:");
Iterator t1=ts.iterator();
while(t1.hasNext())
{
point pt=(point)t1.next();
pt.display();
System.out.println();
}
System.out.println("Distance between every pair:");
Iterator it=hs.iterator();
while(it.hasNext())
{
point t3,t2;
t3=(point)it.next();
t2=(point)it.next();
d1=t3.find_distance(t2);
System.out.print("Distance between the points:");
t3.display();
t2.display();
System.out.println(" = "+d1);
}
}
}
E:java programing>javac HPoint.java
E:java programing>java HPoint
Enter x & y for point 1 1 2
Enter x & y for point 2 2 3
Enter x & y for point 3 3 4
Enter x & y for point 4 4 5
Enter x & y for point 5 5 6
Enter x & y for point 6 6 7
44. Enter x & y for point 7 7 8
Enter x & y for point 8 8 9
Enter x & y for point 9 9 10
Enter x & y for point 10 10 11
Points in ascending Order:
(1.0,2.0)
(2.0,3.0)
(3.0,4.0)
(4.0,5.0)
(5.0,6.0)
(6.0,7.0)
(7.0,8.0)
(8.0,9.0)
(9.0,10.0)
(10.0,11.0)
Distance between every pair:
Distance between the points:(5.0,6.0) (1.0,2.0) = 5.656854249492381
Distance between the points:(2.0,3.0) (7.0,8.0) = 7.0710678118654755
Distance between the points:(3.0,4.0) (6.0,7.0) = 4.242640687119285
Distance between the points:(10.0,11.0) (8.0,9.0) = 2.8284271247461903
Distance between the points:(9.0,10.0) (4.0,5.0) = 7.0710678118654755
EXP.NO:-08 LAB EXERCISE:-
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class tank extends Applet implements Runnable,ActionListener
{
int y=200,sf=0,rf=0;
Thread t;
Button b1,b2,b3,b4;
public void init()
{
b1=new Button("Start");
b2=new Button("Stop");
b3=new Button("Resume");