Java Lab Manual - 1-4
Java Lab Manual - 1-4
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;
int N = Integer.parseInt(args[0]);
Output:
Enter the elements of the first matrix:
Element [0][0]: 1
Element [0][1]: 2
Element [1][0]: 3
Element [1][1]: 4
Enter the elements of the second matrix:
Element [0][0]: 5
Element [0][1]: 6
Element [1][0]: 7
Element [1][1]: 8
The resulting matrix after addition is:
6 8
10 12
public Stack() {
stack = new int[maxSize];
top = -1;
}
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
stack.displayStack();
stack.displayStack();
}
}
Output:
Stack (top to bottom): 50 40 30 20 10
Popped element: 50
Top element: 40
Stack (top to bottom): 40 30 20 10
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.
public class Employee {
private int id;
private String name;
private double salary;
Dr. Azizkhan F Pathan, Dept. of IS&E, JIT, Davangere 3
Object Oriented Programming with JAVA BCS306A
employee.raiseSalary(10);
Output:
Initial Employee Details:
Employee ID: 1 Name: John Doe Salary: $50000.0
John Doe's salary raised by 10%. New salary: $55000.0
Employee Details after Salary Raise:
Employee ID: 1 Name: John Doe Salary: $55000.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.
public class MyPoint
{
int x;
int y;
public MyPoint()
{
this.x = 0;
this.y = 0;
}
class testmypoint{
public static void main(String[] args){
MyPoint point1=new MyPoint(3,4);
MyPoint point2=new MyPoint(6,8);
System.out.println("Point1:"+point1);
System.out.println(("Point2:"+point2);
Output:
Point 1: (3,4)
Point 1: (6,8)
Distance between Point 1and Point 2: 5.0
Distance from Point 1to the origin: 5.0
Point 1 coordinates after setXY: (1,2)