0% found this document useful (0 votes)
7 views16 pages

Java Matrix Addition Program

The document outlines various laboratory programs in Java, including matrix addition, stack operations, employee management, point modeling, shape drawing with polymorphism, and abstract class implementation for calculating area and perimeter. Each program includes code snippets demonstrating the concepts and functionalities required. The document serves as a guide for students to understand and implement these programming tasks.

Uploaded by

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

Java Matrix Addition Program

The document outlines various laboratory programs in Java, including matrix addition, stack operations, employee management, point modeling, shape drawing with polymorphism, and abstract class implementation for calculating area and perimeter. Each program includes code snippets demonstrating the concepts and functionalities required. The document serves as a guide for students to understand and implement these programming tasks.

Uploaded by

ninjadark143
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Laboratory program No.

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 [Link];

public class MatrixAddition {


public static void main(String[] args) {
// creation of Scanner objects sc
Scanner sc = new Scanner([Link]);

[Link]("Enter the order of matrices (n): ");


int n = [Link]();
// Creating 3 matrics objects: matrix1 , matrix2 and sumMatrix
int[][] matrix1 = new int[n][n];
int[][] matrix2 = new int[n][n];
int[][] sumMatrix = new int[n][n];

// Call to read matrix1


[Link]("Enter elements of the first matrix:");
inputMatrix(matrix1, sc);

// Call to read matrix2


[Link]("Enter elements of the second matrix:");
inputMatrix(matrix2, sc);

// Adding matrices
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Call to Display the resultant Matrix


[Link]("Sum of the matrices is:");
displayMatrix(sumMatrix);

[Link]();
} // end of main function
private static void inputMatrix(int[][] matrix, Scanner sc) {
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = [Link]();
}
}
}

// member function to display a matrix


private static void displayMatrix(int[][] matrix) {
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix[0].length; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}
}

} // end of class MatrixAddition


Other Versions of Matrix Program:

import [Link];
class MatrixAddition{
public static void main(String args[]){
int rows,columns, i, j;
Scanner in = new Scanner([Link]);
[Link]("Enter the number of rows and columns of matrix");
rows= [Link]();
columns = [Link]();
int matrix1[][] = new int[rows][columns];
int matrix2[][] = new int[rows][columns];
int sum[][] = new int[rows][columns];
[Link]("Enter the elements of matrix1");
for ( i = 0 ; i < rows ; i++ ){
for ( j = 0 ; j < columns ; j++ ){
matrix1[i][j] = [Link]();
}
}
[Link]("Enter the elements of matrix2");
for ( i = 0 ; i < rows ; i++ ){
for ( j = 0 ; j < columns ; j++ ){
matrix2[i][j] = [Link]();
}
}
for ( i = 0 ; i < rows ; i++ ){
for ( j = 0 ; j < columns ; j++ ){
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
[Link]("Sum of entered matrices:");
for ( i = 0 ; i < rows ; i++ ){
for ( j = 0 ; j < columns ; j++ ){
[Link](sum[i][j]+"\t");
}
[Link]();
}
}
}
Sample Output
Output is:
Enter the number of rows and columns of matrix
3
3
Enter the elements of matrix1
5
8
3
9
2
5
9
4
3
Enter the elements of matrix2
7
0
2
4
7
8
6
5
1
Sum of entered matrices:
12 8 5
13 9 13
15 9 4
import [Link];

class MainClass {
int matrix[][];
int row, column;
void create() {
Scanner in = new Scanner([Link]);
[Link]("Number of rows :");
row = [Link]([Link]());
[Link]("Number of columns :");
column = [Link]([Link]());
matrix = new int[row][column];
[Link]("Enter the data :");
for(int i=0; i < row; i++) {
for(int j=0; j < column; j++) {
matrix[i][j] = [Link]();
}
}
}
void display() {
[Link]("\nThe Matrix is :");
for(int i=0; i < row; i++) {
for(int j=0; j < column; j++) {
[Link]("\t" + matrix[i][j]);
}
[Link]();
}
}
}

class CreateMatrix {
public static void main(String args[]) {
MainClass obj = new MainClass();
[Link]();
[Link]();
}
}
Sample Output
Output is:
Number of rows :
4
Number of columns :
3
Enter the data :
1
56
3
7
2
9
4
3
9
3
78
98

The Matrix is :
1 56 3
7 2 9
4 3 9
3 78 98

Note: Upgradethe above program to add two matrices


Laboratory program No. 2
Develop a stack class to hold a maximum of 10 integers with suitable methods.
Develop a JAVA main method to illustrate Stack operations.

import [Link].*;
//Stack class
class Stack {
int top; //define top of stack
int maxsize = 5; //max size of the stack
int[] stack_arry = new int[maxsize]; //define array that will hold stack elements
Stack(){ //stack constructor; initially top = -1
top = -1;
}
boolean isEmpty(){ //isEmpty () method
return (top < 0);
}
boolean push (int val){ //push () method
if(top == maxsize-1) {
[Link]("Stack Overflow !!");
return false;
}
else {
top++;
stack_arry[top]=val;
return true;
}
}
boolean pop () { //pop () method
if (top == -1) {
[Link]("Stack Underflow !!");
return false;
}
else {

[Link]("\nItem popped: " + stack_arry[top--]);


return true;
}
}
void display () { //print the stack elements
[Link]("Printing stack elements .....");
for(int i = top; i>=0;i--) {
[Link](stack_arry[i] + " ");
}
}
}

public class Main {


public static void main(String[] args) {
//define a stack object
Stack stck = new Stack();
[Link]("Initial Stack Empty : " + [Link]());
//push elements
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link]("After Push Operation...");
//print the elements
[Link]();
//pop two elements from stack
[Link]();
[Link]();
[Link]("After Pop Operation...");
//print the stack again
[Link]();
}
}

Program Testing

Note: Test the Stack Class program display Underflow and Overflow conditions. Students can
modify the above Class Main by adding more elemnts than the stack size. Aso by Poping more
the the stack elements.
Laboratory program No. 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 String firstName;
private String lastName;
private double monthlySalary;

// constructor initializes firstName, lastName and monthlySalary with String


//and double supplied as argument

public Employee (String fname, String lname, double msalary)


{
firstName = fname; // initialize firstName
lastName = lname; // initialize lastName
monthlySalary = msalary; // initialize monthlySalary

} // end constructor

// method to set the first name


public void setFirstName (String fname)
{
firstName = fname; // store the first name

// method to retrieve first name


public String getFirstName ()
{
return firstName;
}
// method to set the last name
public void setLastName (String lname)
{
lastName = lname; // store the last name
}

// method to retrieve last name


public String getLastName ()
{
return lastName;
}

// method to set the monthly salary


public void setMonthlySalary (double msalary)
{
monthlySalary = msalary; // store the monthly salary

// method to retrieve monthly salary


public double getMonthlySalary ()
{
return monthlySalary;
}

// method to retrieve monthly salary after giving 10% raise


public double getRaiseSalary()
{
double raise = monthlySalary * 0.1 ;
double raiseSalary = ( monthlySalary + raise );
return raiseSalary;
} // end method getRaiseSalary

} // end class Employee

public class EmployeeTest


{
// main method begins program execution
public static void main(String[] args)
{
Employee emp1 = new Employee( "John", "Smith", 5000.00 );
Employee emp2 = new Employee( "Jane", "Doe", 8000.00 );

// display employee's initial yearly salary


[Link]( "Yearly salary of %s %s: %.2f\n", [Link](),
[Link](), [Link]() );
[Link]( "Yearly salary of %s %s: %.2f\n", [Link](),
[Link](), [Link]() );

[Link]();

// display employee's salary after giving 10% raise


[Link]( "***** Giving 10% raise for each employee *****" );
[Link]( "Yearly salary of %s %s: %.2f\n", [Link](),
[Link](), [Link]() );
[Link]( "Yearly salary of %s %s: %.2f\n", [Link](),
[Link](), [Link]() );
} // end method main
} // end EmployeeTest
Laboratory 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

public class MyPoint {


private int x;
private int y;

// Default constructor
public MyPoint() {
this.x = 0;
this.y = 0;
}

// Overloaded constructor
public MyPoint(int x, int y) {
this.x = x;
this.y = y;
}

// Method to set both x and y


public void setXY(int x, int y) {
this.x = x;
this.y = y;
}
// Method to get x and y in a 2-element int array
public int[] getXY() {
return new int[]{x, y};
}

// Method to return a string description of the instance


public String toString() {
return "(" + x + ", " + y + ")";
}

// Method to calculate distance to another point (x, y)


public double distance(int x, int y) {
int xDiff = this.x - x;
int yDiff = this.y - y;
return [Link](xDiff * xDiff + yDiff * yDiff);
}

// Overloaded method to calculate distance to another MyPoint instance


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

// Overloaded method to calculate distance to the origin (0,0)


public double distance() {
return distance(0, 0);
}

// Testing the MyPoint class


public static void main(String[] args) {
MyPoint point1 = new MyPoint(1, 2);
MyPoint point2 = new MyPoint(3, 4);
[Link]("Point 1: " + [Link]());
[Link]("Point 2: " + [Link]());
[Link]("Distance between Point 1 and Point 2: " + [Link](point2));
[Link]("Distance from Point 1 to the origin: " + [Link]());
} // end of main

} // end of class MyPoint


[Link] 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.

class shape{
void draw() {[Link]("Drawing Shape");}
void erase(){ [Link]("erasing Shape");}
}

class Circle extends shape{


void draw(){ [Link]("Drawing Circle");}
void erase(){ [Link]("erasing Circle");}
}

class Triangle extends shape{


void draw(){ [Link]("Drawing Triangle");}
void erase(){ [Link]("erasing Triangle");}
}

class Square extends shape{


void draw(){ [Link]("Drawing Square");}
void erase(){ [Link]("erasing Square");}
}

class Demo{
public static void main(String args[]){
shape c=new Circle();
shape t=new Triangle();
shape s=new Square();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
} // end of main

} // end of Demo class


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.

import [Link].*;

abstract class Shape {


int length, breadth, radius;

Scanner input = new Scanner([Link]);

abstract void printArea();

class Rectangle extends Shape {


void printArea() {
[Link]("*** Finding the Area of Rectangle ***");
[Link]("Enter length and breadth: ");
length = [Link]();
breadth = [Link]();
[Link]("The area of Rectangle is: " + length * breadth);
}
}

class Triangle extends Shape {


void printArea() {
[Link]("\n*** Finding the Area of Triangle ***");
[Link]("Enter Base And Height: ");
length = [Link]();
breadth = [Link]();
[Link]("The area of Rectangle is: " + (length * breadth) / 2);
}
}

class Cricle extends Shape {


void printArea() {
[Link]("\n*** Finding the Area of Cricle ***");
[Link]("Enter Radius: ");
radius = [Link]();
[Link]("The area of Rectangle is: " + 3.14f * radius * radius);
}
}
public class AbstractClassExample {
public static void main(String[] args) {
Rectangle rec = new Rectangle();
[Link]();

Triangle tri = new Triangle();


[Link]();
Cricle cri = new Cricle();
[Link]();
}
}

Note : Add metho CalculatePeri();

You might also like