CS 145 Lab 8 – 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 lab. The exchange of new ideas and perspectives is not an opportunity you want to rob yourself of.
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 an otherwise overwhelming task.
Checkpoints 1 and 2
Solve the following problems:
- Write an object to represent two-dimensional
Point
s. Include the following:- A default constructor that initializes the point to sit at the origin.
- A constructor that accepts a
Point
‘s initial x- and y-coordinates. - Getters that return the x- and y-coordinates.
- A
toString
method that returns a String of the form(x-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. 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 (not at the origin), and repeatedly rotate and scale it. After each transformation, print it to the console, separating each
(x, y)
with a comma. Paste the result list into Desmos. - Write a class
RoundRobin
that can be used to fairly distribute responsibility among a group of individuals. It manages rotating through the group. Include the following:- A default constructor that initializes the group to be empty.
- An
add
method that accepts as aString
the name of someone to add to the group. When added, the person should be queued such that they will be chosen as the next responsible. - A
size
method that returns as an int the number of people in the group. - A
rotate
method that returns as aString
the next responsible person. This person becomes the least responsible, while the person just “behind” is the next responsible.
Also write a main method that constructs a
RoundRobin
, adds some names, and rotates through it a few times. Print the names of the responsible parties.