Java Programming Lab Programs
Java Programming Lab Programs
(Marksheet Preparation)
import java.io.*;
class Marksheet
{
int m1,m2,m3,m4,m5,tot;
String name=new String();
String regno=new String();
float avg;
void getdata()throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the student name:");
name=br.readLine();
System.out.println("Enter the Register No:");
regno=br.readLine();
System.out.println("Enter the marks");
m1=Integer.parseInt(br.readLine());
m2=Integer.parseInt(br.readLine());
m3=Integer.parseInt(br.readLine());
m4=Integer.parseInt(br.readLine());
m5=Integer.parseInt(br.readLine());
tot=m1+m2+m3+m4+m5;
avg=tot/5;
}
void putdata()throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Subject JAVA mark : " + m1);
System.out.println("Subject DBMS mark : " + m2);
System.out.println("Subject MP mark : " + m3);
System.out.println("Subject LAB1 mark : " + m4);
System.out.println("Subject LAB2 mark : " + m5);
System.out.println("Total="+tot);
System.out.println("Average="+avg);
if((m1>=40)&&(m2>=40)&&(m3>=40)&&(m4>=40)&&(m5>=40))
{
System.out.println("Result=Pass");
if (avg>=60)
{
System.out.println("Class=First");
}
else if (avg>=50)
{
System.out.println("Class=Second");
}
else
{
System.out.println("Class=Third");
}
}
else
{
System.out.println("Result=Fail");
}
}
}
class Clsmain
{
public static void main(String args[])throws IOException
{
Marksheet r1 = new Marksheet();
r1.getdata();
r1.putdata();
}
}
import java.io.*;
class Payoutofbound extends Exception
{
Payoutofbound(String ExMsg)
{
super(ExMsg);
}
}
class MyException
{
public static void main(String as[])throws Exception
{
String Ename,ch;
float Bpay,DA,HRA,PF,Gpay,NetPay;
DataInputStream di=new DataInputStream(System.in);
do
{
try
{
System.out.println("\t\tEXCEPTION CREATION");
System.out.println("\t\t..................");
System.out.println("Enter the Employee Name:");
Ename=di.readLine();
System.out.println("Enter the Basic Pay:");
Bpay=Float.parseFloat(di.readLine());
if(Bpay>5000)
{
DA=Bpay*(17.0f/100);
HRA=Bpay*(25.0f/100);
Gpay=Bpay+DA+HRA;
PF=Bpay*(5.0f/100);
NetPay=Gpay-PF;
System.out.println("\t\tPAYROLL PREPARATION");
System.out.println("\t\t...................");
System.out.println("Employee Name:"+Ename);
System.out.println("Basic Pay:"+Bpay);
System.out.println("DA:"+DA);
System.out.println("HRA:"+HRA);
System.out.println("Gross Pay:"+Gpay);
System.out.println("Net Pay"+NetPay);
}
else
{
throw new Payoutofbound("Organization does not provide basic pay less than 5000");
}
}
catch(Payoutofbound Ex)
{
System.out.println("Caught My Exception");
System.out.println(Ex.getMessage());
}
System.out.println("Do you want to continue(Y/N)?");
ch=di.readLine();
}while(ch.equals("Y"));
}
}
5. Write a Java program to implement the usage of package.
(PERFORM ARITHMETIC OPERATIONS)
Package Creation:
package ARITHMETIC;
class clspackage
{
public int add(int a,int b)
{
return(a+b);
}
public int sub(int c, int d)
{
return(c-d);
}
public int mul(int e, int f)
{
return(e*f);
}
public int div(int g, int h)
{
return(g/h);
}
}
Program:
package ARITHMETIC;
import java.io.*;
import ARITHMETIC.*;
class Packagecreation
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int a,b;
System.out.println("ARITHMETIC OPERATIONS");
System.out.println("Enter the numbers:");
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
clspackage r = new clspackage();
System.out.println("THE RESULT IS");
System.out.println(a + " + " + b + " = " +r.add(a,b));
System.out.println(a + " - " + b + " = " +r.sub(a,b));
System.out.println(a + " * " + b + " = " +r.mul(a,b));
System.out.println(a + " / " + b + " = " +r.div(a,b));
}
}
6. Write a Java program for handling file operations.
(Count the number of words, lines and characters in a given file)
import java.lang.*;
import java.io.*;
import java.util.*;
class WordCount
{
public static void main(String arg[]) throws Exception
{
int char_count=0,n;
int word_count=0;
int line_count=0;
String s;
StringTokenizer st;
BufferedReader buf=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter filename : ");
s=buf.readLine();
buf=new BufferedReader(new FileReader(s));
while((s=buf.readLine())!=null)
{
line_count++;
st=new StringTokenizer(s," ,;:.");
while(st.hasMoreTokens())
{
word_count++;
s=st.nextToken();
char_count+=s.length();
}
}
System.out.println("Character Count : "+char_count);
System.out.println("Word Count : "+word_count);
System.out.println("Line Count : "+line_count);
buf.close();
}
}
import java.awt.*;
import java.applet.*;
9. Write a Java program to demonstrate the use of swing for front end development.
(Registration Form)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Registration extends JApplet implements ActionListener
{
JLabel l1,l2,l3,l4,l5,l6,l7,l8;
JTextField tf1,tf2,tf5,tf6,tf7;
JButton btn1,btn2;
JPasswordField p1,p2;
public Registration()
{
getContentPane().setLayout(null);
l1=new JLabel("REGISTRATION FORM");
l1.setFont(new Font("ARIAL BLACK",Font.BOLD,20));
l2=new JLabel("Name:");
l3=new JLabel("Email-ID:");
l4=new JLabel("Create Password:");
l5=new JLabel("Confirm Password:");
l6=new JLabel("Country:");
l7=new JLabel("State:");
l8=new JLabel("Phone No:");
tf1=new JTextField();
tf2=new JTextField();
p1=new JPasswordField();
p2=new JPasswordField();
tf5=new JTextField();
tf6=new JTextField();
tf1=new JTextField();
btn1=new JButton("Ok");
btn2=new JButton("Clear");
btn1.addActionListener(this);
btn2.addActionListener(this);
l1.setBounds(100,30,400,30);
l2.setBounds(80,70,200,30);
l3.setBounds(80,110,200,30);
l4.setBounds(80,150,200,30);
l5.setBounds(80,190,200,30);
l6.setBounds(80,230,200,30);
l7.setBounds(80,270,200,30);
l8.setBounds(80,310,200,30);
tf1.setBounds(300,70,200,30);
tf2.setBounds(300,110,200,30);
p1.setBounds(300,150,200,30);
p2.setBounds(300,190, 200,30);
tf5.setBounds(300,230,200,30);
tf6.setBounds(300,270,200,30);
tf7.setBounds(300,310,200,30);
btn1.setBounds(50,350,100,30);
btn2.setBounds(170,350,100,30);
getContentPane().add(l1);
getContentPane().add(l2);
getContentPane().add(tf1);
getContentPane().add(l3);
getContentPane().add(tf2);
getContentPane().add(l4);
getContentPane().add(p1);
getContentPane().add(l5);
getContentPane().add(p2);
getContentPane().add(l6);
getContentPane().add(tf5);
getContentPane().add(l7);
getContentPane().add(tf6);
getContentPane().add(l8);
getContentPane().add(tf7);
getContentPane().add(btn1);
getContentPane().add(btn2);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)
{
String s1=tf1.getText();
String s2=tf2.getText();
char[] s3=p1.getPassword();
char[] s4=p2.getPassword();
String s8=new String(s3);
String s9=new String(s4);
String s5=tf5.getText();
String s6=tf6.getText();
String s7=tf7.getText();
}
else
{
tf1.setText("");
tf2.setText("");
p1.setText("");
p2.setText("");
tf5.setText("");
tf6.setText("");
tf7.setText("");
}
}
}
import java.io.*;
import java.sql.*;
public class Demo
{
public static void main(String args[])throws Exception
{
int c;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:student");
Statement stmt=con.createStatement();
do
{
System.out.println("1.Create Table");
System.out.println("2.Insert Into Table");
System.out.println("3.Select From Table");
System.out.println("4.Modification From Table");
System.out.println("5.Delete From Table");
System.out.println("6.Exit");
System.out.println("Enter your choice");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
c=Integer.parseInt(br.readLine());
switch(c)
{
case 1:
System.out.println("Enter the Query:");
stmt.execute(br.readLine());
System.out.println("Table Created");
break;
case 2:
System.out.println("Enter the Query:");
stmt.execute(br.readLine());
System.out.println("1 Row inserted Successfully");
break;
case 3:
System.out.println("Enter the Query:");
ResultSet rs=stmt.executeQuery(br.readLine());
ResultSetMetaData rm=rs.getMetaData();
int cc=rm.getColumnCount();
int i,j;
for(i=1;i<cc;i++)
{
System.out.print(rm.getColumnLabel(i)+"\t");
}
System.out.println("\n");
while(rs.next())
{
for(j=1;j<cc;j++)
{
System.out.println(rs.getString(j)+"\n");
}
System.out.println("\n");
}
con.close();
break;
case 4:
System.out.println("Enter the Query:");
stmt.execute(br.readLine());
System.out.println("Updated Successfully");
break;
case 5:
System.out.println("Enter the Query:");
stmt.execute(br.readLine());
System.out.println("Deleted Successfully");
break;
}
}
while(c!=6);
System.out.println("System Halted......");
}
}