Home
Preface
Chapter Selection
Programming Examples
Homework Problems
Homework Solutions MEA's
Search Useful Links

Homework Solutions

Chapter 6 Homework Solutions

6.1 hello from ClassA
hello from ClassB
method A from ClassB

6.2 hello from ClassA
hello from ClassB
hello from ClassC
method A from ClassB
method A from ClassC

6.3 hello from ClassA
hello from ClassB
hello from ClassC
method B from ClassC

6.4 a) Polygon has 13 sides
b) Hexagon has 6 sides
c) Polygon has 6 sides

6.5 As seen in Exercise 4, the fact that a class and all its subclasses share the value for a class variable means we may change the value of a class variable even when we do not intend do. In the Polygon class, we initially had the number of sides for a polygon stored in the class variable numSides to have a value of 12. In the Hexagon class, we access numSides and change the value to 6. The value of numSides is now set to 6 even for Polygon objects.

6.6 “eval false; 3” “eval true; nil”

6.7
class Integer
	  def fib
	    if self < 1 return
	    num1 = 0
	    num2 = 1
	    (self -1).times do
	      temp = num1 + num2
	      num1 = num2
	      num2 = temp
	    end
	    num1
	  end
	end
6.8 Open ended. Solutions will vary.

6.9
 for num in (1..50)
    num.fib.isEven?
  end
6.10 The problem is in line 9 of the Airplane class. The object variable @flightNum stores an Integer value and in line 9, we attempt to add this Integer value to the String "Flight Number ==>". A String and an Integer are unmixable data types and we are not allowed to add them directly. In order to correct this, we must first convert the value in @flightNum to a String. To correct the error, line 9 should look like this:
	puts "Flight Number ==>" + @flightNum.to_s
6.11 The way the current class is set up, we are not allowed to access the @flightNum object variable outside the Airplane class. In order to fix this error, we must add the following line of code to the class:
	attr_accessor:flightNum
6.12
	
class Integer 
def fact
  if self<1
     return  end
    product=self
  x=self
  (self-1).times do
    product = product * (x-1)
   x = x-1
   end #end loop
   puts product
  end #end fact methodend
 #end class
6.13
     class Slope
	  def initialize (x1, y1, x2, y2)
	    @x1 = x1
	    @y1 = y1
	    @x2 = x2
	    @y2 = y2
	    @slope = (y1-y2)/(x1-x2)
	    puts "The slope is "  + @slope.to_s
	  end
     end  
6.14
class String
    def invert
      newstring = String.new()
      self.each_char() {|letter|
        newstring = letter + newstring
      }
      return newstring
    end
  end
6.15
1  class Ball

2   def initialize (color, size, texture)
3    @attribute1 = color
4    @attribute2 = size
5    @attribute3 = texture
6   end  

7   def display 
8    puts "The Ball is: " + @attribute1 + “, " + @attribute2 + “, " + @attribute3
9   end
  
10 end

1	require 'Ball'
2	newBall = Ball.new(‘green’, ‘small’, ‘bouncy’)
3	newBall.display
6.16
class Number_comparison
  def initialize (num1, num2)
    @num1 = num1
    @num2 = num2
    if @num1 > @num2
      puts @num1.to_s + " is the larger number."
    else
      if @num1 < @num2
        puts  @num2.to_s + " is the larger number."
      else
        puts  "The numbers are equal."
      end
    end
  end
end  
6.17
class String_comparison

    def initialize (string1, string2)
     @string1 = string1
     @string2 = string2
     if @string1.length == @string2.length
     	puts "The strings have the same length."
     else
	puts "The strings do not have the same length."
	end

    end  
6.18 Need to set object variables.

6.19 Solution: For part a, for example –

6.20
	
class Inv_number < Inventory
  def initialize(item, maker, number)
      super
      @number=number #stored as integer
  end
  
  def display
      super.display
      puts "Number ==>" + @number.to_s
  end
  
end

newInv = Inventory.new("computer", "IBM")
newInvNum = Inv_number.new("computer", "IBM", 15)
6.21 The class will not be able to access the attributes and methods that are defined in the superclass. This will negate the whole point of inheritance.

6.22 Class: Ball (radius, color, texture)
Instance1: Bowling ball – 5 inch, black, hard.
Instance2: Tennis ball – 1.5 inch, green, felt.

Subclass: Basketball (radius, color, texture, indoor/outdoor, level)
Instance1: Child’s basketball – 4 inch, orange, rubber, outdoor, child.
Instance2: NBA basketball – 5.5 inch, brown, leather, indoor, NBA.