BCSL-043 Solved Assignment 2024-25 | learningscience.co.
in 1
Course Code : BCSL-043
Title : Java Programming Lab
Assignment Number : BCA(IV)/L-043/Assignment/2024-25
Maximum Marks : 50
Last date of Submission : 31st October, 2024 (For July Session) 30th April, 2025 (For January Session)
Note:
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.
Q1: (a) Write java program to find the simple interest on a savings account. Define appropriate
class, constructor and methods in your program. Make necessary assumptions. (10 Marks)
Solution:
public class SavingsAccount {
private double principal;
private double rateOfInterest;
private int time;
// This is the constructor to set the initial values for the account
public SavingsAccount(double principal, double rateOfInterest, int time) {
this.principal = principal;
this.rateOfInterest = rateOfInterest;
this.time = time;
}
// This method calculates the simple interest using the formula
public double calculateSimpleInterest() {
return (principal * rateOfInterest * time) / 100;
}
public static void main(String[] args) {
// Let's assume the principal is 10000, interest rate is 5%, and time is 3 years
SavingsAccount account = new SavingsAccount(10000, 5, 3);
// Now, we calculate the simple interest
double simpleInterest = account.calculateSimpleInterest();
// Finally, we print the result
BCSL-043 Solved Assignment 2024-25 | learningscience.co.in 2
System.out.println("Simple Interest: " + simpleInterest);
}
}
So, first, we have a SavingsAccount class to represent our savings account. It has fields
for principal, rateOfInterest, and time, which are set when we create a new account using the
constructor. The calculateSimpleInterest method calculates the interest using the standard formula.
In the main method, we use a Scanner to get the principal, rate of interest (as a percentage), and
time from the user. Then, we create a SavingsAccount object with those values. Finally, we calculate
the simple interest and print the result.
I've made the assumptions that the user will enter the interest rate as a percentage and the time in
years.
Screenshots:
BCSL-043 Solved Assignment 2024-25 | learningscience.co.in 3
Q1: (b) Write a java program to print first 50 fibonacci numbers . Define appropriate class,
constructor and methods in your program. (10 Marks)
Solution:
Here is my program to print the first 50 Fibonacci numbers! I made sure to use a class, constructor,
and methods to keep it organized.
BCSL-043 Solved Assignment 2024-25 | learningscience.co.in 4
public class Fibonacci {
private int n;
// Constructor to set the number of Fibonacci numbers to be generated
public Fibonacci(int n) {
this.n = n;
}
// Method to generate and print the Fibonacci sequence
public void generateFibonacci() {
int first = 0;
int second = 1;
System.out.print("First " + n + " Fibonacci numbers: ");
System.out.print(first + " " + second + " ");
for (int i = 2; i < n; i++) {
int next = first + second;
System.out.print(next + " ");
first = second;
second = next;
}
System.out.println();
}
public static void main(String[] args) {
// Creating a Fibonacci object to generate the first 50 numbers
Fibonacci fibonacci = new Fibonacci(50);
fibonacci.generateFibonacci();
}
}
So, I created a Fibonacci class to hold everything together. It has a n field to store how many
Fibonacci numbers we want to generate.
The constructor just sets the value of n.
The generateFibonacci method does the actual work. It starts with the first two numbers (0 and 1),
then uses a loop to calculate the rest. Inside the loop, I find the next number by adding the previous
two, and then we print it. I also update the first and second variables to be ready for the next
iteration.
In the main method, I create a Fibonacci object with n set to 50 and call the generateFibonacci
method to get the output.
Screenshots:
BCSL-043 Solved Assignment 2024-25 | learningscience.co.in 5
Q2: (a) Write a program to demonstrate multilevel inheritance implementation. Make suitable
provisions of exceptions handling in your program. (10 Marks)
Solution:
class Animal {
public void eat() {
BCSL-043 Solved Assignment 2024-25 | learningscience.co.in 6
System.out.println("Animal is eating.");
}
}
class Mammal extends Animal {
public void walk() {
System.out.println("Mammal is walking.");
}
}
class Dog extends Mammal {
public void bark() {
System.out.println("Dog is barking.");
}
}
public class MultilevelInheritance {
public static void main(String[] args) {
try {
Dog dog = new Dog();
// Accessing methods from all levels of inheritance
dog.eat(); // From Animal
dog.walk(); // From Mammal
dog.bark(); // From Dog
} catch (Exception e) {
System.out.println("Oops! Something went wrong: " + e.getMessage());
}
}
}
So, I've created three classes: Animal, Mammal, and Dog. Mammal inherits from Animal, and Dog
inherits from Mammal. This is multilevel inheritance, because we have a chain of classes inheriting
from each other.
The Animal class has a simple eat method. Mammal adds a walk method, and Dog adds a bark
method.
In the main method, I created a Dog object. Because of inheritance, this Dog object can also use the
eat and walk methods from its parent classes.
I also added a try-catch block to handle exceptions. If something goes wrong, like an error in the
code, the catch block will catch it and print an error message.
Screenshots:
BCSL-043 Solved Assignment 2024-25 | learningscience.co.in 7
BCSL-043 Solved Assignment 2024-25 | learningscience.co.in 8
Q2: (b) Create an applet which take a number as input and display whether the given number is
even or odd. If the input number is less than 1 then ask user to re-enter the input. Use appropriate
components, layout and formatting in your program. (10 Marks)
Solution:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class EvenOddApplet extends Applet implements ActionListener {
TextField inputField;
Label resultLabel;
public void init() {
// Setting up the layout
setLayout(new FlowLayout());
// Creating the input field
inputField = new TextField(10);
add(inputField);
// Creating the button
Button checkButton = new Button("Check");
checkButton.addActionListener(this);
add(checkButton);
// Creating the result label
resultLabel = new Label("");
add(resultLabel);
}
// Handling the button click event
public void actionPerformed(ActionEvent e) {
BCSL-043 Solved Assignment 2024-25 | learningscience.co.in 9
try {
// Getting the input number
int number = Integer.parseInt(inputField.getText());
// Checking if the number is valid
if (number < 1) {
resultLabel.setText("Please enter a number greater than or equal to 1.");
} else {
// Checking if the number is even or odd
if (number % 2 == 0) {
resultLabel.setText(number + " is even.");
} else {
resultLabel.setText(number + " is odd.");
}
}
} catch (NumberFormatException ex) {
// Handling incorrect input
resultLabel.setText("Invalid input. Please enter a valid number.");
}
}
}
Screenshots:
BCSL-043 Solved Assignment 2024-25 | learningscience.co.in 10
BCSL-043 Solved Assignment 2024-25 | learningscience.co.in 11
BCSL-043 Solved Assignment 2024-25 | learningscience.co.in 12
BCSL-043 Solved Assignment 2024-25 | learningscience.co.in 13