0% found this document useful (0 votes)
2 views

Module 3 - Class Writing

The document outlines three programming assignments focusing on object-oriented concepts in Java. The first assignment involves creating a Room class to manage the number of people in a building, the second requires developing a ComplexNumber class for arithmetic operations on complex numbers, and the third task is to implement a TicTacToe class for a game of tic-tac-toe. Each assignment has specific methods and functionalities to be implemented, with a grading scheme provided at the end.

Uploaded by

borat.asdfg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module 3 - Class Writing

The document outlines three programming assignments focusing on object-oriented concepts in Java. The first assignment involves creating a Room class to manage the number of people in a building, the second requires developing a ComplexNumber class for arithmetic operations on complex numbers, and the third task is to implement a TicTacToe class for a game of tic-tac-toe. Each assignment has specific methods and functionalities to be implemented, with a grading scheme provided at the end.

Uploaded by

borat.asdfg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

More Classes

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:

• numberInRoom – the number of people in a room

• totalNumber – the total number of people in all rooms. Since this is


shared among all instances of Room it is a static variable. Make it
private too.

The class has the following methods:

• 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).

• removeOneFromRoom – removes a person from the room, ensuring that


numberInRoom does not go below zero, and decreases the value of
totalNumber as needed.

• getNumber –returns the number of people in this Room.

• getTotal –a static method that returns the total number of people in


all Room objects.

Create a few Room classes and confirm they work.

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.

3 Inconsistent Naming Scheme 3


Create a class called TicTacToe to represent a game of tic-tac-toe. Represent
the state of the game board with a 2D char array (3 rows and columns) and
a variable to tell who’s turn it is.
Methods:

• Add a move, which marks a square with ‘X’ or ‘O’

• Display the board

• Tell whose turn it is

• Detect if there is a winner

• Tell the user(s) who the winner is

• Restart the game

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.

You might also like