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

Document 2

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)
7 views17 pages

Document 2

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/ 17

Task 1: Inventory Management System:

import java.util.Random;

public class InventoryManagement {

public static void main(String[] args) {

// Initializing the number of products (5) and branches (4)

int products = 5;

int branches = 4;

// Create a 2D array to store stock levels for products across branches

int[][] stockLevels = new int[products][branches];

// Create a Random object to generate random stock levels

Random random = new Random();

// Initialize the stock levels with random values between 0 and 100

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

for (int j = 0; j < branches; j++) {

stockLevels[i][j] = random.nextInt(101); // Random stock level between 0


and 100

// Display the stock levels for each product at each branch

System.out.println("Stock levels for each product at each branch:");

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

for (int j = 0; j < branches; j++) {


System.out.print(stockLevels[i][j] + "\t");

System.out.println(); // Newline after each product's stock info

// Calculate the total stock for each product across all branches

int highestStock = 0;

int highestProduct = -1;

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

int totalStock = 0;

for (int j = 0; j < branches; j++) {

totalStock += stockLevels[i][j];

System.out.println("Total stock for Product " + (i + 1) + ": " + totalStock);

// Check if this product has the highest stock

if (totalStock > highestStock) {

highestStock = totalStock;

highestProduct = i;

// Display the product with the highest stock

System.out.println("\nProduct " + (highestProduct + 1) + " has the highest


stock: " + highestStock);

}
Task 2: Username Verification
import java.util.Scanner;

public class UsernameVerification {


public static void main(String[] args) {
// Create a Scanner object to get user input
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter two usernames


System.out.print("Enter the first username: ");
String username1 = scanner.nextLine();

System.out.print("Enter the second username: ");


String username2 = scanner.nextLine();

// Compare the usernames for exact match


if (username1.equals(username2)) {
System.out.println("The usernames are exactly the same.");
} else {
System.out.println("The usernames are not exactly the
same.");
}
// Compare the usernames case-insensitively
if (username1.equalsIgnoreCase(username2)) {
System.out.println("The usernames match case-
insensitively.");
} else {
System.out.println("The usernames do not match case-
insensitively.");
}

// Compare the usernames alphabetically


int result = username1.compareTo(username2);
if (result < 0) {
System.out.println("Username 1 comes first alphabetically.");
} else if (result > 0) {
System.out.println("Username 2 comes first alphabetically.");
} else {
System.out.println("The usernames are the same
alphabetically.");
}

// Close the scanner to prevent resource leak


scanner.close();
}
}
Task 3: Array Reversal
public class ArrayReversal {
public static void main(String[] args) {
// Initialize an array with 6 integers
int[] numbers = {1, 2, 3, 4, 5, 6};

// Print the original array


System.out.println("Original array:");
for (int num : numbers) {
System.out.print(num + " ");
}

// Reverse the array in-place


int left = 0;
int right = numbers.length - 1;
while (left < right) {
// Swap elements at left and right indices
int temp = numbers[left];
numbers[left] = numbers[right];
numbers[right] = temp;

// Move indices towards the center


left++;
right--;
}

// Print the reversed array


System.out.println("\nReversed array:");
for (int num : numbers) {
System.out.print(num + " ");
}
}
}

Task 4: Matrix Multiplication


import java.util.Scanner;

public class MatrixMultiplication {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input the dimensions of the first matrix


System.out.print("Enter the number of rows for Matrix 1: ");
int rows1 = scanner.nextInt();
System.out.print("Enter the number of columns for Matrix 1: ");
int cols1 = scanner.nextInt();

// Input the dimensions of the second matrix


System.out.print("Enter the number of rows for Matrix 2: ");
int rows2 = scanner.nextInt();
System.out.print("Enter the number of columns for Matrix 2: ");
int cols2 = scanner.nextInt();

// Check if matrix multiplication is possible


if (cols1 != rows2) {
System.out.println("Matrix multiplication is not possible. The
number of columns of Matrix 1 must equal the number of rows of
Matrix 2.");
return;
}

// Initialize the matrices


int[][] matrix1 = new int[rows1][cols1];
int[][] matrix2 = new int[rows2][cols2];
int[][] result = new int[rows1][cols2];

// Input values for the first matrix


System.out.println("Enter values for Matrix 1:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
matrix1[i][j] = scanner.nextInt();
}
}
// Input values for the second matrix
System.out.println("Enter values for Matrix 2:");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
matrix2[i][j] = scanner.nextInt();
}
}

// Perform matrix multiplication


for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
// Initialize the result cell as 0
result[i][j] = 0;
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}

// Display Matrix 1
System.out.println("\nMatrix 1:");
displayMatrix(matrix1);

// Display Matrix 2
System.out.println("\nMatrix 2:");
displayMatrix(matrix2);

// Display Result Matrix


System.out.println("\nResulting Matrix (Matrix 1 * Matrix 2):");
displayMatrix(result);

// Close the scanner


scanner.close();
}

// Helper method to display matrices


public static void displayMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println(); // Newline after each row
}
}
}

You might also like