Java Week7
Java Week7
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
2028_REC_OOPS using Java_Week 7_Q1
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : Coding
1. Problem Statement:
7
7
01
01
01
01
Rajiv is analyzing the energy consumption in his household and wants to
01
01
01
01
10
10
10
calculate the total cost based on the daily energy usage. He is given the 10
24
24
24
24
rate per unit of electricity and the energy consumed for multiple days. To
structure this calculation efficiently, he decides to use an interface-based
approach.
Formula
7
7
01
01
01
1
10
01
01
01
Energy Cost for one day = Energy Consumed per day * Rate Per Unit
10
10
10
10
24
24
24
24
17
7
Input Format
01
01
01
10
01
01
01
0
The first line of input consists of the rate per unit as an 'R' (a double value).
10
10
10
10
24
24
24
24
The second line of input consists of the number of days 'N' (an integer).
The third line of input consists of the daily energy consumption values for each
day 'D" (double values), separated by space.
Output Format
The first line of the output prints: "Day-wise Energy Cost:"
The next N lines of the output print the day-wise energy costs(double type) and
the total energy cost (double type) in Indian Rupees in the following format: "Day
7
17
17
01
01
10
0
[day_number]: Rs. [energy_cost]"
01
01
01
00
10
10
10
1
24
24
24
24
The last line of the output prints: "Total Energy Cost: Rs. [total_cost]"
Note: energy_cost and total_cost are rounded off to two decimal points
7
01
01
01
01
01
01
01
01
Sample Test Case
10
10
10
10
24
24
24
24
Input: 0.01
3
10.0 20.0 30.0
Output: Day-wise Energy Cost:
Day 1: Rs. 0.10
Day 2: Rs. 0.20
Day 3: Rs. 0.30
Total Energy Cost: Rs. 0.60
Answer
7
import [Link];
01
01
01
1
10
01
01
01
interface CostCalculator{
10
10
10
10
24
24
24
24
void getEnergyDetails(Scanner scanner);
17
7
01
01
01
void calculateAndDisplayCost();
10
01
01
01
0
}
10
10
10
10
24
24
24
24
class EnergyConsumptionTracker implements CostCalculator{
private double rate;
private int nDays;
private double[] dailyConsumption;
private double[] dailyCosts;
private double totalCost;
17
17
01
01
[Link] = new double[nDays];
10
0
01
01
01
00
10
10
1
}
24
24
24
24
@Override
public void getEnergyDetails(Scanner scanner){
for(int i=0; i<nDays; i++){
dailyConsumption[i] = [Link]();
}
}
@Override
public void calculateAndDisplayCost(){
7
7
01
01
01
01
double totalCost = 0;
01
01
01
01
double[] dailyCosts = new double[nDays];
10
10
10
24
24
24
dailyCosts[i] = dailyConsumption[i] * rate;
totalCost += dailyCosts[i];
}
7
01
01
01
}
10
01
01
01
}
10
10
10
10
24
24
24
24
class EnergyConsumptionApp {
17
7
01
01
01
public static void main(String[] args) {
10
01
01
01
0
10
10
10
24
24
24
24
double ratePerUnit = [Link]();
int numDays = [Link]();
[Link](scanner);
[Link]();
[Link]();
7
17
17
01
01
}
10
0
01
01
01
00
}
10
10
10
1
24
24
24
24
Status : Correct Marks : 10/10
7
7
01
01
01
01
01
01
01
01
10
10
10
10
24
24
24
24
7
7
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24
17 Rajalakshmi Engineering College
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
2028_REC_OOPS using Java_Week 7_Q2
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : Coding
1. Problem Statement
7
7
01
01
01
01
Jaheer is working on a health monitoring system to help individuals
01
01
01
01
10
10
10
calculate their Body Mass Index (BMI). He has implemented a basic BMI 10
24
24
24
24
calculator and an interface called HealthCalculator. It should have a
method called calculateBMI.
You are tasked with creating a program that takes weight and height as
input, calculates the BMI using the BMICalculator class, and displays the
result. If the height or weight is less than or equal to zero, then return -1.
Input Format
7
7
01
01
01
1
10
01
01
01
The first line of input consists of a double value W, the person's weight in
0
10
10
10
10
kilograms.
24
24
24
24
17
7
01
01
01
The second line consists of a double value H, the height of the person in meters.
10
01
01
01
0
10
10
10
10
Output Format
24
24
24
24
The output displays "BMI: " followed by a double value, representing the
calculated BMI, rounded off to two decimal places.
17
17
01
01
10
0
1.75
01
01
01
00
10
10
10
Output: BMI: 22.86
1
24
24
24
24
Answer
import [Link];
interface HealthCalculator{
double calculateBMI(double weight, double height);
}
@Override
7
7
01
01
01
01
public double calculateBMI(double weight, double height){
01
01
01
01
return (weight/(height*height));
10
10
10
} 10
24
24
24
24
}
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
7
01
01
01
1
10
01
01
0
10
10
10
10
24
24
24
24
24 24 24 24
10 10 10 10
}
01 01 01 0
}
01 01 01 10
7 7 7 17
Status : Correct
[Link]();
24 24 24 24
10 10 1 00 10
01 01 10 01
01 01 01
7 7 17 7
[Link]("BMI: %.2f\n", bmi);
24 24 24 24
10 10 10 10
01 01 01 01
01 01 01 01
7 7 7 7
24 24 24 24
10 10 10 10
Marks : 10/10
0 10 01 01 01
1 01 0 01
7 7 17 7
17 Rajalakshmi Engineering College
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
2028_REC_OOPS using Java_Week 7_Q3
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : Coding
1. Problem Statement
7
7
01
01
01
01
A financial analyst, Alex, needs a program to calculate simple interest for
01
01
01
01
10
10
10
24
24
24
in the principal amount, interest rate, and time in years and computes the
interest.
Input Format
7
The first line of input consists of the principal amount P as a double value.
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24
The second line of input consists of the annual interest rate r as a double value.
17
7
01
01
01
10
01
01
01
0
The third line of input consists of the number of years t as a positive integer,
10
10
10
10
which is an integer value.
24
24
24
24
Output Format
The output displays the calculated simple interest in the following format:
"Simple Interest: [interest_value]", Here, [interest_value] should be replaced with
the actual interest value calculated by the program.
17
17
01
01
10
0
01
01
01
Sample Test Case
00
10
10
10
1
24
24
24
24
Input: 1000.00
5.00
2
Output: Simple Interest: 100.0
Answer
import [Link];
interface InterestCalculator{
double simpleInterest(double principal, double rate, int time);
}
7
7
01
01
01
01
01
01
01
01
class SimpleInterestCalculator implements InterestCalculator{
10
10
10
10
24
24
24
24
@Override
public double simpleInterest(double principal, double rate, int time){
return ((principal*rate*time)/100.0);
}
}
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
7
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
17
7
01
01
01
int time = [Link]();
10
01
01
01
0
10
10
10
10
InterestCalculator calculator = new SimpleInterestCalculator();
24
24
24
24
double interest = [Link](principal, rate, time);
}
}
17
17
01
01
10
0
01
01
01
00
10
10
10
1
24
24
24
24
7
7
01
01
01
01
01
01
01
01
10
10
10
10
24
24
24
24
7
7
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24
17 Rajalakshmi Engineering College
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
2028_REC_OOPS using Java_Week 7_Q4
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : Coding
1. Problem Statement
7
7
01
01
01
01
Maria, a software developer, is working on an inventory management
01
01
01
01
10
10
10
24
24
24
store's products.
Each Product object should encapsulate the product's name, price, and
quantity and include a method to calculate its value as price × quantity.
7
7
01
01
01
1
10
01
01
0
10
10
10
10
24
24
24
17
7
Help Maria achieve the task.
01
01
01
10
01
01
01
0
10
10
10
10
Input Format
24
24
24
24
The first line of input consists of an integer to choose one of the following
options:
17
17
01
01
10
0
01
01
01
00
The next input line is the string representing the product name as a string (single
10
10
10
1
24
24
24
24
or multi-word, without quotes).
The next line is a double value representing the price as a decimal value
7
01
01
01
01
- For the addProduct command, the program should display "Product added to
01
01
01
01
10
10
10
10
inventory."
24
24
24
24
- For choice 2, the program should display "Total inventory value [totalvalue].
"The total value should be displayed with one decimal place. If there is no
product in the inventory, print the total as 0.0.
- For choice 3, the program should exit
If the choice is not 1, 2, or 3, then print "Invalid choice. Please select a valid
option (1/2/3).".
7
7
01
01
01
1
10
01
01
0
10
10
10
10
24
24
24
24
Sample Test Case
17
7
01
01
01
10
01
01
01
Input: 1
0
10
10
10
10
Laptop
24
24
24
24
800.0
3
2
5
3
Output: Product added to inventory.
Total inventory value: $2400.0
Invalid choice. Please select a valid option (1/2/3).
Answer
7
17
17
import [Link];
01
01
10
0
01
01
01
00
class Product{
10
10
10
1
24
24
24
24
String name;
double price;
int quantity;
7
01
01
01
01
return ([Link] * [Link]);
01
01
01
01
}
10
10
10
10
24
24
24
24
}
interface Inventory{
void addProduct(String name, double price, int quantity);
double calculateTotalValue();
}
7
01
01
01
1
10
01
01
10
10
10
24
24
24
24
[Link] = 0;
17
7
01
01
01
}
10
01
01
01
0
10
10
10
10
@Override
24
24
24
24
public void addProduct(String name, double price, int quantity){
if(productCount < [Link]){
Product newProduct = new Product(name, price, quantity);
products[productCount] = newProduct;
productCount++;
}
[Link]("Product added to inventory.");
}
@Override
7
17
17
01
01
public double calculateTotalValue(){
10
0
01
01
01
00
10
10
1
24
24
24
totalValue += products[i].getValue();
}
return totalValue;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Inventory inventory = new SimpleInventory(10);
7
7
while (true) {
01
01
01
01
01
01
01
01
int choice = [Link]();
10
10
10
if (choice == 1) {
10
24
24
24
24
[Link]();
String productName = [Link]();
double price = [Link]();
int quantity = [Link]();
[Link](productName, price, quantity);
} else if (choice == 2) {
double totalValue = [Link]();
[Link]("Total inventory value: $" + totalValue);
} else if (choice == 3) {
break;
7
} else {
01
01
01
1
10
01
01
01
10
10
10
(1/2/3).");
24
24
24
24
24 24 24 24
10 10 10 10
}
01 01 01 0
}
01 01 01 10
7 7 7 } 17
}
Status : Correct
[Link]();
24 24 24 24
10 10 1 00 10
01 01 10 01
01 01 01
7 7 17 7
24 24 24 24
10 10 10 10
01 01 01 01
01 01 01 01
7 7 7 7
24 24 24 24
10 10 10 10
Marks : 10/10
0 10 01 01 01
1 01 0 01
7 7 17 7
17 Rajalakshmi Engineering College
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
2028_REC_OOPS using Java_Week 7_Q5
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : Coding
1. Problem Statement
7
7
01
01
01
01
Raj is curious about how old he is in the current year.
01
01
01
01
10
10
10
10
24
24
24
24
He has asked you to create a simple program that calculates a person's
age based on their birth year. You decide to implement this functionality
using the AgeCalculator interface and the HumanAgeCalculator class.
Note: The current year is 2024. Calculate the current age by using the
formula: current year - birth year.
Input Format
The input consists of an integer representing the birth year.
7
7
01
01
01
1
10
Output Format
01
01
01
0
10
10
10
10
24
24
24
24
The output displays "You are X years old." where X is an integer representing the
17
7
01
01
01
calculated age based on the entered birth year.
10
01
01
01
0
10
10
10
10
24
24
24
24
Refer to the sample output for formatting specifications.
Sample Test Case
Input: 1934
Output: You are 90 years old.
Answer
import [Link];
7
17
17
01
01
10
0
01
01
01
interface AgeCalculator{
00
10
10
10
1
24
24
24
}
@Override
public int calculateAge(int birthYear){
return (2024-birthYear);
}
}
class AgeCalculatorApp {
7
7
01
01
01
01
public static void main(String[] args) {
01
01
01
01
10
10
10
10
Scanner scanner = new Scanner([Link]);
24
24
24
24
7
01
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24
17 Rajalakshmi Engineering College
7
01
01
01
10
01
01
01
0
10
10
10
10
Name: Arul Amudhan G Scan to verify results
24
24
24
24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7
17
17
01
01
10
0
2024_28_III_OOPS Using Java Lab
01
01
01
00
10
10
10
1
24
24
24
24
REC_2028_OOPS using Java_Week 7_MCQ
Attempt : 1
Total Mark : 15
Marks Obtained : 8
Section 1 : MCQ
7
01
01
01
01
Answer
01
01
01
01
10
10
10
24
24
24
Answer
They allow an interface to provide helper methods without requiring an
implementing class.
7
01
01
1
10
01
01
01
0
10
10
10
10
24
24
24
24
3. What is the output of the following code?
17
7
01
01
01
10
01
01
01
0
interface X {
10
10
10
10
24
24
24
24
default void show() {
[Link]("X's Default Method");
}
}
interface Y {
default void show() {
[Link]("Y's Default Method");
}
}
7
17
17
01
01
10
0
01
01
01
00
class Z implements X, Y {
10
10
10
1
24
24
24
24
public void show() {
[Link]("Z's Method");
}
}
7
01
01
01
01
}
01
01
01
01
10
10
10
Answer 10
24
24
24
24
Compilation Error
Status : Wrong Marks : 0/1
interface A {
default void show() {
[Link]("A's Default Method");
7
7
01
01
01
}
10
01
01
01
}
10
10
10
10
24
24
24
24
17
7
01
01
01
interface B {
10
01
01
01
0
10
10
10
24
24
24
24
[Link]("B's Default Method");
}
}
class C implements A, B {
public void show() {
[Link]();
}
}
7
17
17
01
01
public class Main {
10
0
01
01
01
00
10
10
1
24
24
24
24
C obj = new C();
[Link]();
}
}
Answer
Compilation Error
Status : Wrong Marks : 0/1
7
7
01
01
01
01
5. How do you call a static method from an interface MyInterface?
01
01
01
01
10
10
10
10
24
24
24
24
Answer
[Link](); (where obj is an instance of a class implementing
MyInterface)
Status : Wrong Marks : 0/1
Answer
01
01
01
1
10
01
01
01
10
10
10
24
24
24
24
Status : Correct Marks : 1/1
17
7
01
01
01
10
01
01
01
0
10
10
10
10
7. What happens when an implementing class does not override a default
24
24
24
24
method from an interface?
Answer
The class will not compile.
Status : Wrong Marks : 0/1
17
17
01
01
10
0
01
01
01
00
10
10
10
Answer
1
24
24
24
24
Using [Link]();
Status : Wrong Marks : 0/1
Answer
Default methods cannot have a body in the interface.
7
7
01
01
01
01
01
01
01
01
Status : Wrong Marks : 0/1
10
10
10
10
24
24
24
24
interface A {
default void show() {
[Link]("A's Default Method");
}
}
class B {
7
7
01
01
01
1
10
01
01
0
10
10
10
10
24
24
24
24
[Link]("B's Method");
17
7
01
01
01
}
10
01
01
01
0
}
10
10
10
10
24
24
24
24
class C extends B implements A {
}
17
17
01
01
10
0
01
01
01
Answer
00
10
10
10
1
24
24
24
24
Compilation Error
Status : Wrong Marks : 0/1
interface MathOperations {
static int square(int x) {
return x * x;
}
7
7
01
01
01
01
}
01
01
01
01
10
10
10
10
24
24
24
24
public class Main {
public static void main(String[] args) {
[Link]([Link](5));
}
}
Answer
25
Status : Correct Marks : 1/1
7
7
01
01
01
1
10
01
01
01
10
10
10
24
24
24
24
both having a method with the same name. Which method gets called?
17
7
01
01
01
10
01
01
01
0
Answer
10
10
10
10
24
24
24
24
The method from the superclass
Status : Correct Marks : 1/1
13. Can a Java interface contain both default and static methods?
Answer
Yes, an interface can have both default and static methods.
Status : Correct Marks : 1/1
7
17
17
01
01
10
0
01
01
01
00
10
10
10
1
24
24
24
24
14. Which of the following is the correct way to declare an interface in
Java?
Answer
interface Vehicle { void start();}
Status : Correct Marks : 1/1
7
01
01
01
01
01
01
01
01
interface A {
10
10
10
24
24
24
[Link]("Static method in A");
}
}
class B implements A {
static void display() {
[Link]("Static method in B");
}
}
7
7
01
01
01
1
10
01
01
01
10
10
10
24
24
24
24
24 24 24 24
10 10 10 10
}
01 01 01 0
}
01 01 01 10
7 7 7 17
Answer
Status : Correct
[Link]();
Static method in B
24 24 24 24
10 10 1 00 10
01 01 10 01
01 01 01
7 7 17 7
public static void main(String[] args) {
24 24 24 24
10 10 10 10
01 01 01 01
01 01 01 01
7 7 7 7
24 24 24 24
10 10 10 10
Marks : 1/1
0 10 01 01 01
1 01 0 01
7 7 17 7