Friday 27 April 2012

Java Lab Programs


/* 1. Develop a program to convert the given temperature in fahreneit to Celsius
using the following connversion formulas C = and display the values in a
tabular form
 *Celsius.java
 *Ravi 24
 *20.01.12
 *To convert temperature scales
*/
import java.lang.*;
import java.io.*;
class Celsius
{
 public static void main(String args[])
 {
   double c[]=new double[20];
   double f[]=new double[20];
   int n;
   DataInputStream dis=new DataInputStream(System.in);
   try
   {
   System.out.println("Enter number of values to be entered");
   n=Integer.parseInt(dis.readLine());
   for(int i=0;i<n;i++)
    {
                   f[i]=Integer.parseInt(dis.readLine());
                }
                for(int i=0;i<n;i++)
    {
                   c[i]=(f[i]-32)/1.8;
                }
                System.out.println(" farenheit-celsius");
                for(int i=0;i<n;i++)
    {
                  System.out.println(" "+f[i]+" "+c[i]);
                }
                }
                catch(Exception e){}
               
 }
}


/*2. Develop a program that will take a string from a command line argument in order to check
whether it is a palindrome.

 * palindrome.java
 * Ravi 24
 * 23/12/2011
 * Checks palindrome
 */
import java.lang.*;
import java.io.*;
import java.util.*;
class palindrome
{
                public static void main(String args[])
                {
                                boolean flag=true;
                                String str;
                                str=args[0];
                                int len= str.length();
                                System.out.println("Length: "+len);
                                for(int i=0;i<(len/2);i++)
                                 {
                                                if(str.charAt(i)==str.charAt((len-1)-i)) ;
                                                else { flag=false;  break;}
                                 }
                                if(flag==true) System.out.println("Palindrome");
                                else System.out.println("Not Palindrome");
                }
}


/*  3. Develop a program by design a class to represent a bank account. Including the following
members:
Data Members:
Name of the Depositor
Account Number
Type of account
Balance
Methods:
To assign initial values
To deposit an amount
To withdraw an amount after checking
balance
To display the name and balance

*Bank.java
*Ravi 24
*10.02.12
*Demonstration of Bank operations
*/
import java.lang.*;
import java.io.*;
import java.util.*;
class Account
{
                String name;
                long accNum=1000;
                int type=1;
                double bal;
                static int count;
               
}
class Bank extends Account
{              void createAccount()
{
                DataInputStream dis=new DataInputStream(System.in);
                try{
                                System.out.print("Enter Name:");
                                name=new String(dis.readLine());
                System.out.print("\n Account Type(1-savings/2-current:");
                type=Integer.parseInt(dis.readLine());
                accNum+=++count;
                assignInitialValues();
                }
                catch(Exception e)
                {
                System.out.println(e);
                }
                }
                void assignInitialValues()
                {
                if(type==1)
                {
                bal=500.0;
                }
                if(type==2)
                {
                                bal=1000.0;
                }
                }
void withdraw(double val)
{
if(bal>=val){bal=bal-val;}
else{ System.out.println("InSufficient Balance"); }
}
void deposit(double val)
{
                bal=bal+val;
}
void display()
{
String accType;
if(type==1){accType=new String("Savings"); }
else{ accType=new String("Current"); }
                System.out.println("\n\n"+accNum+"\t\t"+name+"\t\t"+accType+"\t\t"+bal);
                }
public static void main(String args[])
{
int ch=0;
double val=0,num=0;
boolean flag=false;
Vector v=new Vector(10);
Bank ob=null;
DataInputStream dis=new DataInputStream(System.in);
try
{
loop:      while(true)
                {             
                flag=false;
                System.out.println("\n Enter 1. create 2.Credit 3.Debit 4.display 5.Exit:");
                ch=Integer.parseInt(dis.readLine());
                switch(ch)
                {
                case 1: ob=new Bank(); ob.createAccount();v.add(ob);break;
                case 2: System.out.println("\n Enter accnum:");
                                num=Integer.parseInt(dis.readLine());
                                for(int i=0;i<v.size();i++)
                                {
                                ob=((Bank)v.get(i));
                                if(ob.accNum==num){flag=true; break;}
                                }
                                if(flag)
                                {
                                System.out.println("\n Enter amount:");
                                val=Integer.parseInt(dis.readLine());
                                ob.deposit(val);
                                }
                                else
                                {
                                System.out.println("Enter proper AccNum");
                                continue loop;
                                }
                                break;
                                case 3: System.out.println("\n Enter accnum:");
                                num=Integer.parseInt(dis.readLine());
                                for(int i=0;i<v.size();i++)
                                {
                                ob=((Bank)v.get(i));
                                if(ob.accNum==num){flag=true; break;}
                                }
                                if(flag)
                                {
                                System.out.println("\n Enter amount:");
                                val=Integer.parseInt(dis.readLine());
                                ob.withdraw(val);
                                }
                                else
                                {
                                System.out.println("\n Enter proper accnum");
                                continue loop;
                                }
                                break;
case 4: System.out.println("Acc.Num                     Name                    Type                      BalanceAmount");
                                for(int i=0;i<v.size();i++)
                                {
                                ((Bank)v.get(i)).display();
                                }
                                break;
                case 5:  System.exit(1);
                default: System.out.println("Enter Proper Choice"); continue loop;
                }
                }
}
                catch(Exception e)
                {
                                System.out.println(e);
                }             
                }
}



4. Develop a program to demonstrate multiple inheritance through interface

import java.lang.*;
import java.io.*;
import java.util.*;
interface MyInterface
{
 static final int a=20;
 void display();
}
class A
{
  int b=30;
}
class B extends A implements MyInterface
{
 int c;
 public void display()
 {
 
  c=a+b;
  System.out.println("Sum is "+c);
 }
}
class InterfaceDemo
{
 public static void main(String args[])
 {
  B ob=new B();
  ob.display();
 }
}



/*5. Develop a program to illustrate the use of multithreads
*ThreadTest.java
*Ravi 24
*02.03.12
*Demonstrating Threads
*/
class A extends Thread
{
 public void run()
 {
  for(int i=1;i<=3;i++)
   {
    System.out.println("\tFrom ThreadA : i= "+i);
   }
  System.out.println("Exit from A");
 }
}
class B extends Thread
{
 public void run()
 {
  for(int i=1;i<=3;i++)
   {
    System.out.println("\tFrom ThreadB : i= "+i);
   }
  System.out.println("Exit from B");
 }
}
class C extends Thread
{
 public void run()
 {
  for(int i=1;i<=3;i++)
   {
    System.out.println("\tFrom ThreadC : i= "+i);
   }
  System.out.println("Exit from C");
 }
}
class ThreadTest
{
 public static void main(String args[])
 {
  A a=new A();
  B b=new B();
  C c=new C();
  a.start();
  b.start();
  c.start();
 }
}


6. Develop am applet that receives three numeric values as input from the user and then display the
largest of the three.
 
import java.awt.*;
import java.applet.*;
public class Ex1 extends Applet
{ TextField t1,t2,t3;
  public void init()
  {

   Label l1=new Label("first"); 
   Label l2=new Label("second"); 
   Label l3=new Label("third");
   t1= new TextField(8);
   t2= new TextField(8);
   t3= new TextField(8);
   t1.setText("0");
   t2.setText("0");
   t3.setText("0");
   add(l1);
   add(t1);
   add(l2);
   add(t2);
   add(l3);
   add(t3);
  }
  public void paint(Graphics g)
  {
  
   g.drawString("input the num",10,45);
    int x=0,y=0,z=0,max=0;
                String s1,s2,s3,s;
                try{
                s1=t1.getText();
                s2=t2.getText();
                s3=t3.getText();
                x=Integer.parseInt(s1);
                y=Integer.parseInt(s2);
                z=Integer.parseInt(s3);
    }
  catch(Exception e)
   {} 
  if(x>y&&x>z)
   max=x;
  else if(y>x&&y>z)
   max=y;
  else
   max=z;
  s=String.valueOf(max);
  g.drawString("the max num is "+s,20,55);
  }
  public boolean action(Event event,Object object)
  {
    repaint();
                return true;
  }
}

7. Develop an applet that displays different bar charts
import java.awt.*;
import java.applet.*;
public class BarChart extends Applet
{
 int n=0;
 String label[]={"91","92","93","94"};
 int value[]={110,150,100,170};
 public void paint(Graphics g)
 {
  for(int i=0;i<4;i++)
  {
    g.setColor(Color.red);
                g.drawString(label[i],20,i*50+30);
                g.fillRect(50,i*50+10,value[i],40);
  }
  }
}


/*
7. Develop an applet that displays different bar charts
 *graphs.java
 *Ravi 24
 *16.03.12
 *To display IND-SL scores in BarGraphs
*/
import java.lang.*;
import java.io.*;
import java.awt.*;
import java.applet.*;

public class graphs extends Applet
{
                int sri[]={3,3,1,4,6,2,9,1,0,3,8,7,8,3,5,1,10,2,3,0,5,8,2,5,6,11,3,7,7,4,5,6,4,5,11,4,3,6,3,2,2,1,2,4,3,9,4,9,12,4};
                int ind[]={2,4,7,6,4,1,2,8,8,2,3,2,7,5,7,5,6,0,3,1,10,1,3,5,1,6,5,8,4,9,2,3,3,4,8,2,5,7,4,5,0,2,2,4,5,13,5,4,15,4};
               
                public void init()
                {
               
                }
                                public void paint(Graphics g)
                                {
                                                try
                                                {
                                                                int j=-10;
                                                                g.setColor(Color.black);
                                                                g.drawLine(0,200, 1000,200);
                                                                g.setColor(Color.red);
                                                                g.drawString("SRI LANKA:",20,30);
                                                                g.setColor(Color.blue);
                                                                g.drawString("INDIA:",20,330);
                                                for(int i=0;i<50;i++)
                                                {
                                                                g.setColor(Color.red);
                                                                g.fillRect(j+10,200-(sri[i]*10),10,sri[i]*10);
                                                                j+=20;
                                                }
                                                                j=-10;
                                                for(int i=0;i<50;i++)
                                                {
                                                                g.setColor(Color.blue);
                                                                g.fillRect(j+10,200,10,ind[i]*10);
                                                                j+=20;
                                                }             
                                                }catch(Exception e){}
                                }
}








/*
8. Develop an applet to draw the following shapes
a) Cone
b) Cylinder
c) Cube
d) Square inside a circle e) Circle inside a square

*/

import java.applet.*;
import java.awt.*;
public class Shapes extends Applet 
{
 public void paint(Graphics g)
 {
  int c=100,d=100;//to draw cone

    g.drawOval(c,d,50,25);
                g.drawLine(c,d+10,c+25,d+100);
                g.drawLine(c+50,d+10,c+25,d+100);
               
                c=300;d=300;//to draw cylinder
                g.drawOval(c,d,50,25);
                g.drawOval(c,d+100,50,25);
                g.drawLine(c,d+10,c,d+110);
                g.drawLine(c+50,d+10,c+50,d+110);
               
                c=200;d=200;//cube
                g.drawLine(c,d,c+50,d);
                g.drawLine(c,d+50,c+50,d+50);
                g.drawLine(c,d,c,d+50);
                g.drawLine(c+50,d,c+50,d+50);
               
               
                g.drawLine(c+20,d-20,c+70,d-20);
                g.drawLine(c+20,d+30,c+70,d+30);
                g.drawLine(c+20,d-20,c+20,d+30);
                g.drawLine(c+70,d-20,c+70,d+30);
               
                g.drawLine(c,d,c+20,d-20);
                g.drawLine(c+50,d,c+70,d-20);
                g.drawLine(c,d+50,c+20,d+30);
                g.drawLine(c+50,d+50,c+70,d+30);
               
                c=500;d=200;//circle inside square
                g.drawLine(c,d,c+100,d);
                g.drawLine(c,d+100,c+100,d+100);
                g.drawLine(c,d,c,d+100);
                g.drawLine(c+100,d,c+100,d+100);
                g.drawOval(c,d,100,100);
               
               
                c=600;d=100;//square inside circle
                g.drawOval(c,d,100,100);
                g.drawLine(c+15,d+15,c+85,d+15);
                g.drawLine(c+15,d+85,c+85,d+85);
                g.drawLine(c+15,d+15,c+15,d+85);
                g.drawLine(c+85,d+15,c+85,d+85);
 }
}

  9. Develop a program to display a calendar for a specified month using the Date, Calendar and Date
Format classes.
*MyCalendar.java
*Jagadeesh,10
*31-03-2012
*To print the calendar of a month
*/
import java.util.*;
public class MyCalendar
{
                public static void main(String args[])
                {
                                int m=Integer.parseInt(args[0]);
                                int y=Integer.parseInt(args[1]);
                                Date dt=new Date(y-1900,m-1,1);
                                int NumDays=31, count=0;
                                String Month[]= {"JANUARY","FEBRUARY","MARCH", "APRIL","MAY", "JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
                                if((m==4)||(m==6)||(m==9)||(m==11)){ NumDays=30; }
                                if(m==2){ NumDays=28; if((y%400==0)||((y%4==0)&&(y%100!=0))){ NumDays=29;}}
                                int day=dt.getDay();
                                System.out.println("\n\n\t\t"+Month[m-1]+"\t"+y+"\n\n");
                                System.out.println("  SUN\t  MON\t  TUE\t  WED\t  THU\t  FRI\t  SAT");
                                for(int i=0;i<day;i++){ System.out.print("\t"); count++;}
                                for(int k=1;k<=NumDays;k++){
                                if(count==6){System.out.print("   "+k+"\t"); System.out.println("");count=0;}
                                else { System.out.print("   "+k+"\t"); count++;}
                                }
                }
}



/*
  10. Develop a program for handling Mouse Events.

 MyMouseEvent.java
 *Ravi 24
 *09.04.12
 *Demonstrating Mouse events to tell when when mouse enters
*/
import java.awt.*;
import java.awt.event.*;

public class MyMouseEvent extends Frame implements MouseListener{
                 static Frame f;
                MyMouseEvent()
                {
                 f=new Frame("FIRST CSE");
                f.addMouseListener(this);
                }
                public static void main(String args[])
                {
                                MyMouseEvent m=new MyMouseEvent();
                                f.setBounds(100, 100, 600, 200);
                                f.setVisible(true);
                               
                }
                public void mouseClicked(MouseEvent e){ System.out.println("mouseClicked");}
               
                public void mouseEntered(MouseEvent e){System.out.println("mouseEntered");}
               
                public void mouseExited(MouseEvent e){System.out.println("mouseExited");}
                public void mousePressed(MouseEvent e){ System.out.println("mousePressed");}
                public void mouseReleased(MouseEvent e){System.out.println(" mouseReleased"); }
                //public void windowActivated(WindowEvent e){System.out.println("Window activated");}
                //public void windowDeactivated(WindowEvent e){System.out.println("Window Deactived"); }
}


/*
11. Develop a program to display the mouse position when the mouse is pressed.  MyMouseEvents2.java
 *Ravi 24
 *09.04.12
 *Demonstrating mouse events to give the coordinates
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MyMouseEvents2 extends Applet implements MouseListener{
                int x=0,y=0;
                public void init()
                {
                                addMouseListener(this);
                }

               
    public void paint(Graphics g)
                {
                                g.drawString("("+x+","+y+")", x,y);
                }
               
                  public void mouseClicked(MouseEvent e){
               
    x = e.getX();
                 y = e.getY();
                repaint();
                //g.drawString("cordinate of x"+x+"cordinate of y"+y);       
               
                }
                public void mouseEntered(MouseEvent e){
                          
                 x = e.getX();
                 y = e.getY();
                repaint();  //System.out.println("mouseEntered");
                                                   }
               
                public void mouseExited(MouseEvent e){
                x = e.getX();
                y = e.getY();
                repaint();//System.out.println("mouseExited");
                }
                public void mousePressed(MouseEvent e){
    x = e.getX();
                y = e.getY();
                repaint();             //System.out.println("mousePressed");
               
                }
                public void mouseReleased(MouseEvent e){
                x = e.getX();
                y = e.getY();
                repaint();
                }
                //public void windowActivated(WindowEvent e){System.out.println("Window activated");}
                //public void windowDeactivated(WindowEvent e){System.out.println("Window Deactived"); }
}




/* 12. Develop a program to meet the following requirements
(a) Create a frame with flow layout
(b) Create two panels and add the panels to the frame
(c) Each panel contains three buttons. The panel uses flow layout

*Layout1.java
*Jagadeesh,10
*30-03-2012
*To create 2 panels each with 3 buttons and add them to a frame
*/
import java.awt.*;
import java.lang.*;
import java.applet.*;
public class Layout1
{
static Frame f=new Frame("FIRST CSE");
 public static void main(String args[])
 {
   Container con=f;
   f.setBounds(100,100,600,200);
   Panel p1=new Panel(new FlowLayout());
   p1.setLayout(new FlowLayout());
   p1.add(new Button("One"));
   p1.add(new Button("Two"));
   p1.add(new Button("Three"));
   f.setLayout(new BorderLayout());
   con.add(p1,BorderLayout.NORTH);
   Panel p2=new Panel(new FlowLayout());
   p2.setLayout(new FlowLayout());
   p2.add(new Button("Four"));
   p2.add(new Button("Five"));
   p2.add(new Button("Six"));
   con.add(p2,BorderLayout.SOUTH);
   f.setVisible(true);
 }
}


/*  13. Develop a program to calculate the future value of an investment at a given interest rate for a
specified number of years. The formula for the calculation is as follows:
Future value = Investment Amount (1+ Interest Rate) years.

*Bank.java
*Ravi 24
*09.04.12
*Interactive input to an applet and Item Event
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Bank extends Applet implements ItemListener
{                             
                TextField text1, text2,text3,text4;
                int amt=0, years=0, rate=0,futval=0;
                //String result="";
                public void init()
                {
                                Label lob1=new Label();
                                Label lob2=new Label();
                                Label lob3=new Label();
                                Label lob4=new Label();
                                lob1.setText("Amount");
                                lob2.setText("Years");
                                lob3.setText("Rate");
                                lob4.setText("Future Value");
                                text1=new TextField(8);
                                text2=new TextField(8);
                                text3=new TextField(8);
                                text4=new TextField(8);
                                add(lob1);
                                add (text1);
                                add(lob2);
                                add (text2);
                                add(lob3);
                                add (text3);
                                add(lob4);
                                add (text4);
                                text1.setText ("0");
                                text2.setText ("0");
                                text3.setText ("0");
                                text4.setText ("0");
                                List lt=new List(1,false);
                                lt.add("Calculate");
                                lt.addItemListener(this);             
                                add(lt);
                }
                public void paint(Graphics g)
                {
                                //g.drawString("Result is : "+z,25, 75);
                                //s=text2.getText();
                                                //= Integer.parseInt(s2);
                }
                public void itemStateChanged(ItemEvent event)
                {
                String s1=null, s2=null,s3=null;
                                try
                                {
                                                s1=text1.getText();
                                                amt= Integer.parseInt(s1);
                                                s2=text2.getText();
                                                years= Integer.parseInt(s2);
                                                s3=text3.getText();
                                                rate= Integer.parseInt(s3);
                                                //s=text2.getText();
                                                //= Integer.parseInt(s2);
                                }
                                catch (Exception ex) { System.out.println(ex);}
                                if(event.getStateChange()==ItemEvent.SELECTED)
                                {
                                    String s=null;
                                                int           i=(Integer)event.getItem();
                                                List ob=(List) event.getItemSelectable();
                                                if(ob.getItem(i)=="Calculate"){ futval=amt*((1+rate)*years); }
                                                /*if(ob.getItem(i)=="SUB"){ z=x-y;}
                                                if(ob.getItem(i)=="MUL"){ z=x*y; }
                                                if(ob.getItem(i)=="DIV"){ if(y!=0){ z=x/y; }}*/
                                                s=String.valueOf(futval);
                                                text4.setText(s);
                                }
                                repaint();
                }
}