Course Code
Title
Assignment Number
Maximum Marks
Last date of Submi
BCSL-043
Java Programming Lab
BCA(IV)/L-043/Assignment/2023-24
50
: 31° October, 2023 (For Jul
es
mn)
30" April, 2024 (For January Session)
nment has two questions. Answer all the questions. These questions carry 40
marks. Rest 10 marks are for viva voce. You are advised to give proper comments and do
proper alignments while writing program. Please go through the guidelines regarding the
assignments given in the programme gi
Qn @
(b)
2 @
(b)
Je for the format of presentation.
Write java program to find sum of two matrices. Define appropriate class, constructor
and methods in your program. Make necessary assumptions. ao
Write a java program which take age ( in years) of two persons as input and display
who is older among them. Make provisions for exception handling in the situation
when age entered is either a negative value or more than 150. (0)
Write a program in java program which read the given text file and display its
contents on console. Make suitable provisions of exceptions handling in your
program (oy
Create an applet which take a number between 1-50 as input and display its table
Use appropriate components, layout and formatting in your program. ao
BCopyright with Kunj Publication only Not for resale Ph. 8006184581 (Call Us)
Course Code: BCSL-043
Course Title: Java Programming Lab
Assignment Number: BCA (IV)/ L-043/Assignment/2023-24
DistaimetSpecal Hote: These area the sample of he Answerit sme ofthe Question pen in ihe Asiguments. These
Sample anurans are pepe by Prive Teacher Aatar fore epee gldance othe denn et de of
Ite cam aner the Questions sven he Assignments We donc 10 cca of hse sample avers as hse re buted On Re
Inowledge and capably of rate Teacher ator Semele anoners maybe ton the Cale forte reference fo prepare he
‘oovers of he question gen in theese. As es souion and ensers are prepared by he private each othe cancer
‘feror or mist canna be dene Any Omission or Eror is ih regret owph eer cae as heen hen whe preparing tee
‘Sample Answer Soluions, Please cons our own TeacherTaor efor sox pepe pariaarAnawer an for nde an es)
information, dos an slo. Stadetshudd mus a and fer the oficial dh material provided by th aniverity.
‘This assignment has two questions. Answer all the questions. These questions
carry 40 marks. Rest 10 marks are for viva voce. You are advised to give proper
‘comments and do proper alignments while writing program. Please go through
the guidelines regarding the assignments given in the programme guide for the
format of presentation,
QL. (a) Write java program to-find sumof t trices. Define appropriate
class, constructor-andmethods in your program. Make necessary assumptions.
Ans:
KUNI PUBLICATION
ALL US:- 8006184581
private int rows;
private int columns;
J/ Constructor to initialize the matrix with given rows and columns
public Matrix(int rows, int columns) {
this.rows = rows;
this.columns = columns;
this.matrix = new int{rows][columns];Copyright with Kunj Publication only Not for resale Ph. 8006184581 (Call Us)
// Method to set the elements of the matrix
public void setMatrix(int{]{] data) {
if (data.length == rows && data[0].length == columns) {
this.matrix = data;
J else {
System.out.printin("Invalid matrix dimensions!");
result. matrixfi][j] = thi fill) + Other matrixfijo OO 481
return result;
else {
System.out.println("Matrix dimensions do not match!");
return null;
// Method to display the matrix
public void display() {
for (int[] row : matrix) (Copyright with Kunj Publication only Not for resale Ph. 8006184581 (Call Us)
System.out printIn(Arrays.toString(row));
public class MatrixAddition {
public static void main(String[] args) {
int{][] datal = { {1,2}, (3,4) };
int{][] data2 = { {5,6}, (7,8) }:
+PUBLICATION
System. out.printin("Matrix 12" ALL US:- 8006184581
‘System.out printin("Matrix 2:");
matrix2.display();
Matrix sumMatrix = matrix! add(matrix2);
if (sumMatrix != null) {
System.out.printin(”Sum of matrices:");
sumMatrix.display();Copyright with Kunj Publication only Not for resale Ph. 8006184581 (Call Us)
}
Assumptions made in the program:
» The matrices are assumed to be 2-dimensional arrays of integers.
. The dimensions of the matrices are fixed and known at the time of object
creation.
The matrices provided for addition have the same dimensions.
The display method is used to show the contents of the matrices.
If the dimensions of the matrices do not match during addition, an error
message is displayed, and the result is set to null.
(b) Write a java program which take age (in years) of two persons as input and
display who is older among them. Make provisions for exception handling in the
situation when age entered is either a negative value or more than 150.
Ans:
Here’s a Java program that takes the age of two persons as input and displays who is
older among them. ItalSo in¢ludes provisions for exception handling to deal with
negative values of ages above 150.
import java.util Scanner:
public class AgeComparison {
Public static void main(String{] args) {
try {
‘Scanner seannét = new Scanner(System.in);
System.out.print("Enter age of person 1: ");
int agePerson] = scanner.nextInt();
checkValidA ge(agePerson!);
System.out.print("Enter age of person 2: ");
int agePerson2 = scanner.nextInt();
checkValidA ge(agePerson2);
if (agePersonl > agePerson2) {Copyright with Kunj Publication only Not for resale Ph. 8006184581 (Call Us)
System.out.printin("Person 1 is older.”");
} else if (agePerson1 < agePerson2) {
System.out.println("Person 2 is older.");
J else {
System.out.printin("Both persons are of the same age.
} catch (Exception e) (
System.out printIn("‘Invalid input. Age should be a positive value less than or
‘equal to 150.");
}
private static void checkValidAge(int age) throws Exception {
if (age < O|jage > 150) {
throw new Exception();
In this program, we use the Scanner class to take input from the user for the ages of
the two persons. The check ValidAge method is used to validate if the age is within
the valid range (between 0 and 150, inclusive). If the input is invalid, an Exception is
thrown, and the program catches the exception to display an appropriate error
message. Otherwise, it compares the ages of the two persons and prints the result
accordingly.
Q2. (a) Write a program in java program which read the given text file and
display its contents on console. Make suitable provisions of exceptions handling
in your program.
Ans:
Java program that reads the contents of a given text file and displays them on the
console, The program handles exceptions using try-catch blocks to ensure proper error
handling.Copyright with Kunj Publication only Not for resale Ph, 8006184581 (Call Us)
import java.io.BufferedReader;
import java.io FileReader;
import java.io. IOException;
public class ReadTextFile {
public static void main(String[] args) {
// Provide the file path here
String filePath = "path/to/your/textfile.txt";
try {
(new FileReader(filePath));
UNLPUBLIGATION
System. out.printin(line); ALL US:- 8006184581
1 Close the BufferedReader after reading
reader.close();
} catch (IOException e) {
I/1f any exception occurs, display an error message
System.out.printIn("Error reading the file: " + e.getMessage());Copyright with Kunj Publication only Not for resale Ph. 8006184581 (Call Us)
Before running the program, make sure to replace “path/to/your/texttfile.txt" with
the actual file path of the text file you want to read. The program uses a
BufferedReader to read the file line by line, and it displays each line on the console. If
an exception occurs while reading the file, it will catch the IOException and display
an error message.
Remember to compile and run this program in a Java environment to see the output on
the console.
(b) Create an applet which take a number between 1-50 as input and display
table. Use appropriate components, layout and formatting in your program.
Ans:
Creating a complete applet requires a development environment and access to a web
browser with support for Java applets. However, Java applets have been deprecated in
most modern browsers due to security concerns. Therefore, I'll provide you with a
Java Swing application code instead, Which youlean run on your local machine using a
Java development environment like Eclipse, IntelliJ, or NetBeans.
Below is a Java Swing application that takes a number between 1 and 50 as input and
displays its table:
import javax.swing.*;
import java.awt.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class NumberTableApp extends JFrame implements ActionListener {
private JTextField inputField;
private JTextArea resultArea;
public NumberTableApp() {
setTitle("Number Table App");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);Copyright with Kunj Publication only Not for resale Ph. 8006184581 (Call Us)
ILabel label = new JLabel("Enter a number between | and 50:");
inputField = new JTextField(10);
JButton showButton = new JButton("Show Table");
showButton.addActionListener(this);
resultArea = new JTextArea();
resultArea setEditable(false);
JPanel inputPanel = new JPanel();
inputPanel.add(label);
inputPanel.add(inputFiel
inputPanel add
UNTPUBLICATION
add(inputPanel, BorderLayo ALL US:- 8006184581
\dd(scrollPane, Bors
@Override
public void actionPerformed(ActionEvent e) {
if (e. getActionCommand().equals("Show Table")) {
uy [
int number = Integer.parselnt(inputField.getText());
if (number >= 1 && number <= 50) {
generateTable(number);
J else {
resultArea.setText("Please enter a valid number between | and 50.");Copyright with Kunj Publication only Not for resale Ph. 8006184581 (Call Us)
}
} catch (NumberFormatException ex) {
resultArea.setText("Please enter a valid number.");
private void generateTable(int number) {
StringBuilder sb = new StringBuilder;
for (int i= 1; i <= 10; i++) {
int product = number *
pape” x
it PHBLICATION
ALL US:- 8006184581
iblic static void main(Stri
umberTableApp app = new NumberTableApp();
app.setVisible(true);
Please note that running this code will require you to set up a Java development
environment, and it will run as a standalone Java application rather than a web-based
applet. You can copy the provided code into your favorite Java IDE, compile, and run
the application. When you input a number between I and 50 and click the "Show
Table” button, it will display the multiplication table for that number in the JTextArea.