RECORDS

CONTENTS

1. Introduction
2. Simple record example
3. Inner classes and records


1. INTRODUCTION

Many imperative programming support the concept of a data structure known as a record (Ada, Pascal, Algol) or a structure (C). The idea is that where as an array is used to group together groups of data items of the same type a record is used to group together a number of data items which are not necessarily of the same type. In addition (unlike arrays) records can be recursive, i.e. they can contain data items of the same type as the record it self.

Java, although it supports arrays does not directly support records. The reson for this is that the concept of a record is an ancestor of the class concept. Put simply we can say that whereas a record contains only data items a class contains both data items and the operations required to manipulate that data. There is a liitle more to it than that, strictly speaking a data structure that contains both data and operations is what is known as an Abstract Data Type (ADT); what sets a class apart form an ADT is the idea of inheritance. Thus we trace the evolution of the class concept from records throught ADTs to class hierarchies. What ever the case the upshot of all this is that a class can be used to represent a record.



2. SIMPLE RECORD EXAMPLE

In the "record world" the classic example of a record is a day comprising a day, month and year; where the daya and year are integers and the month is a string. As a class we might express this as follows:

class dayRecord {
    int day;
    String month;
    int year;
    }

We can use records to store dates Etc. Some example code where this is done is given in Table 1.

// RECORD EXAMPLE
// Frans Coenen, 25 January 2000
// Department of Computer Science, The University of Liverpool.

import java.io.*; 
import java.util.*;

class DateRecord {
    int day;
    String month;
    int year;
    } 
	
class RecordExample {

    /* Main method  */

    public static void main(String[] args) throws IOException {
     
    	// Create record with hard coded input.
	
	DateRecord christmas = new DateRecord();
	addInfoToDateRecord1(christmas,25,"December",2000);
	
	// Create record with user provided input.
	
	DateRecord birthday = new DateRecord();
	addInfoToDateRecord2(birthday);
	
	// Output both
	
	outputDateRecord(christmas);
	outputDateRecord(birthday);
	}
	
    /* Input information provided as fornmal parameters into day record */
    
    public static void addInfoToDateRecord1(DateRecord reference, int newday, 
    		String newMonth, int newYear) {
        reference.day = newday;
	reference.month = newMonth;
	reference.year = newYear;
	}
	
    /* Input record information from user */
    
    public static void addInfoToDateRecord2(DateRecord reference) throws IOException {
    	int newDay, newYear;
	String newMonth;
	
	// Create BufferedReader class instance

        InputStreamReader input         = new InputStreamReader(System.in);
        BufferedReader    keyboardInput = new BufferedReader(input);
	
	// Invite input
	
	System.out.print("Enter day in the form `int String int': ");
	StringTokenizer data = new StringTokenizer(keyboardInput.readLine());
	newDay = new Integer(data.nextToken()).intValue();
	newMonth = data.nextToken();
	newYear = new Integer(data.nextToken()).intValue();
	System.out.println();
	
	// Create record
	
	addInfoToDateRecord1(reference,newDay,newMonth,newYear);
        }
    
    /* Output record */
    
    public static void outputDateRecord(DateRecord reference) {
    	System.out.println(reference.day + " " + reference.month + " " +
				reference.year);
	}
    }

Table 1: Record processing

Some points to note about this code:

Some sample output produced by the above code is given in Table 2.

$ java RecordExample
Enter day in the form `int String int': 15 May 1993

25 December 2000
15 May 1993           
             

Table 2: Sample output generate from record processing example code presented in Table 1



3. INNER CLASSES AND RECORDS

Java will allow one class to be nested within another class. Such a class is referred to as an inner class. This feature is useful since it allows you eo incorporate a class definition which represents a "record" into the calss that makes specific use of it. Inner classes used in this way, called a member class is just another field within its outer calss. Some exmaple code is presented in Table 3.

// RECORD EXAMPLE 2
// Frans Coenen, 25 January 2000
// Department of Computer Science, The University of Liverpool.

import java.io.*; 
import java.util.*;
	
class RecordExample2 {

    /* ---------- FIELDS --------- */
    
    class DateRecord {
    	int day;
    	String month;
    	int year;
    	} 
    private DateRecord date = new DateRecord();
    
    /* --------- METHODS --------- */	
    		
    /* Input information provided as fornmal parameters into day record */
    
    public void addInfoToDateRecord1(int newday, String newMonth, int newYear) {
        date.day = newday;
	date.month = newMonth;
	date.year = newYear;
	}
	
    /* Input record information from user */
    
    public void addInfoToDateRecord2() throws IOException {
    	int newDay, newYear;
	String newMonth;
	
	// Create BufferedReader class instance

        InputStreamReader input         = new InputStreamReader(System.in);
        BufferedReader    keyboardInput = new BufferedReader(input);
	
	// Invite input
	
	System.out.print("Enter day in the form `int String int': ");
	StringTokenizer data = new StringTokenizer(keyboardInput.readLine());
	newDay = new Integer(data.nextToken()).intValue();
	newMonth = data.nextToken();
	newYear = new Integer(data.nextToken()).intValue();
	System.out.println();
	
	// Create record
	
	addInfoToDateRecord1(newDay,newMonth,newYear);
        }
    
    /* Output record */
    
    public void outputDateRecord() {
    	System.out.println(date.day + " " + date.month + " " + date.year);
	}
    }

Table 3: Processing an inner class representing a record

The class presented in Table 3 also needs an application class to intercat with it (apart from that it performs exactly the same function as the code presented in table 1). Some appropriate application code is presented in Table 4.

// RECORD EXAMPLE 2 APPLICATION
// Frans Coenen, 25 January 2000
// Department of Computer Science, The University of Liverpool.

import java.io.*; 
import RecordExample2;
	
class RecordExample2App {
    		
    /* Main method  */

    public static void main(String[] args) throws IOException {
     
    	// Create record with hard coded input.
	
	RecordExample2 christmas = new RecordExample2();
	christmas.addInfoToDateRecord1(25,"December",2000);

	// Create record with user provided input.
	
	RecordExample2 birthday = new RecordExample2();
	birthday.addInfoToDateRecord2();
	
	// Output both
	
	christmas.outputDateRecord();
	birthday.outputDateRecord();
	}
    }

Table 4: Application class for class presented in Table 3

Note: Further information on linked lists of records is available.




Created and maintained by Frans Coenen. Last updated 01 February 2000