0% found this document useful (0 votes)
4 views9 pages

Java - Assignment 2 - Google Docs

The document contains Java assignments that include four programs: one for sorting names alphabetically, another for calculating the quotient and remainder of two numbers, a third for calculating the power of a number, and a fourth for adding two 3x3 matrices. Each program includes code snippets with user prompts for input and displays the results. The document serves as a practical guide for basic Java programming concepts and operations.

Uploaded by

hindunation69
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)
4 views9 pages

Java - Assignment 2 - Google Docs

The document contains Java assignments that include four programs: one for sorting names alphabetically, another for calculating the quotient and remainder of two numbers, a third for calculating the power of a number, and a fourth for adding two 3x3 matrices. Each program includes code snippets with user prompts for input and displays the results. The document serves as a practical guide for basic Java programming concepts and operations.

Uploaded by

hindunation69
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
You are on page 1/ 9

‭Java - Assignment 2‬

‭Program 1: Input 10 Names and Sort Alphabetically‬

‭Code:‬
import‬‭
‭ java‬
.‭
‭u‬til‬
.‭
‭S‬canner‬
;‬‭
‭ // Import Scanner class‬‭
for user input‬

import‬‭
‭ java‬
.‭
‭u‬til‬
.‭
‭A‬rrays‬
;‬
‭ // Import Arrays class‬‭
‭ for sorting‬

public‬‭
‭ class‬‭
AlphabeticalOrder‬‭
{‬

public‬‭
‭ static‬‭
void‬‭
main‬
(‬
‭ String‬
‭ []‬‭
‭ args‬
) {‬

Scanner‬‭
‭ inputScanner‬‭
=‬‭
new‬‭
Scanner‬
(‭
‭S‬ystem‬
.‭
‭ i
‬n‬
);‬‭
‭ // Create Scanner object for input‬

String‬
‭ []‬‭
‭ namesList‬‭
=‬‭
new‬‭
String‬
[‭
‭ 1
‬0‬
];‬ ‭
‭ // Array‬‭
to store 10 names‬

// Prompt user to enter 10 names‬


System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭"‬Please enter 10 names:"‬
);‬

// Loop to get 10 names from the user‬


for‬‭
‭ (‭
i‬nt‬‭
i‬‭
=‬‭
0‭
;‬‬‭
i‬‭
<‬‭
10‬
;‬‭
‭ i‭
+‬+‬
) {‬

System‬
‭ .‭
‭o‬ut‬
.‭
‭ p
‬rint‬
(‭
‭ "
‬Enter name #"‬‭
+‬‭
(‭
i‬‬‭
+‬‭
1‬)‬‭
‭ +‬‭
": "‬
);‬

namesList‬
‭ [‭
‭ i
‬‭
]‬‬‭
=‬‭
inputScanner‬
.‭
‭ n
‬extLine‬
();‬‭
‭ // Store each name in the array‬

}‬

// Sort the names alphabetically using Arrays.sort‬


Arrays‬
‭ .‬
‭ sort‬
‭ (‬
‭ namesList‬
‭ );‬

// Print the names in alphabetical order‬


System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭"‬‬
\n‬
‭ Names in alphabetical‬‭
‭ order:"‬
);‬

for‬‭
‭ (‭
S‬tring‬‭
name‬‭
:‬‭
namesList‬
) {‬

System‬
‭ .‭
‭o‬ut‬
.‭
‭ p
‬rintln‬
(‭
‭n‬ame‬
);‬‭
‭ // Print each‬‭
name from the sorted array‬

}‬

inputScanner‬
‭ .‭
‭ c
‬lose‬
();‬‭
‭ // Close the scanner‬‭
to prevent memory leaks‬

}‬

}‬

‭Java - Assignment 2‬

‭1.‬

‭Results:‬
‭Java - Assignment 2‬

‭Program 2: Dividend and Divisor - Find Quotient and Remainder‬


‭Code:‬
import‬‭
‭ java.util.Scanner‬
;‬‭
‭ // Import Scanner class‬‭
for user input‬
public‬‭
‭ class‬‭
DivisionCalculator‬‭
{‬
public‬‭
‭ static‬‭
void‬‭
main‬
(‭
‭S
‬tring‬
[]‬‭
‭ args‬
) {‬

Scanner‬‭
‭ inputScanner‬‭
=‬‭
new‬‭
Scanner‬
(‭
‭S
‬ystem‬
.‭
‭i
‬n‬
);‬‭
‭ // Create Scanner object for input‬

// Prompt user for dividend and divisor‬



System‬
‭ .‬
‭out‬
‭ .‭
‭p
‬rint‬
(‭
‭"
‬Enter the dividend: "‬
);‬

int‬‭
‭ dividend‬‭
=‬‭
inputScanner‬
.‭
‭n
‬extInt‬
();‬‭
‭ //‬‭
Store the dividend‬

System‬
‭ .‬
‭out‬
‭ .‭
‭p
‬rint‬
(‭
‭"
‬Enter the divisor: "‬
);‬

int‬‭
‭ divisor‬‭
=‬‭
inputScanner‬
.‭
‭n
‬extInt‬
();‬‭
‭ // Store‬‭
the divisor‬

// Calculate quotient and remainder‬



int‬‭
‭ quotient‬‭
=‬‭
dividend‬‭
/‬‭
divisor‬
;‬‭
‭ // Integer‬‭
division for quotient‬
int‬‭
‭ remainder‬‭
=‬‭
dividend‬‭
%‬‭
divisor‬
;‬‭
‭ // Modulus‬‭
operation for remainder‬

// Display the results‬



System‬
‭ .‬
‭out‬
‭ .‭
‭p
‬rintln‬
(‭
‭"
‬‭
\
‬n‬
Result: "‬
‭ );‬

‭Java - Assignment 2‬
System‬
‭ .‬
‭out‬
‭ .‭
‭p
‬rintln‬
(‭
‭"
‬Quotient: "‬‭
+‬‭
quotient‬
);‬

System‬
‭ .‬
‭out‬
‭ .‭
‭p
‬rintln‬
(‭
‭"
‬Remainder: "‬‭
+‬‭
remainder‬
);‬

inputScanner‬
‭ .‬
‭close‬
‭ ();‬‭
‭ // Close the scanner‬‭
to prevent memory leaks‬
}‬

}‬

‭1.‬
‭Java - Assignment 2‬

‭Program 3: Calculate Power of a Number‬


‭Code:‬
import‬‭
‭ java.util.Scanner‬
;‬‭
‭ // Import Scanner class‬‭
for user input‬

public‬‭
‭ class‬‭
PowerCalculator‬‭
{‬
public‬‭
‭ static‬‭
void‬‭
main‬
(‭
‭S
‬tring‬
[]‬‭
‭ args‬
) {‬

Scanner‬‭
‭ inputScanner‬‭
=‬‭
new‬‭
Scanner‬
(‭
‭S
‬ystem‬
.‭
‭i
‬n‬
);‬‭
‭ // Create Scanner object for input‬

// Prompt user for base and exponent‬



System‬
‭ .‬
‭out‬
‭ .‭
‭p
‬rint‬
(‭
‭"
‬Enter the base number: "‬
);‬

int‬‭
‭ baseNumber‬‭
=‬‭
inputScanner‬
.‭
‭n
‬extInt‬
();‬‭
‭ //‬‭
Store the base number‬

System‬
‭ .‬
‭out‬
‭ .‭
‭p
‬rint‬
(‭
‭"
‬Enter the exponent: "‬
);‬

int‬‭
‭ exponentValue‬‭
=‬‭
inputScanner‬
.‭
‭n
‬extInt‬
();‬‭
‭ // Store the exponent‬

// Initialize result to 1 (base case for multiplication)‬



int‬‭
‭ result‬‭
=‬‭
1‭
;
‬‬

// Loop to calculate power manually (base^exponent)‬



for‬‭
‭ (‬
int‬‭
‭ i‬‭
=‬‭
1‭
;
‬‬‭
i‬‭
<=‬‭
exponentValue‬
;‬‭
‭ i‬
++‬
‭ ) {‬

result‬‭
‭ *=‬‭
baseNumber‬
;‬‭
‭ // Multiply result‬‭
by base number‬
}‬

// Print the result‬



System‬
‭ .‬
‭out‬
‭ .‭
‭p
‬rintln‬
(‭
‭"
‬‭
\
‬n‬
The result of "‬‭
‭ +‬‭
baseNumber‬‭
+‬‭
" raised to the power of "‬‭
+‬
exponentValue‬‭
‭ +‬‭
" is: "‬‭
+‬‭
result‬
);‬

inputScanner‬
‭ .‬
‭close‬
‭ ();‬‭
‭ // Close the scanner‬‭
to prevent memory leaks‬
}‬

}‬

‭Java - Assignment 2‬

‭Results:‬
‭Java - Assignment 2‬

‭Program 4: Add Two 3x3 Matrices‬


‭ ode:‬
C
import‬‭
‭ java.util.Scanner‬
;‬‭
‭ // Import Scanner class‬‭
for user input‬

public‬‭
‭ class‬‭
MatrixAddition‬‭
{‬
public‬‭
‭ static‬‭
void‬‭
main‬
(‭
‭S
‬tring‬
[]‬‭
‭ args‬
) {‬

Scanner‬‭
‭ inputScanner‬‭
=‬‭
new‬‭
Scanner‬
(‬
‭System‬
‭ .‬
‭in‬
‭ );‬‭
‭ // Create‬
Scanner object for input‬

// Declare two 3x3 matrices for input and‬‭


‭ result‬
int‬
‭ [][]‬‭
‭ firstMatrix‬‭
=‬‭
new‬‭
int‬
[‭
‭3
‬‬
][‬
‭ 3‭
‭]
‬;‬‭
// First‬‭
matrix‬
int‬
‭ [][]‬‭
‭ secondMatrix‬‭
=‬‭
new‬‭
int‬
[‬
‭3‭
‭]
‬[‬
3‬
‭];‬‭
‭ // Second‬‭
matrix‬
int‬
‭ [][]‬‭
‭ resultMatrix‬‭
=‬‭
new‬‭
int‬
[‬
‭3‭
‭]
‬[‬
3‬
‭];‬‭
‭ // Matrix‬‭
to store the‬
result‬

// Input values for the first matrix‬



System‬
‭ .‭
‭o
‬ut‬
.‭
‭p
‬rintln‬
(‬
‭"Enter the elements of‬‭
‭ the first 3x3‬
matrix:"‬
‭ );‬

for‬‭
‭ (‬
int‬‭
‭ i‬‭
=‬‭
0‭
;
‬‬‭
i‬‭
<‬‭
3‬
;‬‭
‭ i‬
++‬
‭ ) {‬

for‬‭
‭ (‭
i
‬nt‬‭
j‬‭
=‬‭
0‭
;
‬‬‭
j‬‭
<‬‭
3‭
;
‬‬‭
j‬
++‬
‭ ) {‬

System‬
‭ .‬
‭out‬
‭ .‬
‭print‬
‭ (‭
‭"
‬Enter element ["‬‭
+‬‭
i‬‭
+‬‭
"]["‬‭
+‬‭
j‬‭
+‬
"]: "‬
‭ );‬

firstMatrix‬
‭ [‭
‭i
‬‬
][‬
‭ j‬
‭]‬‭
‭ =‬‭
inputScanner‬
.‭
‭n
‬extInt‬
();‬‭
‭ // Store‬
each element in the first matrix‬

}‬

}‬

// Input values for the second matrix‬



‭Java - Assignment 2‬

System‬
‭ .‭
‭o
‬ut‬
.‭
‭p
‬rintln‬
(‬
‭"Enter the elements of the second 3x3‬

matrix:"‬
‭ );‬

for‬‭
‭ (‬
int‬‭
‭ i‬‭
=‬‭
0‭
;
‬‬‭
i‬‭
<‬‭
3‬
;‬‭
‭ i‬
++‬
‭ ) {‬

for‬‭
‭ (‭
i
‬nt‬‭
j‬‭
=‬‭
0‭
;
‬‬‭
j‬‭
<‬‭
3‭
;
‬‬‭
j‬
++‬
‭ ) {‬

System‬
‭ .‬
‭out‬
‭ .‬
‭print‬
‭ (‭
‭"
‬Enter element ["‬‭
+‬‭
i‬‭
+‬‭
"]["‬‭
+‬‭
j‬‭
+‬
"]: "‬
‭ );‬

secondMatrix‬
‭ [‬
‭i‭
‭]
‬[‬
j‭
‭]
‬‬‭
=‬‭
inputScanner‬
.‬
‭nextInt‬
‭ ();‬‭
‭ //‬
Store each element in the second matrix‬

}‬

}‬

// Add the two matrices element by element‬



for‬‭
‭ (‬
int‬‭
‭ i‬‭
=‬‭
0‭
;
‬‬‭
i‬‭
<‬‭
3‬
;‬‭
‭ i‬
++‬
‭ ) {‬

for‬‭
‭ (‭
i
‬nt‬‭
j‬‭
=‬‭
0‭
;
‬‬‭
j‬‭
<‬‭
3‭
;
‬‬‭
j‬
++‬
‭ ) {‬

resultMatrix‬
‭ [‬
‭i‭
‭]
‬[‬
j‭
‭]
‬‬‭
=‬‭
firstMatrix‬
[‭
‭i
‬‬
][‬
‭ j‬
‭]‬‭
‭ +‬
secondMatrix‬
‭ [‬
‭i‭
‭]
‬[‬
j‭
‭]
‬;‬‭
// Add corresponding elements‬
}‬

}‬

// Output the result matrix‬



System‬
‭ .‭
‭o
‬ut‬
.‭
‭p
‬rintln‬
(‬
‭"‭
‭\
‬n‬
Result of adding the‬‭
‭ two matrices:"‬
);‬

for‬‭
‭ (‬
int‬‭
‭ i‬‭
=‬‭
0‭
;
‬‬‭
i‬‭
<‬‭
3‬
;‬‭
‭ i‬
++‬
‭ ) {‬

for‬‭
‭ (‭
i
‬nt‬‭
j‬‭
=‬‭
0‭
;
‬‬‭
j‬‭
<‬‭
3‭
;
‬‬‭
j‬
++‬
‭ ) {‬

System‬
‭ .‬
‭out‬
‭ .‬
‭print‬
‭ (‭
‭r
‬esultMatrix‬
[‬
‭i‭
‭]
‬[‬
j‬
‭]‬‭
‭ +‬‭
" "‬
);‬‭
‭ // Print‬
each element of the result matrix‬

}‬

System‬
‭ .‭
‭o
‬ut‬
.‬
‭println‬
‭ ();‬‭
‭ // New line after‬‭
each row‬
}‬

inputScanner‬
‭ .‭
‭c
‬lose‬
();‬‭
‭ // Close the scanner‬‭
to prevent memory‬
leaks‬

}‬

}‬

‭Java - Assignment 2‬

‭Results:‬

You might also like