0% found this document useful (0 votes)
5 views13 pages

java jrnl

Uploaded by

Sahim Tambadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

java jrnl

Uploaded by

Sahim Tambadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Name: Sahim s tambadi USN:2MM23CS056

Program1.
Develop a JAVA program to add TWO matrices of suitable order N (The value of N
should be read from command line arguments).
Add.java
import java.util.Scanner;
public class Add
{
public static void main(String[] args)
{

int b[][] = new int[m][n];


int c[][] = new int[m][n];
System.out.println("Enter all the elements of first matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = scan.nextInt();
}
}
System.out.println("");
System.out.println("Enter all the elements of second matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)

CSE,MMEC,BELGAUM 1
Name: Sahim s tambadi USN:2MM23CS056

{
b[i][j] = scan.nextInt();
}
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("Matrix after addition:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println("");
}
}
}

CSE,MMEC,BELGAUM 2
Name: Sahim s tambadi USN:2MM23CS056

Output:
Microsoft Windows [Version 10.0.22631.4169]
(c) Microsoft Corporation. All rights reserved.
C:\Users\k\Downloads\notes\Java>javac Add.java
C:\Users\k\Downloads\notes\Java>java Add
Enter the number of rows in the matrix:3
Enter the number of columns in the matrix:3
Enter all the elements of first matrix:
123
456
789
Enter all the elements of second matrix:
987
654
321
Matrix after addition:
10 10 10
10 10 10
10 10 10

CSE,MMEC,BELGAUM 3
Name: Sahim s tambadi USN:2MM23CS056

Program 2.
Develop a stack class to hold a maximum of 10 integers with suitable methods.
Develop a JAVA main method to illustrate Stack operations.
Stack.java
import java.util.*;
public class Stack
{
int s[]=new int[10]; int top= -1;
int size=3;
void push(int i)
{
if(top==size-1)
System.out.println("Stack Overflow");
else
{
s[++top] = i;
}
}
void pop( )
{
if (top == -1)
{
System.out.println("Stack Underflow");
}
else

CSE,MMEC,BELGAUM 4
Name: Sahim s tambadi USN:2MM23CS056

{
System.out.println(" Popped Element= " + s[top]);
top--;
}
}
void display( )
{
if(top == -1)
{
System.out.println("Stack is Empty\n");
}
else
{
System.out.println("Stack Elements are:\n");
for (int i = top; i >= 0; i--)
System.out.println(s[i]);
}
}
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
Stack stk = new Stack();
for(;;)
{
System.out.println("\n---Stack Operations---");

CSE,MMEC,BELGAUM 5
Name: Sahim s tambadi USN:2MM23CS056

System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Display");
System.out.println("4. Exit");
System.out.pclrclrintln("Enter your choice:\n");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter the element to push");
stk.push(scan.nextInt());
break;

case 2 : stk.pop();
break;

case 3 : stk.display();
break;

case 4 : System.exit(0);

default :
System.out.println("Invalid Choice\n");
break;
}

CSE,MMEC,BELGAUM 6
Name: Sahim s tambadi USN:2MM23CS056

}
}
}

CSE,MMEC,BELGAUM 7
Name: Sahim s tambadi USN:2MM23CS056

Output:
Microsoft Windows [Version 10.0.22631.4169]
(c) Microsoft Corporation. All rights reserved.
C:\Users\k\Downloads\notes\Java>javac Stack.java
C:\Users\k\Downloads\notes\Java>java Stack
---Stack Operations---
1. Push
2. Pop
3. Display
4. Exit
Enter your choice:
1
Enter the element to push
11
---Stack Operations---
1. Push
2. Pop
3. Display
4. Exit
Enter your choice:
1
Enter the element to push
12
---Stack Operations---
1. Push

CSE,MMEC,BELGAUM 8
Name: Sahim s tambadi USN:2MM23CS056

2. Pop
3. Display
4. Exit
Enter your choice:
3
Stack Elements are:
12
11
---Stack Operations---
1. Push
2. Pop
3. Display
4. Exit
Enter your choice:
2
Popped Element= 12
---Stack Operations---
1. Push
2. Pop
3. Display
4. Exit
Enter your choice:
3
Stack Elements are:
11

CSE,MMEC,BELGAUM 9
Name: Sahim s tambadi USN:2MM23CS056

---Stack Operations---
1. Push
2. Pop
3. Display
4. Exit
Enter your choice:
4
C:\Users\k\Downloads\notes\Java>

CSE,MMEC,BELGAUM 10
Name: Sahim s tambadi USN:2MM23CS056

Program 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.
Employee.java
import java.util.Scanner;
public class Employee
{
int id;
String name;
double salary;
public Employee(int id, String name, double salary)
{
this.name = name;
this.id = id;
this.salary = salary;
}
public void display()
{
System.out.println("Id: " + id);
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
}
public void raiseSalary(double percentage)
{

CSE,MMEC,BELGAUM 11
Name: Sahim s tambadi USN:2MM23CS056

salary = salary + (salary * percentage / 100);


}
public static void main(String[] args)
{
int p;
Employee e1 = new Employee(8, "Rakesh", 2500);
e1.display();
Scanner scan= new Scanner(System.in);
System.out.println("\nEnter the percentage to raise the salary");
p=scan.nextInt();
e1.raiseSalary(p);
System.out.println("\nAfter raising salary");
e1.display();
}
}

CSE,MMEC,BELGAUM 12
Name: Sahim s tambadi USN:2MM23CS056

Output:
Microsoft Windows [Version 10.0.22631.4317]
(c) Microsoft Corporation. All rights reserved.
C:\Users\k\Downloads\notes\Java>javac Employee.java
C:\Users\k\Downloads\notes\Java>java Employee
Id: 8
Name: Rakesh
Salary: 2500.0
Enter the percentage to raise the salary
5
After raising salary
Id: 8
Name: Rakesh
Salary: 2625.0

CSE,MMEC,BELGAUM 13

You might also like