Java Laboratory (4)
Java Laboratory (4)
ENGINEERING
Java Lab manual
2023-2024
Sl Lab Experiments Pg
NO No
PART A
1. Develop a JAVA program to add TWO matrices of suitable order N (The 4
value of N should be read from command line arguments).
10. Develop a JAVA program to create a package named mypack and import 22
and implement it in a suitable class
11. Write a program to illustrate creation of threads using runnable class .(start 24
method start each of the newly created thread .Inside the run method there
is sleep() for suspend the thread for 500 milliseconds)
12. Develop a program to create a class MyThread in this class a 26
conmstructor , 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 thread are excecuting concurrently.
1. Develop a JAVA program to add TWO matrices of suitable order N (The value of N
should be read from command line arguments).
package java22scheme;
import java.util.Scanner;
public class MatrixAddition {
}
System.out.println("The elements for matrix a");
for(int i=0;i<order;i++)
{
for(int j=0;j<order;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("The elements for matrix b");
for(int i=0;i<order;i++)
{
for(int j=0;j<order;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
//Finding the sum
System.out.println("The sum of two matrices is");
for(int i=0;i<order;i++)
{
for(int j=0;j<order;j++)
{
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
Output:
// Creating a stack
StackDemo(int size) {
// initialize the array
// initialize the stack variables
arr = new int[size];
capacity = size;
top = -1;
}
// push elements to the top of stack
public void push(int x) {
if (isFull()) {
System.out.println("Stack OverFlow");
// terminates the program
System.exit(1);
}
Output:
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.
package java22scheme;
import java.util.Scanner;
public class Employee {
int empid;
String name;
float salary;
public void raisesalary()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the percentage of salary increase");
float percentage=sc.nextInt();
float incrsalary=salary+(salary*(percentage/100));
System.out.println("The salary has increased to "+incrsalary);
}
public static void main(String[] args) {
Employee e1=new Employee();
e1.empid=1001;
e1.name="Harsha";
e1.salary=45000;
e1.raisesalary();
}}
Output:
4. A class called MyPoint, which models a 2D point with x and y coordinates, is designed
as follows:
a. Two instance variables x (int) and y (int).
b. A default (or "no-arg") constructor that construct a point at the default location of (0,
0).
c. A overloaded constructor that constructs a point with the given x and y coordinates.
d. A method setXY() to set both x and y.
e. A method getXY() which returns the x and y in a 2-element int array.
f. A toString() method that returns a string description of the instance in the format "(x,
y)".
g. A method called distance(int x, int y) that returns the distance from this point to
another point at the
given (x, y) coordinates
h. An overloaded distance(MyPoint another) that returns the distance from this point to
the given
MyPoint instance (called another)
i. 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
int x,y;
MyPoint()
{
x=10;
y=10;
}
MyPoint(int x,int y)
{
this.x=x;
this.y=y;
}
public void setXY(int x,int y)
{
this.x=x;
this.y=y;
}
public void getXY()
{
int a[]=new int[2];
a[0]=x;
a[1]=y;
}
package java22scheme;
mp1.distance(15,10);
mp1.distance();
mp2.distance(15,10);
mp2.distance();
mp3.distance(mp2);
}
}
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.
package java22scheme;
public class Shape {
public void draw() {
System.out.println("Drawing shape");
}
package java22scheme;
@Override
public void erase() {
System.out.println("Erasing Circle");
}
}
package java22scheme;
@Override
public void erase() {
System.out.println("Erasing Triangle");
}
}
package java22scheme;
@Override
public void erase() {
System.out.println("Erasing Square");
}
}
package java22scheme;
shape1.draw();
shape1.erase();
shape2.draw();
shape2.erase();
shape3.draw();
shape3.erase();
}}
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.
package java22scheme;
public abstract class Shape {
public abstract double calculateArea();
public abstract double calculatePerimeter();
}
package java22scheme;
@Override
public double calculateArea() {
return Math.PI * Math.pow(radius, 2);
}
@Override
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}
package java22scheme;
@Override
public double calculateArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
public double calculatePerimeter() {
return side1 + side2 + side3;
}
}
package java22scheme;
System.out.println("Circle:");
System.out.println("Area: " + circle.calculateArea());
System.out.println("Perimeter: " + circle.calculatePerimeter());
System.out.println("\nTriangle:");
System.out.println("Area: " + triangle.calculateArea());
System.out.println("Perimeter: " + triangle.calculatePerimeter());
}
}
Output:
@Override
public void resizeWidth(int width)
{
if (width > 0)
{
this.width = width;
}
else
{
System.out.println("Invalid width. Width must be greater than 0.");
}
}
@Override
public void resizeHeight(int height)
{
if (height > 0)
{
this.height = height;
}
else
{
System.out.println("Invalid height. Height must be greater than 0.");
}
}
System.out.println("Original Rectangle:");
rectangle.display();
System.out.println("Resized Rectangle:");
rectangle.display();
}
}
Output:
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.
package java22scheme;
void display() {
System.out.println("Outer display()");
}
package java22scheme;
void display() {
System.out.println("Inner display()");
}
}
package java22scheme;
Output :
9.Develop a JAVA program to raise a custom exception (user defined exception) for
Division by Zero using try , catch , throw and finally.
package java22scheme;
package java22scheme;
}
public static void main(String args[]) {
try {
//compute(1,0);
compute(20,5);
} catch (MyException e) {
System.out.println("Caught " + e);
}
}
}
Output:
10.Develop a JAVA program to create a package named mypack and import and
implement it in a suitable class.
package mypack;
package mypackaccess;
import mypack.Calculator;
Output:
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).
Program:
package java22scheme;
package java22scheme;
Output:
12.Develop a program to create a class MyThread in this class a conmstructor , 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 thread are
excecuting concurrently.
Output :