Vtu Java Lab Manual Bcs306a (1)
Vtu Java Lab Manual Bcs306a (1)
1. Develop a JAVA program to add TWO matrices of suitable order N (The value of N
should be read from command line arguments).
2. Develop a stack class to hold a maximum of 10 integers with suitable methods. Develop a
JAVA main method to illustrate Stack operations.
3. A class called Employee, which models an employee with an ID, name and salary, is
designed as shown in the following class diagram. The method raise Salary (percent)
increases the salary by the given percentage. Develop the Employee class and suitable
main method for demonstration.
4. A class called MyPoint, which models a 2D point with x and y coordinates, is designed as
follows:
● Two instance variables x (int) and y (int).
● A default (or "no-arg") constructor that construct a point at the default location of
(0, 0).
● A overloaded constructor that constructs a point with the given x and y coordinates.
● A method setXY() to set both x and y.
● A method getXY() which returns the x and y in a 2-element int array.
● A toString() method that returns a string description of the instance in the format "(x,
y)".
● A method called distance(int x, int y) that returns the distance from this point to another
point at the given (x, y) coordinates
● An overloaded distance(MyPoint another) that returns the distance from this point to
the given MyPoint instance (called another)
● Another overloaded distance() method that returns the distance from this point to the
origin (0,0)
Develop the code for the class MyPoint. Also develop a JAVA program (called
TestMyPoint) to test all the methods defined in the class.
5. Develop a JAVA program to create a class named shape. Create three sub classes namely:
circle, triangle and square, each class has two member functions named draw () and erase
(). Demonstrate polymorphism concepts by developing suitable methods, defining
member data and main program.
6. Develop a JAVA program to create an abstract class Shape with
abstract methods calculateArea() and calculate Perimeter(). Create subclasses Circle and
Triangle that extend the Shape class and implement the respective methods to calculate
the area and perimeter of each shape.
7. Develop a JAVA program to create an interface Resizable with methods resizeWidth(int
width) and resizeHeight(int height) that allow an object to be resized. Create a class
Rectangle that implements the Resizable interface and implements the resize methods
8. Develop a JAVA program to create an outer class with a function display.
Create another class inside the outer class named inner with a function called display and
call the two functions in the main class.
9. Develop a JAVA program to raise a custom exception (user defined exception) for
}
}
//Print the resultant matrix
System.out.println("Matrix after addition:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println("");
}
}
}
OUTPUT:
void push(int i)
{
if(top==size-1)
System.out.println("Stack Overflow");
else
{
s[++top] = i;
}
}
void pop( )
{
if (top == -1)
{
System.out.println("Stack Underflow");
}
else
{
System.out.println(" Popped Element= " + s[top]);
top--;
}
}
void display( )
{
if(top == -1)
{
System.out.println("Stack is Empty\n");
}
else
{
System.out.println("Stack Elements are:\n");
for (int i = top; i >= 0; i--)
System.out.println(s[i]);
}
}
case 2 : stk.pop();
break;
case 3 : stk.display();
break;
case 4 : System.exit(0);
default :
System.out.println("Invalid Choice\n");
break;
}
}
}
}
OUTPUT:
‐‐‐‐‐‐‐Stack Operations‐‐‐‐‐‐‐ 4. Exit
1. Push Enter your choice: 1
2. Pop
3. Display Enter the element to push 60
4. Exit
Enter your choice: 3 ‐‐‐‐‐‐‐Stack Operations‐‐‐‐‐‐‐
1. Push
Stack is Empty 2. Pop
3. Display
‐‐‐‐‐‐‐Stack Operations‐‐‐‐‐‐‐ 4. Exit
1. Push Enter your choice: 1
2. Pop
3. Display Enter the element to push 30
4. Exit
Enter your choice: 2 ‐‐‐‐‐‐‐Stack Operations‐‐‐‐‐‐‐
1. Push
Stack Underflow 2. Pop
3. Display
‐‐‐‐‐‐‐Stack Operations‐‐‐‐‐‐‐ 4. Exit
1. Push Enter your choice: 1
2. Pop
3. Display Enter the element to push 50
4. Exit Stack Overflow
Enter your choice: 1
‐‐‐‐‐‐‐Stack Operations‐‐‐‐‐‐‐
Enter the element to push 10 1. Push
‐‐‐‐‐‐‐Stack Operations‐‐‐‐‐‐‐ 2. Pop
1. Push 3. Display
2. Pop 4. Exit
3. Display
3. A class called Employee, which models an employee with an ID, name and salary, is
designed as shown in the following class diagram. The method raiseSalary (percent)
increases the salary by the given percentage. Develop the Employee class and
suitable main method for demonstration.
Employee.java
import java.util.Scanner;
public class Employee
{
int id;
String name;
double salary;
e1.display();
Scanner scan= new Scanner(System.in);
OUTPUT:
Id: 8
Name: Rakesh
Salary: 2500.0
Enter the percentage to raise the salary
5
After raising salary
Id: 8
Name: Rakesh
Salary: 2625.0
4. A class called MyPoint, which models a 2D point with x and y coordinates, is designed as
follows:
● Two instance variables x (int) and y (int).
● A default (or "no-arg") constructor that construct a point at the default location of (0, 0).
● A overloaded constructor that constructs a point with the given x and y coordinates.
● A method setXY() to set both x and y.
● A method getXY() which returns the x and y in a 2-element int array.
● A toString() method that returns a string description of the instance in the format "(x, y)".
● A method called distance(int x, int y) that returns the distance from this point to another
point at the given (x, y) coordinates
● An overloaded distance(MyPoint another) that returns the distance from this point to
the given MyPoint instance (called another)
● Another overloaded distance() method that returns the distance from this point to the origin
(0,0) Develop the code for the class MyPoint. Also develop a JAVA program (called
TestMyPoint) to test all the methods defined in the class.
Mypoint.java
public MyPoint()
{
this.x = 0;
this.y = 0;
}
OUTPUT:
5. Develop a JAVA program to create a class named shape. Create three sub classes
namely: circle, triangle and square, each class has two member functions named
draw () and erase (). Demonstrate polymorphism concepts by developing suitable
methods, defining member data and main program.
Shape.java
public class Shape
{
// Member functions
public void draw()
{
System.out.println("Drawing a shape");
}
OUTPUT:
6. Develop a JAVA program to create an abstract class Shape with abstract methods
calculateArea() and calculatePerimeter(). Create subclasses Circle and Triangle that
extend the Shape class and implement the respective methods to calculate the area and
perimeter of each shape.
Shape.java
// Constructor
public Triangle(double side1, double side2, double side3)
{
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
class Main {
public static void main(String[] args) {
// Creating objects of Circle and Triangle
Circle circle = new Circle(5.0);
Triangle triangle = new Triangle(3.0, 4.0, 5.0);
OUTPUT:
Circle - Area: 78.53981633974483
Circle - Perimeter: 31.41592653589793
Triangle - Area: 6.0
Triangle - Perimeter: 12.0
Rectangle.java
interface Resizable
{
void resizeWidth(int width);
void resizeHeight(int height);
}
rectangle.resizeWidth(8);
rectangle.resizeHeight(15);
rectangle.display();
}
}
Output:
Rectangle width: 5, height: 10
Rectangle width: 8, height: 15
8. Develop a JAVA program to create an outer class with a function display. Create another
class inside the outer class named inner with a function called display and call the two
functions in the main class.
Outerclass.java
class Outerclass
{
void display()
{
System.out.println("Outer class display method.");
}
class Innerclass
{
void display()
{
System.out.println("Inner class display method.");
}
}
Output:
Outer class display() method.
Inner class display() method.
9. Develop a JAVA program to raise a custom exception (user defined exception) for
DivisionByZero using try, catch, throw and finally.
CustomDivision.java
public class CustomDivision
{
try
{
if (denominator == 0)
{
throw new DivisionByZeroException("Division by zero is not allowed!");
}
int result = numerator / denominator;
System.out.println("Result: " + result);
}
catch (DivisionByZeroException e)
{
System.err.println("Error: " + e.getMessage());
}
finally
{
System.out.println("This block always executes,
regardless of exceptions.");
}
}
}
OUTPUT:
This block always executes, regardless of exceptions.
Error: Division by zero is not allowed!
10. Develop a JAVA program to create a package named mypack and import & implement
it in a suitable class.
Package: mypack
Class name: MyClass
package mypack;
Example.java
import mypack.MyClass;
public class Example
{
public static void main(String[] args)
{
MyClass obj = new MyClass();
obj.display(); // Access the method from the imported package
}
}
11. Write a program to illustrate creation of threads using runnable class. (start method start
each of the newly created thread. Inside the run method there is sleep() for suspend the
thread for 500 milliseconds).
Mythread.java
class ThreadExample
{
public static void main(String[] args)
{
Mythread myThread = new Mythread();
//thread 1
Thread t1 =new Thread(myThread);
//thread2
Thread t2 =new Thread(myThread);
//thread 3
Thread t3 =new Thread(myThread);
Thread-1 i is 2
Thread-2 i is 2
Thread-0 i is 3
Thread-2 i is 3
Thread-1 i is 3
Thread-0 i is 4
Thread-1 i is 4
Thread-2 i is 4
Thread-0 i is 5
Thread-2 i is 5
Thread-1 i is 5
12. Develop a program to create a class MyThread in this class a constructor, call the base
class constructor, using super and start the thread. The run method of the class starts after
this. It can be observed that both main thread and created child thread are executed
concurrently.
TestMyThread.java
public class TestMyThread
{
public static void main(String args[])
{
new MyThread();
try
{
for( int k = 5; k > 0; k--)
{
System.out.println ("Running main thread :" + k);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
}
System.out.println ("Exiting main thread . . .");
}
}
Output:
Child thread:Thread[Using Thread class,5,main]
Running main thread :5
Child thread5
Child thread4
Child thread3
Running main thread :4 Child
thread2
Running main thread :3 Child
thread1
exiting child thread …
Running main thread :2
Running main thread :1
Exiting main thread . . .