Assign 02 PF
Assign 02 PF
Assignment 02
Name: _______________________________
Section: ______________________________
Instructions:
1. Attempt all questions.
2. You can attempt the implementation on any Java compiler but have to submit the
assignment in handwritten form also on Odoo (attach all .java files)
3. The assignment should be submitted on A4 sheets or Assignment sheets only.
Violation will result in a deduction of 1 mark from the scored marks.
4. Each student must attach this front page to his/her assignment. Violation will result in
a deduction of 1 mark from the scored marks.
5. The Due Date for the Assignment on the portal is 29th April, 2025, and the hardcopy
submission deadline will be in the coming class right after 29 th April, 2025.
6. Late submission will result in a 10% deduction in marks.
7. No request for late submissions will be considered this time.
University of Central Punjab
Faculty of Information Technology and Computer Science
Question 01 : Marks: 10
You are working with a tech company that specializes in smart building automation systems.
One of their products is an intelligent elevator system designed to enhance security in
corporate buildings. This system grants access to floors only if a user enters a valid 3-digit
access code that meets specific logic-based criteria.
As a developer, your task is to design and implement a secure validation mechanism using
functions only (no arrays or strings allowed). You must write a program that asks the user for a
3-digit code and evaluates its validity based on the following rules:
Rules:
1. The code must consist of exactly three digits (e.g., 123, 741).
2. The digits must be either in strictly ascending (e.g., 257) or descending (e.g., 841)
order.
3. The middle digit (2nd digit) must be a prime number (2, 3, 5, or 7).
4. The product of all three digits must not exceed 100.
If the code satisfies all of the above rules, access is granted. Otherwise, the system will display
"Access Denied".
Functions to Implement:
1. int readAccessCode()
Prompts the user to enter a 3-digit code. Validates the input to ensure it is
between 100 and 999. Re-prompts if invalid.
2. int extractDigit(int number, int position)
Extracts a digit from the number based on position:
1. 1 = Hundreds (leftmost)
2. 2 = Tens (middle)
3. 3 = Units (rightmost)
3. boolean isPrime(int n)
Returns true if the given digit is a prime number (2, 3, 5, 7); otherwise, false.
4. boolean isAscending(int code)
Returns true if the digits are in strictly ascending order.
5. boolean isDescending(int code)
Returns true if the digits are in strictly descending order.
6. boolean isAccessGranted(int code)
Coordinates the validation by calling the appropriate functions to verify all rules.
7. void showAccessStatus(boolean access)
Displays a message based on the final result:
1. "Access Granted. Welcome!"
2. "Access Denied. Invalid Code."
(Shipping and handling are normally $10 per spool.) If there are special charges, the program
should ask for the special charges per spool.
The gathered data should be passed as arguments to another function that displays
The shipping and handling parameter in the second function should have the default argument
10.00.
Input Validation: Do not accept numbers less than 1 for spools ordered. Do not accept a number
less than 0 for spools in stock or shipping and handling charges.
Question 04 : Marks: 10
A train ticket booking system offers a special discount to passengers whose seat number is
considered "lucky" based on specific criteria. A seat number is 3-digits only, and the luck
criteria are:
Required Functions:
1. int readSeatNumber()
Reads and validates that the seat number is a 3-digit number.
2. boolean isPalindrome(int number)
Returns true if the number is a 3-digit palindrome.
3. int digitSum(int number)
Returns the sum of digits of the number.
4. boolean isPrime(int n)
Returns true if the sum is a prime number.
5. boolean isMiddleDigitDominant(int number)
Checks if middle digit ≥ first and last.
6. boolean isDivisibleBy11(int number)
Returns false if divisible by 11.
7. boolean isLuckySeat(int number)
Checks all conditions.
8. void showSeatResult(boolean isLucky)
Prints "Lucky Seat! Discount Applied." or "Not Eligible."
Output Example: