Java Lab Manual
Java Lab Manual
Program:1.
Develop a JAVA program to add TWO matrices of suitable order N (The value of N
should be read from command line arguments).
import java.util.Scanner;
OUTPUT
1. Enter the value of N:
2
Enter the elements of first matrix:
34
45
Enter the elements of second matrix:
56
78
The resultant matrix is:
11 10
11 13
2. Enter the value of N:
3
Enter the elements of first matrix:
345
567
10 22 45
Enter the elements of second matrix:
67 8 9
346
3 67 8
The resultant matrix is:
70 12 14
8 10 13
12 89 53
OUTPUT:
0123456789
Popping integers off the stack:
Stack after pop:
012345678
Stack after pop:
01234567
Stack after pop:
0123456
Stack after pop:
012345
Stack after pop:
012340123456789
Popping integers off the stack:
Stack after pop:
012345678
Stack after pop:
01234567
Stack after pop:
0123456
Stack after pop:
012345
Stack after pop:
01234
@Override
public String toString() {
return "Employee{" +
"id=" + id +
employee.raiseSalary(i);
System.out.println("Employee after salary increase: \n" + employee);
}
}
OUTPUT :
Program 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.
Aim: Demonstrating creation of java classes, object, declaration and initializationof
variables.
}
private int x;
private int y;
public My_Point1(int i, int j) {
this.x = x;
this.y = y;
}
public My_Point1() {
this(0, 0);
}
}
public int[] getXY() {
int[] xy = new int[2];
xy[0] = x;
xy[1] = y;
return xy;
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
public double distance(int x, int y) {
return Math.sqrt(Math.pow(this.x - x, 2) + Math.pow(this.y - y, 2));
}
public double distance(My_Point1 another) {
return Math.sqrt(Math.pow(this.x - another, 2) + Math.pow(this.y - another, 2));
}
public double distance() {
return Math.sqrt(Math.pow(this.x , 2) + Math.pow(this.y , 2));
}
OUTPUT :
Program: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.
Aim: Demonstrate constructor and method overloading in java programming.
}
OUTPUT:
1. Enter Radius of circle:
6
Area of circle: 113.09733552923255
Perimeter of circle: 37.69911184307752
Enter sides of Triangle j, k, l:
2
3
2
Area of triangle: 1.984313483298443
Perimeter of Triangle:7.0
Program: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.
interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
OUTPUT:
Original width: 10
Original height: 20
New width: 30
New height: 40
Program: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.
class Inner {
public void display() {
System.out.println("Inner class display method");
}
}
}
OUTPUT
Program:9
Develop a JAVA program to raise a custom exception (user defined exception) for Division
By Zero using try, catch, throw and finally
import java.util.Scanner;
float result = a / b;
System.out.println("Result: " + result);
}
catch (ArithmeticException e)
{
System.out.println(e.getMessage());
} finally {
scanner.close();
}
}
OUTPUT:
Enter two numbers:
10
2
Result: 5.0
Enter two numbers:
10
0
Division by zero is not allowed!
Program:10.
Develop a JAVA program to create a package named mypack and import & implement it
in a suitable class.
OUTPUT: