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…
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!
Declare and implement a function called gameOver.
gameOver receives a single 3x3 char[][] array as a parameter, representing a tic-tac-toe board,
with each cell containing either a X, a O, or a (blank space).
gameOver should return X if X has won the game, O if O has won the game, and a blank space (' ')
otherwise, including in the case of ties.
(Ties should probably be handled differently, but you can use a ' ' for now.)
Do not check the diagonals!
For those unfamiliar with Tic-Tac-Toe, here is link to the rules: https://en.wikipedia.org/wiki/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:
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:
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 Java class. That was fun! Don’t worry—you’ll get a lot more practice.
Define a public class named Counter with a single public instance method named increment.
increment takes no parameters and returns an int.
The first time it is called it should return 0, the next 1, the next 2, and so on.
Each instance of Counter should maintain its own private counter.
Before we finish this lesson, I want to introduce you to a new(ish) Java feature which was released in Java 16. However, it’s really exciting, and provides a much simpler way of working with classes.
As a reminder: In CS 124, in addition to teaching you how to write correct code, we also want to teach you how to write good code: Code that’s beautiful and elegant and that you and those you work with will be proud of.
There are multiple aspects of code quality that we’ll be introducing throughout the semester.
Previously we talked about ensuring beautiful code formatting using checkstyle, removing unnecessary debugging output, and eliminating dead code.
checkstyle rules are now enforced on homework and quizzes, and starting today, there will be a one-point deduction for correct submissions that produce unnecessary debugging output or contain dead code.
The next aspect of code quality we’ll discuss is pretty straightforward: avoiding overly-long code. By overly-long we mean just that: code that contains way, way more lines that necessary. Here our thresholds tend to be very generous, since we want you to feel free to explore various approaches. But still: at some point, if our solution needed 4 lines and you’ve authored 40, there’s probably something going wrong.
One thing to check if you encounter this error is whether you’re reimplementing a method that’s available as part of a library.
For example, if you reimplement the String split method, rather than just calling String.split, that could add a bunch of lines to your program.
It might still work, but there’s just no need to do this, and embedding implementations of widely-available library methods makes you look like an untrained programmer.
We’ll be warning you about extremely verbose programs starting today, and in a few weeks you’ll lose one point on the graded homework if you don’t reduce the length of unnecessarily-long programs. Let’s see an example of how that works using a familiar practice problem:
Create a public class named Restaurant.
Restaurant should store two pieces of data: a name (as a String) and a capacity (as an int).
Provide a public constructor that allows both fields to be set, with the name first.
Following the encapsulation pattern we have introduced, provide both a setter and a getter for the capacity as
getCapacity and similar.
Provide only a getter for the name as getName.
Finally, reject negative and zero capacity values and null names using assert.
Create a public class named Location.
Location should store three pieces of data: a latitude and a longitude, both as double values, and a
description as a String.
Provide a public constructor that allows all fields to be set, with the description first, followed by the
latitude and longitude.
Following the encapsulation pattern we have introduced, provide both a setter and a getter for the description as
setDescription and similar.
Provide only a getter for the latitude and longitude.
Finally, reject invalid latitude and longitude values and null descriptions using assert.
Valid longitude values are between -180.0 and 180.0, inclusive, while valid latitude values are between
-90.0 and 90.0, also inclusive.
Need more practice? Head over to the practice page.