1 Lab Objectives
2 Understanding Casts
3 Working with Extended Interfaces
4 Handling Errors
5 If You Finish Early

Lab 3–Standard: Understanding Types and Casts in Java

1 Lab Objectives

  1. Understand extended interfaces in Java

  2. Understand casting in Java

  3. Review the binary search tree implementation from class

The binary search tree implementation we developed in class had a lot of details in it, many of them essential to programming well in Java. This lab has you work with that code to understand those issues.

Several of the problems ask you to write down a couple of sentences describing your understanding of a problem. Doing these will make sure you understand the issues – take them seriously!. If you can’t put your understanding into words, you don’t understand the concept well enough yet. Ask for help either in lab or in office hours. If you are more comfortable in a language other than English, answer these first in your preferred language then try to translate them.

This is a great lab to work on with other students, if you wish. Just make sure that everyone participates in explaining what you are observing in prose.

Before you start, download the BST implementation to your computer.

2 Understanding Casts

There are several places in which the implementation has to use casts to tell Java that an object should be viewed as a different type than what Java would have assumed. Your goal in this section is to understand why these casts are necessary.

3 Working with Extended Interfaces

At least some of the challenges we face in this implementation come from the fact that we have one interface (IBST) extending another (ISet). This led to some of the apparent inconsistencies in the types.

One way to avoid the casts of the previous section is to try to copy all the method headers from ISet to IBST. The starter file has a commented out line showing how you might do this.

4 Handling Errors

The implementation has to include a largestElt method in the MtBST class, even though this method does not make sense on an empty tree. Your goal in this section is to understand why that method is there.

5 If You Finish Early

Spend time reviewing the BST code and make sure you understand it. You should be able to articulate where functions exploit the BST invariant and how functions preserve the BST invariant. Try writing answers to each of these down in prose. If you can’t, come see someone for help.