teaching machines

CS 145 Lab 8 – Objects

November 26, 2014 by . Filed under cs145, fall 2014, labs.

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:

  1. Write an object to represent two-dimensional  Points. 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 a Point‘s distance between the point and the origin.
    • A method translate that accepts x- and y-offset parameters. Translating shifts a Point 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 and rotatedY = 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.

  2. 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 a String 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 a String 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.