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

Java Programming: Arrays, Matrices, and Threads

The document contains Java programming exercises focusing on packages, arrays, matrix operations, multithreading, exception handling, and GUI programming. It includes code snippets for creating packages, summing arrays, adding and multiplying matrices, implementing multithreading, handling exceptions, and designing a GUI for calculating factorials. Each section provides a practical example of Java concepts and their implementations.

Uploaded by

vishpawar784
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)
9 views9 pages

Java Programming: Arrays, Matrices, and Threads

The document contains Java programming exercises focusing on packages, arrays, matrix operations, multithreading, exception handling, and GUI programming. It includes code snippets for creating packages, summing arrays, adding and multiplying matrices, implementing multithreading, handling exceptions, and designing a GUI for calculating factorials. Each section provides a practical example of Java concepts and their implementations.

Uploaded by

vishpawar784
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

Problemsheet

[F]PackagesandArrays
[Link],Addthenecessaryclassesandimportthepackagein
java class.
packagepack;
publicclassArrayOperations{
//Methodtofindthesumofelementsinanarray
publicintsum(int[]a
rr){ int total
= 0;
for(intnum:arr)
{ total +=
num;
}
returntotal;
}
[Link]
ons; public class Q22 {

publicstaticvoidmain(String[]args){
// Create an instance of the ArrayOperations
class
ArrayOperationsoperations=newArrayOperations()
;
//Defineanarrayofintegers
int[]numbers={10,20,30,40,50};
//
Calculatethesumofthearrayelements
int result =
[Link](numbers);
//Printtheresult
[Link]("Sumofthearrayelementsis:"+result);
}}

[Link].
package pack;
[Link].S
canner; public
class Q23 {
publicstaticvoidmain(String[]args){
Scannersc=newScanner([Link]);
// Taking user input for matrix size
[Link]("Enternumberofrowsandcolumns:
"); int rows = [Link]();
intcols= [Link]();
//Declaringmatrices
int[][]matrix1=newint[rows]
[cols]; int[]
[]matrix2=newint[rows]
[cols]; int[][] sum = new
int[rows][cols];
// Taking input for first matrix
[Link]("Enterelementsoffirstmatrix
:"); for (int i = 0; i < rows; i++)
for (int j = 0; j < cols;
j++) matrix1[i]
[j]=[Link]();
//Addingthetwomatrices
for(inti=0;i<rows;i++)
for(intj=0;j<cols;j++)
sum[i][j]=matrix1[i][j]+matrix2[i][j];
// Printing the resultant
matrix
[Link]("Sumofmatric
es:"); for (int i = 0; i <
rows; i++) {
for (int j = 0; j < cols; j++)
[Link](sum[i][j]+"");

[Link]();
}
[Link]();
}
}

24. Writeajavaprogramformultiplyingtwomatricesandprintthep
roduct for the same.
packagepack;
[Link].S
canner; public
class Q24 {
publicstaticvoidmain(String[]args){
Scannersc=newScanner([Link]);
//Takinguserinputformatrix size
[Link]("Enterrowsandcolumnsoffirstmatrix:")
; int r1 = [Link]();
intc1= [Link]();
[Link]("Enterrowsandcolumnsofsecondmatrix:"
); int r2 = [Link]();
intc2= [Link]();
//
Conditionformatrixmultiplica
tion if (c1 != r2) {
[Link]("Matrixmultiplicationnotpossible(Columnsoffirs
t must match rows of second).");
return;
}
//Declaringmatrices
int[]
[]matrix1=newint[r1]
[c1]; int[]
[]matrix2=newint[r2]
[c2]; int[]
[]product=newint[r1]
[c2];
// Taking input for first matrix
[Link]("Enterelementsoffirstmatrix
:"); for (int i = 0; i < r1; i++)
for (int j = 0; j < c1; j+
+) matrix1[i]
[j]=[Link]();
// Taking input for second matrix
[Link]("Enterelementsofsecondmatri
x:"); for (int i = 0; i < r2; i++)
for (int j = 0; j < c2; j+
+) matrix2[i]
[j]=[Link]();
//Multiplyingmatrices
for(inti=0;i<r1;i++)
for(intj=0;j<c2;j++)
for(intk=0;k<c1;k++)
product[i][j]+=matrix1[i][k]*matrix2[k][j];
// Printing the resultant matrix
[Link]("Productofmatri
ces:"); for (int i = 0; i < r1; i+
+) {
for(intj=0;j<c2;j++)
[Link](product[i][j]+"");
[Link]();
}
[Link]();
}
}

[g]MultithreadingandExceptionHandling
[Link].
packagepack;
classQ25extendsTh
read{ public
void run() {
for (int i = 1; i <= 5; i++)
{ [Link]([Link]().getName()+"-
Count:"+i);
try{
[Link](500);//Sleepfor500ms
}catch(InterruptedException
e){
[Link](e);
}}}
publicstaticvoidmain(String[]arg
s){ Q25 t1 = new Q25();

Q25t2=new Q25();
[Link]("Thread1");
[Link]("Thr
ead2");
[Link]();
[Link]();
}}

[Link].
packagepack;
[Link]
.Scanner; class
Q26 {
publicstaticvoidmain(String[]a
rgs)
{ Scannersc=newScanner(Sys
[Link]); try {
[Link]("Enter
numerator: "); int num =
[Link]();
[Link]("Enterdenomin
ator:"); int den =
[Link]();
int result = num / den;
[Link]("Result:"+resu
lt);
}catch(ArithmeticExceptione){
[Link]("Error:Divisionbyzeroisnotallowed.");
} catch (Exception e)
{ [Link]("Error:Invalid
input.");
}finally{
[Link]();
[Link]("Programexecutioncompleted.");}}}
[h]GUIProgramming.
[Link].
import
[Link].*;
[Link]
ent.*; public
class Q27 {
publicstaticvoidmain(String[]args){
//Createtheframe
JFrameframe=newJFrame("FactorialCalculator");
//Createcomponents
JLabelinputLabel=newJLabel("Enteranumber:");
JTextField inputField = new JTextField(10);
JButtoncalculateButton=newJButton("CalculateFactorial
"); JLabel resultLabel = new JLabel("Factorial: ");
//
Layoutmanager(forplacingcomponentsintheframe)
JPanel panel = new JPanel();
[Link](inputLabel);
[Link](inputField);
[Link](calculateButton);
[Link](resultLabel);
// Action listener for button click
[Link](newActionListener(){
publicvoidactionPerformed(ActionEven
te){ try {
//Getnumberfromtextfieldandcalculatefactorial
int num = [Link]([Link]());
long factorial = calculateFactorial(num);
[Link]("Factorial:"+factorial);
} catch (NumberFormatException ex)
{ [Link]("Pleaseenteravalidnumbe
r.");

}}});
// Set up the
frame
[Link](3
00,150);
[Link](JFrame.EXIT_ON_C
LOSE); [Link](panel);
[Link](true);}
//Methodtocalculatefactorial
publicstaticlongcalculateFactorial(intnum
){ long fact = 1;
for(inti=1;i<=num;i++)
{ fact *= i;
}
returnfact;
}}

You might also like