Professional Documents
Culture Documents
Oops Control Structures
Oops Control Structures
Oops Control Structures
AIM:
To write a Java program to sort the elements of an array in ascending and descending order.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare a[], temp, n
Step 3: set temp =0
Step 4: Input the number of elements in array (n)
Step 5: Repeat step 6 until i<n //for(i = 0; i <n ; i++)
Step 6: Print a[i]
Step 7: Repeat step 8 to step 9 until i<n
//for(i=0; i<n; i++ )
Step 8: Repeat step 9 until j<n
//for(j=i+1; j<n; j++)
Step 9: For Descending order
if(a[i] < a[j]) then
temp = a[i]
a[i]=a[j]
a[j]=temp
Step 10: Similarly, for Ascending order (Repeat step 7 and 8)
if(a[i] > a[j]) then
temp = a[i]
a[i]=a[j]
a[j]=temp
Step 11: Print "elements of array sorted in descending order and ascending order"
Step 12: Repeat step 13 until i<n //for(i=0;i<n;i++)
Step 13: Print a[i]
Step 14: End the program.
PROGRAM:
import java.util.Scanner;
public class ascending
{
public static void main(String[] args)
{
int n, temp;
Scanner sc = new Scanner(System.in);
System.out.println("Enter no.of elements in array:");
n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = sc.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Ascending Order: ");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ", ");
}
System.out.println(a[n - 1]);
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Descending Order: ");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ", ");
}
System.out.print(a[n - 1]);
}
}
OUTPUT:
RESULT:
For the above program, the output was verified successfully.
EX NO: 2B
ADDITION & MULTIPLICATION OF MATRICES
DATE:
AIM:
To write a java program that creates a class called 'Matrix' containing constructor that
initializes the number of rows and columns of a new Matrix object and perform addition &
multiplication of two matrices.
ALGORITHM:
Step 1: Start the program.
Step 2: Create class named Matrix with constructor that inputs the order of the matrix[m,n].
Step 3: Input the matrix a elements.
Step 4: Input the matrix b elements.
Step 5: from set_Matrix() function ,Repeat from i = 0 to row
Step 6: Repeat from j = 0 to column
Step 7: Print the two matrices
Step 8: Define the functions add() & product() and print the corresponding results using
printMatrix function and call them in the main class (javaprog).
Step 9: End the program.
PROGRAM:
import java.util.Scanner;
class Matrix
{
int row;
int column;
int[][] mat;
Scanner sc = new Scanner(System.in);
public Matrix(int r, int c) //parameterized constructor
{
row = r;
column = c;
mat = new int[row][column];
}
public Matrix()
{
//constructor;
}
public int getRows()
{
System.out.println("Enter no. of rows:");
row = sc.nextInt();
return row;
}
public int getColumns()
{
System.out.println("Enter no. of columns:");
column = sc.nextInt();
return column;
}
public void set_Matrix()
{
int i=0,j=0;
System.out.println("Enter the matrix elements(row-wise)");
for (i=0; i < row; i++)
{
for(j=0; j<column; j++)
{
mat[i][j] = sc.nextInt();
}
}
}
public Matrix add(Matrix m1)
{
Matrix ans = new Matrix(row,column);
int i=0,j=0;
if (row != m1.row || column != m1.column)
{
System.out.println("ERROR: The two matrices should have same no. of rows and columns
for addition!");
}
else
{
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
ans.mat[i][j] = mat[i][j] + m1.mat[i][j];
}
}
}
return ans;
}
class javaprog
{
public static void main(String args[])
{
System.out.println("First matrix:");
Matrix m1=new Matrix();
int r1=m1.getRows();
int c1=m1.getColumns();
Matrix a = new Matrix(r1,c1);
a.set_Matrix();
System.out.println("Second matrix:");
Matrix m2=new Matrix();
int r2 =m2.getRows();
int c2=m2.getColumns();
Matrix b = new Matrix(r2,c2);
b.set_Matrix();
ans=a.product(b);
System.out.println("\nProduct of the two matrices is:>>");
ans.printMatrix();
}
}
OUTPUT:
RESULT:
For the above program, the output was verified successfully.