Java Lab Manual
Java Lab Manual
Programming in
JAVA
Lab – Manual
LIST OF EXPERIMENTS
1. Write a program to check whether a number is an Armstrong number or not.
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.
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.
16. Design a text editor which is having some of the features of notepad.
EXPERIMENT NO. 1
Pragram:
OUTPUT :-
H:\>cd java
EXPERIMENT NO. 2
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
H:\java>
EXPERIMENT NO. 3
}
}
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
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;
}
}
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());
}
}
OUTPUT:-
H:\java>javac Constructor.java
H:\java>java Constructor
Volume of Box1 : 750.0
Surface Area of Box2 : 350.0
H:\java>
EXPERIMENT NO. 5
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)
H:\java>
EXPERIMENT NO. 6
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
{
OUTPUT:-
H:\java>javac Override.java
H:\java>java Override
Length and Breadth of Rectangle is : 10,10
H:\java>
EXPERIMENT NO. 7
{
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>
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);
}
}
}
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>
EXPERIMENT NO. 9
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>
EXPERIMENT NO. 10
{
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>
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)
{
e.printStackTrace();
}
}
}
OUTPUT:-