CS 145 Lab 9 – Objects
Prelab
- Read chapter 8!
Reminder
- Show your completed lab 8 checkpoints to your instructor or TA in the first 20 minutes of this lab.
Object-orientation
As we’ve seen in lecture, objects are the marriage of data (instance variables, declared at the class level) and methods. Up until now, our programming has been action- or procedure-oriented. We wrote methods that performed some actions. Now, we’ll write actors. The difference may appear subtle at first, but the goal is to build software that is better organized.
Bear in mind, object-oriented programming languages are a response to the headaches of producing big, big software in procedure-oriented languages. You’ve likely not coded any big, big software, so you will probably not be able to truly appreciate its elegance without a good open mind.
What’s Different
When you sit down to solve a problem using object-oriented principles, follow these steps:
- Write a class, per usual.
- What do objects of your class need to be able to do? What should you be able to ask of them? You’ve been using Scanner, String, Random, and Robot, so you have a lot of experience asking objects to do things. Make methods out of these actions, but omit the keyword static.
- Whenever you have A variable that needs to persist across method calls, i.e., that is part of the state of your object, like a sequence of characters are part of a String object, declare a private instance variable at the class level.
- Write a constructor to initialize your instance variables. This gets called when someone says “new MyObject(…).”
Problems
Complete two of the following checkpoints:
- Write a class representing a 3-D mathematical vector. Include methods for getting the vector’s length, adding an argument vector, normalizing, and calculating a dot product between this vector and an argument vector. Consult your favorite Internet encyclopedia for mathematical details. Write a short main to test.
- Write a class called Country. Countries can befriend each other, go to war with each other, and host Olympics. Countries also have a name, population, gross domestic product, and so on. Write methods for at least the three actions mentioned above, altering the Country’s state in appropriate ways. The first two methods should accept another Country as an argument. Write a short main method that tells a little story of a Country or two.
- Write a Raffle class. When raffles are constructed, they are told of their maximum possible number of contestants. They support adding in new names and drawing out random names. Once a name is drawn, it is removed from the raffle. We shouldn’t have to sell all the tickets before we randomly draw a name. Write a short main to test.
- Write an Interval class, which represents a number that we know to be in a certain range. For example, you may know someone is [30, 35] years old. Offer methods for adding on an exact number to this interval, adding on another interval, and multiplying this interval by another. Consult your favorite Internet encyclopedia for mathematical details. (Addition is easy, but multiplication is more involved.) Write a short main to test.
TODO
- Complete homework 3.