In this IB Computer Science lesson you will be learning about:
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:
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
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.
The next part of the lesson will model the example above using Data Objects instead of Parallel Arrays
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:
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.
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
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.
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.
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.
The values in s1 changed even though you never made a statement saying s1.name = something or s1.age = something.
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:
Suppose you wanted to create a phonebook. You could create an array of Data Objects representing each household
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
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
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:
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).
Check out our programing in python courses that focus on high school level coding.
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