Java Method Overloading Examples
Java Method Overloading Examples
Name: Somesh
Email: somesh.24scse1180003@[Link]
Section: Section-37
)
.in
ac
1. Problem Statement:Write a program to implement method
ty.
rsi
overloading in which a class provides multiple calculate methods
ive
that handle addition of two integers, two floating-point numbers,
n
and concatenation of two strings. su
tia
lgo
overloaded methods named calculate. The main method should take user input,
3@
determine the type of data entered, and call the appropriate method dynamically.
00
Input Format:First line: Data type (int, float, or string)Next two lines: Values of that
80
type
1
e1
Output Format:
cs
4s
Sample Input:int1020
m
so
Sample Output:30
h(
es
m
Concepts Included:
GU 28 3rd Sem Java Prog. Pre-Midterm
Source Code:
import [Link];
class Calculator {
int calculate(int a, int b) {
return a + b;
}
)
.in
ac
if (![Link]()) {
ty.
[Link]();
rsi
return;
ive
}
n
su
tia
String type = [Link]().trim().toLowerCase(); // reads the data type token (handles
lgo
[Link]();
3@
00
switch (type) {
80
case "int":
1
e1
case "integer": {
cs
// read two integer tokens (works if they are on same or different lines)
4s
if ([Link]()) {
m
}
h(
if ([Link]()) {
es
}
So
// if not both read from that line, read remaining tokens from sc
if (a == null || b == null) {
if (a == null && [Link]()) a = [Link]();
else if (a == null && [Link]()) {
try { a = [Link]([Link]()); } catch (Exception e) {}
}
if (b == null && [Link]()) b = [Link]();
else if (b == null && [Link]()) {
try { b = [Link]([Link]()); } catch (Exception e) {}
}
}
[Link]();
if (a != null && b != null) {
[Link]([Link](a, b));
}
break;
}
case "float":
case "double": {
// read two numeric tokens (double covers float and double inputs)
Scanner lineScanner = new Scanner([Link]().trim());
Double a = null, b = null;
if ([Link]()) {
try { a = [Link]([Link]()); } catch (Exception e) { a = null; }
}
if ([Link]()) {
try { b = [Link]([Link]()); } catch (Exception e) { b = null; }
}
if (a == null || b == null) {
if (a == null && [Link]()) {
)
.in
try { a = [Link]([Link]()); } catch (Exception e) {}
ac
}
ty.
if (b == null && [Link]()) {
rsi
try { b = [Link]([Link]()); } catch (Exception e) {}
ive
}
n
}
su
tia
[Link]();
lgo
if (res == [Link](res)) {
00
[Link]((long)[Link](res));
80
} else {
1
e1
[Link](res);
cs
}
4s
}
h.2
break;
es
}
m
so
case "string":
h(
case "str": {
es
// If the first line is empty (could be because type was on its own line), keep reading
if ([Link]() && [Link]()) s1 = [Link]();
String s2 = [Link]() ? [Link]() : "";
if ([Link]() && [Link]()) s2 = [Link]();
// fallback to tokens if lines are empty
if ((s1 == null || [Link]() == 0) && [Link]()) s1 = [Link]();
if ((s2 == null || [Link]() == 0) && [Link]()) s2 = [Link]();
[Link]([Link](s1 == null ? "" : s1, s2 == null ? "" : s2));
break;
}
default:
[Link]();
}
}
Compilation Details:
TestCase1:
Input:
)
< hidden >
.in
ac
Expected Output:
ty.
rsi
< hidden >
n ive
Output: su
tia
lgo
19.8
ga
Execution Time:
801
0.089s
e1
cs
4s
TestCase2:
h.2
Input:
es
m
so
Expected Output:
es
m
So
Output:
HelloWorld
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
Output:
-1
TestCase4:
)
.in
ac
Input:
ty.
rsi
< hidden >
ive
Expected Output:
n
su
tia
< hidden >
lgo
Output:
ga
3@
OpenAI
00
80
Execution Time:
cs
4s
0.094s
h.2
es
m
so
money using either int or double. Compute the total balance after deposits.
So
Input Format:
Output Format:
Sample Input:
500031000 2000 500
Sample Output:
Total Balance: 8500
Concepts Included:
GU 28 3rd Sem Java Prog. Pre-Midterm
Source Code:
)
.in
import [Link].*;
ac
class BankAccount {
ty.
double balance;
rsi
BankAccount(double initialBalance) {
ive
[Link] = initialBalance;
n
} su
tia
lgo
balance += amount;
3@
}
00
80
balance += amount;
e1
}
cs
double getBalance() {
4s
return balance;
h.2
}
es
}
m
int n = [Link]();
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
)
.in
ac
Total Balance: 2000
ty.
rsi
Compilation Status: Passed
ive
Execution Time:
n
su
tia
0.12s
lgo
ga
TestCase2:
3@
00
Input:
801
Expected Output:
4s
h.2
Output:
so
h(
Execution Time:
0.126s
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Total Balance: 1000
)
.in
Another should accept base salary, bonus, and deductions.
ac
ty.
Description:Use method overloading to calculate salary with different parameters.
rsi
ive
Input Format:
n
First line: base salary su
tia
lgo
Sample Input:
cs
4s
3000050002000
h.2
es
Sample Output:
m
so
h(
33000
es
m
So
Concepts Included:
GU 28 3rd Sem Java Prog. Pre-Midterm
Source Code:
import [Link].*;
class Employee {
)
.in
[Link]([Link]());
ac
}
ty.
[Link]();
rsi
double result = 0.0;
n ive
if ([Link]() == 1) {
su
tia
result = [Link]([Link](0));
lgo
} else if ([Link]() == 2) {
result = [Link]([Link](0), [Link](1));
ga
}
801
e1
if (result == [Link](result)) {
cs
[Link]((int) result);
4s
} else {
h.2
[Link](result);
es
}
m
}
so
}
h(
es
m
Compilation Details:
So
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
55000
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
30000
)
.in
ac
Compilation Status: Passed
ty.
rsi
Execution Time:
n ive
0.101s
su
tia
lgo
TestCase3:
ga
Input:
3@
00
Expected Output:
e1
cs
Output:
es
m
8000
so
h(
Execution Time:
So
0.105s
TestCase4:
Input:
< hidden >
Expected Output:
< hidden >
Output:
4500
Compilation Status: Passed
Execution Time:
0.122s
4. Volume Finder
Problem Statement:Write a program using method overloading
1. Cube (side)
)
3. Cylinder (radius, height)
.in
ac
Input Format:User chooses shape and provides dimensions.
ty.
rsi
Output Format:Volume of the chosen shape.
n ive
Sample Input:cylinder3 7
su
tia
lgo
Sample Output:
ga
3@
197.92
00
80
Concepts Included:
h.2
Source Code:
So
import [Link].*;
class VolumeFinder {
switch (shape) {
case "cube": {
double side = [Link]();
result = [Link](side);
break;
}
case "cuboid": {
double l = [Link]();
)
.in
double b = [Link]();
ac
double h = [Link]();
ty.
result = [Link](l, b, h);
rsi
break;
ive
}
n
case "cylinder": {
su
tia
double r = [Link]();
lgo
double h = [Link]();
result = [Link](r, h, true);
ga
break;
3@
}
00
default:
80
[Link]("Invalid shape");
1
e1
[Link]();
cs
return;
4s
}
h.2
es
[Link]("%.2f%n", result);
m
[Link]();
so
}
h(
}
es
m
So
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
125.00
Compilation Status: Passed
Execution Time:
0.108s
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
)
.in
ac
24.00
ty.
rsi
Compilation Status: Passed
n ive
Execution Time:
su
tia
0.107s
lgo
ga
TestCase3:
3@
00
Input:
801
Expected Output:
4s
h.2
Output:
so
h(
785.40
es
m
Execution Time:
0.11s
TestCase4:
Input:
< hidden >
Expected Output:
< hidden >
Output:
15.63
)
.in
methods: deposit(amount) Adds money to balance. withdraw(amount) Deducts
ac
money if sufficient balance exists. calculateInterest() Returns interest based on
ty.
current balance and static interest rate.4. After deposit and withdrawal, calculate
rsi
interest and display the final balance and interest.
n ive
Input FormatInitial deposit amountWithdrawal amount
su
tia
Output FormatBalanceInterest
lgo
ga
Sample Input50002000
3@
Sample Output3000.0150
00
801
e1
Concepts Included:
es
m
Source Code:
import [Link].*;
class BankAccount {
double balance;
static double interestRate = 5.0;
BankAccount(double initialDeposit) {
balance = initialDeposit;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) balance -= amount;
}
)
.in
}
ac
}
ty.
rsi
Compilation Details:
n ive
su
tia
TestCase1:
lgo
Input:
ga
3@
Expected Output:
1
e1
Output:
h.2
es
6000.0
m
300
so
h(
Execution Time:
So
0.105s
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
5000.0
250
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
)
.in
ac
Output:
ty.
rsi
10000.0
ive
500
n
Compilation Status: Passed su
tia
lgo
Execution Time:
ga
3@
0.114s
00
80
TestCase4:
1
e1
Input:
cs
4s
Expected Output:
m
so
Output:
m
So
7000.0
350
The Car object uses the this keyword to call the parkCar(Car c) method of the Garage.
The garage should also be able to display the list of all parked cars.
Input Format:
Car brand
Car model
)
.in
ac
Output Format:
ty.
rsi
Confirmation message for each parked car with its slot number.
ive
A garage status list showing all parked cars and their slot assignments.
n
su
tia
lgo
Sample Input:
ga
2TeslaModel SBMWX5
3@
00
Sample Output:
801
e1
Car Tesla Model S has been parked in slot #1Car BMW X5 has been parked in slot #2
cs
4s
Concepts Included:
m
So
Source Code:
import [Link].*;
class Car {
String brand;
String model;
class Garage {
private static int slotCounter = 0;
private LinkedHashMap<Integer, Car> parkedCars = new LinkedHashMap<>();
)
.in
}
ac
ty.
public void displayGarageStatus() {
rsi
[Link]("\n--- Garage Status ---");
ive
for ([Link]<Integer, Car> entry : [Link]()) {
n
[Link]("Slot " + [Link]() + ": " + [Link]().brand + " " +
su
tia
[Link]().model);
lgo
}
}
ga
}
3@
00
int n = [Link]();
h.2
[Link]();
es
[Link](garage);
m
}
So
[Link]();
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Car Ford Mustang has been parked in slot #1
TestCase2:
)
.in
Input:
ac
ty.
< hidden >
rsi
ive
Expected Output:
n
< hidden > su
tia
lgo
Output:
ga
3@
Execution Time:
es
m
0.141s
So
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Car Mercedes GLA has been parked in slot #1
Car Jaguar XF has been parked in slot #2
--- Garage Status ---
Slot 1: Mercedes GLA
Slot 2: Jaguar XF
TestCase4:
Input:
< hidden >
)
.in
Expected Output:
ac
ty.
< hidden >
rsi
ive
Output:
n
Car Honda Civic has been parked in slot #1 su
tia
Car Toyota Fortuner has been parked in slot #2
lgo
Slot 3: Audi A4
4s
Slot 4: BMW X3
h.2
Execution Time:
so
h(
0.14s
es
m
So
Input Format:
Two integers representing the length and width of the [Link] Format:
Concepts Included:
GU 28 3rd Sem Java Prog. Pre-Midterm
Source Code:
import [Link].*;
class Rectangle{
int l , w;
Rectangle(int l , int w){
this.l = l;
)
.in
this.w = w;
ac
}
ty.
int calculateArea(){
rsi
return l*w;
ive
}
n
} su
tia
public class Main{
lgo
[Link]([Link]());
e1
}
cs
}
4s
h.2
Compilation Details:
es
m
so
TestCase1:
h(
es
Input:
m
So
Expected Output:
< hidden >
Output:
30
Expected Output:
< hidden >
Output:
21
)
.in
0.089s
ac
ty.
rsi
TestCase3:
n ive
Input:
su
tia
< hidden >
lgo
ga
Expected Output:
3@
Output:
1
e1
36
cs
4s
Execution Time:
m
so
0.095s
h(
es
m
TestCase4:
So
Input:
< hidden >
Expected Output:
< hidden >
Output:
16
Input FormatThe first line contains the name (a string).The second line contains the
initial age (an integer).The third line contains the number of years to add (an
)
.in
integer).Output FormatA single line containing the updated age.
ac
Sample Input:Diana2015Sample Output:35
ty.
rsi
n ive
Completion Status: Completed
su
tia
lgo
Concepts Included:
ga
3@
Source Code:
4s
h.2
import [Link].*;
es
class Person{
m
String name;
so
int age;
h(
[Link] = name;
m
[Link] = age;
So
}
void displayDetails(){
// [Link](name);
[Link](age);
}
}
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner([Link]);
String name = [Link]();
int age1 = [Link]();
int age2 = [Link]();
int age = age1 + age2;
Person p = new Person(name , age);
[Link]();
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
)
.in
ac
41
ty.
rsi
Compilation Status: Passed
ive
Execution Time:
n
su
tia
0.09s
lgo
ga
TestCase2:
3@
00
Input:
801
Expected Output:
4s
h.2
Output:
so
h(
50
es
m
Execution Time:
0.092s
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
Output:
28
TestCase4:
Input:
< hidden >
Expected Output:
< hidden >
)
.in
ac
Output:
ty.
rsi
35
ive
Compilation Status: Passed
n
su
tia
Execution Time:
lgo
ga
0.092s
3@
00
80
constructor chaining. Create Product Electronics Laptop. Compute final price after
4s
computation.
so
h(
Input Format:
es
m
Output Format:
Sample Input:
50000 5000 2000
Sample Output:
Final Price: 57000
Source Code:
import [Link];
// Base class
class Product {
int price;
// Constructor for Product
Product(int price) {
[Link] = price;
)
}
.in
ac
}
ty.
// Derived class Electronics (extends Product)
rsi
class Electronics extends Product {
ive
int tax;
n
// Constructor for Electronics
Electronics(int price, int tax) { su
tia
super(price); // Calls Product constructor
lgo
[Link] = tax;
ga
}
3@
int priceAfterTax() {
80
}
e1
}
cs
4s
[Link] = warrantyCost;
m
}
So
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
)
.in
Output:
ac
ty.
Final Price: 44500
rsi
ive
Compilation Status: Passed
n
Execution Time: su
tia
lgo
0.118s
ga
3@
TestCase2:
00
80
Input:
1
e1
Expected Output:
h.2
es
Output:
h(
es
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Final Price: 33000
TestCase4:
Input:
< hidden >
Expected Output:
)
.in
ac
< hidden >
ty.
rsi
Output:
n ive
Final Price: 51000
su
tia
Compilation Status: Passed
lgo
ga
Execution Time:
3@
0.106s
00
801
e1
accumulate discounts across all items. Compute total discount for multiple items.
es
m
creation.
h(
es
Input Format:
m
So
itemPrice discountPercent
Output Format:
Sample Input:
1000 10500 5
Sample Output:
Total Discount: 125
Completion Status: Completed
Concepts Included:
GU 28 3rd Sem Java Prog. Pre-Midterm
Source Code:
import [Link].*;
class Item {
double price;
double discountPercent;
static double totalDiscount = 0.0; // static variable to accumulate total discount
)
.in
// Constructor
ac
Item(double price, double discountPercent) {
ty.
[Link] = price;
rsi
[Link] = discountPercent;
ive
addDiscount();
n
} su
tia
// Method to calculate and add discount for this item
lgo
void addDiscount() {
ga
totalDiscount += discount;
00
80
}
1
}
e1
while ([Link]()) {
es
}
es
m
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Total Discount: 500
TestCase2:
Input:
< hidden >
)
.in
ac
Expected Output:
ty.
rsi
< hidden >
ive
Output:
n
su
tia
Total Discount: 250
lgo
Execution Time:
00
80
0.116s
1
e1
cs
TestCase3:
4s
h.2
Input:
es
m
Expected Output:
es
m
Output:
Total Discount: 450
TestCase4:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Total Discount: 280
)
.in
ac
Description:Design a program where LibraryItem is a base class. Book and Magazine
ty.
inherit from it. Use method overriding to calculate the late fee depending on the type
rsi
of item.1. Books charge ₹5/day late2. Magazines charge ₹2/day lateUse constructors
ive
and super keyword to initialize.
n
su
tia
Input Format:First line: type of item (book or magazine)Second line: number of late
lgo
days
ga
Sample Input:book4
80
Sample Output:20
1
e1
cs
4s
Concepts Included:
so
h(
Source Code:
import [Link].*;
class LibraryItem {
String itemType;
// Constructor
LibraryItem(String itemType) {
[Link] = itemType;
}
// Method to calculate late fee (overridden in subclasses)
double calculateLateFee(int daysLate) {
return 0.0;
}
}
// Book class inherits LibraryItem
class Book extends LibraryItem {
// Constructor uses super to call parent constructor
Book(String itemType) {
super(itemType);
}
// Overridden method
double calculateLateFee(int daysLate) {
return daysLate * 5; // ₹5 per day
}
}
// Magazine class inherits LibraryItem
class Magazine extends LibraryItem {
// Constructor uses super
Magazine(String itemType) {
)
.in
super(itemType);
ac
ty.
}
rsi
// Overridden method
ive
double calculateLateFee(int daysLate) {
n
return daysLate * 2; // ₹2 per day
su
tia
}
lgo
}
public class Main {
ga
LibraryItem item;
cs
if ([Link]("book")) {
4s
} else if ([Link]("magazine")) {
es
} else {
so
return;
es
}
m
[Link]((int) [Link](days));
So
[Link]();
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
30
TestCase2:
Input:
< hidden >
)
.in
ac
Expected Output:
ty.
rsi
< hidden >
ive
Output:
n
su
tia
20
lgo
Execution Time:
00
80
0.094s
1
e1
cs
TestCase3:
4s
h.2
Input:
es
m
Expected Output:
es
m
Output:
0
TestCase4:
Input:
< hidden >
Expected Output:
< hidden >
Output:
6
)
.in
Problem Statement:Create a Product class and a subclass SpecialProduct. Use final
ac
variable maxDiscount and calculate discount for multiple products.
ty.
rsi
Description:Demonstrates final variables, inheritance, and arithmetic calculations.
ive
Input Format:
n
su
tia
price discount
lgo
Output Format:
ga
3@
Sample Input:
1
e1
cs
2000 500
4s
h.2
Sample Output:
es
m
Concepts Included:
GU 28 3rd Sem Java Prog. Pre-Midterm
Source Code:
import [Link].*;
class Product {
final double maxDiscount = 500; // final variable
double price;
double discount;
// Constructor
Product(double price, double discount) {
[Link] = price;
[Link] = discount;
}
// Method to calculate applied discount
double calculateDiscount() {
// Apply maxDiscount limit
if (discount > maxDiscount) {
return maxDiscount;
} else {
return discount;
}
}
}
)
.in
// Subclass for possible future extensions
ac
class SpecialProduct extends Product {
ty.
SpecialProduct(double price, double discount) {
rsi
super(price, discount);
ive
}
n
// Could override calculateDiscount() if needed
su
tia
}
lgo
while ([Link]()) {
00
}
h.2
}
es
}
m
so
Compilation Details:
h(
es
m
TestCase1:
So
Input:
< hidden >
Expected Output:
< hidden >
Output:
Applied Discount: 500
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Applied Discount: 300
)
.in
ac
Execution Time:
ty.
rsi
0.12s
n ive
TestCase3: su
tia
lgo
Input:
ga
Expected Output:
801
Output:
4s
h.2
Execution Time:
es
m
0.118s
So
TestCase4:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Applied Discount: 400
Input Format:
)
.in
ac
Output Format:
ty.
rsi
Perimeter: <value>
n ive
Sample Input:
su
tia
triangle 3 4 5
lgo
ga
3@
Sample Output:
00
Perimeter: 12
801
e1
cs
Concepts Included:
m
so
Source Code:
import [Link].*;
abstract class Shape{
abstract int perimeter();
}
class Triangle extends Shape{
int l , b, h;
Triangle(int l , int b , int h){
this.l = l;
this.b = b;
this.h = h;
}
@Override
int perimeter(){
return l+b+h;
}
}
class Main{
public static void main(String[] args){
Scanner sc = new Scanner([Link]);
// Read entire line as string
String line = [Link]().trim();
String[] parts = [Link]("\\s+"); // split by spaces
if ([Link] != 4 || !parts[0].equalsIgnoreCase("triangle")) {
[Link]("Invalid input");
[Link]();
return;
}
)
.in
try {
ac
int s1 = [Link](parts[1]);
ty.
int s2 = [Link](parts[2]);
rsi
int s3 = [Link](parts[3]);
ive
Shape triangle = new Triangle(s1, s2, s3);
n
[Link]("Perimeter: " + [Link]());
su
tia
} catch (NumberFormatException e) {
lgo
[Link]("Invalid sides");
}
ga
}
3@
}
00
801
Compilation Details:
e1
cs
4s
TestCase1:
h.2
es
Input:
m
so
Expected Output:
m
So
Output:
Perimeter: 24
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Perimeter: 18
TestCase3:
)
.in
ac
Input:
ty.
rsi
< hidden >
ive
Expected Output:
n
su
tia
< hidden >
lgo
Output:
ga
3@
Perimeter: 29
00
80
Execution Time:
cs
4s
0.105s
h.2
es
m
TestCase4:
so
h(
Input:
es
m
Expected Output:
< hidden >
Output:
Perimeter: 9
Input Format:
Output Format:
)
.in
Sample Input:
ac
ty.
Car 500 25
rsi
ive
Sample Output:
n
su
tia
Fuel Efficiency: 20.0
lgo
ga
3@
Concepts Included:
1
e1
Source Code:
h(
es
import [Link].*;
m
// Abstract class
So
// Truck class
class Truck extends Vehicle {
@Override
double fuelEfficiency(double distance, double fuel) {
return distance / (fuel * 1.25); // trucks are less efficient
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String vehicleType = [Link]();
double distance = [Link]();
double fuel = [Link]();
Vehicle vehicle;
if ([Link]("Car")) {
vehicle = new Car();
} else if ([Link]("Truck")) {
vehicle = new Truck();
} else {
[Link]("Invalid vehicle type");
)
.in
[Link]();
ac
return;
ty.
}
rsi
double efficiency = [Link](distance, fuel);
ive
// Print with one decimal place
n
[Link]("Fuel Efficiency: %.1f%n", efficiency);
su
tia
[Link]();
lgo
}
}
ga
3@
00
Compilation Details:
801
e1
TestCase1:
cs
4s
Input:
h.2
Expected Output:
so
h(
Output:
So
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Fuel Efficiency: 20.0
TestCase3:
Input:
)
.in
ac
< hidden >
ty.
rsi
Expected Output:
n ive
< hidden >
su
tia
Output:
lgo
ga
Execution Time:
1
e1
0.103s
cs
4s
h.2
TestCase4:
es
m
Input:
so
h(
Expected Output:
So
Output:
Fuel Efficiency: 30.0
Description:The program should accept insurance type and base premium from the
[Link] should calculate the final premium based on the given [Link] output should
display the insurance type and final premium.
Input Format:
)
.in
A string (insurance type: life/health/car)
ac
ty.
An integer (base premium)
rsi
ive
Output Format:
n
Insurance type su
tia
lgo
Final premium
ga
3@
Sample Input:
00
80
car5000
1
e1
cs
Sample Output:
4s
h.2
Concepts Included:
So
Source Code:
import [Link].*;
// Abstract class
abstract class Insurance {
abstract double calculatePremium(double basePremium);
}
// Life Insurance
class LifeInsurance extends Insurance {
@Override
double calculatePremium(double basePremium) {
return basePremium * 1.10; // +10%
}
}
// Health Insurance
class HealthInsurance extends Insurance {
@Override
double calculatePremium(double basePremium) {
return basePremium * 1.20; // +20%
}
}
// Car Insurance
class CarInsurance extends Insurance {
@Override
)
.in
double calculatePremium(double basePremium) {
ac
return basePremium * 1.30; // +30%
ty.
}
rsi
}
ive
public class Main {
n
public static void main(String[] args) {
su
tia
Scanner sc = new Scanner([Link]);
lgo
Insurance insurance;
3@
switch (type) {
00
case "life":
80
break;
4s
case "health":
h.2
break;
so
case "car":
h(
break;
So
default:
[Link]("Invalid insurance type");
[Link]();
return;
}
double finalPremium = [Link](basePremium);
// Print with one decimal place
[Link]("Premium: %.1f%n", finalPremium);
[Link]();
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Insurance Type: Life Insurance
Premium: 11000.0
)
Execution Time:
.in
ac
0.112s
ty.
rsi
ive
TestCase2:
n
Input: su
tia
lgo
Expected Output:
00
Output:
cs
4s
Premium: 2400.0
es
Execution Time:
h(
es
0.104s
m
So
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Insurance Type: Car Insurance
Premium: 6500.0
Compilation Status: Passed
Execution Time:
0.115s
TestCase4:
Input:
< hidden >
Expected Output:
< hidden >
Output:
)
.in
ac
Insurance Type: Life Insurance
ty.
Premium: 2750.0
rsi
ive
Compilation Status: Passed
n
Execution Time: su
tia
lgo
0.112s
ga
3@
00
travel [Link] program should use an abstract class Ticket with an abstract method
cs
Bus = 5
m
so
Train = 8
h(
es
Flight = 15
m
So
Input Format:
Output Format:
Ticket type
Fare
Sample Input:
bus120
Sample Output:
Ticket Type: Bus TicketFare: 600
Concepts Included:
GU 28 3rd Sem Java Prog. Pre-Midterm
)
.in
ac
Source Code:
ty.
rsi
import [Link].*;
ive
// Abstract class
n
abstract class Ticket {
abstract int calculateFare(int distance); su
tia
}
lgo
// Bus Ticket
ga
@Override
00
}
e1
cs
}
4s
// Train Ticket
h.2
}
es
}
m
// Flight Ticket
So
)
.in
int fare = [Link](distance);
ac
[Link]("Fare: " + fare);
ty.
[Link]();
rsi
}
ive
}
n
su
tia
Compilation Details:
lgo
ga
TestCase1:
3@
00
Input:
801
Expected Output:
4s
h.2
Output:
so
h(
Fare: 500
m
So
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Ticket Type: Train Ticket
Fare: 400
TestCase3:
Input:
< hidden >
)
.in
Expected Output:
ac
ty.
< hidden >
rsi
ive
Output:
n
Ticket Type: Flight Ticket su
tia
Fare: 300
lgo
ga
Execution Time:
00
80
0.109s
1
e1
cs
TestCase4:
4s
h.2
Input:
es
m
Expected Output:
es
m
Output:
Ticket Type: Bus Ticket
Fare: 1250
Input Format:
Output Format:
)
.in
Sample Input:
ac
ty.
song.mp3
rsi
ive
Sample Output:
n
su
tia
Audio file detected: Playing with 128kbps quality
lgo
ga
3@
Concepts Included:
1
e1
Source Code:
h(
es
import [Link].*;
m
So
// AudioPlayer interface
interface AudioPlayer {
void playAudio(String fileName);
}
// VideoPlayer interface
interface VideoPlayer {
void playVideo(String fileName);
}
// MediaCenter class implements both interfaces
class MediaCenter implements AudioPlayer, VideoPlayer {
// Supported audio and video extensions
private static final Set<String> audioExt = new HashSet<>([Link]("mp3", "wav",
"aac", "flac"));
private static final Set<String> videoExt = new HashSet<>([Link]("mp4", "avi",
"mkv", "mov"));
@Override
public void playAudio(String fileName) {
[Link]("Audio file detected: Playing with 128kbps quality");
}
@Override
public void playVideo(String fileName) {
[Link]("Video file detected: Playing at 1080p resolution");
}
// Auto-detect method
public void play(String fileName) {
if () {
[Link]("Unknown file type");
return;
}
String ext = [Link]([Link](".") + 1).toLowerCase();
if ([Link](ext)) {
)
.in
playAudio(fileName);
ac
} else if ([Link](ext)) {
ty.
playVideo(fileName);
rsi
} else {
ive
[Link]("Unknown file type");
n
}
su
tia
}
lgo
}
ga
[Link](fileName);
4s
h.2
[Link]();
es
}
m
}
so
h(
Compilation Details:
es
m
So
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Video file detected: Playing at 1080p resolution
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Audio file detected: Playing with 128kbps quality
)
.in
ac
Compilation Status: Passed
ty.
rsi
Execution Time:
n ive
0.087s
su
tia
lgo
TestCase3:
ga
Input:
3@
00
Expected Output:
e1
cs
Output:
es
m
Execution Time:
So
0.096s
TestCase4:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Video file detected: Playing at 1080p resolution
Compilation Status: Passed
Execution Time:
0.09s
)
.in
Description:
ac
ty.
User first adds one or more items to the cart using the add command.
rsi
ive
When the user types pay, the system processes the payment and shows how many
n
items were purchased.
su
tia
Input Format:
lgo
ga
Output Format:
1
e1
Sample Input:
so
h(
add Laptoppay
es
m
Sample Output:
So
Concepts Included:
GU 28 3rd Sem Java Prog. Pre-Midterm
Source Code:
import [Link].*;
// Interface for adding items
interface AddToCart {
void addItem(String item);
}
// Interface for payment
interface Payment {
void makePayment();
}
// ShoppingCart implements both interfaces
class ShoppingCart implements AddToCart, Payment {
private List<String> items;
ShoppingCart() {
items = new ArrayList<>();
}
@Override
)
.in
public void addItem(String item) {
ac
[Link](item);
ty.
[Link]("Item added: " + item);
rsi
}
ive
@Override
n
public void makePayment() {
su
tia
[Link]("Payment successful for " + [Link]() + " items.");
lgo
}
}
ga
3@
while ([Link]()) {
4s
if ([Link]("pay")) {
es
[Link]();
m
[Link](item);
m
} else {
So
[Link]("Invalid command");
}
}
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Item added: Mobile
Payment successful for 1 items.
TestCase2:
)
.in
Input:
ac
ty.
< hidden >
rsi
ive
Expected Output:
n
< hidden > su
tia
lgo
Output:
ga
3@
Execution Time:
4s
h.2
0.115s
es
m
TestCase3:
so
h(
Input:
es
m
Expected Output:
< hidden >
Output:
Item added: Shoes
Payment successful for 1 items.
Expected Output:
< hidden >
Output:
Item added: Bag
Payment successful for 1 items.
)
Execution Time:
.in
ac
0.108s
ty.
rsi
ive
19. Student Result Evaluation
n
su
tia
Problem Statement:Write a program that evaluates whether a student passes or fails
lgo
based on marks and attendance. The program should use multiple inheritance with
ga
interfaces.
3@
Implement both interfaces in the Student class and provide a method checkResult()
cs
that checks:
4s
h.2
Marks must be ≥ 40
es
m
If both conditions are met, display "Result: Pass", otherwise "Result: Fail".
es
m
Description:
So
Input Format:
Output Format:
Sample Output:
Result: Pass
Concepts Included:
GU 28 3rd Sem Java Prog. Pre-Midterm
)
.in
Language Used: JAVA 8
ac
ty.
rsi
Source Code:
n ive
import [Link].*;
interface Marks{ su
tia
public void enterMarks(int marks);
lgo
}
ga
3@
interface Attendance{
00
}
1
@Override
public void enterMarks(int marks) {
es
[Link] = marks;
m
so
}
h(
@Override
es
[Link] = attendance;
So
}
void checkResult(){
if(marks>=40 && attendance >= 75){
[Link]("Result: Pass");
}else{
[Link]("Result: Fail");
}
}
}
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner([Link]);
Student s = new Student();
int marks = [Link]();
int attendance = [Link]();
[Link](marks);
[Link](attendance);
[Link]();
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
)
.in
< hidden >
ac
ty.
Output:
rsi
ive
Result: Pass
n
Compilation Status: Passed su
tia
lgo
Execution Time:
ga
3@
0.101s
00
80
TestCase2:
1
e1
Input:
cs
4s
Expected Output:
m
so
Output:
m
So
Result: Fail
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Result: Fail
TestCase4:
Input:
< hidden >
)
.in
ac
Expected Output:
ty.
rsi
< hidden >
ive
Output:
n
su
tia
Result: Pass
lgo
Execution Time:
00
80
0.092s
1
e1
cs
4s
implements both.
h(
es
Description:
m
So
Input Format:
Output Format:
Sample Output:
Book added: JavaProgrammingBook borrowed: JavaProgramming
Concepts Included:
GU 28 3rd Sem Java Prog. Pre-Midterm
)
.in
Language Used: JAVA 8
ac
ty.
rsi
Source Code:
n ive
import [Link].*;
// Interface for book operations su
tia
interface BookOperations {
lgo
}
3@
interface UserOperations {
80
}
e1
}
h(
@Override
es
[Link](book);
So
[Link]("Invalid command");
}
}
}
Compilation Details:
)
.in
ac
ty.
TestCase1:
rsi
ive
Input:
n
< hidden > su
tia
lgo
Expected Output:
ga
3@
Output:
801
e1
Execution Time:
m
so
0.093s
h(
es
m
TestCase2:
So
Input:
< hidden >
Expected Output:
< hidden >
Output:
Book added: DataStructures
Book borrowed: DataStructures
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Book added: OOPConcepts
)
.in
Book not available
ac
ty.
Compilation Status: Passed
rsi
ive
Execution Time:
n
0.102s su
tia
lgo
TestCase4:
ga
3@
Input:
00
80
Expected Output:
cs
4s
Output:
m
so
Execution Time:
0.096s
Input Format:
Output Format:
Sample Input:
song.mp3
Sample Output:
)
.in
Audio file detected: Playing with 128kbps quality
ac
ty.
rsi
ive
Completion Status: Completed
n
su
tia
Concepts Included:
lgo
Source Code:
cs
4s
import [Link].*;
h.2
interface AudioPlayer {
m
}
h(
interface VideoPlayer {
m
}
// MediaCenter class implements both interfaces
class MediaCenter implements AudioPlayer, VideoPlayer {
// Supported audio and video extensions
private static final Set<String> audioExt = new HashSet<>([Link]("mp3", "wav",
"aac", "flac"));
private static final Set<String> videoExt = new HashSet<>([Link]("mp4", "avi",
"mkv", "mov"));
@Override
public void playAudio(String fileName) {
[Link]("Audio file detected: Playing with 128kbps quality");
}
@Override
public void playVideo(String fileName) {
[Link]("Video file detected: Playing at 1080p resolution");
}
)
.in
}
ac
}
ty.
public class Main {
rsi
public static void main(String[] args) {
ive
Scanner sc = new Scanner([Link]);
n
String fileName = [Link]().trim();
su
tia
MediaCenter mc = new MediaCenter();
lgo
[Link](fileName);
}
ga
}
3@
00
80
Compilation Details:
1
e1
cs
TestCase1:
4s
h.2
Input:
es
m
Expected Output:
es
m
Output:
Video file detected: Playing at 1080p resolution
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Audio file detected: Playing with 128kbps quality
TestCase3:
Input:
)
.in
ac
< hidden >
ty.
rsi
Expected Output:
n ive
< hidden >
su
tia
Output:
lgo
ga
Execution Time:
1
e1
0.088s
cs
4s
h.2
TestCase4:
es
m
Input:
so
h(
Expected Output:
So
Output:
Video file detected: Playing at 1080p resolution
Sample Input:
abc
Sample Output:
Weak Password: Must be at least 6 characters
)
.in
ac
Completion Status: Completed
ty.
rsi
Concepts Included:
n ive
GU 28 3rd Sem Java Prog. Pre-Midterm su
tia
lgo
Source Code:
00
80
import [Link].*;
1
e1
// Custom Exception
cs
WeakPasswordException(String message) {
h.2
super(message);
es
}
m
}
so
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
)
.in
Output:
ac
ty.
Valid Password
rsi
ive
Compilation Status: Passed
n
Execution Time: su
tia
lgo
0.091s
ga
3@
TestCase2:
00
80
Input:
1
e1
Expected Output:
h.2
es
Output:
h(
es
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Valid Password
TestCase4:
Input:
< hidden >
Expected Output:
)
.in
ac
< hidden >
ty.
rsi
Output:
n ive
Valid Password
su
tia
Compilation Status: Passed
lgo
ga
Execution Time:
3@
0.091s
00
801
e1
InvalidAgeException. The program should read a person’s age and throw the
es
Input Format:
So
Output Format:
Sample Input:
16
Sample Output:
Invalid Age: must be 18 or above
Completion Status: Completed
Concepts Included:
GU 28 3rd Sem Java Prog. Pre-Midterm
Source Code:
import [Link].*;
class InvalidAgeException extends Exception {
public InvalidAgeException (String message){
super(message);
}
)
.in
}
ac
public class Main {
ty.
public static void main(String[] args){
rsi
Scanner sc = new Scanner([Link]);
ive
try{
n
// User-Define Exception su
tia
int age = [Link]();
lgo
}else{
[Link]("Eligible to Vote");
00
80
}
1
}catch(InvalidAgeException e){
e1
[Link]([Link]());
cs
}
4s
}
h.2
}
es
m
so
Compilation Details:
h(
es
m
TestCase1:
So
Input:
< hidden >
Expected Output:
< hidden >
Output:
Eligible to Vote
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Eligible to Vote
)
.in
ac
Execution Time:
ty.
rsi
0.09s
n ive
TestCase3: su
tia
lgo
Input:
ga
Expected Output:
801
Output:
4s
h.2
Execution Time:
es
m
0.09s
So
TestCase4:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Eligible to Vote
Input Format:
)
.in
Second line: withdrawal amount
ac
Output Format:
ty.
rsi
Balance after withdrawal or exception message
n ive
Sample Input: su
tia
lgo
12342000
ga
3@
Sample Output:
00
80
Insufficient balance!
1
e1
cs
4s
Concepts Included:
m
so
h(
Source Code:
import [Link].*;
try {
if(pin != 1111) throw new InvalidPINException("Invalid PIN!");
if(amount > balance) throw new InsufficientBalanceException("Insufficient balance!");
balance -= amount;
[Link]("Balance: " + balance);
} catch(ATMException e) {
)
.in
[Link]([Link]());
ac
}
ty.
}
rsi
}
n ive
Compilation Details: su
tia
lgo
TestCase1:
ga
3@
Input:
00
80
Expected Output:
cs
4s
Output:
m
so
Invalid PIN!
h(
es
Execution Time:
0.089s
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Insufficient balance!
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
)
.in
ac
Output:
ty.
rsi
Balance: 500
ive
Compilation Status: Passed
n
su
tia
Execution Time:
lgo
ga
0.106s
3@
00
TestCase4:
801
Input:
e1
cs
Expected Output:
es
m
Output:
es
m
Balance: 0
So
Input Format:
The first line contains an integer, n (1 ≤ n ≤ 100), denoting the number of elements in
the first [Link] second line contains n space-separated [Link] third line
contains an integer, m (1 ≤ m ≤ 100), denoting the number of elements in the second
[Link] fourth line contains m space-separated [Link] Format:
Print the elements in the intersection of the two sets, one per line. If there are no
common elements, print "No common elements."
Sample Output:34
Concepts Included:
)
.in
ac
GU 28 3rd Sem Java Prog. Pre-Midterm
ty.
rsi
Language Used: JAVA 8
n ive
Source Code: su
tia
lgo
import [Link].*;
ga
3@
int n = [Link]();
4s
h.2
[Link]([Link]());
m
so
}
h(
es
int m = [Link]();
So
// Find intersection
[Link](set2);
// Output result
if ([Link]()) {
[Link]("No common elements");
} else {
List<Integer> result = new ArrayList<>(set1);
[Link](result);
for (int num : result) {
[Link](num);
}
}
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
)
.in
< hidden >
ac
ty.
Output:
rsi
ive
3
n
4
5 su
tia
lgo
Execution Time:
00
80
0.093s
1
e1
cs
TestCase2:
4s
h.2
Input:
es
Expected Output:
h(
es
Output:
200
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
Output:
10
30
TestCase4:
)
.in
Input:
ac
ty.
< hidden >
rsi
ive
Expected Output:
n
< hidden > su
tia
lgo
Output:
ga
3@
No common elements
00
Execution Time:
cs
4s
0.092s
h.2
es
m
Description:You need to read a list of key-value pairs from the user and calculate the
m
sum of values for each unique key. Use Java's Map interface to store the key-value
So
Input Format:
The first line contains an integer, n (1 ≤ n ≤ 100), denoting the number of key-value
[Link] next n lines each contain a key (string) and a value (integer).Output Format:
Print each unique key and its summed value, one per line.
Source Code:
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
Map<String, Integer> map = new LinkedHashMap<>(); // maintain insertion order
for (int i = 0; i < n; i++) {
)
String key = [Link]();
.in
ac
int value = [Link]();
ty.
// Sum values for the same key
rsi
[Link](key, [Link](key, 0) + value);
ive
}
n
// Print each key and summed value
su
for ([Link]<String, Integer> entry : [Link]()) {
tia
[Link]([Link]() + " " + [Link]());
lgo
}
ga
[Link]();
3@
}
00
}
801
e1
Compilation Details:
cs
4s
h.2
TestCase1:
es
Input:
m
so
h(
Expected Output:
m
So
Output:
apple 30
banana 15
orange 15
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
red 10
blue 4
green 7
)
.in
0.129s
ac
ty.
rsi
TestCase3:
n ive
Input:
su
tia
< hidden >
lgo
ga
Expected Output:
3@
Output:
1
e1
cs
book 1
4s
pen 2
h.2
Execution Time:
so
h(
0.112s
es
m
So
TestCase4:
Input:
< hidden >
Expected Output:
< hidden >
Output:
cat 10
dog 8
Input Format:
The first line contains an integer, n (1 ≤ n ≤ 100), denoting the number of [Link]
second line contains n space-separated [Link] Format:
)
.in
Print each word and its frequency, one per line.
ac
ty.
Sample Input:4apple banana apple grape
rsi
ive
Sample Output:banana 1apple 2grape 1
n
su
tia
Concepts Included:
00
80
Source Code:
es
m
import [Link].*;
so
int n = [Link]();
Map<String, Integer> freqMap = new LinkedHashMap<>(); // preserve insertion order
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
cat 3
dog 1
)
.in
Compilation Status: Passed
ac
ty.
Execution Time:
rsi
ive
0.111s
n
su
tia
TestCase2:
lgo
ga
Input:
3@
Expected Output:
1
e1
Output:
h.2
es
dog 3
m
cat 1
so
fish 1
h(
es
Execution Time:
0.107s
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
Output:
red 2
blue 1
TestCase4:
Input:
< hidden >
Expected Output:
)
.in
< hidden >
ac
ty.
Output:
rsi
ive
pen 3
n
pencil 2
marker 1 su
tia
lgo
Execution Time:
00
0.111s
801
e1
cs
Description:You are given a list of integers. Your task is to write a Java program that
h(
removes duplicate elements from the list while maintaining the original order of
es
elements. The program should read the list of integers from the user, process it to
m
Input Format:
The first line contains an integer n, representing the number of elements in the
[Link] next n lines each contain one integer, representing elements of the [Link]
Format:
Output the modified list after removing duplicates, each element in a new line.
Sample Input:71232431
Sample Output:1234
Source Code:
import [Link].*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner([Link]);
int n = [Link]();
List<Integer> list = new ArrayList<>();
for(int i = 0 ; i < n ; i++){
)
[Link]([Link]());
.in
ac
}
ty.
Set <Integer> set = new LinkedHashSet<>(list);
rsi
for (int num : set) {
ive
[Link](num);
n
}
} su
tia
}
lgo
ga
3@
Compilation Details:
00
80
TestCase1:
1
e1
Input:
cs
4s
Expected Output:
m
so
Output:
m
So
1
2
3
4
5
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
1
2
3
)
.in
ac
TestCase3:
ty.
rsi
Input:
n ive
< hidden >
su
tia
Expected Output:
lgo
ga
Output:
00
80
1
1
e1
Execution Time:
h.2
es
0.099s
m
so
h(
es
Description:
Input Format:
Output Format:
The maximum element
Sample Input:
523 78 45 12 89
Sample Output:
Maximum: 89
Concepts Included:
)
.in
GU 28 3rd Sem Java Prog. Pre-Midterm
ac
ty.
rsi
Language Used: JAVA 8
n ive
Source Code: su
tia
lgo
import [Link].*;
// Generic class to find maximum
ga
return null;
1
e1
}
cs
T max = [Link](0);
4s
if ([Link](max) > 0) {
es
max = item;
m
}
so
}
h(
es
return max;
m
}
So
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
List<Integer> intList = new ArrayList<>();
for (int i = 0; i < n; i++) {
[Link]([Link]());
}
MaxFinder<Integer> finder = new MaxFinder<>();
[Link]("Maximum: " + [Link](intList));
[Link]();
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Maximum: 30
)
.in
ac
Execution Time:
ty.
rsi
0.098s
n ive
TestCase2: su
tia
lgo
Input:
ga
3@
Expected Output:
801
Output:
4s
h.2
Maximum: -3
es
m
Execution Time:
es
m
0.094s
So
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Maximum: 200
TestCase4:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Maximum: 35
)
.in
ac
Compilation Status: Passed
ty.
rsi
Execution Time:
n ive
0.101s
su
tia
lgo
Description:
e1
cs
Implement a static generic method with bounded type <T extends Comparable<T>>.
4s
h.2
Input Format:
m
so
Next n elements
m
So
Output Format:
Sorted list
Sample Input:
5banana apple mango cherry grape
Sample Output:
apple banana cherry grape mango
Source Code:
import [Link].*;
)
[Link]([Link](i));
.in
ac
if (i != [Link]() - 1) {
ty.
[Link](" ");
rsi
}
ive
}
n
[Link]();
} su
tia
lgo
int n = [Link]();
00
[Link]([Link]());
e1
}
cs
4s
sortList(list);
h.2
}
}
es
m
so
Compilation Details:
h(
es
m
TestCase1:
So
Input:
< hidden >
Expected Output:
< hidden >
Output:
apple banana kiwi mango orange zebra
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
apple banana peach
)
.in
ac
Execution Time:
ty.
rsi
0.088s
n ive
TestCase3: su
tia
lgo
Input:
ga
Expected Output:
801
Output:
4s
h.2
Execution Time:
es
m
0.092s
So
Description:
Output Format:
Sample Input:
4push 10push 20poppeek
)
.in
ac
Sample Output:
ty.
rsi
Pushed: 10Pushed: 20Popped: 20Peek: 10
n ive
su
tia
Completion Status: Completed
lgo
ga
Concepts Included:
3@
00
Source Code:
h.2
es
import [Link].*;
m
so
class GenericStack<T> {
h(
public GenericStack() {
So
)
.in
GenericStack<String> stack = new GenericStack<>();
ac
for (int i = 0; i < n; i++) {
ty.
String line = [Link]();
rsi
String[] parts = [Link](" ", 2);
n ive
switch (parts[0]) {
su
tia
case "push":
lgo
[Link](parts[1]);
break;
ga
case "pop":
3@
[Link]();
00
break;
80
case "peek":
1
e1
[Link]();
cs
break;
4s
default:
h.2
[Link]("Invalid command");
es
}
m
}
so
[Link]();
h(
}
es
}
m
So
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Pushed: apple
Pushed: banana
Peek: banana
TestCase2:
Input:
< hidden >
Expected Output:
)
.in
< hidden >
ac
ty.
Output:
rsi
ive
Pushed: A
n
Pushed: B
Pushed: C su
tia
Popped: C
lgo
Peek: B
ga
3@
Execution Time:
1
e1
0.102s
cs
4s
h.2
TestCase3:
es
Input:
m
so
h(
Expected Output:
m
So
Output:
Pushed: dog
Pushed: cat
Peek: cat
Popped: cat
Expected Output:
< hidden >
Output:
Pushed: 100
Pushed: 200
Popped: 200
)
.in
Execution Time:
ac
ty.
0.097s
rsi
n ive
32. Frequency Counter su
tia
lgo
Description:
00
80
each element.
4s
h.2
Input Format:
so
h(
Output Format:
Sample Input:
8apple mango apple orange mango apple grape mango
Sample Output:
apple -> 3mango -> 3orange -> 1grape -> 1
Source Code:
import [Link].*;
)
.in
ac
for (T item : list) {
ty.
[Link](item, [Link](item, 0) + 1);
rsi
}
n ive
for ([Link]<T, Integer> entry : [Link]()) {
su
[Link]([Link]() + " -> " + [Link]());
tia
}
lgo
}
ga
3@
int n = [Link]();
1
countFrequency(list);
m
}
So
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
1 -> 2
2 -> 2
3 -> 1
TestCase2:
Input:
< hidden >
Expected Output:
)
.in
ac
< hidden >
ty.
rsi
Output:
n ive
cat -> 2
dog -> 3 su
tia
lion -> 1
lgo
ga
Execution Time:
80
0.119s
1
e1
cs
4s
TestCase3:
h.2
Input:
es
m
Expected Output:
es
m
Output:
A -> 4
TestCase4:
Input:
< hidden >
Expected Output:
< hidden >
Output:
red -> 3
green -> 2
blue -> 2
)
.in
ac
ty.
33. Sum of Generic Numbers
rsi
ive
Problem Statement:Write a program to compute the sum of elements in a generic list.
n
The program should accept numeric values of Integer, Double, or Float type using
wrapper classes and generics. su
tia
lgo
Description:
ga
3@
Input Format:
4s
h.2
Output Format:
h(
es
Sample Input:
510 20 30 40 50
Sample Output:
Sum: 150.0
Concepts Included:
GU 28 3rd Sem Java Prog. Pre-Midterm
Source Code:
import [Link].*;
class GenericSum<T extends Number> {
public double sum(List<T> list) {
double total = 0;
for (T num : list) {
total += [Link]();
}
return total;
}
)
.in
}
ac
public class Main {
ty.
public static void main(String[] args) {
rsi
Scanner sc = new Scanner([Link]);
ive
int n = [Link]();
n
List<Double> list = new ArrayList<>();
for (int i = 0; i < n; i++) { su
tia
[Link]([Link]());
lgo
}
ga
[Link]();
80
}
1
e1
}
cs
4s
Compilation Details:
h.2
es
m
TestCase1:
so
h(
Input:
es
m
Expected Output:
< hidden >
Output:
Sum: 15.0
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Sum: 101.0
)
.in
ac
TestCase3:
ty.
rsi
Input:
n ive
< hidden >
su
tia
Expected Output:
lgo
ga
Output:
00
80
Sum: 600.0
1
e1
Execution Time:
h.2
es
0.12s
m
so
h(
TestCase4:
es
m
Input:
So
Expected Output:
< hidden >
Output:
Sum: 23.1
Description:
Input Format:
)
.in
Second line: n space-separated numbers
ac
ty.
Output Format:
rsi
Median value
n ive
Sample Input: su
tia
lgo
510 20 30 40 50
ga
3@
Sample Output:
00
80
Median: 30.0
1
e1
cs
4s
Concepts Included:
m
so
h(
Source Code:
import [Link].*;
class MedianFinder<T extends Number & Comparable<T>> {
public double findMedian(List<T> list) {
if (list == null || [Link]()) {
return 0;
}
)
.in
}
ac
MedianFinder<Double> finder = new MedianFinder<>();
ty.
[Link]("Median: " + [Link](list));
rsi
[Link]();
ive
}
n
}
su
tia
lgo
Compilation Details:
ga
3@
TestCase1:
00
80
Input:
1
e1
Expected Output:
h.2
es
Output:
h(
es
Median: 17.5
m
So
TestCase2:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Median: 6.0
TestCase3:
Input:
< hidden >
Expected Output:
)
.in
ac
< hidden >
ty.
rsi
Output:
n ive
Median: 100.0
su
tia
Compilation Status: Passed
lgo
ga
Execution Time:
3@
0.127s
00
801
TestCase4:
e1
cs
Input:
4s
h.2
Expected Output:
so
h(
Output:
So
Median: 4.0
Description:
Create a generic class SumOfSquares<T extends Number>.
Input Format:
Output Format:
Sum of squares
Sample Input:
)
.in
32 3 4
ac
ty.
rsi
Sample Output:
n ive
Sum of Squares: 29.0
su
tia
lgo
Concepts Included:
00
80
Source Code:
m
so
import [Link].*;
h(
Compilation Details:
TestCase1:
Input:
< hidden >
)
.in
Expected Output:
ac
ty.
< hidden >
rsi
ive
Output:
n
Sum of Squares: 55.0 su
tia
lgo
Execution Time:
00
80
0.12s
1
e1
cs
TestCase2:
4s
h.2
Input:
es
Expected Output:
h(
es
Output:
Sum of Squares: 3000.0
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Sum of Squares: 50.5
TestCase4:
Input:
)
.in
ac
< hidden >
ty.
rsi
Expected Output:
n ive
< hidden >
su
tia
Output:
lgo
ga
Execution Time:
1
e1
0.106s
cs
4s
h.2
es
maximum and minimum values of a numeric list using wrapper classes and generics.
es
m
Description:
So
Input Format:
Output Format:
Sample Output:
Difference: 25.0
Concepts Included:
GU 28 3rd Sem Java Prog. Pre-Midterm
)
.in
Language Used: JAVA 8
ac
ty.
rsi
Source Code:
n ive
import [Link].*;
su
class MaxMinDifference<T extends Number & Comparable<T>> {
tia
public double findDifference(List<T> list) {
lgo
return 0;
3@
}
00
T max = [Link](0);
80
T min = [Link](0);
1
if ([Link](max) > 0) {
cs
max = num;
4s
h.2
}
if ([Link](min) < 0) {
es
min = num;
m
so
}
h(
}
es
}
So
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
List<Double> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
[Link]([Link]());
}
MaxMinDifference<Double> calculator = new MaxMinDifference<>();
double difference = [Link](list);
[Link]("Difference: " + difference);
[Link]();
}
}
Compilation Details:
TestCase1:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Difference: 75.0
)
.in
ac
Execution Time:
ty.
rsi
0.123s
n ive
TestCase2: su
tia
lgo
Input:
ga
3@
Expected Output:
801
Output:
4s
h.2
Difference: 3.5
es
m
Execution Time:
es
m
0.127s
So
TestCase3:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Difference: 15.0
TestCase4:
Input:
< hidden >
Expected Output:
< hidden >
Output:
Difference: 60.0
)
.in
ac
Compilation Status: Passed
ty.
rsi
Execution Time:
n ive
0.123s
su
tia
lgo
ga
3@
00
801
e1
cs
4s
h.2
es
m
so
h(
es
m
So