IB Computer Science Unit 1
IB Computer Science Unit 2
IB Computer Science Unit 3
IB Computer Science Unit 4
IB Computer Science Unit 5
IB Computer Science Unit 6

IB Computer Science Data Objects in Java

IB Computer Science Learning Goals

In this IB Computer Science lesson you will be learning about:

  • Using classes to create data objects
  • Manipulate arrays of objects

What is a data structure?

A data structure in programming is a way to store and organize data. They provide a way to manage large amounts of data effectively. You have already used a data structure in grade 11 and earlier in this course: An array is probably the simplest form of a data structure available to a programmer. You can read an store large sections of data in your program and do something with them.

Example:

  • Suppose you had the names and ages of 10 people in a file and you wanted to print out the name and age of the youngest person
  • You would create an array to store the names and create an array to store the ages. These pair of arrays are called parallel arrays.
  • Search through the array of ages to find the location of the smallest value
  • Display on the screen the values of both arrays at the found index.
IB Computer Science Parallel Arrays

Objects instead of Parallel Arrays

An alternate approach to using two arrays in the above example is to create a custom data object to represent the data.

This new data type is called different things in different books, or programming languages. They might be referred to as a

  • REcord
  • Structure
  • Data Object

Data objects or Records are usually used in data base programs where there is a large amount of similar data that stored in files, read, and searched through.

  • Suppose you had a car dealership that needed to store information about cars. You might design a data object as follows
  • Suppose you wanted to create a phonebook. You could create an array of Data Objects representing each household

The next part of the lesson will model the example above using Data Objects instead of Parallel Arrays

 

Creating Data Objects

To create an object in Java you must write it in a separate Class than your main program.

It does NOT have to be in the same folder as your main program but for simplicity sake for the time being just save everything in the same folder.

Create the Student Object:

  • Create a new Class called Student.
  • The name you choose is important, as that is the name you will have to use when creating the objects in your main program.
  • This class will NOT have a main method
  •  It will only contain the data you wish the Student Object to store: name & age.
  •  The keyword public in front of the variable type is letting Java know that the programmer using this class is allowed to access those variables. The variables inside a data object are often called fields or attributes.
    Creating a Data Objects
IB Computer Science Java Define Class

Using Data Objects

The class you created above was a template that tells the program how all objects of that type are to be created. To use this class in your program you will first need to create an instance of your data object.

The general rule for creating instances of an object is as follows:

ClassName instanceName = new ClassName()

Or on separate lines as

ClassName instanceName;
instanceName = new ClassName()

To use the data stored inside this object you will use the instance name you create with the dot operator.

instanceName.fieldName

Some sample code you could use for the Student objects in shown below.

IB Computer Science Java Use Data Object

This code created two instances of a Student Object called s1 and s2. It assigned values to the names and ages of those students.

You can use the fields in your main program just like you would any other variable as long as you put the object name out in front of the field name

Arrays of Data Objects

If you wanted to you could even created arrays of Data Objects instead of just arrays of primitive data types. This would be useful in our student example where you were reading values from a file.

IB Computer Science Array of Data Objects

Its important to note when you create the array, it doesn’t actually create any instances of objects, it only creates space in memory for those data objects.

  • The variable s points to those memory locations, so you will have to create the objects separately.
  • If you forget to create the objects you might get a nullpointerException in your program

Passing Data Objects as Parameters to Methods

Like arrays, objects are passed by reference. That is they pass the memory location to the method and not the actual data. Any changes to the data in that memory location will be reflected everywhere in the program.

IB Computer Science Methods with Objects

The values in s1 changed even though you never made a statement saying s1.name = something or s1.age = something.

Other Examples

Here are the classes that would need to be created in the other examples talked about at the start

Suppose you had a car dealership that needed to store information about cars. You might design a data object as follows:

IB Computer Science Java Car Object

Suppose you wanted to create a phonebook. You could create an array of Data Objects representing each household

IB Computer Science House Object

Hockey Team Object

Suppose you had data for a hockey team

#
Last Name
First Name
G
A
PTS

You could create a data object that looks as follows

IB Computer Science Java Hockey Team Object

Lets assume all the data was in a file, with the first line being the number of players

Here is the code that would create all the objects and read in the data from the file

Notice how I used a loop to create the data objects after the array declaration

Complete the following as Practice

Create data objects when writing these programs

1. Attendance
An elementary School, which has grades 1 through eight, has requested your assistance with their attendance procedures.

The input file will contain eight lines of data, one line for each grade. The data for each grade consists of the Teacher’s name, the number of students in the class, the number of students absent that day and the number of students late that day (in that order). Each of this information is separated by a single space.

The output will contain five lines of data. The lines, in order will contain the following information:

  • The total number of students in the school
  • The total number of students absent
  • The teacher’s name of the grade with the best attendance. The best attendance is calculated as the highest percentage of students that are not absent.
  •  The average number of students late per class, roundest to the nearest whole number

2. Movie Database
Create a record data object that will be used to store movie data (Max 5 fields in the record, you can choose what you want to store).

  •  Create a GUI that will let the user enter information about a movie and write that data to a file.
  •  Create another GUI that will read the information from a file and let the user enter a search String. If you find information about that movie then display the data. If multiple movies match the criteria, display them all.

Looking to Learn More about Computer Science and Coding?

Check out our programing in python courses that focus on high school level coding.  

  • Grade 11 Computer Science ICS3U
  • Grade 12 Computer Science ICS4U

These courses are complete with interactive coding lessons, teacher led videos, and more practice questions with complete solutions

Return To International Baccalaureate Computer Science Main Page