CS 1110/1111: Introduction to Programming

Lecture 29

Announcements

Test 2 grading has been delayed by unanticipated forces outside our control. We anticipate discussing it on Friday instead of today.

From the test itself: You may not discuss any part of the content or format of this exam with anyone before it is graded. Discussing it before it is graded is a violation of the Honor Code. We will thus delay all conversation about the exam until such time as grading is complete.

Talking Points

In chapter 8, we already discussed 8.8 and will ignore 8.5, 8.6, 8.9, and 8.11. That leaves just 8.1–8.4, 8.7–8.8, and 8.10.

8.1: Instance members vs static members

Fields and methods are collectively called members of a class.

Each object has is own copy of the instance members and a shared copy of the static members.

public class ChessPiece {
    private int row; // an instance member: each chess piece has its own row field
    private int col; // an instance member: each chess piece has its own col field

    private static int pieceCount; // a static member: all pieces share this count
    
    // constructors are always instance methods because they talk about one object
    public ChessPiece() { ... }
    
    // moving happens to just one piece at a time, so it is an instance member
    public void moveTo(int r, int c) { ... }
    
    // a game of chess is not restricted to just one piece, so it is a static member
    public static void playChess() { ... }
}

8.2, 8.3, 8.7: Using classes as types

Classes can be used anywhere a type is needed, including as fields, parameters and return types. Often it is wiser to make a class to do a task than to try to force an existing type to handle a new roll.

public class SportsTeam {
    // we could use String or even int to represent the players on a team,
    // but a team isn't made up of Strings it's made up of Players,
    // so let's add a Player type in a new Player.java file
    private ArrayList<Player> members;
}
public class Player {
    // even if we only have a name right now, having the Player class
    // lets us add other things (stats, methods, etc) later if we wish.
    private String name;
}

When the members of one class are themselves classes, we call that aggregation.

Classes can also be members of themselves:

public class President {
    String name;
    // Every president can keep track of the president that came before
    private President predecessor;
}
President name predecessor James Knox Polk John Tyler William Henry Harrison String String String President name predecessor President name predecessor

8.4: Overriding toString

Every object gets the methods of the built-in Object class for free. We can override them to do what we want to do instead.

The public String toString() method is easy to override: just return a String.

public class Player {
    private String name;
    private int number;
    
    public String toString() {
        return name + " (player number " + this.number + ")";
    }
}

You can also override the public boolean equals(Object o) and public MyClassName clone() methods, but doing so is not very straightforward. Sections 8.5 and 8.6 in the book give not-quite-right ways to do this; we'll ignore those sections, instead preferring the correct way, which you'll see in CS 2110.

From Class

3pm (001): slides, SportsTeam.java, Person.java, Mascot.java.

1pm (002): slides, SportsTeam.java, Player.java, Person.java.

Copyright © 2014 by Luther Tychonievich. All rights reserved.