Lecture overview
Classes of Data
Examples of Data
Anatomy of the Class Definition
5.3.5

Lecture: Data Definitions in Java

Design of simple classes and classes that contain objects in another class. Uses the tester library to display the data.

     DataDefinitions.java   

Lecture overview

The first programming example had shown us that in Java we can work with some basic types of data (they are called primitive types), namely integers, (defined as int that represent natural numbers, and boolean values that can only be true or false.

Our goal now is to learn how to represent more complex information in Java.

Classes of Data

The Design Recipe for Data Definitions says that if the information consists of several components, it should be represented by a structure. For example, here is the data definition for a book in a bookstore in DrRacket:

;; to represent a book in a bookstore

;; A Book is (make-book String Author Number)

(define-struct book (title author price))

 

;; to represent an author of a book (with the year of birth)

;; An Author is (make-author String Number)

(define-struct author (name yob))

The Design Recipe for Data Definitions tells us to follow up with concrete examples of data:

;; Examples:

(define pat (make-author "Pat Conroy" 1948))

(define beaches (make-book "Beaches" pat 20))

All this information can be represented concisely as a class diagram like this:

+---------------+

| Book          |

+---------------+

| String title  |

| Author author |--+

| Number price  |  |

+---------------+  |

                   v

            +-------------+

            | Author      |

            +-------------+

            | String name |

            | Number yob  |

            +-------------+

Indeed, we say that the class of all books is represented by the book struct and the class of all authors is represented by the author struct.

In Java, the class of books and the class of autors is specified by defining a class, the individual books and authors are called instances of their class, or objects that belong to that class.

The code below shows how this class diagram and our examples can be translated into a representation as Java classes (a Java class hierarchy):

// to represent a book in a bookstore

class Book{

  String title;

  Author author;

  int price;

 

  // the constructor

  Book(String title, Author author, int price){

    this.title = title;

    this.author = author;

    this.price = price;

  }

}

 

// to represent a author of a book in a bookstore

class Author{

  String name;

  int yob;

 

  // the constructor

  Author(String name, int yob){

    this.name = name;

    this.yob = yob;

  }

}

Examples of Data

The examples of data are defined in the class that uses our code, we call it Examples or ExamplesClassName indicating for what problem or for what class are we generating the examples.

// examples and tests for the class hierarchy that represents

// books and authors

class ExamplesBooks{

  ExamplesBooks(){}

 

  Author pat = new Author("Pat Conroy", 1948);

  Book beaches = new Book("Beaches", this.pat, 20);

}

Anatomy of the Class Definition

The class definition consists of the keyword class followed by the name we choose for this class of data.

Next are the definitions of fields of the class, similar to the fields or components of DrRacket structs. However, the field definitions provide both, the type of data that the field represents, and the name of the field, so we can refer to it. In DrRacket the information about the types of data the fields represent was given only in the comments.

We see that there are two types of fields in this definition: String and int. The String represents instances of the String class - similar to Strings in DrRacket. The int represents a primitive type of data whose values are natural numbers. We will also use two other primitive types: double that represent inexact numerical values and boolean whose values can be either true or false. In our examples we record the book price in whole dollars only (for convenience).

The form define-struct in DrRacket generated for us the constructor for the newly-defined struct (in our cases these were make-book and make-author). In Java we need to define the constructors explicitly, though for the time beeing, they will all look the same: the name of the class, then in parentheses the list of argument types and names, that mimic exactly the field definitions, then enclosed in curly braces the initialization statements that all look the same (this.fieldname = argumentname;) though we use the same names for the arguments as for the fields.

The Examples class shows us how are the instances of the class defined. We start by providing the type of data the variable will represent, followed by the variable name, then followed by the = sign and finally the definition of the desired object by invoking a constructor for its class with the desired argument values. The constructor invocation starts with the keyword new followed by the name of the class, and in parentheses the list of required argument values, ending with a semicolon.

If you run this program with the tester library, it will display the values of the variables defined in the Examples class.

Note: The variables defined in the Examples class are actually the fields of this class, but their values are specified within the class definition once and for all, and so the constructor for the Examples class does not consume any arguments and does not initialize any additional fields. Every instance ot the Examples class would have the fields with the same values. It does not matter to us, as this is only used once by the tester library to generate examples of data (and later run and evaluate tests).

Note: Finally, the keyword this that appears before the names of the fields in the Intializer statements, and in the definition of the example of the book Beach Music when referring to the earlier-defined author is there to remind us that we are talking about the field values of this object, the one we are defining or using.