← Computer Programming II

Problem 1 (Easy): Music Player Song Class

Create a simple music library system by defining a Song class that stores basic information about music tracks.

Requirements:

  1. Define a class named Song with an __init__ method that takes three parameters: title, artist, and duration (in minutes)
  2. Store these parameters as instance variables: self.title, self.artist, and self.duration
  3. Create a method named display_info() that prints song details in this exact format: "Title: {title}, Artist: {artist}, Duration: {duration} min"
  4. Create a method named get_duration_seconds() that returns the duration converted to seconds (duration × 60)
  5. Create two Song objects with the test data provided below
  6. Call display_info() on both songs
  7. Print the duration in seconds for the second song using get_duration_seconds()

Input

First song: "Yesterday", "The Beatles", 2.5
Second song: "Bohemian Rhapsody", "Queen", 6.0

Expected Output

Title: Yesterday, Artist: The Beatles, Duration: 2.5 min
Title: Bohemian Rhapsody, Artist: Queen, Duration: 6.0 min
360

Problem 2 (Medium): Bank Account Management

Create a BankAccount class that manages individual accounts while tracking bank-wide statistics.

Requirements:

  1. Define a class BankAccount with these class variables:
    • bank_name = “Urgench Bank”
    • total_accounts = 0 (tracks how many accounts have been created)
    • min_balance = 10 (minimum balance requirement)
  2. Define __init__ with parameters owner and initial_balance:
    • Store as instance variables: self.owner and self.balance
    • Increment the class variable BankAccount.total_accounts by 1 each time an account is created
  3. Create a method deposit(self, amount):
    • Add the amount to the balance
    • Print: "Deposited {amount}. New balance: {new_balance}"
    • Only process if amount > 0
  4. Create a method withdraw(self, amount):
    • Check if withdrawal would leave balance below BankAccount.min_balance
    • If sufficient funds: subtract amount and print "Withdrew {amount}. New balance: {new_balance}"
    • If insufficient: print "Insufficient funds or below minimum balance"
  5. Create a method display_account_info(self) that prints: "Owner: {owner}, Balance: {balance}, Bank: {bank_name}"

  6. Create two accounts and demonstrate all functionality:
    • Account 1: “Ali”, 100
    • Account 2: “Vali”, 50
    • Display Ali’s account info
    • Deposit 50 to Ali’s account
    • Withdraw 80 from Ali’s account (should work)
    • Display Vali’s account info
    • Withdraw 100 from Vali’s account (should fail - below minimum)
    • Print total accounts count

Expected Output

Owner: Ali, Balance: 100, Bank: Urgench Bank
Deposited 50. New balance: 150
Withdrew 80. New balance: 70
Owner: Vali, Balance: 50, Bank: Urgench Bank
Insufficient funds or below minimum balance
Total accounts created: 2

Problem 3 (Medium): Shopping Cart System

Create a ShoppingCart class that manages a collection of items for an online store.

Requirements:

  1. Define a class ShoppingCart with these class variables:
    • store_name = “Online Bazaar”
    • tax_rate = 0.08 (8% tax)
  2. Define __init__ with parameter customer_name:
    • Store as instance variable: self.customer_name
    • Initialize self.items as an empty list to hold cart items
    • Each item in the list should be a dictionary with keys: "name" and "price"
  3. Create a method add_item(self, item_name, price):
    • Append a dictionary {"name": item_name, "price": price} to self.items
    • Print: "Added {item_name} (${price}) to cart"
    • If price <= 0, print: "Invalid price. Must be greater than 0" and do not add the item
  4. Create a method remove_item(self, item_name):
    • Find and remove the first item with matching name from self.items
    • Print: "Removed {item_name} from cart" if found
    • Print: "Item '{item_name}' not found in cart" if not found
  5. Create a method get_subtotal(self) that returns the sum of all item prices

  6. Create a method get_total(self) that returns subtotal + (subtotal × tax_rate)

  7. Create a method display_cart(self) that prints:
    • "Cart for {customer_name} at {store_name}:"
    • Then each item on a new line: " - {item_name}: ${price}"
    • Then: "Subtotal: ${subtotal}"
    • Then: "Total (with tax): ${total}"
  8. Test with:
    • Cart for “Dilshod”
    • Add: “Laptop” ($999.99), “Mouse” ($25.50), “Keyboard” ($75.00)
    • Remove: “Mouse”
    • Display cart
    • Print subtotal and total separately

Input

Customer: "Dilshod"
Items to add:
- "Laptop", 999.99
- "Mouse", 25.50
- "Keyboard", 75.00
Item to remove: "Mouse"

Expected Output

Added Laptop ($999.99) to cart
Added Mouse ($25.5) to cart
Added Keyboard ($75.0) to cart
Removed Mouse from cart
Cart for Dilshod at Online Bazaar:
  - Laptop: $999.99
  - Keyboard: $75.0
Subtotal: $1074.99
Total (with tax): $1160.9892
1074.99
1160.9892

Problem 4 (Medium): Smart Thermostat System

Create a Thermostat class that monitors and controls room temperature with safety constraints and historical tracking.

Requirements:

Design a Thermostat class with the following capabilities:

Class-level Configuration:

  • Define temperature safety limits (min_temp = 15.0°C, max_temp = 30.0°C) as class variables
  • Track how many thermostats have been created using device_count

Instance Management:

  • Each thermostat should have a location identifier and track its current_temp
  • Maintain a history of all temperature readings in a readings list
  • When creating a thermostat, validate the initial_temp against safety limits. If the initial temperature is out of range, set it to min_temp

Core Methods:

  • set_temperature(new_temp) - Attempt to change temperature with validation (reject and keep current temperature if out of range)
  • get_average_temp() - Calculate the average of all historical readings
  • display_status() - Show current state including location, temperature, reading count, and average
  • is_comfortable() - Determine if current temperature is in a comfortable range (20-25°C)

Test Scenario: Create two thermostats:

  • One for “Living Room” starting at 22.0°C
  • One for “Garage” starting at 10.0°C (out of safe range)

Demonstrate:

  • Adjusting the Living Room to 26.5°C
  • Attempting to set Living Room to 35.0°C (should be rejected)
  • Displaying status for both devices
  • Checking comfort status of the Living Room
  • Displaying the total thermostat count

Expected Output

Temperature set to 22.0°C

Initial temperature out of range. Set to minimum.

Temperature set to 26.5°C

Temperature 35.0°C is out of allowed range (15.0-30.0)

Thermostat in Living Room: 26.5°C
Reading count: 2
Average temperature: 24.25°C

Thermostat in Garage: 15.0°C
Reading count: 1
Average temperature: 15.0°C

False

2

Problem 5 (Advanced): Student Transcript System

Create a StudentTranscript class that manages academic records with weighted GPA calculation and honors classification.

Requirements:

Design a StudentTranscript class with the following capabilities:

Class-level Configuration:

  • University name: “Al-Khwarizmi University”
  • Maximum courses per student: 6
  • Grade point mapping: {"A": 4.0, "B": 3.0, "C": 2.0, "D": 1.0, "F": 0.0}

Student Information:

  • Track student name, ID, and major
  • Maintain a list of completed courses, where each course stores: name, credits (1-4), and letter grade
  • The list of completed courses should be optional

Core Methods:

  • add_course(course_name, credits, grade) - Add a course with validation (check grade, credits, and max courses)
  • calculate_gpa() - Compute weighted GPA based on credits and grades (rounded to 2 decimal places)
  • get_standing(gpa=None) - Return academic standing (≥3.5 Excellent, ≥3.0 Good, ≥2.0 Satisfactory, ≥1.0 Poor, <1.0 Failing). (should work with both internally calculated and provided GPA)
  • get_honors_status() - Determine honors eligibility based on GPA and course count (min 4 courses; ≥3.8 Highest Honors, ≥3.5 High Honors, ≥3.0 Honors, else No Honors)
  • generate_transcript() - Display formatted academic record: header with university name, student info (name, ID, major), separator line, each course (name, credits, grade), separator line, total credits, GPA (x.xx/4.00), and standing

Test Scenario: Create a student: “Dilshod Rahimov”, ID “2024001”, major “Computer Science”

Initialize with courses (one invalid):

  • Programming I (3 credits, A)
  • Math (4 credits, B)
  • Invalid (5 credits, A) - should be rejected

Then add:

  • Database Systems (3 credits, A)
  • Algorithms (3 credits, B)
  • Invalid Grade (3 credits, E) - should be rejected
  • Networks (3 credits, A)

Generate the transcript and check honors status.

Expected Output

Invalid course skipped: {'name': 'Invalid', 'credits': 5, 'grade': 'A'}

Added Database Systems (3 credits): A
Added Algorithms (3 credits): B
Invalid grade E. Must be A, B, C, D, or F
Added Networks (3 credits): A

OFFICIAL TRANSCRIPT - Al-Khwarizmi University
Student: Dilshod Rahimov | ID: 2024001 | Major: Computer Science
--------------------------------------------------
  Programming I        3 cr  Grade: A
  Math                 4 cr  Grade: B
  Database Systems     3 cr  Grade: A
  Algorithms           3 cr  Grade: B
  Networks             3 cr  Grade: A
--------------------------------------------------
Total Credits: 16
GPA: 3.56/4.00
Standing: Excellent

Honors: High Honors