Java Lab(Bcs306a) 4
Java Lab(Bcs306a) 4
Aim: Develop a JAVA program to add TWO matrices of suitable order N (The value of N should
be read from command line arguments).
Program:
import java.util.Scanner;
public class AddMatrix
{
public static void main(String args[])
{
int n=Integer.parseInt(args[0
Scanner s = new Scanner(System.in);
int a[][]=new int[n][n];
int b[][]=new int[n][n];
int c[][]=new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
}
System.out.println("The Matrix after addition :");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
System.out.print(c[i][j] + " ");
System.out.println("");
}
}
}
Output:
PS D:\JAVA BCS306A\lab1> javac Example.java
PS D:\JAVA BCS306A\lab1> java Example 3
Enter the elements of First Matrix :
243
540
132
Enter the elements of Second Matrix :
420
156
217
The Matrix after addition :
6 6 3
6 9 6
3 4 9
EXPERIMENT 2
Aim: Develop a stack class to hold a maximum of 10 integers with suitable methods. Develop
a JAVA main method to illustrate Stack operations.
Program:
import java.io.*;
import java.util.*;
import java.util.Scanner;
class P2_Stack{
int stack[];
int tos,sos;
P2_Stack(int size)
{
stack = new int[size];
sos=size;
tos = -1;
}
public void Push(int x)
{
if (isFull())
{
System.out.println("Stack OverFlow");
System.exit(1);
}
stack[++tos] = x;
}
public int Pop()
{
if (isEmpty())
{
System.out.println("Stack Empty");
System.exit(1);
}
return stack[tos--];
}
public int getSize() {
return tos + 1;
}
public Boolean isEmpty() {
return tos == -1;
}
public Boolean isFull() {
return tos == sos-1;
}
Output:
Enter the size of the Stack : 3
Output:
PS D:\JAVA BCS306A\lab1> javac P4_TestMyPoint.java
PS D:\JAVA BCS306A\lab1> java P4_TestMyPoint
Enter the coordinates for point1 :2 3
Point1: (2,3)
Point2: 1,2
Distance between point1 and point2 is:1.4142135623730951
Distance between point1 and Origin(0,0) is:3.605551275463989
EXPERIMENT 5
Aim: 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.
Program:
class Shape {
void drawShape() {
System.out.println("Drawing a Shape.");
}
void eraseShape() {
System.out.println("Erasing a Shape.");
}
}
class Circle extends Shape {
void drawShape() {
System.out.println("Drawing a Circle.");
}
void eraseShape() {
System.out.println("Erasing a Circle.");
}
}
class Triangle extends Shape {
void drawShape() {
System.out.println("Drawing a Triangle.");
}
void eraseShape() {
System.out.println("Erasing a Triangle.");
}
}
class Square extends Shape {
void drawShape() {
System.out.println("Drawing a Square.");
}
void eraseShape() {
System.out.println("Erasing a Square.");
}
}
class P5_Shape {
public static void main(String[] args) {
Shape c = new Circle();
Shape t = new Triangle();
Shape s = new Square();
c.drawShape();
c.eraseShape();
t.drawShape();
t.eraseShape();
s.drawShape();
s.eraseShape();
}
}
Output:
Drawing a Circle.
Erasing a Circle.
Drawing a Triangle.
Erasing a Triangle.
Drawing a Square.
Erasing a Square.
EXPERIMENT 6
Aim: 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.
Program:
abstract class Shape {
abstract double calculateArea();
abstract double calculatePerimeter();
}
Output:
PS D:\JAVA BCS306A\lab1> javac P6_Shape.java
PS D:\JAVA BCS306A\lab1> java P6_Shape
Area of the Circle : 314.1592653589793
Perimeter of the Circle : 62.83185307179586
Area of the Triangle : 7.869841167393405
Perimeter of the Triangle : 13.6
EXPERIMENT 7
Aim: 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.
Program:
interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
}
Rectangle(int w, int h) {
this.width = w;
this.height = h;
}
void displayRectangle() {
System.out.println("Width : " + this.width + "\t" + "Height : " + this.height);
}
}
Output:
PS D:\JAVA BCS306A\lab1> javac P7_Interface.java
PS D:\JAVA BCS306A\lab1> java P7_Interface
Original dimension of the Rectanlge : Width : 8 Height : 6
New dimension of the Rectanlge (after resize) : Width : 10 Height : 8
EXPERIMENT 8
Aim: 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.
Program:
class Outer {
void funcDisplay() {
System.out.println("Outer Class -> funcDisplay() invoked");
}
class Inner {
void funcDisplay() {
System.out.println("Inner Class -> funcDisplay() invoked");
}
}
}
class P8_OuterInnerClass {
public static void main(String[] args) {
Outer out = new Outer();
out.funcDisplay();
Outer.Inner oi = out.new Inner();
oi.funcDisplay();
}
}
Output:
PS D:\JAVA BCS306A\lab1> javac P8_OuterInnerClass.java
PS D:\JAVA BCS306A\lab1> java P8_OuterInnerClass
Outer Class -> funcDisplay() invoked
Inner Class -> funcDisplay() invoked
EXPERIMENT 9
Aim: Develop a JAVA program to raise a custom exception (user defined exception) for
DivisionByZero using try, catch, throw and finally.
Program:
import java.util.Scanner;
class DivisionByZeroException extends Exception {
DivisionByZeroException(String msg) {
super(msg);
}
}
Output:
PS D:\JAVA BCS306A\lab expeiments> javac P9_Exception.java
PS D:\JAVA BCS306A\lab expeiments> java P9_Exception
Enter the Numerator and Denominator : 5 3
5 / 3 = 1.0
Executing the Finally Block...
PS D:\JAVA BCS306A\lab expeiments> java P9_Exception
Enter the Numerator and Denominator : 5 0
Error : Arithmetic Exception (Division By Zero) - Cannot Divide a Number by Zero.
Executing the Finally Block...
EXPERIMENT 10
Aim: Develop a JAVA program to create a package named mypack and import & implement
it in a suitable class.
Program:
// Inside a folder named 'mypack'
package mypack;
/Create the main program in a different file outside the mypack folder:
// Main program outside the mypack folder
import mypack.MyPackageClass;
Output:
PS D:\JAVA BCS306A\lab expeiments> javac PackageDemo.java
PS D:\JAVA BCS306A\lab expeiments> java PackageDemo
Hello from MyPackageClass in mypack package!
Result of adding numbers: 8
EXPERIMENT 11
Aim: 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).
Program:
class RunnableThread implements Runnable {
RunnableThread(String tn) {
Thread.currentThread().setName(tn);
}
class P11_Thread_Runnable {
public static void main(String args[]) {
RunnableThread rt1 = new RunnableThread("T1");
rt1.start();
RunnableThread rt2 = new RunnableThread("T2");
rt2.start();
RunnableThread rt3 = new RunnableThread("T3");
rt3.start();
}
}
Output:
PS D:\JAVA BCS306A\lab1> javac P11_Thread_Runnable.java
PS D:\JAVA BCS306A\lab1> java P11_Thread_Runnable
T1 Created and is Running
T2 Created and is Running
T3 Created and is Running
EXPERIMENT 12
Aim: 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.
Program:
class myThread extends Thread {
myThread(String tname) {
super(tname);
}
Output:
PS D:\JAVA BCS306A\lab1> javac P12_Thread.java
PS D:\JAVA BCS306A\lab1> java P12_Thread
Main Thread Started.
Main Thread Continues...
Child-Thread Started...
Child-Thread Continues...
Main Thread Completed.
Child-Thread Completed.