Module 3 - Class Writing
Module 3 - Class Writing
Andrew Rosen
1 Assignment 1
Our first assignment will reinforce the difference between static and non-
static variables and methods. This one is the easiest.
Create a class Room. We will use Room objects to record the number of
people in a building. A room is defined by the number of people it holds, but
in addition, we also want to know the total number of people in all Rooms.
That’s something that is shared among all the Rooms, so that’s a static
variable. The class has the attributes:
• addOneToRoom– adds a person to the room and also increases the value
of totalNumber (right? you’ve added one person to the room, so not
only does the number of people in this room increase, so does the
total number of people from every room).
1
2 Exercise 2
Create a ComplexNumber class to represent a complex number. A complex
number is a number√that’s of the form a+bi, where a and b are real numbers
(a, b ∈ R), but i = −1, also called the imaginary unit.1 Check Wikipedia
if you need a refresher.
The fields for a ComplexNumber should be a and b, . Create a constructor
that initializes values for both a and b. You do not need a variable to
represent i, the imaginary unit.
Create instance methods add, subtract, multiply, and divide. Each
of these methods will take in one other ComplexNumber and performs the
stated operation to produce a new ComplexNumber.
For example, the add method should look like this:
public ComplexNumber add ( ComplexNumber other ) {
Demo by creating some complex numbers and testing them out.
Write a main method for the class that will allow two players to play a
game of tic-tac-toe.
1
This is a bit of a misnomer, as it implies it doesn’t exist.
2
4 Grading
The first two problems are worth 25 points each. Tic-tac-toe is worth 50
points.