| Exercises 1-3 are based on the following lines of code. Write down the expected output for each exercise.
1 class ClassA
2 def initialize
3 puts "hello from ClassA"
4 end
5 def methodA
6 puts "method A from ClassA"
7 end
8 def methodB
9 puts "method B from ClassA"
10 end
11 end #end ClassA
12 class ClassB < ClassA
13 def initialize
14 super
15 puts "hello from ClassB"
16 end
17 def methodA
18 puts "method A from ClassB"
19 end
20 def methodB
21 super
22 puts "method B from ClassB"
23 end
24 end #end ClassB
25 class ClassC < ClassB
26 def initialize
27 super
28 puts "hello from ClassC"
29 end
30 def methodA
31 super
32 puts "method A from ClassC"
33 end
33 def methodB
34 puts "method B from ClassC"
35 end
36 end #end ClassC
|
| 6.1 |
b1 = ClassB.new
b1.methodA
|
| 6.2 |
c1 = ClassC.new
c1.methodA
|
| 6.3 |
c2 = ClassC.new
c2.methodB
|
| 6.4 |
1 class Polygon
2 @@numSides = 12
3 def initialize
4 @@numSides = @@numSides + 1
5 end
6 def display
7 puts "Polygon has " + @@numSides.to_s + " sides"
8 end
9 end #end of Polygon Class
10 class Hexagon < Polygon
11 def initialize
12 @@numSides = 6
13 end
14 def display
15 puts "Hexagon has " + @@numSides.to_s + " sides"
16 end
17 end #end of Hexagon Class
Using the Polygon and Hexagon classes above, write down the output at the indicated spots of the following lines of code
p1 = Polygon.new
p1.display #Part A
h1 = Hexagon.new
h1.display #Part B
p1.display #Part C
|
| 6.5 | What is a disadvantage of the use of class variables?
|
|
|
| 6.6 | What does the following function return if you run it with nil?
With any other value?
1 def foo (a)
2 if a
3 puts “eval true”
4 return
5 else
6 puts “eval false”
7 return 3
8 end
9 end
|
|
|
| 6.7 | Add Fibonacci sequence functionality to the Integer class as
such: The integer that is being acted upon is the nth number in the sequence, and will be returned. IE: “3.fib” will return “2”. Note: the keyword “self” is how you refer to the number being accessed. For the same example “3.fib”, “puts self” would output “3.” Be sure to check for proper input (ie: positive value greater than 0).
|
|
|
| 6.8 | Create a class to represent an object of your choice. Include
an initialize function and several methods to interact with your object.
|
|
|
| 6.9 | Using the isEven? function from this chapter, create a loop
that tells whether each of the first 50 numbers in the Fibonacci sequence are odd or even.
|
|
|
| Exercises 10 and 11 are based on the following lines of code
1 class Plane
2 def initialize(airline,flightNum)
3 @airline=airline #stored as String
4 @flightNum=flightNum #stored as Integer
5 end
6
7 def display
8 puts "Airline ==> " + @airline
9 puts "Flight Number ==>" + @flightNum
10 end
11 end
|
| 6.10 | Suppose we ran the following lines of code outside the
Airplane class
A1 = Plane.new("US",2907)
A1.display
An error is returned by the compiler. What is the problem? What should be changed in the Airplane class to fix this error?
|
|
|
| 6.11 | Suppose we ran the following lines of code outside the
Airplane class
B1 = Plane.new("US",2907)
B1.flightNum
An error is returned by the compiler. What is the problem? What should be changed in the Airplane class to fix this error?
|
|
|
| 6.12 | Add a factorial method to the Integer class that will find the
factorial of a given integer. For example:
irb(main):001:0> 4.fact
=> 24
Note: the keyword “self” is how you refer to the number being accessed. For the example “4.fact”, “puts self” would output “4.” Be sure to check for proper input (ie: positive value greater than 0).
|
|
|
| 6.13 | Given two Cartesian points (x1, y1) and (x2, y2), the slope of the
line segment connecting them is given by the formula
Write an object that computes the slope of the line segment that connects two given points. Test your object with the following:
a. (0,0) (3,4)
b. (2,3) (6,5)
c. (2,2) (2,7) What happens here? Why does that happen?
|
|
|
| 6.14 | Write a method that can be applied to a String object to invert
it. For example,
irb(main):001:0> string = "Hello"
=> "Hello"
irb(main):002:0> string.invert
=> "olleH"
|
| 6.15 | Think of an object you own. Turn it into a Ruby object by
filling out the following with the appropriate information, then create an instance of it:
1 class <object name>
2
3 def initialize (<attribute 1>, <attribute 2>, <attribute 3>)
4
5 @attribute1 = <attribute 1>
6 @attribute2 = <attribute 2>
7 @attribute3 = <attribute 3>
8 end
9
10 def display
11 puts "The <object name> is: " + @attribute1 + ", " +
12 @attribute2 + ", " + @attribute3
13 end
14 end
15
|
| 6.16 | Define an object that compares two numbers and outputs the
bigger one. Test your solution.
|
|
|
| 6.17 | Define an object that compares two strings and checks if they
have the same length. Test your solution.
|
|
|
| 6.18 | What is wrong with the following code?
1 class Car
2 def initialize (color, model, year)
3 @color
4 @model
5 @year
6 end
7
8 def display
9 puts "Color ==> " + @color
10 puts "Model ==> " + @model
11 puts "Year ==> " + @year
12 end
13 end
|
| 6.19 | Draw an inheritance tree that describes:
a. How to set a table for Breakfast, Lunch, Dinner
b. Different kinds of sandwiches
c. Different types of cars
d. Different species within an animal family
|
|
|
| 6.20 | The following class is used to keep track of equipment a
company has. It allows the user to assign an item name and manufacturer to each item.
1 class Inventory
2 def initialize(item, maker)
3 @item=item #stored as String
4 @maker=maker #stored as String
5 end
6
7 def display
8 puts "Item ==> " + @item
9 puts "Maker ==>" + @maker
10 end
11
12 end
Write a class Inv_number that inherits from Inventory, and that lets the user input a number of items owned for each item. Create an instance of the Inventory class, and then an instance of the Inv_number class.
|
|
|
| 6.21 | What happens if you do not include the super keyword in an
inherited class?
|
|
|
| 6.22 | Give an example of a real-world thing that could be
represented by a class. Give two examples of instances of this class. Give an example of a subclass that would inherit from this class. Give two instances of this subclass.
|
|
|