0% found this document useful (0 votes)
42 views

Java Lab Manual - 1-4

Java

Uploaded by

hemanthhr817
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Java Lab Manual - 1-4

Java

Uploaded by

hemanthhr817
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Object Oriented Programming with JAVA BCS306A

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;

public class MatrixAddition {


public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Please provide the order of the matrix (N) as a command-line argument.");
return;
}

int N = Integer.parseInt(args[0]);

int[][] matrix1 = new int[N][N];


int[][] matrix2 = new int[N][N];
int[][] resultMatrix = new int[N][N];

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the elements of the first matrix:");


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print("Element [" + i + "][" + j + "]: ");
matrix1[i][j] = scanner.nextInt();
}
}

System.out.println("Enter the elements of the second matrix:");


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print("Element [" + i + "][" + j + "]: ");
matrix2[i][j] = scanner.nextInt();
}
}

for (int i = 0; i < N; i++) {


for (int j = 0; j < N; j++) {
resultMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

System.out.println("The resulting matrix after addition is:");


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(resultMatrix[i][j] + " ");
}
System.out.println();
}
scanner.close();
}
}

Dr. Azizkhan F Pathan, Dept. of IS&E, JIT, Davangere 1


Object Oriented Programming with JAVA BCS306A

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

2. Develop a stack class to hold a maximum of 10 integers with suitable methods.


Develop a JAVA main method to illustrate Stack operations.
public class Stack {
private int maxSize=10;
private int[] stackArray;
private int top;

public Stack() {
stack = new int[maxSize];
top = -1;
}

public void push(int value) {


if (isFull()) {
System.out.println("Stack is full. Cannot push element.");
}
top++;
stackArray[top] = value;
}

public int pop() {


if (isEmpty()) {
System.out.println("Stack is empty. Cannot pop element.");
return -1;
}
int poppedValue = stackArray[top];
top--;
return poppedValue;
}

public int peek() {


if (isEmpty()) {
System.out.println("Stack is empty. No element to peek.");
return -1;
}
return stackArray[top];

Dr. Azizkhan F Pathan, Dept. of IS&E, JIT, Davangere 2


Object Oriented Programming with JAVA BCS306A

public boolean isEmpty() {


return (top == -1);
}

public boolean isFull() {


return (top == maxSize - 1);
}

public void displayStack() {


System.out.print("Stack (top to bottom): ");
for (int i = top; i >= 0; i--) {
System.out.print(stackArray[i] + " ");
}
System.out.println();
}

public static void main(String[] args) {


Stack stack = new Stack();

stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);

stack.displayStack();

System.out.println("Popped element: " + stack.pop());

System.out.println("Top element: " + stack.peek());

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

public Employee(int id, String name, double salary) {


this.id = id;
this.name = name;
this.salary = salary;
}

public void raiseSalary(double percent) {


if (percent > 0) {
double raiseAmount = salary * (percent / 100);
salary += raiseAmount;
System.out.println(name + "'s salary raised by " + percent + "%. New salary: $" + salary);
} else {
System.out.println("Invalid percentage. Salary remains unchanged.");
}
}

public String toString() {


return "Employee ID: " + id + ", Name: " + name + ", Salary: $" + salary;
}

public static void main(String[] args) {


Employee employee = new Employee(1, "John Doe", 50000.0);

System.out.println("Initial Employee Details:");


System.out.println(employee);

employee.raiseSalary(10);

System.out.println("\nEmployee Details after Salary Raise:");


System.out.println(employee);
}
}

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)".

Dr. Azizkhan F Pathan, Dept. of IS&E, JIT, Davangere 4


Object Oriented Programming with JAVA BCS306A

 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;
}

public 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 int[] getXY()


{
int[] coordinates={x,y};
return coordinates;
}

public String toString()


{
return "(" + x + ", " + y + ")";
}

public double distance(int x, int y)


{
int xDiff = this.x - x;
int yDiff = this.y - y;
return Math.sqrt(xDiff*xDiff + yDiff*yDiff);
}

Dr. Azizkhan F Pathan, Dept. of IS&E, JIT, Davangere 5


Object Oriented Programming with JAVA BCS306A

public double distance(MyPoint another)


{
return distance(another.x, another.y);
}

public double distance()


{
return distance(0,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);

System.out.println("Distance between Point 1and Point 2:"+point1.distance(point2));


System.out.println("Distance from Point 1to the origin:"+point1.distance());
point1.setXY(1,2);
System.out.println("Point 1 coordinates after setXY:"+point1. toString ());
}
}

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)

Dr. Azizkhan F Pathan, Dept. of IS&E, JIT, Davangere 6

You might also like