10.7 Exercises

  1. The class shown in Example 10-4 is used to keep track of the inventory for a company. It allows the user to assign a name and a manufacturer to each item. Write a class ItemNumber that inherits from Item that lets the user input a number of items owned for each item. Create an instance of the Item class and then an instance of the ItemNumber class.

    Example 10-4. Code for Exercise 1
         1 class Item
         2 	def initialize(item, maker)
         3 		@item = item
         4 		@maker = maker
         5 	end
         6 
         7 	def display
         8 		puts "Item ==> " + @item
         9 		puts "Maker ==> " + @maker
        10 	end
        11 end
    
  2. Explain polymorphism.

  3. Define OOP.

  4. A university has three kinds of students: full-time students, part-time students, and off-campus students. Each student has the following information: name, address, phone number, Social Security number, student ID, and GPA. The full-time students should take at least 12 credits per semester. The part-time students should take less than 12 credits per semester. The off-campus students have no limit on credits per semester. The tuition fee for full-time students is $8,500 for up to 18 credits per semester and $600 for every credit over 18 credits. The tuition fee for part-time students is $750 per credit. The tuition fee for off-campus students is $520 per credit. Write a program that lists student information and calculates their tuition fees.

  5. Write a program for the grocery store example described in Section 10.5.2, “Grocery Store.”

Get Computer Science Programming Basics in Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.