Write a standalone method named count_letters

Assignment Help Programming Languages
Reference no: EM13755451

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.

 

Reference no: EM13755451

Questions Cloud

Determine the architect of a global information system : Describe the four (4) organizational structures, as discussed, which determine the architect of a global information system
What is the required rate of return on stock : Atlas Mines has adopted a policy of increasing the annual dividend on its common stock at a constant rate of 2.75 percent annually. The firm just paid an annual dividend of $1.67. What will the dividend be six years from now?
Determine the depreciation-straight-line method : Determine the depreciation for each of the three years, using the straight-line method, the double declining-balance method, the sum-of-the-years'-digits method, and the units-of-production method.
The extra credit assignment is as follows for leg 500 : The extra credit assignment is as follows for LEG 500:Please write up to two pages analyzing how each of the ethical theories would view the use of militarized police forces against citizens while exercising their right to protest.
Write a standalone method named count_letters : Write a standalone method named count_letters that returns the number of occurrences of a specified letter lines in an input string
Explain the role of buddhism in medieval japan : Explain the role of Buddhism in medieval Japan. What were the characteristics of "feudalism" in Japan? Describe the relationship between Shinto and Buddhism in Japan.
Compute the amount of dividends : Compute the amount of dividends that must have been paid to preferred stockholders and common stockholders in each of the years, given the following independent assumptions:
Open a competing company against the original company? : 10 new shareholders purchase their shares when the company was in a financial difficulty. What kind of equitable remedies could be used by those 2 new shareholders? If one of these 2 new shareholders has started a competing company, is that ok? Is a ..
Financial statements for two companies : Research and find financial statements for two companies of your choosing. Drawing on information from this module and the course, analyze the statements and write an essay summarizing which of the two is a better investment.

Reviews

Write a Review

Programming Languages Questions & Answers

  Write a haskell program to calculates a balanced partition

Write a program in Haskell which calculates a balanced partition of N items where each item has a value between 0 and K such that the difference b/w the sum of the values of first partition,

  Create an application to run in the amazon ec2 service

In this project you will create an application to run in the Amazon EC2 service and you will also create a client that can run on local machine and access your application.

  Explain the process to develop a web page locally

Explain the process to develop a Web page locally

  Write functions

These 14 questions covers java class, Array, link list , generic class.

  Programming assignment

If the user wants to read the input from a file, then the output will also go into a different file . If the user wants to read the input interactively, then the output will go to the screen .

  Write a prolog program using swi proglog

Write a Prolog program using swi proglog

  Create a custom application using eclipse

Create a custom Application Using Eclipse Android Development

  Create a application using the mvc architecture

create a application using the MVC architecture. No scripting elements are allowed in JSP pages.

  Develops bespoke solutions for the rubber industry

Develops bespoke solutions for the rubber industry

  Design a program that models the worms behavior

Design a program that models the worm's behavior.

  Writing a class

Build a class for a type called Fraction

  Design a program that assigns seats on an airplane

Write a program that allows an instructor to keep a grade book and also design and implement a program that assigns seats on an airplane.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd