JavaLabManual For 2 ND Years

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 51

SITA2302 - Programming in Java Lab

Exp no: 1
Exp name: classes and objects

/*program to find the factorial of a given number using classes and objects*/

import java.io.*;
import java.util.Scanner;
class Factorial
{
void fact()
{
int fact=1,n,i;
Scanner s=new Scanner(System.in);
System.out.println("Enter a number to find the factorial of that number");
n=s.nextInt();
for(i=1;i<=n;i++)
fact=fact*i;
System.out.println("The factorial of a given number "+n+" is"+fact);
}
}
class FactorialDemo
{
public static void main(String args[])
{
Factorial f= new Factorial();
f.fact();
}
}

Output:
Exp no:2
Exp name: constructors
2a)
/* program to add 2 numbers using default constructor*/

import java.io.*;
import java.util.*;
class Add
{
int a,b;
Add()
{
a=10;
b=10;
}
void show()
{
System.out.println("the sum of 2 numbers is "+(a+b));
}
public static void main(String args[])
{
Add ob=new Add();
ob.show();
}
}

Output:
2 b)
/*program to add 2 numbers using parameter constructor*/

import java.io.*;
import java.util.*;
class Add
{
int a,b;
Add(int x,int y)
{
a=x;
b=y;
}
void show()
{
System.out.println("the sum of 2 numbers is "+(a+b));
}
public static void main(String args[])
{
Add ob=new Add(20,30);
ob.show();
}
}

Output:
2 C)

/*program to add 2 number using copy constructor*/


import java.io.*;
import java.util.*;
class Add
{
int a,b;
Add(int x,int y)
{
a=x;
b=y;
}
Add( Add c)
{
a=c.a*2;
b=c.b*2;
}
void show()
{
System.out.println("the sum of 2 numbers is "+(a+b));
}
public static void main(String args[])
{
Add ob=new Add(20,30);
Add ob1=new Add(ob);
ob.show();
ob1.show();
}
}
Output:
2 D)

/*program to add 2 numbers using constructor overloading*/

import java.io.*;
class Add
{
int a, b;
Add(int x)
{
a=x;
b=x;
}
Add(int a1,int b1)
{
a=a1;
b=b1;
}
Add( Add c)
{
a=c.a*3;
b=c.b-3;
}
void cal()
{
System.out.println("the sum of 2 numbers is "+(a+b));
}
public static void main(String args[])
{
Add c1=new Add(101);
Add c2=new Add(10,14);
Add c3=new Add(c2);
c1.cal();
c2.cal();
c3.cal();
}
}
Output:
Exp no:3
Exp name: Inheritance
3A)
/*program to read to display student details using single inheritance*/

import java.io.*;
import java.util.Scanner;
class Student
{
String name,dept;
int id;
Scanner s=new Scanner(System.in);
void get()
{
System.out.println("Enter name");
name=s.next();
System.out.println("Enter id ");
id=s.nextInt();
System.out.println("Enter department");
dept=s.next();
}
void show()
{
System.out.println("Student name:"+name);
System.out.println("Department :"+dept);
System.out.println("ID no :"+id);
}
}
class Area extends College
{
String state,a;
void gets()
{
System.out.println("Enter area");
a=s.next();
System.out.println("Enter state");
state=s.next();
}
void show()
{
System.out.println("Area:"+a);
System.out.println("State:"+state);
}
}
class Details
{
public static void main(String args[])
{
Student c=new Student();
Area a=new Area();
c.get();
a.gets();
c.show();
a.show();
}
}

Output:
3B)

/*program to find the gross salary of an employee using multi level inheritance*/

import java.io.*;
import java.util.Scanner;
class Employee
{
String name;
int empid;
double bp;
Scanner s=new Scanner(System.in);
void get()
{
System.out.println("Enter employee name");
name=s.next();
System.out.println("Enter employee id");
empid=s.nextInt();
System.out.println("Enter the basic pay of "+name);
bp=s.nextDouble();
}
void show()
{
System.out.println("EMPLOYEE DETAIL");
System.out.println("Name :"+name);
System.out.println("Empid :"+empid);
System.out.println("basic pay :"+bp);
}
}
class Payment extends Employee
{
double gr,hra,da;
void showpay()
{
da=0.1*bp;
hra=0.12*bp;
gr=bp+hra+da;
System.out.println("DA :"+da);
System.out.println("HRA :"+hra);
System.out.println("Gross salary:"+gr);
}
}
class Display extends Payment
{
void showall()
{
show();
showpay();
}
}
class Sample
{
public static void main(String args[])
{
Display ob=new Display();
ob.get();
ob.showall();
}
}

Output:
3C)

/*program to compute customer balance from the bank using hierarchial inheritance*/

import java.io.*;
import java.util.Scanner;
class Bank
{
String name;
long acno;
double bal;
Scanner s=new Scanner(System.in);
void get()
{
System.out.println("Enter bank name ");
name=s.next();
System .out.println("Enter acc no.");
acno=s.nextLong();
}
void show()
{
System.out.println("bank name:"+name);
System.out.println("acc no."+acno);
}
}
class Deposit extends Bank
{
void dep(Bank b)
{
double dp;
System.out.println("enter the amount to be deposited");
dp=s.nextDouble();
b.bal=b.bal+dp;
b.show();
System.out.println("after depositing ur account balance is "+b.bal);
}
}
class Withdraw extends Bank
{
void withd(Bank b)
{
if(b.bal==0)
System.out.println("deposit first...ur balance is"+b.bal);
else
{
double w;
System.out.println("enter the amount to withdraw");
w=s.nextDouble();
if(w>b.bal)
{
b.show();
System.out.println("balance is low ..ur balance is "+b.bal);
}
else
{
b.bal=b.bal-w;
System.out.println("ur balance after withdrawing is"+b.bal);
}
}
}
}
class Balance
{
public static void main(String args[])
{
int n;
Bank b1=new Bank();
Deposit d=new Deposit();
Withdraw w1=new Withdraw();
b1.get();
System.out.println("Current balance is "+b1.bal);
System.out.println("1.deposit 2.withdraw");
System.out.println("enter your choice");
Scanner i=new Scanner(System.in);
n=i.nextInt();
if(n==1)
d.dep(b1);
else if(n==2)
w1.withd(b1);
else
System.out.println("invalid choice");
}
}
Output:
Exp no:4
Exp name: Super keyword

4 A)
/*program to invoke base class constructor using super keyword*/

import java.io.*;
import java.util.*;
class Addition
{
int a,b;
Addition(int x,int y)
{
a=x;
b=y;
}
void show()
{
System.out.println("The values are"+a+" and "+b);
}
}
class Execute extends Addition
{
int c,sum;
Execute(int x,int y,int z)
{
super(x,y);
c=z;
}
void showc()
{
System.out.println("and "+c+" is");
}
void cal()
{
int sum=a+b+c;
System.out.println("the sum of the given values is "+sum);
}
}
class Demo
{
public static void main(String args[])
{
int a1,b1,c1;
Scanner s=new Scanner(System.in);
System.out.println("enter 3 values to add them");
a1=s.nextInt();
b1=s.nextInt();
c1=s.nextInt();
Execute e=new Execute(a1,b1,c1);
e.show();
e.showc();
e.cal();
}
}
Output:
4 B)
/* program using super keyword to access method to display college details*/

import java.io.*;
import java.util.Scanner;
class College
{
String name,add;
int code;
Scanner s=new Scanner(System.in);
void get()
{
System.out.println("Enter college name");
name=s.next();
System.out.println("Enter counselling code of college");
code=s.nextInt();
System.out.println("Enter address");
add=s.next();
}
void show()
{
System.out.println("College name:"+name);
System.out.println("Address :"+add);
System.out.println("counselling code :"+code);
}
}
class Area extends College
{
String state,country;
void gets()
{
System.out.println("Enter state");
state=s.next();
System.out.println("Enter country");
country=s.next();
}
void show()
{
super.show();
System.out.println("State:"+state);
System.out.println("Country:"+country);
}
}
class Details
{
public static void main(String args[])
{
Area a=new Area();
a.get();
a.gets();
a.show();
}
}

Output:
4 C)

/*program using super keyword to access the attribute*/

import java.io.*;
class Employee
{
float salary=10000;
}
class HR extends Employee
{
float salary=20000;
void display()
{
System.out.println("Salary: "+super.salary);
System.out.println("Salary: "+salary);
}
}
class Super
{
public static void main(String[] args)
{
HR obj=new HR();
obj.display();
}
}
Output:
Exp no: 5
Exp name: Abstract class

/*program to find the interest using abstract class*/

import java.io.*;
import java.util.Scanner;
abstract class bank
{
abstract void roi();
double bal,interest;
long accountno;
Scanner i = new Scanner(System.in);
void getaccountno()
{
System.out.println("\nEnter the account number");
accountno=i.nextLong();
System.out.println("\nEnter the account balance");
bal=i.nextDouble();
}
void printaccdetails()
{
roi();
System.out.println("Account no:"+accountno);
System.out.println("Interest Rate:"+interest);
System.out.println("Balance after interest:"+bal);
}
}
class Bank1 extends bank
{
void roi()
{
interest=0.07;
bal=(bal+(bal*interest));
}
}
class Bank2 extends bank
{
void roi()
{
interest=0.06;
bal=(bal+(bal*interest));
}
}
class Demo
{
public static void main(String args[]){
Bank1 acc1= new Bank1();
Bank2 acc2= new Bank2();
acc1.getaccountno();
acc1.printaccdetails();
acc2.getaccountno();
acc2.printaccdetails();
}
}

Output:

Ex no : 6
Exp name: Interface

/*program to find the area for different shapes using interface*/

interface Area
{
double pi = 3.14;
double calc(double x,double y);
}

class Rect implements Area


{
public double calc(double x,double y)
{
return(x*y);
}
}

class Cir implements Area


{
public double calc(double x,double y)
{
return(pi*x*x);
}
}

class Test
{
public static void main(String arg[])
{
Rect r = new Rect();
Cir c = new Cir();
Area a;
a = r;
System.out.println("\nArea of Rectangle is : " +a.calc(10,20));
a = c;
System.out.println("\nArea of Circle is : " +a.calc(15,15));
}
}
Exp No :7
Exp name : Simple Calculator

/*program to create Custom package to implement calculator operations*/

Add.java

package p1;
import java.util.*;
public class Add
{
int s;
public void sum()
{
System.out.print("Enter the first number: ");
Scanner scan=new Scanner(System.in);
int x=scan.nextInt();
System.out.print("Enter the second number: ");
Scanner scan1=new Scanner(System.in);
int y=scan1.nextInt();
s=x+y;
System.out.println("sum="+s);
}
}
Sub.java

package p2;
import java.util.*;
public class Sub
{
int d;
public void diff()
{
System.out.print("Enter the first number: ");
Scanner scan=new Scanner(System.in);
int x=scan.nextInt();
System.out.print("Enter the second number: ");
Scanner scan1=new Scanner(System.in);
int y=scan1.nextInt();
d=x-y;
System.out.println("Difference="+d);
}
}
Mult.java
package p3;
import java.util.*;
public class Mult
{
int m;
public void pro()
{
System.out.print("Enter the first number: ");
Scanner scan=new Scanner(System.in);
int x=scan.nextInt();
System.out.print("Enter the second number: ");
Scanner scan1=new Scanner(System.in);
int y=scan1.nextInt();
m=x*y;
System.out.println("Product="+m);
}
}
Div.java

package p4;
import java.util.*;
public class Div
{
int q;
public void divd()
{
System.out.print("Enter the first number: ");
Scanner scan=new Scanner(System.in);
int x=scan.nextInt();
System.out.print("Enter the second number: ");
Scanner scan1=new Scanner(System.in);
int y=scan1.nextInt();
q=x/y;
System.out.println("Division="+q);
}
}
Calculator.java

package p5;
//importing pre-defined package
import java.util.*;
//importing user-defined package
import p1.Add;
import p2.Sub;
import p3.Mult;
import p4.Div;
public class Calculator
{
public static void main(String args[])
{
System.out.print("Enter your choice: ");
Scanner scan=new Scanner(System.in);
int t=scan.nextInt();
switch(t)
{
case 1:
Add a=new Add();
a.sum();
break;
case 2:
Sub s=new Sub();
s.diff();
break;
case 3:
Mult m=new Mult();
m.pro();
break;
case 4:
Div d=new Div();
d.divd();
break;
}
}
}
Output:

Enter your choice: 3


Enter the first number: 2
Enter the second number: 23
Product=46

Exp no:8
Exp name: Exception handling

/*program to implement Exception handling and multiple catch block*/

import java.io.*;
import java.util.Scanner;
class ExcepText
{
public static void main(String args[]) {
try
{
int select;
Scanner i= new Scanner(System.in);
System.out.println("\nEnter the Exception to be Handled");
System.out.println("\n1.ArrayIndexOutOfBoundsException\n2.ArithmeticException");
System.out.println("3.NullException");
select=i.nextInt();
switch(select)
{
case 1://ArrayIndexOutOfBoundsException
int a[] = new int[2];
System.out.println("Access element three :" + a[3]);
break;
case 2://ArithmeticException
int num1=30, num2=0;
int output=num1/num2;
System.out.println ("Result = " +output);
break;
case 3://NullPointerException
String str=null;
System.out.println (str.length());
break;
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Exception thrown :" + e);
}
catch(ArithmeticException e1)
{
System.out.println("Exception thrown :" + e1);
}
catch(NullPointerException e2)
{
System.out.println("Exception thrown :" +e2);
}
System.out.println("Out of the block");
}
}

Output:

Exp No 9
Exp name : User defined Exception
/*program that define user defined exception */

//MyException.java
public class MyException extends Exception {
public MyException(String mymsg) {
super(mymsg);
}
}
//Sample.java
import java.io.*;
class Sample {
public static void main(String args[]) throws Exception {
Sample es = new Sample();
es.displayMymsg();
}
public void displayMymsg() throws MyException {
for(int j=8;j>0;j--) {
System.out.println("j= "+j);
if(j==7)
throw new MyException("This is my own Custom Message");
}
}
}

Output:
Exp no: 10 a
Exp name: Thread by extending Thread class

/*program to extend thread class*/

Import java.io.*;
class Test extends Thread
{
public void run()
{
for(int i=1;i<5;i++)
{
try
{
Thread.sleep(5000);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(i);
}
}
public static void main(String args[])
{
Test t1=new Test();
Test t2=new Test();
t1.start();
t2.start();
}
}
Output:
Exp no:10 b

Exp name: implementing thread by Runnable interface

/*program to implement the concept of threading by implementing Runnable interface*/


Import java.io.*;
class FirstThread implements Runnable
{
public void run()
{
for ( int i=1; i<=10; i++){
System.out.println( "Message from First Thread : " +i);
Try
{
Thread.sleep (1000);
}
catch (InterruptedException interruptedException){
System.out.println( "First Thread is interrupted when it is sleeping" +interruptedException);
}
}
}
}
class SecondThread implements Runnable
{
public void run(){
for ( int i=1; i<=10; i++)
{
System.out.println( "Message from Second Thread : " +i);
Try
{
Thread.sleep(1000);
}
catch (InterruptedException interruptedException)
{
System.out.println( "Second Thread is interrupted when it is sleeping" +interruptedException);
}
}
}
class ThreadDemo
{
public static void main(String args[]){
FirstThread firstThread = new FirstThread();
SecondThread secondThread = new SecondThread();
Thread thread1 = new Thread(firstThread);
thread1.start();
Thread thread2 = new Thread(secondThread);
thread2.start();
}
}

Output:

Exp no :11
Exp name: Clonnable class

/*program to create copy of a given object using Clonable interface*/

class Student implements Cloneable{


int rollno;
String name;

Student18(int rollno,String name){


this.rollno=rollno;
this.name=name;
}

public Object clone()throws CloneNotSupportedException{


return super.clone();
}

public static void main(String args[]){


try{
Student s1=new Student(101,"amit");

Student s2=(Student)s1.clone();

System.out.println(s1.rollno+" "+s1.name);
System.out.println(s2.rollno+" "+s2.name);

}catch(CloneNotSupportedException c){}

}
}
Output:101 amit
101 amit

Exp no: 12
Exp name: Wrapper class

/*program to implement Wrapper class*/

Import java.io.*;
class WrapperExample1
{
public static void main(String args[])
{
//Converting int into Integer
int a=20;
float b=56.3f;
Integer i=Integer.valueOf(a);
Integer j=a;
Float i1=Float.valueOf(b);
Float j1=b;
System.out.println(a+" "+i+" "+j);
System.out.println(b+" "+i1+" "+j1);
}
}

Output:
Exp no: 13
Exp name: Files
13.a
/*program to implement FileInputStream and FileOutputStream*/

import java.io.*;
public class CopyFile
{
public static void main(String args[]) throws IOException
{
FileInputStream in = null;
FileOutputStream out = null;
try
{
in = new FileInputStream("input.txt");
out = new FileOutputStream("output.txt");
int c;
while ((c = in.read()) != -1)
{
out.write(c);
}
}
finally
{
if (in != null)
{
in.close();
}
if (out != null)
{
out.close();
}
}
}
}
Output:
13 B)

/*program to implement FileReader and FileWriter class*/

import java.io.*;
public class CopyFile {
public static void main(String args[]) throws IOException {
FileReader in = null;
FileWriter out = null;
try {
in = new FileReader("input.txt");
out = new FileWriter("output.txt");
int c;
while ((c = in.read()) != -1)
out.write(c);
}
finally
{
if (in != null)
in.close();
if (out != null)
out.close();
}
}
}

Output:
13 C)

/*program to implemrnt DataInputStream and DataOutputStream*/

import java.io.*;
public class DataInput_Stream {

public static void main(String args[])throws IOException {

// writing string to a file encoded as modified UTF-8


DataOutputStream dataOut = new DataOutputStream(new FileOutputStream("E:\\file.txt"));
dataOut.writeUTF("hello");

// Reading data from the same file


DataInputStream dataIn = new DataInputStream(new FileInputStream("E:\\file.txt"));

while(dataIn.available()>0) {
String k = dataIn.readUTF();
System.out.print(k+" ");
}
}
}

Output:
Exp no: 14
Exp name: Applet

/*program to implement applet*/

import java.applet.*;
import java.awt.*;
/*<applet code="MyApplet" width="200" height="200">
<param name="num1" value="70">
<param name="num2" value="67">
</applet>*/
public class MyApplet extends Applet
{
int x,y;
public void start()
{
x=Integer.parseInt(getParameter("num1"));
y=Integer.parseInt(getParameter("num2"));
}
public void paint(Graphics g)
{
int z;
z=x+y;
g.drawString("Product is "+z,50,50);
}
}

Output:
Exp no :15
Exp name: Passing parameters to Applet

/* java program to pass parameters to Applet and display the output*/

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

/*
<applet code="Applet8" width="400" height="200">
<param name="Name" value="Roger">
<param name="Age" value="26">
<param name="Sport" value="Tennis">
<param name="Food" value="Pasta">
<param name="Fruit" value="Apple">
<param name="Destination" value="California">
</applet>
*/
public class Applet8 extends Applet
{
String name;
String age;
String sport;
String food;
String fruit;
String destination;
public void init()
{
name = getParameter("Name");
age = getParameter("Age");
food = getParameter("Food");
fruit = getParameter("Fruit");
destination = getParameter("Destination");
sport = getParameter("Sport");
}
public void paint(Graphics g)
{
g.drawString("Reading parameters passed to this applet -", 20, 20);
g.drawString("Name -" + name, 20, 40);
g.drawString("Age -" + age, 20, 60);
g.drawString("Favorite fruit -" + fruit, 20, 80);
g.drawString("Favorite food -" + food, 20, 100);
g.drawString("Favorite destination -" + name, 20, 120);
g.drawString("Favorite sport -" + sport, 20, 140);
}

}
Exp no :16
Exp name : Event Handling using AWT

/*java program for event handling using awt controls*/

import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener{
TextField tf;
AEvent(){

//create components
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);

//register listener
b.addActionListener(this);//passing current instance

//add components and set size, layout and visibility


add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
tf.setText("Welcome");
}
public static void main(String args[]){
new AEvent();
}
}
Exp no : 17
Exp name : Mouse and Key Events in Java

/* program to handle Mouse and Key events */

import java.awt.*;
import java.awt.event.*;
public class MouseListenerExample extends Frame implements MouseListener{
Label l;
MouseListenerExample(){
addMouseListener(this);

l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
l.setText("Mouse Released");
}
public static void main(String[] args) {
new MouseListenerExample();
}
}
Exp no : 18
Exp name : Swing Controls

/* program to create a Java Application Programming Interface by using swing controls */

import javax.swing.*;
import java.awt.*;
class gui {
public static void main(String args[]) {

//Creating the Frame


JFrame frame = new JFrame("Chat Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);

//Creating the MenuBar and adding components


JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("FILE");
JMenu m2 = new JMenu("Help");
mb.add(m1);
mb.add(m2);
JMenuItem m11 = new JMenuItem("Open");
JMenuItem m22 = new JMenuItem("Save as");
m1.add(m11);
m1.add(m22);

//Creating the panel at bottom and adding components


JPanel panel = new JPanel(); // the panel is not visible in output
JLabel label = new JLabel("Enter Text");
JTextField tf = new JTextField(10); // accepts upto 10 characters
JButton send = new JButton("Send");
JButton reset = new JButton("Reset");
panel.add(label); // Components Added using Flow Layout
panel.add(tf);
panel.add(send);
panel.add(reset);

// Text Area at the Center


JTextArea ta = new JTextArea();

//Adding Components to the frame.


frame.getContentPane().add(BorderLayout.SOUTH, panel);
frame.getContentPane().add(BorderLayout.NORTH, mb);
frame.getContentPane().add(BorderLayout.CENTER, ta);
frame.setVisible(true);
}
}
Exp no : 19
Exp name: Database connectivity using JDBC

/* java program for database connectivity using JDBC.*/

import java.sql.*;
// Importing required classes
import java.util.*;

// Main class
class Main {

// Main driver method


public static void main(String a[])
{

// Creating the connection using Oracle DB


// Note: url syntax is standard, so do grasp
String url = "jdbc:oracle:thin:@localhost:1521:xe";

// Username and password to access DB


// Custom initialization
String user = "system";
String pass = "12345";

// Entering the data


Scanner k = new Scanner(System.in);

System.out.println("enter name");
String name = k.next();

System.out.println("enter roll no");


int roll = k.nextInt();

System.out.println("enter class");
String cls = k.next();

// Inserting data using SQL query


String sql = "insert into student1 values('" + name
+ "'," + roll + ",'" + cls + "')";
// Connection class object
Connection con = null;

// Try block to check for exceptions


try {

// Registering drivers
DriverManager.registerDriver(
new oracle.jdbc.OracleDriver());

// Reference to connection interface


con = DriverManager.getConnection(url, user,
pass);

// Creating a statement
Statement st = con.createStatement();

// Executing query
int m = st.executeUpdate(sql);
if (m == 1)
System.out.println(
"inserted successfully : " + sql);
else
System.out.println("insertion failed");

// Closing the connections


con.close();
}

// Catch block to handle exceptions


catch (Exception ex) {
// Display message when exceptions occurs
System.err.println(ex);
}
}
}

You might also like