Lists Review and Performance : 03/31/2023
Linked Lists : 03/30/2023
Algorithms and Lists : 03/29/2023
Lambda Expressions : 03/24/2023
Anonymous Classes : 03/23/2023
Practice with Interfaces : 03/22/2023
Implementing Interfaces : 03/21/2023
Using Interfaces : 03/20/2023
Working with Exceptions : 03/10/2023
Throwing Exceptions : 03/09/2023
Catching Exceptions : 03/08/2023
References and Polymorphism : 03/07/2023
References : 03/06/2023
Data Modeling 2 : 03/03/2023
Equality and Object Copying : 03/02/2023
Polymorphism : 03/01/2023
Inheritance : 02/28/2023
Data Modeling 1 : 02/27/2023
Companion Objects : 02/24/2023
Encapsulation : 02/23/2023
Constructors : 02/22/2023
Objects, Continued : 02/21/2023
Introduction to Objects : 02/20/2023
Compilation and Immutability : 02/17/2023
Practice with Collections : 02/16/2023
Maps and Sets : 02/15/2023
Lists and Type Parameters : 02/14/2023
Imports and Libraries : 02/13/2023
Multidimensional Arrays : 02/10/2023
Practice with Strings : 02/09/2023
null : 02/08/2023
Algorithms and Strings : 02/07/2023
Strings : 02/06/2023
Functions and Algorithms : 02/03/2023
Practice with Functions : 02/02/2023
More About Functions : 02/01/2023
Errors and Debugging : 01/31/2023
Functions : 01/30/2023
Practice with Loops and Algorithms : 01/27/2023
Algorithms : 01/26/2023
Loops : 01/25/2023
Arrays : 01/24/2023
Compound Conditionals : 01/23/2023
Conditional Expressions and Statements : 01/20/2023
Operations on Variables : 01/19/2023
Variables and Types : 01/18/2023
Welcome to CS 124 : 01/17/2023
Data Modeling 1
You’ve seen how objects combine state and behavior, and how we can construct them and protect their private state.
In this lesson we’ll put all of those capabilities to use in modeling a real-world entity!
After all, that’s one of the things that objects are for…
First, as a warm-up, let’s construct the imperative logic that we need to check the board.
We’ll then use this later in our solution!
As our example we’ll model a game of tic-tac-toe.
Whenever we model something using a Kotlin class, we want to consider:
- What does it store? This will guide our design of the object instance variables.
- What does it do? This will guide our design of the object instance methods.
Note that frequently these considerations overlap.
We may need to add state to support some action that we realize that we want our class to support.
But, anyway, we’ll get there.
Let’s get started!
But before we can even start designing our class, we need to choose a name!
That’s not as simple as it sounds!
Frequently deciding on a name can encourage you to give some initial productive consideration to exactly what it is that you’re modeling…
First, let’s think about what a class
modeling a tic-tac-toe game needs to store.
Let’s work through that together and begin designing our classes instance variables:
With an initial set of instance variables, let’s think through various things that we want our game model to be able to do.
This is not a complete list!
You may have other ideas of what to add:
- Let players add a mark to the board
- Check the board after each move to see if the game is over
- Ensure that players take turns correctly
- Display the board
Now let’s have some fun implementing these features!
Note that, as we go, we may find places where we need to add state, or create some helper functions.
We’ll see!
Cool!
You designed and implemented your first interesting Kotlin class.
That was fun!
Don’t worry—you’ll get a lot more practice.
More Practice
Need more practice? Head over to the practice page.