Kotlin
Java

Data Modeling 1
Java

Created By: Geoffrey Challen
/ Updated: 2021-09-20

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 Java objects are for...

Two-Dimensional Array Warmup

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!

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough

Tic-Tac-Toe

As our example we'll model a game of tic-tac-toe. Whenever we model something using a Java 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!

Class Name

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

Discuss possible names for the tic-tac-toe class. Include ones that are not specific enough, like Game or Board.

Object State

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:

Object Methods

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!

Implement the method that allows players to make a move. Use assert to make sure that the coordinates are valid. Don't check the state of the board yet—we'll add that method next.

Reuse the check board method developed previously. Talk about maintaining the state of the game, whether it is over or not, and checking that before each move.

Continue by adding a way to enforce turn-taking. Point out how this requires additional state, and make that state private.

Finally, add a method to display the board. Simple! And then stand back and marvel.

There It Is

Cool! You designed and implemented your first interesting Java class. That was fun! Don't worry—you'll get a lot more practice.

Java Records

Before we finish this lesson, I want to introduce you to a brand new Java feature. It's so new, that it's not an official part of Java yet—it's still in so-called preview mode in Java 15, which just came out. However, it's really exciting, and provides a much simpler way of working with classes.

Show how you can replace the code above with a record.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough