Write a Java program that creates three threads. First thread displays “Good Morning” every one second, the second thread displays “Hello” every two seconds and the third thread displays “Welcome” every three seconds.

Write a Java program that creates three threads. First thread displays “Good Morning” every one second, the second thread displays “Hello” every two seconds and the third thread displays “Welcome” every three seconds.


class A extends Thread
{
synchronized public void run()
{
try
{
while(true)
{
sleep(10);
System.out.println("good morning");
}
}
catch(Exception e)
{ }
}
}
class B extends Thread
{
synchronized public void run()
{
try
{
while(true)
{
sleep(20);
System.out.println("hello");
}
}
catch(Exception e)
{ }
}
}
class C extends Thread
{
synchronized public void run()
{
try
{
while(true)
{
sleep(30);
System.out.println("welcome");
}
}
catch(Exception e)
{ }
}
}
class ThreadDemo
{
public static void main(String args[])
{
A t1=new A();
B t2=new B();
C t3=new C();
t1.start();
t2.start();
t3.start();
}
}

Suppose that a table named Table.txt is stored in a text file. The first line in the file is the header, and the remaining lines correspond to rows in the table. The elements are separated by commas. Write a java program to display the table using Jtable component.

Suppose that a table named Table.txt is stored in a text file. The first line in the file is the header, and the remaining lines correspond to rows in the table. The elements are separated by commas. Write a java program to display the table using Jtable component.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class Table1 extends JFrame
{
int i=0;
int j=0,k=0;
Object data[][]=new Object[5][4];
Object list[][]=new Object[5][4];
JButton save;
JTable table1;
FileInputStream fis;
DataInputStream dis;
public Table1()
{
String d= " ";
Container con=getContentPane();
con.setLayout(new BorderLayout());
final String[] colHeads={"Name","Roll Number","Department","Percentage"};
try
{
String s=JOptionPane.showInputDialog("Enter the File name present in the current directory");
FileInputStream fis=new FileInputStream(s);
DataInputStream dis = new DataInputStream(fis);
while ((d=dis.readLine())!=null)
{
StringTokenizer st1=new StringTokenizer(d,",");
while (st1.hasMoreTokens())
{
for (j=0;j<4;j++)
{
data[i][j]=st1.nextToken();
System.out.println(data[i][j]);
}
i++;
}
System.out.println("______________");
}
}
catch (Exception e)
{
System.out.println("Eception raised" +e.toString());
}
table1=new JTable(data,colHeads);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane scroll=new JScrollPane(table1,v,h);
con.add(scroll,BorderLayout.CENTER);
}
public static void main(String args[])
{
Table1 t=new Table1();
t.setBackground(Color.green);
t.setTitle("Display Data");
t.setSize(500,300);
t.setVisible(true);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Write a java program that simulates a traffic light. The program lets the user select one of three lights: red, yellow, or green. When a radio button is selected, the light is turned on, and only one light can be on at a time No light is on when the program starts.

Write a java program that simulates a traffic light. The program lets the user select one of three lights: red, yellow, or green. When a radio button is selected, the light is turned on, and only one light can be on at a time No light is on when the program starts.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TrafficLight extends JFrame implements ActionListener
{
String msg=" " ;
private JLabel label;
private JTextField display;
private JRadioButton r1,r2,r3;
private ButtonGroup bg;
private Container c;
public TrafficLight()
{
setLayout(new FlowLayout());
c=getContentPane();
label=new JLabel(" Traffic Light");
display =new JTextField(10);
r1=new JRadioButton("RED");
r2=new JRadioButton("GREEN");
r3=new JRadioButton("YELLOW");
bg=new ButtonGroup();
c.add(label);
c.add(r1);
c.add(r2);
c.add(r3);
c.add(display);
bg.add(r1);
bg.add(r2);
bg.add(r3);
r1.addActionListener(this);
r2.addActionListener(this);
r3.addActionListener(this);
setSize(400,400);
setVisible(true);
c.setBackground(Color.pink);
}
public void actionPerformed(ActionEvent ie)
{
msg=ie.getActionCommand();
if (msg.equals("RED"))
{
c.setBackground(Color.RED);
display.setText(msg+ " :TURN ON");
}
else if (msg.equals("GREEN"))
{
c.setBackground(Color.GREEN);
display.setText(msg+ " :TURN ON");
}
else if (msg.equals("YELLOW"))
{
c.setBackground(Color.YELLOW);
display.setText(msg+ " :TURN ON");
}
}
public static void main(String args[])
{
TrafficLight light=new TrafficLight();
light.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Write a Java program that correctly implements producer consumer problem using the concept of inter thread communication.

Write a Java program that correctly implements producer consumer problem using the concept of inter thread communication.

class Q
{
boolean valueSet=false;
int n;
synchronized int get()
{
if(!valueSet)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("Exception is:"+e);
}
System.out.println("got:"+n);
notify();
return n;
}
synchronized void put(int n)
{
if(valueSet)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println
("\n Exception in put:"+e);
}
this.n=n;
valueSet=true;
System.out.println("\nput:"+n);
notify();
}
}
class Producer implements Runnable
{
Q q;
Producer(Q q)
{
this.q=q;
new Thread(this,"Producer").start();
}

Write a Java program for handling mouse events.

Write a Java program for handling mouse events.

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
/*<applet code="Mouseevents"width=200 height=100>
</applet>*/
public class Mouseevents extends Applet implements MouseListener,MouseMotionListener
{
String msg="";
int x=0,y=0;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
x=10;
y=20;
msg="mouse clicked";
repaint();
}
public void mouseEntered(MouseEvent me)
{
x=10;
y=20;
msg="mouse entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
x=10;
y=20;
msg="mouse exited";
repaint();
}
public void mousePressed(MouseEvent me)
{
x=me.getX();
y=me.getY();
msg="down";
repaint();
}
public void mouseReleased(MouseEvent me)
{
x=me.getX();
y=me.getY();
msg="up";
repaint();
}
public void mouseDragged(MouseEvent me)
{
x=me.getX();
y=me.getY();
msg="*";
showStatus("dragging mouse at"+x+","+y);
repaint();
}
public void mouseMoved(MouseEvent me)
{
showStatus("moving mouse at"+me.getX()+","+me.getY());
}
public void paint(Graphics g)
{
g.drawString(msg,x,y);
}
}

JAVA program that works as a simple calculator using grid layout

JAVA program that works as a simple calculator using grid layout

//<applet code="Calculator.class" height="150" width="700"> </applet>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public  class  Calculator extends Applet implements ActionListener
{
JTextField t1,t2,t3;
JLabel l1,l2,l3;
JButton b1,b2,b3,b4;
JPanel p1,p2;
public void init()
{
p1=new JPanel();
p2=new JPanel();
p1.setBackground(Color.gray);
p2.setBackground(Color.gray);
setBackground(Color.lightGray);
l1=new JLabel("Enter the First number :");
p1.add(l1);
t1=new JTextField(10);
p1.add(t1);
l2=new JLabel("Enter the Second number :");
p1.add(l2);
t2=new JTextField(10);
p1.add(t2);
l3=new JLabel("Result");
p1.add(l3);
t3=new JTextField(10);
p1.add(t3);
b1=new JButton("ADD");
p2.add(b1);
b1.addActionListener(this);
b2=new JButton("SUB");
p2.add(b2);
b2.addActionListener(this);
        b3=new JButton("MUL");
p2.add(b3);
b3.addActionListener(this);
b4=new JButton("DIVISION");
p2.add(b4);
b4.addActionListener(this);
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
t1.setBackground(Color.yellow);
t2.setBackground(Color.yellow);
t3.setBackground(Color.yellow);
}
public void actionPerformed(ActionEvent ae)
{
try
{
if (ae.getSource()==b1)
{
int x=Integer.parseInt(t1.getText());
int y=Integer.parseInt(t2.getText());
int sum=x+y;
t3.setText(" "+sum);
}
if (ae.getSource()==b2)
{
int x=Integer.parseInt(t1.getText());
int y=Integer.parseInt(t2.getText());
int sub=x-y;
t3.setText(" "+sub);
}
if (ae.getSource()==b3)
{
int x=Integer.parseInt(t1.getText());
int y=Integer.parseInt(t2.getText());
int mul=x*y;
t3.setText(" "+mul);
}
if (ae.getSource()==b4)
{
int x=Integer.parseInt(t1.getText());
int y=Integer.parseInt(t2.getText());
int div=x/y;
t3.setText(" "+div);
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,e);
}
}
}

Write a program to declare & instantiate an 2D-array to hold marks obtained by students in different subjects in a class. Assume that there are up to 10 students in a class & there are 5 subjects.Find out the best student according to average marks of all subjects and display all the marks of him/her.

Write a program to declare & instantiate an 2D-array to hold marks obtained by students in different subjects in a class. Assume that there are up to 10 students in a class & there are 5 subjects.Find out the best student according to average marks of all subjects and display all the marks of him/her.

package assignment;
import java.io.*;
class student{
    double s1,s2,s3,s4,s5,avg;
    String name,s;
    void add()
    {
        try{
             System.out.print("Give the name :");
             BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
             name=br.readLine();
             System.out.print("Give the Marks :");
             s=br.readLine();
             s1=Double.parseDouble(s);
             s=br.readLine();
             s2=Double.parseDouble(s);
             s=br.readLine();
             s3=Double.parseDouble(s);
             s=br.readLine();
             s4=Double.parseDouble(s);
             s=br.readLine();
             s5=Double.parseDouble(s);
             avg=(s1+s2+s3+s4+s5)/5;
           }
        catch(Exception e)
        {
            System.out.println(e);
        }
   }

   void show()
   {
       System.out.println(name);
       System.out.println(s1);
       System.out.println(s2);
       System.out.println(s3);
       System.out.println(s4);
       System.out.println(s5);

   }
}
public class as16
{

    public static void main(String[] args) {
      try{
             student[] ob=new student[10];
       int i,j=0;
       for(i=0;i<10;i++)
       {
           ob[i]=new student();
           ob[i].add();
       }
       for(i=0;i<9;i++)
       {
        if(ob[i].avg>ob[i+1].avg){
            j=i;
       }
        else
            j=i+1;
       }
       System.out.println("The best student is\n");
       ob[j].show();
       }
       catch(Exception e)
       {
        System.out.println(e);
       }
    }

    }

Write a program to maintain the office database using single inheritance. Superclass is Employee that contain the information as follows- Emp_code, Emp_name, Address, Ph_no, Da-10%, Hra-20%. Create three subclass of Manager, Typist, officer each class having their own basic pay & da,hra remain same.

Write a program to maintain the office database using single inheritance. Superclass is Employee that contain the information as follows- Emp_code, Emp_name, Address, Ph_no, Da-10%, Hra-20%. Create three subclass of Manager, Typist, officer each class having their own basic pay & da,hra remain same.

 
import java.io.*;
class Employee{
    double basic,da,hra;
    String Emp_name,Emp_code,Address,Ph_no;
    void entry()
    {
         try{
             String s;
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
         System.out.println("Give the employee name: ");
         Emp_name=br.readLine();
         System.out.println("Give the employee code: ");
         Emp_code=br.readLine();
         System.out.println("Give the employee address: ");
         Address=br.readLine();
         System.out.println("Give the employee ph no: ");
         Ph_no=br.readLine();
         }
         catch(Exception e)
         {
             System.out.println(e);
         }
        }
    void show(){
        System.out.println("Employee name :"+Emp_name);
        System.out.println("Employee code :"+Emp_code);
        System.out.println("Employee address :"+Address);
        System.out.println("Employee ph no :"+Ph_no);
        System.out.println("Enployee basic :"+basic);
        System.out.println("Employee name :"+da);
        System.out.println("Employee hra: "+hra);

    }
}
class Manager extends Employee{
    void entM()
    {
       try{
        entry();
        String s;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Give the manager basic: ");
        s=br.readLine();
        basic=Double.parseDouble(s);
        da=basic*0.1;
        hra=basic*0.2;
       }
       catch(Exception e)
       {
           System.out.println(e);
       }
    }

}

class Typist extends Employee{
    void entT()
    {
       try{
           entry();
        String s;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Give the Typist basic: ");
        s=br.readLine();
        basic=Double.parseDouble(s);
        da=basic*0.1;
        hra=basic*0.2;
       }
       catch(Exception e)
       {
           System.out.println(e);
       }
    }

}
class Officer extends Employee{
    void entO()
    {
       try{
        entry();
        String s;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Give the officer basic: ");
        s=br.readLine();
        basic=Double.parseDouble(s);
        da=basic*0.1;
        hra=basic*0.2;
       }
       catch(Exception e)
       {
           System.out.println(e);
       }
    }

}
public class as11 {
    public static void main(String aarg[])
    {
        try
        {
        int i,num=0;
        String s;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Give the no. of manager:\n");
        s=br.readLine();
        num=Integer.parseInt(s);
        Manager[] man=new Manager[num];
        for(i=0;i<num;i++)
        {
            man[i]=new Manager();
            man[i].entM();
        }
        System.out.println("Give the no. of Typist :");
        s=br.readLine();
        num=Integer.parseInt(s);
        Typist[] typ=new Typist[num];
        for(i=0;i<num;i++)
        {
            typ[i]=new Typist();
            typ[i].entT();
        }
        System.out.println("Give the no. of Officer:\n");
        s=br.readLine();
        num=Integer.parseInt(s);
        Officer[] off=new Officer[num];
        for(i=0;i<num;i++)
        {
            off[i]=new Officer();
            off[i].entO();
        }

        System.out.println("Manager details\n");
         for(i=0;i<num;i++)
        {
         man[i].show();
        }
        System.out.println("Typist Details\n");
         for(i=0;i<num;i++)
        {
         typ[i].show();
        }
        System.out.println("officer Details\n");
         for(i=0;i<num;i++)
        {
         off[i].show();
        }


    }
        catch(Exception e){

        }

}
}