0% found this document useful (0 votes)
68 views

Java Programming Lab Programs

The documents provide examples of Java programs demonstrating various OOP concepts - classes, methods, constructors, inheritance, exceptions, packages and applets. The programs include marksheet preparation, area calculation, employee salary calculation, arithmetic operations, file handling and mouse event handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Java Programming Lab Programs

The documents provide examples of Java programs demonstrating various OOP concepts - classes, methods, constructors, inheritance, exceptions, packages and applets. The programs include marksheet preparation, area calculation, employee salary calculation, arithmetic operations, file handling and mouse event handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1.Write a Java program to demonstrate the use of class and methods.

(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();
}
}

2. Write a Java program to demonstrate the constructor.


(Area of Rectangle)
import java.io.*;
class shape
{
int l;
int b;
shape()
{
l=b=0;
}
shape(int w)
{
l=b=w;
}
shape(int len,int bre)
{
l=len;
b=bre;
}
int area()
{
return(l*b);
}}
class Constructor
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int d,e;
System.out.println("Enter the input values here");
d=Integer.parseInt(br.readLine());
e=Integer.parseInt(br.readLine());
shape r1=new shape();
System.out.println("The area is"+r1.area());
shape r2=new shape(d);
System.out.println("The area is"+r2.area());
shape r3=new shape(d,e);
System.out.println("The area is"+r3.area());
}
}
3. Write a Java program to implement inheritance
(Area of rectangle and circle)
import java.io.*;
interface Area
{
final static float pi=3.14f;
float compute(float x, float y);
}
class Rectangle implements Area
{
public float compute(float x, float y)
{
return(x * y);
}
}
class Circle implements Area
{
public float compute(float x,float y)
{ return(pi*x*x);
}
}
class InterfaceArea
{
public static void main(String args[])throws IOException
{
float len,bre,rad;
Rectangle rect = new Rectangle();
Circle cir = new Circle();
Area area;
DataInputStream di=new DataInputStream(System.in);
area = rect;
System.out.println("CALCULATING AREA OF RECTANGLE USING MULTIPLE
INHERITANCE");
System.out.println("Enter the Length and Breadth of the Rectangle");
len=Float.parseFloat(di.readLine());
bre=Float.parseFloat(di.readLine());
System.out.println("Area of Rectangle:"+area.compute(len,bre));
area = cir;
System.out.println("CALCULATING AREA OF CIRCLE USING MULTIPLE
INHERITANCE");
System.out.println("Enter the Radius of the Rectangle");
rad=Float.parseFloat(di.readLine());
System.out.println("Area of Circle:"+area.compute(rad,0));
}
}

4. Write a Java program to create an exception and throw the exception.


(Employee Salary Calculation)

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();
}
}

7. Write a Java program to create applets.


(Display different shapes using Applet)

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

public class LineRect extends Applet


{
public void paint(Graphics g)
{
g.drawString("Display diffrent Shapes",100,180);
g.drawLine(10,10,50,50);
g.drawRect(10,60,40,30);
g.setColor(Color.blue);
g.fillRect(60,10,30,80);
g.setColor(Color.green);
g.drawRoundRect(10,100,80,50,10,10);
g.setColor(Color.yellow);
g.fillRoundRect(20,110,60,30,5,5);
g.setColor(Color.red);
g.drawLine(100,10,230,140);
g.drawLine(100,140,230,10);
g.drawOval(230,10,200,150);
g.setColor(Color.blue);
g.fillOval(245,25,100,100);
}
};

8. Write a Java program for handling mouse events.


(Mouse Handling Operations using Applet)
import java.awt.*;
import java.awt.event.*;
class MouseEvents extends Frame implements MouseListener, MouseMotionListener
{
String s="";
int x=100,y=100;
public MouseEvents()
{
setTitle("MOUSE CLICK OPERATIONS");
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{
s="MOUSE PRESSED";
repaint();
}
public void mouseReleased(MouseEvent e)
{
s="MOUSE RELEASED";
repaint();
}
public void mouseEntered(MouseEvent e)
{ }
public void mouseExited(MouseEvent e)
{
s="MOUSE EXITED";
repaint();
}
public void mouseDragged(MouseEvent e)
{
s="MOUSE DRAGGED";
repaint();
}
public void mouseMoved(MouseEvent e)
{
s="MOUSE MOVED";
repaint();
}
public void paint(Graphics g)
{
Font f=new Font("Arial", Font.BOLD,20);
g.setFont(f);
g.drawString("PERFORMING VARIOUS MOUSE CLICK OPERATIONS",100,50);
g.drawString(s,200,400);
}
public static void main(String as[])
{
MouseEvents mm=new MouseEvents();
mm.resize(600,600);
mm.show();
mm.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}

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("");
}
}
}

10. Write a Java program to develop an application with JDBC.

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......");
}
}

You might also like