57
PROGRAM 1: /*calculate electricity bill Program to compute the electricity bill of a person under the following units consumed for the first 100 units: Rs. 1.20 per unit For the next 200 units- Rs. 2 per unit For the next 300 units-Rs. 3 per unit */ class ElectricityBill { private int customerNo; private String name; private int units; void setData(int num, String cname, int noUnits) { customerNo= num; name= cname; units= noUnits; } void show() { System.out.println("Customer Number--"+customerNo); System.out.println("Customer Name---"+units); System.out.println("Units Consumed---"+name);

void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Embed Size (px)

Citation preview

Page 1: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

PROGRAM 1:

/*calculate electricity bill Program to compute the electricity bill of a

person under the following units consumed for the first 100 units: Rs.

1.20 per unit For the next 200 units- Rs. 2 per unit For the next 300

units-Rs. 3 per unit */

class ElectricityBill

{

private int customerNo;

private String name;

private int units;

void setData(int num, String cname, int noUnits)

{

customerNo= num;

name= cname;

units= noUnits;

}

void show()

{

System.out.println("Customer Number--"+customerNo);

System.out.println("Customer Name---"+units);

System.out.println("Units Consumed---"+name);

Page 2: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

}

double billCalculate()

{

double bill;

if(units<100)

bill=units*1.20;

else if(units<=300)

bill=100*1.20+(units-100)*2;

else

bill=100*1.20+200*2+(units-300)*3;

return bill;

}

}

class ElectricityBillDemo

{

public static void main(String[] args)

{

ElectricityBill b = new ElectricityBill();

b.setData(101,"Somesh",200);

double billPay=b.billCalculate();

b.show();

Page 3: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

System.out.println("Bill to pay---"+billPay);

}

}

Output:

Page 4: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 2:

//Implement basic operation of stacks

class Stack

{

private int [] element;

private int top;

Stack()

{

element = new int [10];

top=-1;

}

void push(int num)

{

if (top==9)

System.out.println("OverFlow");

else

{

top++;

element [top]=num;

System.out.println("Element pushed is="+element[top]);

Page 5: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

}

}

void pop()

{

if(top==-1)

System.out.println("underflow");

else

{

System.out.println("Element popped is="+element[top]);

top--;

}

}

void display()

{

for( int i=0; i<=top; i++)

System.out.println(element[i]+" ");

}

}

class StackDemo

{

public static void main(String[] args)

Page 6: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

{

Stack s= new Stack();

s.push(10);

s.push(20);

s.push(30);

s.pop();

System.out.println("Current elements in stack=");

s.display();

}

}

Output:

Page 7: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 3: // Inheritance abstract class Worker { String name; double rate; Worker(String n, double r) { name = n; rate = r; } double getRate() { return rate; } abstract double computePay (int hours); } class HourlyWorker extends Worker { double pay; HourlyWorker(String n, double r) {

Page 8: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

super(n, r); } double computePay (int hours) { if(hours<40) pay = getRate() * hours; if (hours > 40) pay = (getRate()*40) + (getRate() * (hours - 40) / 2); return pay; } } class SalariedWorker extends Worker { SalariedWorker(String n, double r) { super(n, r); } double computePay(int hours) { return getRate() * 40; }

Page 9: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

} class WorkerTester { public static void main(String[] args) { SalariedWorker s = new SalariedWorker("Sally", 40); HourlyWorker h = new HourlyWorker("Harry", 40); System.out.println(s.computePay(30)); System.out.println(h.computePay(30)); System.out.println(s.computePay(50)); System.out.println(h.computePay(50)); } }

Page 10: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Output:

Page 11: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 4: //searching an element in an array import java.util.*; class srch_arr { public static void main(String arg[]) { int a[]=new int[10]; Scanner ins=new Scanner(System.in); int i,n=0,q=0; System.out.print("Enter no. of elements "); n=ins.nextInt(); System.out.println("Enter elements "); for(i=0;i<n;i++) { a[i]=ins.nextInt(); } System.out.println("Enter element to be searched "); q=ins.nextInt(); for(i=0;i<n;i++) { if(a[i]==q) System.out.println("Element found at "+(i+1)+" position"); } } }

Page 12: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Output:

Page 13: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 5:

// Abstract class import java.util.*; abstract class AreaMethods { abstract void rect(int l,int b); abstract void sqr(int a); abstract void tri(int ba,int h); } class Area extends AreaMethods { void rect(int l,int b) { System.out.println("Area of the Rectangle : "+(l*b)); } void sqr(int a) { System.out.println("Area of the Square : "+(a*a)); } void tri(int ba,int h)

Page 14: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

{ System.out.println("Area of the Triangle : "+(0.5*ba*h)); } } class AbstractDemo { public static void main(String args[]) { int l,b,a,ba,h; Scanner sc=new Scanner(System.in); System.out.println("Enter the length of the Rectangle:"); l=sc.nextInt(); System.out.println("Enter the breadth of the Rectangle:"); b=sc.nextInt(); System.out.println("Enter the side of the Square:"); a=sc.nextInt(); System.out.println("Enter the base of the Triangle:"); ba=sc.nextInt(); System.out.println("Enter the height of the Triangle:"); h=sc.nextInt();

Page 15: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Area obj=new Area(); obj.rect(l,b); obj.sqr(a); obj.tri(ba,h); } }

Page 16: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Output:

Page 17: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 6: //PROGRAM TO GENERATE FIBONACCI SERIES USING COMMAND //LINE ARGUMENT class Fibonacci { public static void main(String args[]) { int a=0,b=1,c=0; int n=Integer.parseInt(args[0]); System. out. print(a+" "+b); for (int i=1;i<n-1;i++) { c=a+b; a=b; b=c; System.out.print(" "+c); } } }

Page 18: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;
Page 19: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 7: //implement a queue using interfaces import java.util.*; interface QueueInterface { void insert(int item); void delete(); void display(); } class QueueClass implements QueueInterface { int size=5; int que[]; int rear,front; QueueClass() { que=new int[size]; rear=0; front=0; } public void insert(int x) {

Page 20: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

if(rear>=size) System.out.println("QUEUE IS FULL"); else { que[rear]=x; rear++; System.out.println("Element Inserted"); } } public void delete() { if(front==rear) System.out.println("QUEUE IS EMPTY"); else { front++; System.out.println("Element Deleted"); } } public void display() {

Page 21: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

if(front==rear) System.out.println("QUEUE IS EMPTY"); else { System.out.println("Elements in the Queue are"); for(int i=front;i<rear;i++) System.out.print("\t"+que[i]); System.out.println(); } } } class Queue { public static void main(String args[]) { int x,choice; QueueClass q=new QueueClass(); Scanner br=new Scanner(System.in); do { System.out.println("\t"+"MENU");

Page 22: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

System.out.println("1. Insert into the Queue"); System.out.println("2. Delete from the Queue"); System.out.println("3. Display Queue"); System.out.println("4. Exit"); System.out.println("Enter the choice:"); choice=br.nextInt(); switch(choice) { case 1: System.out.println("Enter the element to be inserted:"); x=br.nextInt(); q.insert(x); break; case 2: q.delete(); break; case 3: q.display(); break;

Page 23: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

case 4: System.exit(0); default: System.out.println("Wrong Choice!!!"); } }while(true); } }

Page 24: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Output:

Page 25: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 8:

// Comparing two strings by various String comparison techniques of //String class class Simple { public static void main(String args[]) { String s1= "Sachin"; String s2="Sachin"; String s3="SACHIN"; String s4= new String("Sachin"); String s5= "Sourav"; //Using equals() method System.out.println(s1.equals(s2)); System.out.println(s1.equals(s4)); System.out.println(s1.equals(s5)); System.out.println(s1.equals(s3)); System.out.println(s1.equalsIgnoreCase(s3)); // Using == operator System.out.println(s1 ==s2); System.out.println(s1==s4); // Using compareTo() method System.out.println(s1.compareTo(s2)); System.out.println(s1.compareTo(s3)); System.out.println(s1.compareTo(s5)); System.out.println(s5.compareTo(s1)); } }

Page 26: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Output:

Page 27: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 9: /*WAP to create a package p with class A with 4 types of access protected methods. How we will use these methods in different packages class i.e. there is main() in class B in package Q and 4 methods are in Class A in package p*/ //Program A.java package p; public class A { public void m1() { System.out.println("public method"); } protected void m2() { System.out.println("protected method"); } private void m3() { System.out.println("private method"); } void m4() { System.out.println("default method"); }

Page 28: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

//Program B.java package q; import p.*; class B extends A { public static void main(String[] args) { A a=new A(); a.m1(); //calling public method new B().m2(); // calling protected method a.m5(); //calling private method a.m6();//calling default method } } Output:

Page 29: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 10 /*WAP to prompt a question, what is your name? and you have to provide an answer, if answer is correct in first attempt print well done otherwise you have more chance to print, if answer is correct in second attempt ok will print otherwise sorry */ import java.util.*; class Use { public static void main(String s[]) { String s1="ankit"; Scanner scr= new Scanner(System.in); System.out.println("Enter your name"); String s2= scr.next(); if(s1.equals(s2)) { System.out.println("well done"); } else { System.out.println("You have got one more choice"); System.out.println("Enter your name again"); Scanner scr3= new Scanner(System.in); String s3= scr3.next(); if(s1.equals(s3)) { System.out.println("Good"); } else { System.out.println("Sorry");

Page 30: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

} } } } Output:

Page 31: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 11:

// WAP to compute the payment of a Loan based on the amount of Loan, the interest rate and the number of the months. It takes one parameter from user monthly rate; if true the interest rate is per month otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*; /*<applet code= "Loan" width=300 height=500 </applet> */ public class Loan extends Applet implements ActionListener,ItemListener { double p,r,n,total,i; String param1; boolean month; Label l1,l2,l3,l4; TextField t1,t2,t3,t4; Button b1,b2; CheckboxGroup cbg; Checkbox c1,c2; String str; public void init() { l1=new Label("Balance Amount",Label.LEFT); l2=new Label("Number of Months",Label.LEFT); l3=new Label("Interest Rate",Label.LEFT); l4=new Label("Total Payment",Label.LEFT); t1=new TextField(5); t2=new TextField(5); t3=new TextField(15); t4=new TextField(20); b1=new Button("OK");

Page 32: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

b2=new Button("Delete"); cbg=new CheckboxGroup(); c1=new Checkbox("Month Rate",cbg,true); c2=new Checkbox("Annual Rate",cbg,true); t1.addActionListener(this); t2.addActionListener(this); t3.addActionListener(this); t4.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); c1.addItemListener(this); c2.addItemListener(this); add(l1); add(t1); add(l2); add(t2); add(l3); add(t3); add(l4); add(t4); add(c1); add(c2); add(b1); add(b2); } public void itemStateChanged(ItemEvent ie) { } public void actionPerformed(ActionEvent ae) { str=ae.getActionCommand(); if(str.equals("OK")) { p=Double.parseDouble(t1.getText()); n=Double.parseDouble(t2.getText()); r=Double.parseDouble(t3.getText());

Page 33: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

if(c2.getState()) { n=n/12; } i=(p*n*r)/100; total=p+i; t4.setText(" "+total); } else if(str.equals("Delete")) { t1.setText(" "); t2.setText(" "); t3.setText(" "); t4.setText(" "); } } }

Output:

Page 34: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;
Page 35: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 12:

//WAP to create a Quiz application with at least 5 questions using //CardLayout (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*; public class quiz extends Applet implements ActionListener { Button b; CardLayout c1; Panel p1,p2,p3,p4,p5,p6; public void init() { b = new Button("Click next"); p1=new Panel(); p2=new Panel(); p3=new Panel(); p4=new Panel(); p5=new Panel(); p6=new Panel(); c1= new CardLayout(); p1.setLayout(c1); setLayout(new GridLayout(2,1)); b.addActionListener(this); p2.add(new Label("Q1:What is the capital of India?")); p2.add(new Label("1. Beijing 2. Delhi")); p3. add(new Label("Q2: The international Airport of Tamil nadu is:")); p3.add(new Label("1. Meenambakkam 2. Palam")); p4.add(new Label("Q3:Who is the chief minister of UP?")); p4.add(new Label("1.Mayawati 2. Akhilesh yadav"));

Page 36: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

p5.add(new Label("Q4: What is the capital of Andhra pradesh?")); p5.add(new Label("1. Bangalore 2. Hyderabad")); p6.add(new Label("Q5: What is the other name of Jaipur?")); p6.add(new Label("1. Pink City 2. Green City")); p1.add(p2,"A"); p1.add(p3,"B"); p1.add(p4,"C"); p1.add(p5,"D"); p1.add(p6,"E"); add(p1); add(b); } public void actionPerformed(ActionEvent e) { c1.next(p1); } } /* <applet code="quiz.java" width="300" height="300"> </applet> */

Page 37: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Output:

Page 38: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 13: // Design an Applet program to perform addition, subtraction, multiplication, division and modulus operation i.e. Calculator. import java.awt.*; import java.awt.event.*; import java.applet.*; /*applet code="Cal" width=300 height=300 > </applet> */ public class Cal extends Applet implements ActionListener { String msg=" "; int v1,v2,result; TextField t1; Button b[]=new Button[10]; Button add,sub,mul,div,clear,mod,EQ; char OP; public void init() { //Color k=new Color(120,89,90); //setBackground(k); t1=new TextField(10); GridLayout gl=new GridLayout(4,5); setLayout(gl); for(int i=0;i<10;i++) { b[i]=new Button(""+i); } add=new Button("add"); sub=new Button("sub");

Page 39: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

mul=new Button("mul"); div=new Button("div"); mod=new Button("mod"); clear=new Button("clear"); EQ=new Button("EQ"); t1.addActionListener(this); add(t1); for(int i=0;i<10;i++) { add(b[i]); } add(add); add(sub); add(mul); add(div); add(mod); add(clear); add(EQ); for(int i=0;i<10;i++) { b[i].addActionListener(this); } add.addActionListener(this); sub.addActionListener(this); mul.addActionListener(this); div.addActionListener(this); mod.addActionListener(this); clear.addActionListener(this); EQ.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String str=ae.getActionCommand(); char ch=str.charAt(0); if ( Character.isDigit(ch))

Page 40: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

t1.setText(t1.getText()+str); else if(str.equals("add")) { v1=Integer.parseInt(t1.getText()); OP='+'; t1.setText(""); } else if(str.equals("sub")) { v1=Integer.parseInt(t1.getText()); OP='-'; t1.setText(""); } else if(str.equals("mul")) { v1=Integer.parseInt(t1.getText()); OP='*'; t1.setText(""); } else if(str.equals("div")) { v1=Integer.parseInt(t1.getText()); OP='/'; t1.setText(""); } else if(str.equals("mod")) { v1=Integer.parseInt(t1.getText()); OP='%'; t1.setText(""); } if(str.equals("EQ")) { v2=Integer.parseInt(t1.getText()); if(OP=='+')

Page 41: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

result=v1+v2; else if(OP=='-') result=v1-v2; else if(OP=='*') result=v1*v2; else if(OP=='/') result=v1/v2; else if(OP=='%') result=v1%v2; t1.setText(""+result); } if(str.equals("clear")) { t1.setText(""); } } } Output:

Page 42: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;
Page 43: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 14: //calculate sum of elements of each row in two D array public class SumRowCol { public static void main(String args[]) { int [] [] num={ {7,6,5},{4,5,3}}; int i, j, sumrow, sumcol; for( i=0;i<num.length;i++) { sumrow=0; for(j=0;j<num[i].length;j++) { sumrow+=num[i][j]; System.out.print(num[i][j]+" "); } System.out.println("--------"+sumrow); } System.out.println("--------------------"); for (i=0;i<num[0].length;i++) { sumcol=0; for(j=0;j<num.length;j++) sumcol+=num[j][i]; System.out.print(sumcol+" "); } } }

Page 44: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Output:

Page 45: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 15: //Write a program to implement the concept of Exception Handling //using predefined exception. import java.lang.*; class Exception_handle { public static void main(String args[]) { int a=10,b=5,c=5,x,y; try { x=a/(b-c); } catch(ArithmeticException e) { System.out.println("DIVISION BY ZERO"); } y=a/(b+c); System.out.println("y="+y); } }

Page 46: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Output:

Page 47: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 16: //Write a program to implement the concept of Exception Handling //by creating user defined exceptions. import java.lang.Exception; import java.lang.*; import java.util.*; class MyException extends Exception { MyException(String message) { super(message); } } class userdef { public static void main(String a[]) { int age; Scanner ds=new Scanner(System.in); try { System.out.println("Enter the age (above 15 and below 25) :"); age=ds.nextInt(); if(age<15 || age> 25) { throw new MyException("Number not in range"); } System.out.println(" the number is :" +age); }

Page 48: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

catch(MyException e) { System.out.println("Caught MyException"); System.out.println(e.getMessage()); } catch(Exception e) { System.out.println(e); } } } Output:

Page 49: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 17: //Write a program to implement the concept of threading by //extending Thread Class import java.lang.Thread; class A extends Thread { public void run() { System.out.println("thread A is started:"); for(int i=1;i<=5;i++) { System.out.println("\t from thread A:i="+i); } System.out.println("exit from thread A:"); } } class B extends Thread { public void run() { System.out.println("thread B is started:"); for(int j=1;j<=5;j++) { System.out.println("\t from thread B:j="+j); } System.out.println("exit from thread B:"); } } class C extends Thread { public void run()

Page 50: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

{ System.out.println("thread C is started:"); for(int k=1;k<=5;k++) { System.out.println("\t from thread C:k="+k); } System.out.println("exit from thread C:"); } } class Threadtest { public static void main(String arg[]) { new A().start(); new B().start(); new C().start(); } }

Page 51: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Output:

Page 52: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 18: //Write a program to implement the concept of threading by //implementing Runnable Interface import java.lang.Runnable; class X implements Runnable { public void run() { for(int i=1;i<10;i++) { System.out.println("\t Thread X:"+i); } System.out.println("End of Thread X"); } } class Runnabletest { public static void main(String arg[]) { X R=new X(); Thread T=new Thread(R); T.start(); } }

Page 53: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Output:

Page 54: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Program 19: //Write the java program using interface to do the following //a) Binary to Decimal b) Decimal to Binary c) Two’s Complement of a //Binary number d) Addition of two Binary numbers import java.util.*; interface BinaryInterface { int toDecimal(String binary); String toBinary(int decimal); String twosComplement(String binary); String binaryAdd(String binary1,String binary2); } class BinaryClass implements BinaryInterface { public int toDecimal(String binary) { return(Integer.parseInt(binary,2)); } public String toBinary(int decimal) {

Page 55: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

return(Integer.toBinaryString(decimal)); } public String binaryAdd(String binary1,String binary2) { return(Integer.toBinaryString(Integer.parseInt(binary1,2)+Integer.parseInt(binary2,2))); } public String twosComplement(String binary) { int i=Integer.parseInt(binary,2); int j=~i+1; return(Integer.toBinaryString(j)); } } class Binary { public static void main(String args[]) { Scanner br=new Scanner(System.in); BinaryClass bc=new BinaryClass(); System.out.println("BINARY TO DECIMAL"); System.out.println("Enter the binary number:");

Page 56: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

System.out.println("Binary-->Decimal = "+bc.toDecimal(br.next())); System.out.println("DECIMAL TO BINARY"); System.out.println("Enter the decimal number:"); System.out.println("Decimal-->Binary = " + bc.toBinary (br.nextInt())); System.out.println("TWO'S COMPLEMENT"); System.out.println("Enter the binary number:"); System.out.println("Two's Complement = "+bc.twosComplement(br.next())); System.out.println("ADDITION OF TWO BINARY NUMBERS"); System.out.println("Enter two binary numbers:"); System.out.println("Addition Result = " + bc.binaryAdd (br.next(),br.next())); } }

Page 57: void setData(int num, String cname, int noUnits) otherwise the interest rate is annual (using Applet as Container). import java.awt.*; import java.awt.event.*; import java.applet.*;

Output: