JAVA LAB MANUAL Stu
JAVA LAB MANUAL Stu
PROGRAM:
import java.util.*;
public class Example
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int a;
System.out.println("enter the a value");
a=s.nextInt();
int b;
System.out.println("enter the b value");
b=s.nextInt();
int c;
System.out.println("enter the c value");
c=s.nextInt();
double temp1 = Math.sqrt(b * b - 4 * a * c);
double root1 = (-b + temp1) / (2*a) ;
double root2 = (-b - temp1) / (2*a) ;
System.out.println("The roots of the Quadratic Equation \"2x2 + 6x + 4 = 0\" are "+root1+"
and "+root2);
}
}
OUTPUT:
1
RESULT:
2
2. FIND THE SUM OF DIGITS
PROGRAM:
import java.util.Scanner;
public class Digit
{
public static void main(String args[])
{
int m, n, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
m = s.nextInt();
while(m > 0)
{
n = m % 10;
sum = sum + n;
m = m / 10;
}
System.out.println("Sum of Digits:"+sum);
}
}
OUTPUT:
3
RESULT:
4
3. FIND THE SUM OF ‘N’ NUMBERS
PROGRAM:
import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
int i, n, sum=0, num;
Scanner scan = new Scanner(System.in);
OUTPUT:
5
RESULT:
6
4. REVERSE THE GIVEN NUMBER
PROGRAM:
import java.util.Scanner;
class ReverseNumber
{
public static void main(String args[])
{
int n, reverse = 0;
System.out.println("Enter the number to reverse");
Scanner in = new Scanner(System.in);
n = in.nextInt();
while( n != 0 )
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
System.out.println("Reverse of entered number is "+reverse);
}
}
OUTPUT:
7
RESULT:
AIM:
ALGORITHM:
8
PROGRAM:
import java.util.Scanner;
public class Examples
{
public static void main (String[] args)
{
int i,x,a[] = new int[10];
int n=0,p=0,e=0,o=0;
Scanner input = new Scanner(System.in);
for(i=0;i<5;i++) {
System.out.println("Enter Number :");
a[i] = input.nextInt();
}
for(i=0;i<5;i++) {
if(a[i]<0)
n++;
else
p++;
x=a[i]%2;
if(x==0)
e++;
else
o++;
}
9
System.out.println("Total Even Numbers = "+e);
System.out.println("Total Odd Numbers = "+o);
System.out.println("Total Negative Numbers = "+n);
System.out.println("Total Positive Numbers = "+p);
}
}
OUTPUT:
RESULT:
ALGORITHM:
10
PROGRAM:
import java.util.*;
class Vehicle
{
String color;
int speed;
int size;
void attributes()
{
System.out.println("Color : " + color);
System.out.println("Speed : " + speed);
System.out.println("Size : " + size);
}
}
class Car extends Vehicle
{
int gears;
void attributescar()
{
System.out.println("Color of Car : " + color);
System.out.println("Speed of Car : " + speed);
System.out.println("Size of Car : " + size);
System.out.println("No of gears of Car : " + gears);
}
}
public class Test {
11
public static void main(String args[]) {
Car b1 = new Car();
b1.color = "Blue";
b1.speed = 200 ;
b1.size = 22;
b1.gears = 5;
b1.attributescar();
}
}
OUTPUT:
RESULT:
ALGORITHM:
12
PROGRAM:
import java.util.*;
class overload
void area(float x)
void area1(double r)
double z=3.14*r*r;
class polymorphism
s.area(5);
13
s.area(5,6);
s.area1(5);
OUTPUT
RESULT:
AIM
ALGORITHM
14
PROGRAM
import java.io.*;
class A
{
void callme() {
System.out.println("Inside A's callme method");
}
}
class B extends A
{
// override callme()
void callme() {
System.out.println("Inside B's callme method");
}
}
class C extends A
{
// override callme()
void callme() {
System.out.println("Inside C's callme method");
}
}
class Dispatch{
public static void main(String args[]) {
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
15
r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object
r.callme(); // calls B's version of callme
r = c; // r refers to a C object
r.callme(); // calls C's version of callme
}
}
OUTPUT
D:\p1>javac Dispatch.java
D:\p1>java Dispatch
D:\p1>
RESULT
ALGORITHM:
16
PROGRAM:
import java.util.Scanner;
class LinearSearch
{
public static void main(String args[])
{
int c, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find");
search = in.nextInt();
17
}
if (c == n) /* Element to search isn't present */
System.out.println(search + " isn't present in array.");
}
}
OUTPUT:
RESULT:
ALGORITHM:
18
PROGRAM:
import java.util.Scanner;
size = scan.nextInt();
arr[i] = scan.nextInt();
19
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
OUTPUT:
20
RESULT:
ALGORITHM:
21
PROGRAM:
import java.util.*;
class CountWord{
char[] ch = str.toCharArray();
String s = "";
s = s + ch[i];
i++;
if (s.length() > 0)
22
}
count(str);
OUTPUT:
RESULT:
ALGORITHM:
23
PROGRAM:
import java.util.*;
OUTPUT:
24
RESULT:
ALGORITHM:
25
PROGRAM:
import java.util.*;
System.out.println(str.length());
System.out.println(str.replace('m','M'));
System.out.println(str.toUpperCase());
String str1="Java";
String str2=str1.concat("Programming");
System.out.println(str2);
26
OUTPUT:
RESULT:
ALGORITHM:
27
PROGRAM:
import java.util.*;
class arraylist
ArrayList<String>obj=new ArrayList<String>();
obj.add("Name: User");
obj.add("Branch: CSE");
obj.add("Reg.no: 19171****");
System.out.println("Student Information\n"+obj);
OUTPUT:
28
RESULT:
ALGORITHM:
29
PROGRAM
import java.util.*;
class Hash
cust.put(101,"A");
cust.put(102,"b");
cust.put(103,"d");
System.out.println("Record are"+cust);
cust.remove(102);
System.out.println("after removing"+cust);
System.out.println(cust.size());
30
OUTPUT:
RESULT:
ALGORITHM
31
PROGRAM
import java.util.*;
public class IteratorDemo
{
public static void main(String args[])
{
// Create an array list
ArrayList al = new ArrayList();
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
// Use iterator to display contents of al
System.out.print("Original contents of al: ");
Iterator itr = al.iterator();
while(itr.hasNext()) {
Object element = itr.next();
System.out.print(element + " ");
}
32
System.out.println();
// Modify objects being iterated
ListIterator litr = al.listIterator();
while(litr.hasNext()) {
Object element = litr.next();
litr.set(element + "+");
}
System.out.print("Modified contents of al: ");
itr = al.iterator();
while(itr.hasNext()) {
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();
// Now, display the list backwards
System.out.print("Modified list backwards: ");
while(litr.hasPrevious()) {
Object element = litr.previous();
System.out.print(element + " ");
}
System.out.println();
}
}
OUTPUT
D:\javac IteratorDemo.java
D:\java IteratorDemo
33
Modified list backwards: F+ D+ B+ E+ A+ C+
D:\
RESULT
ALGORITHM:
34
PROGRAM
import java.util.*;
try
}catch(Exception e)
35
OUTPUT:
RESULT:
Hence the java program for dividing number by zero is executed successfully
AIM:
36
ALGORITHM:
PROGRAM:
import java.util.*;
class Exceptions
{
public static void main(String args[])
{
int a[]={5,10};
int b=5,y;
try
{
int x=a[2 ] / b-a[1];
}
catch(ArithmeticException e)
{
System.out.println("Division by Zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index error");
}
catch(ArrayStoreException e){
37
System.out.println("Wrong data Type");
}
y=a[1]/a[0];
System.out.println("y= " +y);
}
}
OUTPUT:
RESULT:
AIM:
38
ALGORITHM:
PROGRAM:
import java.util.*;
for(int i=1;i<=5;i++)
try
Thread.sleep(500);
catch(Exception e)
System.out.println(e);
39
System.out.println(i);
t1.start();
try
t1.join();
catch(Exception e){
System.out.println(e);
t2.start();
t3.start();
OUTPUT:
40
RESULT:
20. SET AND GET THE NAME FOR THE MULTIPLE THREADS
AIM:
41
ALGORITHM:
PROGRAM:
import java.io.*;
42
System.out.println("Thread 1: " + t1.getName());
System.out.println("Thread 2: " + t2.getName());
t1.start();
t2.start();
}
}
OUTPUT:
RESULT:
THREADS
43
AIM:
ALGORITHM:
PROGRAM:
import java.lang.*;
class ThreadDemo extends Thread
{
public void run()
{
System.out.println("Inside run method");
}
public static void main(String[]args)
{
ThreadDemo t1 = new ThreadDemo();
ThreadDemo t2 = new ThreadDemo();
ThreadDemo t3 = new ThreadDemo();
System.out.println("t1 thread priority : " +
t1.getPriority());
System.out.println("t2 thread priority : " +
t2.getPriority());
System.out.println("t3 thread priority : " +
t3.getPriority());
44
t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);
System.out.println("t1 thread priority : " +
t1.getPriority());
System.out.println("t2 thread priority : " +
t2.getPriority());
System.out.println("t3 thread priority : " +
t3.getPriority());
System.out.print(Thread.currentThread().getName());
System.out.println("Main thread priority : "
+ Thread.currentThread().getPriority());
Thread.currentThread().setPriority(10);
System.out.println("Main thread priority : "
+ Thread.currentThread().getPriority());
}
}
OUTPUT:
RESULT:
45
AIM
ALGORITHM
PROGRAM
class Customer{
int amount=10000;
if(this.amount<amount)
{
System.out.println("Less balance; waiting for deposit...");
try
{
wait();
}
catch(Exception e)
{}
}
this.amount-=amount;
System.out.println("withdraw completed...");
System.out.println(amount);
46
}
class TestIC{
public static void main(String args[]){
final Customer c=new Customer();
new Thread()
{
public void run()
{
c.withdraw(15000);
}
}.start();
new Thread()
{
public void run()
{
c.deposit(10000);
}
}.start();
}
}
OUTPUT:
D:\javac TestIC.java
D:\java TestIC
going to withdraw...
Less balance; waiting for deposit...
going to deposit...
deposit completed...
withdraw completed...
15000
D:\
RESULT
47
AIM
ALGORITHM
PROGRAM
import java.io.*;
if(y.compareTo(max) > 0) {
if(z.compareTo(max) > 0) {
48
}
3, 4, 5, maximum( 3, 4, 5 ));
OUPUT
D:\javac MaximumTest.java
D:\java MaximumTest
Max of 3, 4 and 5 is 5
D:\
RESULT
49
ALGORITHM:
Program-1
Program-2
PROGRAM 1:
import java.io.FileOutputStream;
try
50
fout.write(65);
fout.close();
System.out.println("success...");
catch(Exception e)
System.out.println(e);
PROGRAM 2:
import java.io.FileInputStream;
try {
int i=fin.read();
System.out.print((char)i);
fin.close();
}catch(Exception e){System.out.println(e);}
OUTPUT 1:
51
OUTPUT 2:
RESULT:
52
ALGORITHM:
Program-1
Program-2
PROGRAM1:
import java.io.FileOutputStream;
try
53
byte b[]=s.getBytes();//converting string into byte array
fout.write(b);
fout.close();
System.out.println("success...");
catch(Exception e)
System.out.println(e);
PROGRAM 2:
import java.io.FileInputStream;
public class DataStreamExample
{
public static void main(String args[])
{
try
{
FileInputStream fin=new FileInputStream("D:\\testout.txt");
int i=0;
while((i=fin.read())!=-1)
{
System.out.print((char)i);
}
fin.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT 1:
54
OUTPUT 2:
RESULT:
55
ALGORITHM:
PROGRAM:
import java.util.*;
out.write("Welcome to javaTpoint");
out.writeTo(f1);
out.writeTo(f2);
out.writeTo(f3);
out.writeTo(f4);
56
f1.close();
f2.close();
f3.close();
f4.close();
System.out.println("Success...");
OUTPUT:
RESULT:
57
ALGORITHM:
Program-1:
Program-2
PROGRAM 1:
import java.io.Console;
class User{
Console c=System.console();
String n=c.readLine();
System.out.println("Welcome "+n);
58
}
PROGRAM 2:
import java.io.Console;
class ReadPasswordTest{
Console c=System.console();
char[] ch=c.readPassword();
OUTPUT 1:
59
OUTPUT 2:
RESULT:
60
28. COUNT THE NUMBER OF CHARACTERS, WORDS AND
LINES FROM THE FILE USING STREAM I/O
AIM
ALGORITHM
PROGRAM
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
61
int charCount = 0;
int wordCount = 0;
int lineCount = 0;
try
{
//Creating BufferedReader object
reader = new BufferedReader (new FileReader("D:\\p1\\sample.txt"));
lineCount++;
62
//Updating the charCount
currentLine = reader.readLine();
}
63
}
OUPUT
D:\javac WordCountInFile.java
D:\java WordCountInFile
D:\
RESULT
64
29. JDBC CONNECTIVITY - STUDENT DATABASE
AIM:
ALGORITHM:
Start
Create a jdbc class
Load the driver using class.forName()
Create connections using DriverManager.getConnection()
Create the statements using createStatment()
Execute the query
Using Resultset object display the details of student
Close the connections
Stop
PROGRAM :
import java.sql.*;
class FirstExample
try
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Emp","root","manuel28");
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
65
sql = "SELECT id, first, last, age FROM Employees";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
int id = rs.getInt("id");
rs.close();
stmt.close();
conn.close();
catch(SQLException se)
se.printStackTrace();
catch(Exception e){
e.printStackTrace();
System.out.println("Goodbye!");
66
OUTPUT:
RESULT:
67
30. CREATE DIFFERENT GEOMETRIC SHAPE USING APPLETS
AIM:
ALGORITHM:
PROGRAM :
import java.applet.Applet;
import java.awt.*;
public class Graphics extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawString(“welcome”,50,50);
g.drawLine(20,30,50,100);
g.drawRect(70,30,100,30);
g.fillRect(170,100,30,30);
g.drawOval(70,100,30,30);
g.setColor(Color.pink);
g.fillOvel(70,200,30,30);
g.drawArc(90,150,30,50,70,60);
g.fillArc(20,30,40,50,60,70);
}
}
68
OUTPUT:
RESULT:
69
31.CREATE A HOUSE SHAPE BY USING APPLETS
AIM:
ALGORITHM:
PROGRAM:
import java.applet.*;
public class house extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
int xs[]={100,160,220};
int ys[]={100,50,100};
polygon poly=new polygon(xs,ys,3);
g.fillpolygon(poly);
g.setColor(Color.white);
g.fillRect(145,160,30,60);
g.setColor(Color.yellow);
g.setLine(30,50,60,70);
g.setColor(Color.black);
g.fillRect(120,55,10,30);
}
70
OUTPUT:
RESULT:
71
32.EVENT HANDLING - MOUSE
AIM
ALGORITHM
PROGRAM
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Mouse" width=500 height=500>
</applet>
*/
public class Mouse extends Applet
implements MouseListener,MouseMotionListener
{
int X=0,Y=20;
String msg="MouseEvents";
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
setBackground(Color.black);
setForeground(Color.red);
}
public void mouseEntered(MouseEvent m)
72
{
setBackground(Color.magenta);
showStatus("Mouse Entered");
repaint();
}
public void mouseExited(MouseEvent m)
{
setBackground(Color.black);
showStatus("Mouse Exited");
repaint();
}
public void mousePressed(MouseEvent m)
{
X=10;
Y=20;
msg="NEC";
setBackground(Color.green);
repaint();
}
public void mouseReleased(MouseEvent m)
{
X=10;
Y=20;
msg="Engineering";
setBackground(Color.blue);
repaint();
}
public void mouseMoved(MouseEvent m)
{
X=m.getX();
Y=m.getY();
msg="College";
setBackground(Color.white);
showStatus("Mouse Moved");
repaint();
}
public void mouseDragged(MouseEvent m)
{
msg="CSE";
setBackground(Color.yellow);
showStatus("Mouse Moved"+m.getX()+" "+m.getY());
repaint();
}
public void mouseClicked(MouseEvent m)
73
{
msg="Students";
setBackground(Color.pink);
showStatus("Mouse Clicked");
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}
D:\javac Mouse.java
D:\Appletviewer Mouse.java
OUTPUT
RESULT
74
33.SIMPLE APPLET APPLICATION – CREATING BIO DATA
FORM
AIM
ALGORITHM
PROGRAM
//Tes.java
import java.awt.*;
setLayout(new FlowLayout(FlowLayout.LEFT));
add(new TextField(10));
75
add(new Label("Address :"));
add(new TextField(10));
add(new TextField(10));
gender.addItem("Man");
gender.addItem("Woman");
add(new Button("Register"));
add(new Button("Exit"));
//Tes.html
<html>
<head><title>Register</title></head>
76
<body>
</body>
</html>
OUTPUT
D:/Appletviewer Tes.html
RESULT:
77