Kotlinlearncs.online LogoJava

    ← Prev

    Index

    Next →

    Kotlin
    Java
    • Inheritance : 30

    • Data Modeling 1 : 29

    • Static : 28

    • Encapsulation : 27

    • Constructors : 26

    • Objects, Continued : 25

    • Introduction to Objects : 24

    • Compilation and Type Inference : 23

    • Practice with Collections : 22

    • Maps and Sets : 21

    • Lists and Type Parameters : 20

    • Imports and Libraries : 19

    • Multidimensional Arrays : 18

    • Practice with Strings : 17

    • null : 16

    • Algorithms and Strings : 15

    • Strings : 14

    • Functions and Algorithms : 13

    • Practice with Functions : 12

    • More About Functions : 11

    • Errors and Debugging : 10

    • Functions : 9

    • Practice with Loops and Algorithms : 8

    • Algorithms I : 7

    • Loops : 6

    • Arrays : 5

    • Compound Conditionals : 4

    • Conditional Expressions and Statements : 3

    • Operations on Variables : 2

    • Variables and Types : 1

    • Hello, world! : 0

    Data Modeling 1

    public class TicTacToeGame {
    // Let's model a game!
    }

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

    Created By: Geoffrey Challen
    / Version: 2020.9.0

    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.

    Tic-Tac-Toe
    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!

    Class Name
    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…

    // What should we name our class?

    Object State
    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:

    // Add game instance variables

    Object Methods
    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:

    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 add mark method
    // Implement check board method
    // Enforce turn taking between players
    // Add a method to display the board

    There It Is
    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.

    Practice: Counter Object

    Created By: Geoffrey Challen
    / Version: 2020.9.0

    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.

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

    public class Person {
    private String name;
    private double age;
    Person(String setName, double setAge) {
    name = setName;
    age = setAge;
    }
    public String getName() {
    return name;
    }
    public double getAge() {
    return age;
    }
    }

    Homework: Restaurant Getters and Setters

    Created By: Geoffrey Challen
    / Version: 2021.9.0

    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.

    More Practice

    Need more practice? Head over to the practice page.