Ask Programming Language Expert

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.

 

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M91313747
  • Price:- $100

Guranteed 48 Hours Delivery, In Price:- $100

Have any Question?


Related Questions in Programming Language

Assignment - haskell program for regular expression

Assignment - Haskell Program for Regular Expression Matching Your assignment is to modify the slowgrep.hs Haskell program presented in class and the online notes, according to the instructions below. You may carry out th ...

Assignment task -q1 a the fibonacci numbers are the numbers

Assignment Task - Q1. (a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the ...

Question - create a microsoft word macro using vba visual

Question - Create a Microsoft Word macro using VBA (Visual Basic for Applications). Name the macro "highlight." The macro should highlight every third line of text in a document. (Imagine creating highlighting that will ...

Assignmentquestion onegiving the following code snippet

Assignment Question One Giving the following code snippet. What kind of errors you will get and how can you correct it. A. public class HelloJava { public static void main(String args[]) { int x=10; int y=2; System.out.p ...

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

Task silly name testeroverviewcontrol flow allows us to

Task: Silly Name Tester Overview Control flow allows us to alter the order in which our programs execute. Building on our knowledge of variables, we can now use control flow to create programs that perform more than just ...

Structs and enumsoverviewin this task you will create a

Structs and Enums Overview In this task you will create a knight database to help Camelot keep track of all of their knights. Instructions Lets get started. 1. What the topic 5 videos, these will guide you through buildi ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As