When complete, fill out this form.
For these questions, write a short snippet of code that meets
the requirement. In cases where the question mentions a "given"
data value, use the variable given
to refer to it (instead of re-writing
the information).
class PizzaOven
def cook_pizza
"mmm 'za"
end
end
2. Define a class called Student
which is instantiated with a "name" value and which has a method name
that returns this value
class Student
attr_reader :name
def initialize(name)
@name = name
end
given.map {|num| num*2}.inject(:+)
git init
Given a hypothetical Pizza
class which has an instance method is_tasty?
that always
returns true, write a simple Minitest test that tests this behavior.
pizza = Pizza.new
assert pizza.is_tasty?, "For some reason, this pizza is not tasty!"
Suppose the Pizza
class also has a method style
which randomly returns one of:
"supreme"
, "mediterranean"
, or "cheese"
. Write a test that confirms that the
returned pizza style is within this list.
assert ["supreme","mediterranean","cheese"].include?(pizza.style)
git add filename
git commit -m "Message of the updates made to the file"
Define a Student
class which, when created, has an attitude
attribute.
attitude
should start out with the value "cheerful", and the Student
class should
provide a "reader" method that allows us to access the value of its attitude
.
Additionally, add an assign_homework
method to Student
. When assigned_homework
is
invoked, if the student's attitude
is "cheerful"
, it should become "dubious"
. If
the value is currently "dubious"
it should become "perturbed"
. If the value is currently
"perturbed"
, it should become "dazed"
. Assigning homework to a "dazed"
student has no
effect.
class Student
attr_reader :attitude
def initialize(attitude="cheerful")
@attitude = attitude
end
def assign_homework
@attitude = "dubious" if @attitude == "cheerful"
@attitude = "perturbed" if @attitude == "dubious"
@attitude = "dazed" if @attitude == "perturbed"
end
end
Building on the Student
class from the previous example, update the assign_homework
method
to accept an argument. The argument will be a String
containing a short description of the
assignment.
Then, add an assignments
method to Student
. assignments
should return a list of
all the assignments that have been given, separated by a comma and a space. For example:
class Student
attr_reader :attitude
def initialize(attitude="cheerful")
@attitude = attitude
@list_of_assignments = String.new
end
def assign_homework(assignment)
if @attitude == "cheerful"
@attitude = "dubious"
elsif @attitude == "dubious"
@attitude = "perturbed"
elsif @attitude == "perturbed"
@attitude = "dazed"
end
@assignment = assignment
end
def assignments
if @list_of_assignments.empty?
@list_of_assignments = @assignment
else
@list_of_assignments += ", #{@assignment}"
end
@list_of_assignments
end
end
Create a new class SurlyStudent
which inherits from the Student
class above.
However, whenever you ask a SurlyStudent
for their attitude, they always respond
with "disgruntled"
class SurlyStudent < Student
def initialize
@attitude = "disgruntled"
@list_of_assignments = String.new
end
def assign_homework(assignment)
@attitude = "disgruntled"
assignments(assignment)
end
end
s1 = Student.new
s2 = Student.new
s3 = Student.new
s1.assign_homework("linked list")
s1.assign_homework("sorting algos")
s2.assign_homework("write a c compiler")
s2.assign_homework("write a pacman game")
s3.assign_homework("headcount")
s3.assign_homework("sales engine")
students = [s1,s2,s3]
students.map {|student| student.assignments}.join
=> "linked list, sorting algos, write a c compiler, write a pacman game, headcount, sales engine"