9.5 Exercises

  1. Create two classes to represent the following two objects: televisions and speakers. Include an initialize function and several methods to interact with your objects.

  2. Given two Cartesian points (x 1, y 1) and (x 2, y 2), the slope of the line segment connecting them is given by the formula (y 2 - y 1)/(x 2 - x 1).

    Write a class that represents a Cartesian point. Define a method find_slope that takes in a Cartesian point object and finds the slope between the two points. Test your class with the following:

    1. (0,0) (3,4)

    2. (2,3) (6,5)

    3. (2,2) (2,7)

    What happens in the last case? Why does that happen?

  3. Define a class that compares two numbers and outputs the larger one. Test your solution.

  4. Briefly explain the code illustrated in Example 9-10.

    Example 9-10. Code for Exercise 4
         1 class Profile
         2 	def initialize(name, phone_number)
         3 		@name = name
         4 		@phone_number = phone_number
         5 	end
         6 
         7 	def display
         8 		puts "Name ==> " + @name
         9 		puts "Phone number ==> " + @phone_number.to_s
        10 	end
        11 end
    
  5. Write a Student class that contains a student’s name, gender, phone number, and exam score. It should also include the initialize, accumulated_score, and display methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.253.223