Write a standalone method named countletters that returns


Part A:

  1. Which of the following is always private?

    a. constructor                b. instance variables

    c. setters                      d. to_s method 
  2. Which symbol is used in Windows and Unix to redirect STDOUT to a file?

    a. >                    b. <                    c. <<                    d. 2> 
  3. Which method is automatically called when a class object is printed with the print method?

    a. new              b. super              c. to_i              d. to_s  
  4. What is the output of this print statement? Explain your answer or show your work.

    print "2" * 3, "\n"

    a. 222                    b. 33                   c. 6

    d. An error message is written to STDERR. 
  5. *After these assignment statements

    x = 5.64

    y = "inches"

    Which of these print statements is correct? Explain your answer or show your work.

    a. print x + y, "\n"          b. print x.to_i + y, "\n"

    c. print x, y, '\n'            d. print "%5.2f %s\n" % [x, y]  
  6. *What is output? Explain your answer or show your work.

    x = 43

    y = 109

    print "%08b %02x %d" % [x & y, x | y, x ^ y]

    a. 01000110 29 70         b. 00101001 6f 70

    c. 00101001 6f 70         d. 01101111 6f 41  
  7. *What is output? Explain your answer or show your work.

    "!@#".each_byte do |c|

       print "%02x" % [c]

    end

    a. 212223         b. 214023         c. 336435         d. 336699  
  8. *What is the output? Explain your answer or show your work.

    n = 38

    i = 9

    while i < 100

      print n <=> i

      i *= 3

    end 

    a. -1-11           b. 11-1           c. 92781           d. truetruefalse  
  9. *Which of these methods swaps the top two elements of the input array? (The two top elements are the elements with the highest indices.) Explain your answer or show your work.

    a. def swap(input)       b. def swap(input)

         s = input.pop            s = input.pop

         t = input.pop            t = input.pop

         input.push(s)            input.push(t) 

         input.push(t)            input.push(s)

       end                      end

    c. def swap(input)      d. def swap(input)

         s = input.shift         t = input.shift

         t = input.pop           s = input.shift

         input.push(s)           input.unshift(t)

         input.push(g)           input.unshift(s)

       end                      end  
  10. Which test is used to check if inheritance makes sense?

    a. is-a           b. has-a           c. substitution           d. collection 

Part B: Predict the Output.

Predict the output by performing a hand variable trace. For problems 1, 2, and 3, the output is worth 20% of the credit; the explanation of how you obtained your output is worth 80%. In each case, you may check your answer by running the Ruby script.

Predict the output and explain how you obtained the answer:

def f(n)

  (2..n).each do |m|

    if n % m == 0

      return m

    end

  end

end

print f(35)

Part C:

The Car and SportsCar objects both drive on a straight-line track of unlimited length. The       only difference between Car and SportsCar objects is that a sportscar has greater acceleration and maximum speed. Predict the output and explain how you obtained your answer:

== File: Car.rb ==========================

class Car

  attr_reader :vin, :color, :location,

    :speed, :max_speed, :acceleration

  def initialize(the_vin, the_color)

    @vin = the_vin

    @color = the_color

    @location = 0

    @speed = 0

    @max_speed = 100

    @acceleration = 10

  end

  def press_accelerator

    if @speed < @max_speed

      @speed += @acceleration

    end

  end

  def press_brake

    if @speed > 0

      @speed -= 20

    end

  end

  def drive(the_time)

    @location += @speed * the_time

  end

  def to_s

    return "car: Color=#{@color}, VIN=#{@vin}"

         end

end

== File: SportsCar.rb ====================

require './Car'

        class SportsCar < Car

 def initialize(the_vin, the_color)

    super(the_vin, the_color)

    @max_speed = 200

    @acceleration = 20

  end

  def to_s

    return "sports" + super

  end  

end

== File: Test.rb =============

require './Car'

require './SportsCar'

c1 = Car.new('1G6AF5SX6D0125409', 'silver')

print c1, "\n"

(1..5).each do |i|

  c1.press_accelerator

end

c1.drive(60)

(1..3).each do |i|

  c1.press_brake

end

c1.drive(60)

print "c1 location: ", c1.location, "\n"

c2 = Car.new('2H7BG5TY2E0243862', 'red')

print c2, "\n"

(1..5).each do |i|

  c2.press_accelerator

end

c2.drive(60)

(1..3).each do |i|

  c2.press_brake

end

c2.drive(60)

print "c2 location: ", c2.location, "\n"

==========================================

Part D: Find the Errors.

Find the errors in the following class and test script. There are about 5 errors in each class.

== File: CelestialObject.rb ==========================

class Celestial_Object

  def initialize(the_name)

    @name = the_name

  end

  def name

    return @name

  end

  def name<-(the_name)

    @name = the_name

  def toString

    return "Name of Celestial Object: %s % name

  end

end

== File: Planet.rb ===================================

require 'CelestialObject'

class Planet > CelestialObject

  attr_accessor :day, year

  def initialize(the_name, the_day, the_year)

    super(the_name)

    @day = the_day

    @year = the_year

 end

  def to_s

    return super + "\n" +

      ("Length of Day: %d\nLength of Year: %d" %

      @day, @year)

  end

end

== File: Star.rb =====================================

require './CelestialObject'

class Star < CelestialObject

  attr-accessor :declination, :r_ascension, :magnitude

  def initialize(the_name, the_dec, the_asc, the_mag)

    super(the_name)

    @declination = the_dec

    @r_ascension = the_asc

    @magnitude = the_mag

  end

  def to_s

    return super + "\n" +

      ("Declination: %s\n" +

       "Right Ascension: %s\n" +

       "Magnitude: %6.2f") %

      [@declination, @r_ascension; magnitude]

  end

== File: Test.rb =====================================

require './Planet'

require './Star'

p1 = Planet.initialize("Mars', 24.7, 687.0)

print p1, "\n\n"

s1 = Star.new("Spica:", %Q=-11deg 9' 41"/,

  "13h 25m 12s, 0.98)

print s1, '\n'

======================================================

Part E: Write a Standalone Method and a Unit Test Class

  1. Write a standalone method named count_letters that returns the number of occurrences of a specified letter lines in an input string.

    Running this test code should produce the output shown:

state = %Q/mississippi/

print count_letters(state, "i"), "\n"

print count_letters(state, "p"), "\n"

print count_letters(state, "w"), "\n"

# Output:

4

2

0

  1. Write a unit test class that tests the count_letters method.  Either use this Unit Test Template for your test class or create a Minitest test class as shown in the 1/12 Lecture Notes.

 

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: Write a standalone method named countletters that returns
Reference No:- TGS01008613

Expected delivery within 24 Hours