0% found this document useful (0 votes)
4 views22 pages

Java Lab Manual

The document is a lab manual for a Programming in Java course at Shri Shankaracharya Group of Institutions, detailing a list of experiments that students must complete. Each experiment includes a specific programming task, such as checking for Armstrong numbers, sorting strings, and creating classes with constructors and exceptions. Sample code and outputs are provided for several experiments to illustrate the expected results.

Uploaded by

tikeshsahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views22 pages

Java Lab Manual

The document is a lab manual for a Programming in Java course at Shri Shankaracharya Group of Institutions, detailing a list of experiments that students must complete. Each experiment includes a specific programming task, such as checking for Armstrong numbers, sorting strings, and creating classes with constructors and exceptions. Sample code and outputs are provided for several experiments to illustrate the expected results.

Uploaded by

tikeshsahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Shri Shankaracharya Group of Institutions, SSTC Bhilai

Programming in
JAVA
Lab – Manual

Department of Computer Science and Engineering (FET) Page 1


Shri Shankaracharya Group of Institutions, SSTC Bhilai

LIST OF EXPERIMENTS
1. Write a program to check whether a number is an Armstrong number or not.

2. Write a program to sort a stream of Strings.

3. Write a program to perform multiplication of two matrices.

4. Write a program to find the volume of a box having its side w, h, d means width,
height and depth. Its volume is v=w*h*d and also find the surface area given by the
formula s=2(wh+hd+dw), use appropriate constructors for the above.

5. Develop a program to illustrate a copy constructor so that a string may be duplicated


into another variable either by assignment or copying.

6. Create a base class called shape. It contains two methods getxyvalue() and
showxyvalue() for accepting co-ordinates and to display the same. Create the subclass
called Rectangle which contains a method to display the length and breadth of the
rectangle called showxyvalue().Use overriding concept.

7. Write a program that creates an abstract class called dimension, creates two subclasses,
rectangle and triangle. Include appropriate methods for both the subclass that calculate
and display the area of the rectangle and triangle.

8. Write a program which throws Arithmetic Exception. Note the output; write another
class (in a different file) that handles the Exception.

9. Create a user defined Exception class which throws Exception when the user inputs the
marks greater than 100.

10. Write a program in which a Mythread class is created by extending the Thread class. In
another class, create objects of the Mythread class and run them. In the run method
print “CSVTU” 10 times. Identify each thread by setting the name.

11. Write a program using InetAddress class and also show the utility of URL and URL
Connection classes.

12. Write a program which illustrates capturing of Mouse Events. Use Applet class for
this.

13. Write a program using RMI in which a simple remote method is implemented.

14. Write a servlet program using HttpServlet class. Also give the appropriate HTML file
which posts data to the servlet.

15. Write a JDBC program for Student Mark List Processing.

16. Design a text editor which is having some of the features of notepad.

Department of Computer Science and Engineering (FET) Page 2


Shri Shankaracharya Group of Institutions, SSTC Bhilai

EXPERIMENT NO. 1

Write a program to check whether a number is an Armstrong


number or not.

Pragram:

public class Armstrong {


public static void main(String args[]) {
int Number = Integer.parseInt(args[0]);
int Temp, Arm = 0, Mod, Loop = 0;
Temp = Number;
while (Number > 0) {
Mod = Number % 10;
Number = Number / 10;
Loop++;
}
Number = Temp;
while (Number > 0) {
Mod = Number % 10;
Number = Number / 10;
Arm = (int)(Arm + Math.pow(Mod, Loop));
}
if (Arm == Temp) {
System.out.println("Number " + Temp + " is
'Armstrong Number!' ");
} else {
System.out.println("Number " + Temp + " is
'Not An Armstrong Number!' ");
}
}
}

OUTPUT :-
H:\>cd java

H:\java>javac Armstrong.java H:\java>java Armstrong 126


Number 126 is 'Not An Armstrong Number!'

H:\java>java Armstrong 153


Number 153 is 'Armstrong Number!'

H:\java>java Armstrong 351


Number 351 is 'Not An Armstrong Number!'

H:\java>java Armstrong 1634


Number 1634 is 'Armstrong Number!'
Department of Computer Science and Engineering (FET) Page 3
Shri Shankaracharya Group of Institutions, SSTC Bhilai

EXPERIMENT NO. 2

Write a program to sort a stream of Strings.

Pragram:
public class SortArray
{
public static void main(String args[])
{
System.out.println("Init String array.");
String Array[] = { "java", "is", "platform",
"independent","language"};
System.out.println("Sorting...");
for (int i = 0; i < Array.length; i++)
{
for (int j = 0; j < i; j++)
{
if (Array[i].compareTo(Array[j]) < 0)
{
String temp = Array[i];
Array[i] = Array[j];
Array[j] = temp;
}
}
}
System.out.println("After Sorting.");
for (int i = 0; i < Array.length; i++)
{
System.out.println(Array[i]);
}
}
}
OUTPUT:-
H:\java>javac SortArray.java
H:\java>java SortArray
Init String array.
Sorting...
After Sorting.
independent
is
java language
platform

Department of Computer Science and Engineering (FET) Page 4


Shri Shankaracharya Group of Institutions, SSTC Bhilai

H:\java>

EXPERIMENT NO. 3

Write a program to perform multiplication of two matrices.


import java.util.*;
public class MatrixMult
{
public static void main(String args[])
{
Scanner Input = new Scanner(System.in);
System.out.println("Enter Size of Matrix ? ");
int Size = Input.nextInt();
int MatA[][] = new int[Size][Size];
int MatB[][] = new int[Size][Size];
int MatC[][] = new int[Size][Size];
System.out.println("Enter Matrix - A");
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
MatA[i][j] = Input.nextInt();
}
}
System.out.println("Enter Matrix - B");
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
MatB[i][j] = Input.nextInt();
}
}
System.out.println("Multiplying");
int Sum = 0;
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
for (int k = 0; k < Size; k++)
{
Sum = Sum + MatA[i][k] * MatB[k][j];
}
MatC[i][j] = Sum;
Sum = 0;

Department of Computer Science and Engineering (FET) Page 5


Shri Shankaracharya Group of Institutions, SSTC Bhilai

}
}
System.out.println("Output");
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
System.out.print(MatC[i][j] + "\t");
}
System.out.print("\n");
}
Input.close();
}
}

OUTPUT:-
H:\java>javac MatrixMult.java
H:\java>java MatrixMult Enter Size of Matrix ? 3
Enter Matrix - A
-9
7
-1
6
-5
2
6
-4
1
Enter Matrix - B 1
-1
3
2
-1
4
2
2
1
Multiplying Output
3 0 0
0 3 0
0 0 3

Department of Computer Science and Engineering (FET) Page 6


Shri Shankaracharya Group of Institutions, SSTC Bhilai

H:\java>

EXPERIMENT NO. 4

Write a program to find the volume of a box having its side w, h, d means
width, height and depth. Its volume is v=w*h*d and also find the surface
area given by the formula s=2(wh+hd+dw), use appropriate constructors
for the above.
class Box
{ double width;
double height;
double depth;

Box(double w, double h, double d)


{
this.width = w;
this.height = h;
this.depth = d;
}
double Vol()
{
return (this.depth * this.height * this.width);
}
double SurfaceArea()
{
return (2 * ((this.depth * this.height) + (this.width * this.depth)
+ (this.width * this.height)));

}
}
public class Constructor
{
public static void main(String args[])
{
Box Box1 = new Box(10, 15, 5);
System.out.println("Volume of Box1 : " +Box1.Vol());
Box Box2 = new Box(5, 15, 5);
System.out.println("Surface Area of Box2 : "
+Box2.SurfaceArea());
}
}

Department of Computer Science and Engineering (FET) Page 7


Shri Shankaracharya Group of Institutions, SSTC Bhilai

OUTPUT:-
H:\java>javac Constructor.java
H:\java>java Constructor
Volume of Box1 : 750.0
Surface Area of Box2 : 350.0
H:\java>

Department of Computer Science and Engineering (FET) Page 8


Shri Shankaracharya Group of Institutions, SSTC Bhilai

EXPERIMENT NO. 5

Develop a program to illustrate a copy constructor so that a string may be


duplicated into another variable either by assignment or copying.

class Complex
{
private double re, im;
Complex(double re, double im)
{
this.re = re;
this.im = im;
}
Complex(Comex c)
{
System.out.println("Copy constructor called");
re = c.re;
im = c.im;
}
public String toString()
{
return "(" + re + " + " + im + "i)";
}
}
public class CopyConstructor
{
public static void main(String[] args)
{
Complex c1 = new Complex(10, 15);
c2 = new Complex(c1);
Complex c3 = c2;
System.out.println(c2);
}
}

OUTPUT:-
H:\java>javac CopyConstructor.java
H:\java>java CopyConstructor
Copy constructor called
(10.0 + 15.0i)

Department of Computer Science and Engineering (FET) Page 9


Shri Shankaracharya Group of Institutions, SSTC Bhilai

H:\java>

EXPERIMENT NO. 6

Create a base class called shape. It contains two methods getxyvalue()


and showxyvalue() for accepting co-ordinates and to display the same.
Create the subclass called Rectangle which contains a method to
display the length and breadth of the rectangle called
showxyvalue().Use overriding concept.

import java.io.*;
class shape
{
int i, j;
void getxyvalue()
{
i = 5;
j = 5;
}
void showxyvalue()
{
System.out.println("Coordinates of shape are : " + i + " , " + j);
}
}
class Rect extends shape
{
int l, br;
Rect(int a, int b)
{
l = a;
br = b;
}
void showxyvalue()
{
System.out.println("Length and Breadth of Rectangle is : " + l + ","
+ br);
}
}
class Override
{

Department of Computer Science and Engineering (FET) Page 10


Shri Shankaracharya Group of Institutions, SSTC Bhilai

public static void main(String ar[])


{
Rect r = new Rect(10, 10);
r.showxyvalue();
}
}

OUTPUT:-
H:\java>javac Override.java
H:\java>java Override
Length and Breadth of Rectangle is : 10,10
H:\java>

Department of Computer Science and Engineering (FET) Page 11


Shri Shankaracharya Group of Institutions, SSTC Bhilai

EXPERIMENT NO. 7

Create a base class called shape. It contains two methods getxyvalue()


and showxyvalue() for accepting co-ordinates and to display the same.
Create the subclass called Rectangle which contains a method to display
the length and breadth of the rectangle called showxyvalue().Use
overriding concept.

abstract class dimension


{
double dim1;
double dim2;
dimension(double a, double b)
{
dim1 = a;
dim2 = b;
}
abstract double area();
}
class Rectangle extends dimension
{
Rectangle(double a, double b)
{
super(a, b);
}
double area()
{
System.out.println("Inside Rectangle");
return dim1 * dim2;
}
}
class Triangle extends dimension
{
Triangle(double a, double b)
{
super(a, b);
}
double area()

Department of Computer Science and Engineering (FET) Page 12


Shri Shankaracharya Group of Institutions, SSTC Bhilai

{
System.out.println("Inside Triangle");
return dim1 * dim2 / 2;
}
}
public class Abstract
{
public static void main(String ar[])
{
Rectangle r = new Rectangle(10, 10);
Triangle t = new Triangle(10, 5);
dimension d;
d = r;
System.out.println("Area of Rectangle is : " + d.area());
System.out.println("
"); d = t;
System.out.println("Area of Triangle is : " + d.area());
}
}

OUTPUT:-
H:\java>javac Abstract.java
H:\java>java Abstract Inside Rectangle
Area of Rectangle is : 100.0
Inside Triangle
Area of Triangle is : 25.0
H:\java>

Department of Computer Science and Engineering (FET) Page 13


Shri Shankaracharya Group of Institutions, SSTC Bhilai

EXPERIMENT NO. 8

Create a user defined Exception class which throws Exception when the
user inputs the marks greater than 100.

import java.io.*;
class MyException extends Exception
{
String msg;
MyException(int n)
{
msg = "Entered marks is : " + n + " " + "but the marks
should be less than 100";
}
public String toString()
{
return msg;
}
}
public class CustomExp
{
public static void main(String args[])
{
int num;
DataInputStream in = new DataInputStream(System.in);
try {
System.out.println("Enter the Marks: ");
num = Integer.parseInt( in .readLine());
if (num > 0 && num < 100)
System.out.println("Marks are : " + num);
else
throw new MyException(num);
}
catch (Exception e)
{
System.out.println(e);

Department of Computer Science and Engineering (FET) Page 14


Shri Shankaracharya Group of Institutions, SSTC Bhilai

}
}
}

OTUPUT:-
H:\java>javac CustomExp.java
Note: CustomExp.java uses or overrides a deprecated API. Note:
Recompile with -Xlint:deprecation for details.
H:\java>java CustomExp Enter the Marks:
125
Entered marks is : 125 but the marks should be less than 100
H:\java>java CustomExp
Enter the Marks:
85
Marks are : 85
H:\java>
H:\java>
Marks are : 85
H:\java>

Department of Computer Science and Engineering (FET) Page 15


Shri Shankaracharya Group of Institutions, SSTC Bhilai

EXPERIMENT NO. 9

Write a program in which a Mythread class is created by extending the


Thread class. In another class, create objects of the Mythread class and
run them. In the run method print “CSVTU” 10 times. Identify each
thread by setting the name.

import java.lang.*;
class MyThread extends Thread
{
String str;
Thread t; MyThread(String s)
{
str = s;
t = new Thread(this, str);
System.out.println("MyThread: " + t);
System.out.println(" ");
t.start();
}
public void run()
{
try
{
for (int i = 1; i <= 5; i++)
{
System.out.println(str + " : " + "CSVTU");
System.out.println(" "); Thread.sleep(1000);
}
}
catch (Exception e)
{
System.out.println(e);
}
System.out.println(" "); System.out.println(str + ":" + "Exiting");
}
}
public class ThreadClass
Department of Computer Science and Engineering (FET) Page 16
Shri Shankaracharya Group of Institutions, SSTC Bhilai

{
public static void main(String ar[])
{
MyThread m1 = new yThread("MyThread_1");
System.out.println(" ");
MyThread m2 = new MyThread("MyThread_2");
}
}

OUTPUT:-
H:\java>javac ThreadClass.java
H:\java>java ThreadClass
MyThread: Thread[MyThread_1,5,main]
MyThread_1 : CSVTU
MyThread: Thread[MyThread_2,5,main]
MyThread_2 : CSVTU
MyThread_1 : CSVTU
MyThread_2 : CSVTU
MyThread_1 : CSVTU
MyThread_2 : CSVTU
MyThread_1 : CSVTU
MyThread_2 : CSVTU
MyThread_1 : CSVTU
MyThread_2 : CSVTU
MyThread_1:Exiting
MyThread_2:Exiting
H:\java>

Department of Computer Science and Engineering (FET) Page 17


Shri Shankaracharya Group of Institutions, SSTC Bhilai

EXPERIMENT NO. 10

Write a program which illustrates capturing of Mouse Events. Use


Applet class for this.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class MouseEvents extends Applet implements
MouseListener
{
int x = 0;
int y = 0;
String msg;
public void init()
{
msg = new String("");
addMouseListener(this);
}
public void paint(Graphics g)
{
g.drawString(msg, x, y);
}
public void mousePressed(MouseEvent e)
{
x = 100;
y = 100;
msg = "Mouse Pressed";
repaint();
}
public void mouseClicked(MouseEvent e)
{
x = e.getX();
y = e.getY();
msg = "Mouse Clicked";
repaint();
}
public void mouseEntered(MouseEvent e)
Department of Computer Science and Engineering (FET) Page 18
Shri Shankaracharya Group of Institutions, SSTC Bhilai

{
showStatus("Mouse Entered");
}
public void mouseExited(MouseEvent e)
{
showStatus("Mouse Exited");
}
public void mouseReleased(MouseEvent e)
{
showStatus("Mouse Released");
}
}

HTML CODE:-
<html>
<head>
<title>Mouse Events</title>
</head>
<body>
<hr>
<applet code="MouseEvents.class" width="320" height="120">
If your browser was Java-enabled, a "Hello, World" message would
appear here.
</applet>
</body>
</html>

OUTPUT:-
H:\java>javac MouseEvents.java
H:\java>appletviewer MouseEvents.html
H:\java>

Department of Computer Science and Engineering (FET) Page 19


Shri Shankaracharya Group of Institutions, SSTC Bhilai

EXPERIMENT NO. 11
Write a JDBC program for Student Mark List Processing
package jdbc_sample;
import java.sql.*;
public class Main
{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/emp";
static final String USER = "root"; static final String PWD = "root";
public static void main(String[] args)
{
Connection conn = null; Statement stml = null; try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to Database...");
conn = DriverManager.getConnection(DB_URL,USER, PWD);
System.out.println("Creating statement...");
stml = conn.createStatement(); String sql;
sql = "SELECT f_name FROM emp_name"; ResultSet rs =
stml.executeQuery(sql);
while (rs.next())
{
String name = rs.getString("f_name"); System.out.println("Name
is : " + name);

}
rs.close();
stml.close();
conn.close();
}
catch (SQLException se)
{
se.printStackTrace();
}
catch (Exception e)
{

Department of Computer Science and Engineering (FET) Page 20


Shri Shankaracharya Group of Institutions, SSTC Bhilai

e.printStackTrace();
}
}
}
OUTPUT:-

Department of Computer Science and Engineering (FET) Page 21


Shri Shankaracharya Group of Institutions, SSTC Bhilai

Department of Computer Science and Engineering (FET) Page 22

You might also like