Java pragrams

Programs list : :

  • Celsius
  • Palindrome
  • Bank account
  • Interfaces
  • Multiple threads
  • Applet to calculate largest of three
  • Mouse Event
  • Mouse Position
  • Flowlayout

Celsius

import java.io.*;

class celsius{
 public static void main(String args[]){

   double   cel[]=new double[15];
   double   far[]=new double[15];

   for(int i=0;i<args.length;i++)
   {
    far[i]=Double.parseDouble(args[i]);
    cel[i]=(far[i]-32)/1.8;
   }

  System.out.println("S.no\tFarenhit \tcelsius");
  for(int i=0;i<args.length;i++)
    System.out.println((i+1)+"\t"+far[i]+"\t"+cel[i]);
 }
}

Palindrome

import java.io.*;

class palindrome{
 public static void main(String args[]){

 String s=new String(args[0]);
 String rs=new String();

 for(int i=s.length()-1;i>=0;i--){
   System.out.println(rs+"\n");
   rs=rs+s.charAt(i);
 }

 if(s.equals(rs)) System.out.println("palindrome");
 else System.out.println("Not palindrome");
}
}

Bank account

import java.io.*;

class account{

  String name;
  int number,balance;
  char type;

  account(String n,int b,char t)
  {
    number=1000212;
    name=n;
    balance=b;
    type=t;
  }

void deposit(int amt){
        if(amt>0) balance=balance+amt;
}

void withdraw(int amt){
  System.out.println("Previous account balance"+balance);
  balance=balance-amt;
  System.out.println("Current account balance"+balance);
}

void display(){
  System.out.println("Name:"+name);
  System.out.println("Account no:"+number);
  System.out.println("Account type:"+type);
  System.out.println("Account balance:"+balance);
}

public static void main(String args[]){

 if(args.length<3){
   System.out.println("Run the program with
      String arguments:java account   ");
    return;
   }

account acc=new account(args[0],
           Integer.parseInt(args[1]),args[2].charAt(0));
acc.display();

DataInputStream in=new DataInputStream(System.in);
try{
   System.out.println("Enter deposit amt:");
 acc.deposit(Integer.parseInt(in.readLine()));
   System.out.println("Enter withdraw amt:");
 acc.withdraw(Integer.parseInt(in.readLine()));
 }
catch(IOException e){}
}

}

Interfaces

import java.io.*;

class student{
int rlno;
     void getnum(int n){
       rlno=n;
      }

      void putnum(){
       System.out.println("Rollno=="+rlno);
      }
}

class test extends student{
      float part1,part2;

      void getmarks(float m1,float m2){
          part1=m1;
          part2=m2;
      }

      void putmarks(){
          System.out.println("Marks obtained");
          System.out.println("Part1="+part1);
          System.out.println("Part2="+part2);
      }
}

interface sports{
         float sportwt=6.0f;
         void putwt();
}

class results extends test implements sports{
        public void putwt(){
               System.out.println("sports wt="+sportwt);
         }

         void display(){
           float total=part1+part2+sportwt;
           putnum();
           putmarks();
           putwt();
           System.out.println("Total Score="+total);
         }
}

class intfc{
          public static void main(String args[]){
          results re=new results();
          re.getnum(2755);
          re.getmarks(27.5F,33.0F);
          re.display();
}
}

MultiThreads

import java.io.*;
import java.lang.*;

class A extends Thread{
    public void run(){
    System.out.println("Thread A started");
    for(int i=0;i<5;i++)
      System.out.println("From Thread A :i="+i);

    System.out.println("Exit from A");
    }
}

class B extends Thread{
    public void run(){

    System.out.println("Thread B started");
    for(int j=0;j<5;j++)
      System.out.println("From Thread B :j="+j);

    System.out.println("Exit from B");
}
}

class C extends Thread{
    public void run(){

    System.out.println("Thread C started");
    for(int k=0;k<5;k++)
      System.out.println("From Thread C :k="+k);

    System.out.println("Exit from C");
    }
}

class ThreadTest{
     public static void main(String args[]){
       new A().start();
       new B().start();
       new C().start();
}
}

Applet for largest of 3 numbers

import java.applet.*;
import java.awt.*;

public class large extends Applet{

   TextField t1,t2,t3;

   public void init(){
     t1=new TextField(8);
     t2=new TextField(8);
     t3=new TextField(8);
     add(t1);
     add(t2);
     add(t3);
   t1.setText("0");
   t2.setText("0");
   t3.setText("0");
   }

public void paint(Graphics g){

   int x=0,y=0,max=0,z=0;
   g.drawString("Input no. is each box",10,50);

   try{
    x=Integer.parseInt(t1.getText());
    y=Integer.parseInt(t2.getText());
    z=Integer.parseInt(t3.getText());
   }catch(Exception e){}

   max=x;
   if(y>max)max=y;
   if(z>max)max=z;

g.drawString("The max is "+String.valueOf(max),10,76);
}

public boolean action(Event e,Object o)
{
   repaint();
   return(true);
}
}

//HTML CODE

</pre>
&nbsp;
<pre>

Mouse Event

import java.awt.*;
import java.awt.event.*;
public class msevent  extends Frame implements MouseListener{

   int x=0,y=0;

   public msevent(){
    setTitle("Msevent");
    addMouseListener(this);
   }

   public static void main(String args[]){
    Frame f=new msevent();
    f.setSize(200,200);
    f.setVisible(true);
   }

public void mousePressed(MouseEvent e)
   {  x=e.getX();  y=e.getY();  repaint();}
public void mouseClicked(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}

 public void paint(Graphics g){
  g.setColor(Color.blue);
  g.fillRect(x-5,y-5,50,50);
}
}

Mouse position

import java.awt.*;
import java.awt.event.*;
public class msposition  extends Frame implements MouseListener
{
  int x=0,y=0;

  public msposition(){
    setTitle("Msevent");

    addMouseListener(this);
  }

  public static void main(String args[]){
    Frame f=new msposition();
    f.setSize(200,200);
    f.setVisible(true);
  }

   public void mousePressed(MouseEvent e)
   {x=e.getX();y=e.getY();repaint();}

public void mouseClicked(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}

   public void paint(Graphics g){
     g.drawString(x+"   "+y,50,50);
   }
}

Flowlayout

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

public class layout extends Frame implements ActionListener{
    Button numBt[]=new Button[10];

    public layout(){
     setTitle("FlowLayout");
     setLayout(new FlowLayout(FlowLayout.LEFT,5,10));

      Panel p1=new Panel();
      p1.setLayout(new FlowLayout(FlowLayout.LEFT,5,10));

        for(int i=0;i<3;i++)
         p1.add(numBt[0]=new Button("Button 1"+i));

        add(p1);

        Panel p2=new Panel();
        p2.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));

        for(int i=0;i<3;i++)
         p2.add(numBt[0]=new Button("Button 2"+i));

add(p2);
}

public static void main(String args[]){
   Frame f=new layout();
   f.setSize(500,500);
   f.setVisible(true);
}

public void actionPerformed(ActionEvent e){}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s