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.
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.
Person A types.
Write an object to represent two-dimensional
Points. 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
parameterswith 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.
toStringmethod that returns a String of the form
- A method
scalethat accepts a scale factor parameter. Scaling multiplicatively extends or shrinks a a
Point‘s distance between the point and the origin.
- A method
translatethat accepts x- and y-offset parameters. Translating shifts a
Pointby the offsets.
- A method
rotatethat 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 angleand
rotatedY = 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.
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.
addmethod that accepts as a
Stringthe 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 appropriate
ArrayListmethod makes this a one-liner.
sizemethod that returns as an
intthe number of names in the list.
rotatemethod that returns as a
Stringthe 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
carbonite, you’ll generate the following sequence when you call
rotate 8 times:
carbonite banana apple carbonite banana apple carbonite banana