CS 145 Lab 6 – Objects
First, if you have checkpoints left over from last lab, get them inspected during the first 15 minutes of this lab. No credit will be awarded past these 15 minutes.
Don’t forget to work in pairs! Where possible, please work with someone that you did not work with last week. The exchange of new ideas and perspectives is not an opportunity of which you want to rob yourself.
Objective
In this lab you will learn about bundling data and its related code into objects. Large software is built out of hundreds of interacting objects, each of which is managing a small chunk of a complex task.
Checkpoint 1
Person A types.
Write an object to represent two-dimensional Point
s. Include the following:
- A constructor that accepts a
Point
‘s initial x- and y-coordinates. - A default constructor that initializes the point to sit at the origin. Instead of assigning the instance variables directly, call your fellow constructor with something like this, replacing
parameters
with the appropriate actual parameters:/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError) Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec' from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem' from ./coderay:24:in `
' - Getters that return the x- and y-coordinates.
- A
toString
method that returns a String of the formx-coordinate, y-coordinate
. - A method
scale
that accepts a scale factor parameter. Scaling multiplicatively extends or shrinks a aPoint
‘s distance between the point and the origin. - A method
translate
that accepts x- and y-offset parameters. Translating shifts aPoint
by the offsets. - A method
rotate
that accepts an angle parameter in degrees. It rotates the point about the origin. We won’t derive the formulas for rotation here. The new coordinates can be found with the following expressions:rotatedX = x * cos angle - y * sin angle
androtatedY = x * sin angle + y * cos angle
.
Now write a main method that produces a spiral:
- construct a point at the origin
- translate it 5 units on the x-axis
- repeatedly rotate and scale the point
After each rotate/scale transformation, print it to the console, separating each x, y
with a linebreak. Paste the result list into Desmos.
Checkpoint 2
Person B types.
Write a class RoundRobinList
that can be used to repeatedly and fairly cycle through a list of people or things. Include the following:
- A default constructor that initializes the list to be empty. You will need some sort of backing list data structure like an
ArrayList
, as well as some way of knowing what name is next to be drawn. - An
add
method that accepts as aString
the name of the person or thing to add to the group. When added, the name should be queued such that it will be the next name chosen. An appropriateArrayList
method makes this a one-liner. - A
size
method that returns as anint
the number of names in the list. - A
rotate
method that returns as aString
the next name. The succeeding name is now the next to be drawn, and the name just drawn won’t be drawn again until all names after and before it have been drawn again.
Also write a main method that constructs a RoundRobinList
, adds some names, and rotates through it a few times. Print the names.
For example, if you create a list and add to it apple
, banana
, and carbonite
, you’ll generate the following sequence when you call rotate
8 times:
carbonite
banana
apple
carbonite
banana
apple
carbonite
banana