Multithreading Using Priorities Class Mythread1 Extends Thread (
Multithreading Using Priorities Class Mythread1 Extends Thread (
Multithreading Using Priorities Class Mythread1 Extends Thread (
}
}
Output:
classWordCount {
while(wordScanner.hasNext())
{
Temp = wordScanner.next(); // return next word if presents.
words++;
chars = chars + temp.length();
}
while(lineScanner.hasNextLine())// return boolean if line presents.
{
lineScanner.nextLine(); // return next line if presents.
lines++;
}
System.out.println("\nTotal Words = "+words+"\nTotal line =
"+lines+ "\nTotal characters = "+chars);
}catch(IOException e)
{
System.out.println(e);
}
}
}
Output:
importjava.awt.Font;
importjava.awt.FontMetrics;
importjava.awt.Graphics;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.Color;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.Timer;
publicFontSizeAnimation() {
timer = new Timer(20, this);
timer.setInitialDelay(500);
timer.start();
}
intstringWidth = fm.stringWidth(s);
g.setColor(Color.blue);
g.drawString(s, (w - stringWidth) / 2, h / 2);
}
Public SwingMenu(){
JFrame frame = new JFrame("Creating a JMenuBar, JMenu, JMenuItem,
separator Component and Popup Menu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenu Barmenubar = new JMenuBar();
JMenu filemenu = new JMenu("File");
filemenu.add(new JSeparator());
JMenu editmenu = new JMenu("Edit");
editmenu.add(new JSeparator());
JMenuItem fileItem1 = new JMenuItem("New");
fileItem1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{JOptionPane.showMessageDialog(null, "new", "You Have Clicked New",
JOptionPane.PLAIN_MESSAGE);}
});
JMenuItem fileItem2 = new JMenuItem("Open");
JMenuItem fileItem3 = new JMenuItem("Close");
fileItem3.add(new JSeparator());
filemenu.add(fileItem1);
filemenu.add(fileItem2);
filemenu.add(fileItem3);
filemenu.add(fileItem4);
editmenu.add(editItem1);
editmenu.add(editItem2);
editmenu.add(editItem3);
editmenu.add(editItem4);
menubar.add(filemenu);
menubar.add(editmenu);
frame.setJMenuBar(menubar);
frame.setSize(400,400);
frame.setVisible(true);
}
}
Output:
5. Implement calculator using Java AWT controls.
importjava.awt.*;
importjava.awt.event.*;
importjava.applet.*;
importjavax.swing.*;
lcdField.setEditable(false);
//Create buttons
for (int i = 0; i <=9; i++)
keysArray[i] = new JButton(String.valueOf(i));
keysArray[10] = new JButton("/");
keysArray[11] = new JButton("*");
keysArray[12] = new JButton("-");
keysArray[13] = new JButton("+");
keysArray[14] = new JButton("=");
keysArray[15] = new JButton(".");
keysArray[16] = new JButton("CLR");
Calc.html:
<html>
<body>
<applet code="CalculatorApplet.class" width="350" height="350">
Java applet that draws calculator.
</applet>
</body>
</html>
Output:
6. Create a Student mark statement using JDBC control and display the
information using Table.
importjava.util.Scanner;
importjava.sql.*;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:std");
}
catch(Exception e)
{
e.printStackTrace();
}
for(i=0;i<choice;i++)
{
System.out.println("Enter your name:");
stud.name=nameread.nextLine();
System.out.println("Enter your id:");
stud.id=nameread.nextLine();
stud.avg=0;
stud.result = "Pass";
for(int j=0;j<5;j++)
{
System.out.println("Enter the "+(j+1)+" subject marks:");
stud.marks[j]=readmarks.nextDouble();
stud.avg += stud.marks[j];
if (stud.marks[j] < 50)
stud.result = "Fail";
}
stud.avg = stud.avg/5;
System.out.println("Name of student:"+stud.name+"\n"+"ID of student:"
+stud.id+"\n"+"Percentage obtained:"+stud.avg+" Result: "+stud.result);
try
{
Statement stmt=conn.createStatement();
String query = "insert into std
(id,name,mark1,mark2,mark3,mark4,mark5,avg,result) " +
"values('"+stud.id+"','"+stud.name+"',"+stud.marks[0]+
","+stud.marks[1]+","+stud.marks[2]+","+stud.marks[3]+","+
stud.marks[4]+","+stud.avg+",'"+stud.result+"')";
System.out.println(query);
stmt.executeUpdate(query);
}
catch(Exception e)
{
e.printStackTrace();
}
}
System.out.println("\nStudents List"+"\n");
System.out.println("ID"+"\t"+"Name"+"\t"+"Mark1"+"\t"+"Mark2"+"\t"+"Mark3"+
"\t"+"Mark4"+"\t"+"Mark5"+"\t"+"Avg"+"\t"+"Result"+"\n");
try
{
Statement stmt1=conn.createStatement();
ResultSetrs=stmt1.executeQuery("select * from std");
while(rs.next())
{
System.out.println(rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getDouble(3)
+"\t"+rs.getDouble(4)
+"\t"+rs.getDouble(5)+"\t"+rs.getDouble(6)+"\t"+rs.getDouble(7)+"\t"+rs.ge
tDouble(8)+"\t"+rs.getString(9));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Output:
Name of student:Thangam
ID of student:s001
Percentage obtained:88.4
Result: Pass
Students List
//SendConsole.java
import java.net.*;
import java.io.*;
importjava.util.*;
//ReceiveConsole.java
import java.net.*;
import java.io.*;
importjava.util.*;
try
{
intlen=64000;
FileOutputStreamfout = new FileOutputStream(filename);
byte[] arr = new byte[64000];
DatagramSocket ds = new DatagramSocket(777);
DatagramPacketdp = new DatagramPacket(arr,len);
System.out.println("\nSocketCreated");
ds.receive(dp);
System.out.println("Packet Received");
for(int i=0;i<dp.getLength();i++)
{
fout.write((byte)arr[i]);
}
fout.close();
ds.close();
ds=null;
dp=null;
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
Enter Filename
SendConsole.java
Enter Machinename :
localhost
SendConsole.java 997
is created
Enter Filename
Temp.java
Socket created
Packet received
8. Write a Java program to create an Employee pay bill calculation using
various swing controls
import java.io.*;
importjava.awt.*;
importjavax.swing.*;
importjava.awt.event.*;
importjava.sql.*;
importjava.util.*;
classEmployeeInformation {
JFrame f;
JPanel p1,p2,p3;
JTabbedPanetp;
ImageIcon btnimg1,btnimg2;
JLabel l1, l2, l3, l4,l5,l6,l7,l8,l9,l10;
JTextField tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8,tf9,tf10;
JScrollPane sp1;
JButton savebtn,resetbtn,editbtn1,editbtn2,deletebtn ;
EmployeeInformation(){
f=new JFrame("Form");
p1=new JPanel(new GridLayout(5,2));
p2=new JPanel(new GridLayout(5,2));
p3=new JPanel(new GridLayout(2,2));
tp=new JTabbedPane();
l1=new JLabel("Employee ID:");
l2=new JLabel("Employee Name:");
l3=new JLabel("Employee Address:");
l4=new JLabel("Salary:");
l5=new JLabel("Enter Employee ID to delete record:");
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(l3);
p1.add(tf3);
p1.add(l4);
p1.add(tf4);
p1.add(savebtn);
p1.add(resetbtn);
p2.add(l7);
p2.add(tf7);
p2.add(l8);
p2.add(tf8);
p2.add(l9);
p2.add(tf9);
p2.add(l10);
p2.add(tf10);
p2.add(editbtn1);
p2.add(editbtn2);
p3.add(l5);
p3.add(tf5);
p3.add(deletebtn);
resetbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEventae){
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
}
});
savebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEventae){
String value1=tf1.getText();
String value2=tf2.getText();
String value3=tf3.getText();
String value4=tf4.getText();
Connection con = null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:std");PreparedStatement
st=con.prepareStatement("insert into emp(emp_id,emp_name,emp_address,salary)
values(?,?,?,?)");
st.setString(1,value1);
st.setString(2,value2);
st.setString(3,value3);
st.setString(4,value4);
st.executeUpdate();
JOptionPane.showMessageDialog(p1,"Data is successfully inserted into database.");
con.close();
}
catch(Exception e){
System.out.println(e);
JOptionPane.showMessageDialog(p1,"Error in submitting data!");
}
}
});
deletebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEventae){
String value1=tf5.getText();
Connection con = null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:std");
PreparedStatementst=con.prepareStatement("DELETE FROM emp WHERE emp_id = ?");
st.setString(1,value1);
st.executeUpdate();
JOptionPane.showMessageDialog(p3,"Record is deleted successfully.");
con.close();
}
catch(Exception exp3)
{
JOptionPane.showMessageDialog(p3,"Error in deleting record.");
System.out.println(exp3);
}
}
});
editbtn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEventae){
String value=tf7.getText();
Connection con = null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:std");
PreparedStatementst=con.prepareStatement("select * from emp where emp_id=?");
st.setString(1,value);
ResultSet res=st.executeQuery();
res.next();
tf7.setText(Integer.toString(res.getInt(1)));
tf8.setText(res.getString(2));
tf9.setText(res.getString(3));
tf10.setText(Integer.toString(res.getInt(4)));
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(p2,"Can not edit data");
}
}
});
editbtn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEventae){
Connection con = null;
try
{
int x=JOptionPane.showConfirmDialog(p2,"Confirm edit? All data will be replaced");
if(x==0){
try{
String value1=tf7.getText();
String value2=tf8.getText();
String value3=tf9.getText();
String value4=tf10.getText();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:std");
Statement st=con.createStatement();
st.executeUpdate("update emp set emp_name='"+value2+"',
emp_address='"+value3+"', salary='"+value4+"' where emp_id='"+value1+"'");
JOptionPane.showMessageDialog(p2,"Updated successfully");
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(p2,"Error in updating edit fields");
System.out.println(ex);
}
}
}
catch(Exception ex)
{
System.out.println(ex);
JOptionPane.showMessageDialog(p2,"Error");
}
}
});
}
void dis()
{
f.getContentPane().add(tp);
tp.addTab("Add Record",p1);
tp.addTab("Edit Record",p2);
tp.addTab("Delete Record",p3);
f.setSize(350,180);
f.setVisible(true);
f.setResizable(true);
}
public static void main(String z[]){
EmployeeInformation pro=new EmployeeInformation();
pro.dis();
}
}
Output:
CONTENT
S.No Page
Date Name of the Experiment Initial
. No.