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 Java Simple Arrays

IB Computer Science Learning Goals

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

  • Writing programs that use one dimensional arrays of primitive data types

Array Basics

Recall that an array is like a long list of variables of the same type, but handles the naming of those variables in a nice, compact way. Arrays in combination with loops are a very powerful tool in programming and allow more complex problems to be solved
Array Fundamentals
It is best to view an array as a list of variables which has a 2 part name

  • 1 part as an identifier
  • 1 part as an index

Before an array can be used in Java, it must be declared, just like any other variable must be declared. Like strings, index values start at 0 for the first variable of the list

Array Declaration

To declare an array in Java, you must indicate the type of values that array will store, an identifier for the name of the array, and how many values that array can store. Arrays can be any type of variable we have previously discussed and later you will learn how to create an array of objects

The syntax for an array declaration is as follows:

type [ ] identifier = new type [ size ] ;

IB Computer Science Declare Array in java

In the example above 3 arrays were created called: values, temperature, name. By making those declarations, space has been reserved in memory for the specified number of variables

For example, in the second example an array called temperature consisting of 5 variables all of type double, and named :

temperature [ 0 ], temperature [ 1 ] , temperature [ 2 ], temperature [ 3 ], temperature [ 4 ]

The numbers in the square brackets are called the index of the array. temperature [ 0 ], temperature [ 1 ] . . . etc. are called the elements of the array

Assigning Values to Arrays

There are several different ways to assign values to array elements

We will first look at how to assign values at the time of declaration

IB Computer Science Assign Values to Array Java

Looking at the second example again, the following values were assigned to the elements in the array


temperature [ 0 ] = 7.4, temperature [ 1 ] = 3.5, temperature [ 2 ] = -5.1, temperature [ 3 ] = 12.4, temperature [ 4 ] = 28.5

Array elements can also be assigned values at any point in the program. The following program assigns a random number to each value for an array

IB Computer Science Assign array values in loop java

Using Arrays in a Java Program

The following example can be used to ask the user for the names and ages of 5 people and store them in memory. The program then calculates the average age and displays the spread of data in a nicely formatted way on the screen.

Here is a program that generates 15 random integers from the user and stores them in array. It will display all the numbers entered on 1 line and count how many numbers are less than 25, and display that result on the screen.

Using Arrays in Methods

IMPORTANT -> Java passes all primitive data types such as int, char, double and even Strings by
value, however arrays and objects are a different matter. These are passed by reference.
When java passes something by reference it basically passes the memory address of the argument,
so any modification made to that memory address will be reflected in the rest of the program

Example

Here is a method that makes all the values in an array equal to zero

IB Computer Science Java Passing Array by Reference

Before the method is called the data stored in the array is a bunch of random numbers. After the method is called the data stored in the array is a bunch of zero’s.

There was no need to return a new array since the array b just pointed to the memory location of the passed array values, so when you made modifications to array b, it changed the data in that location which array a was already pointing to back in your main method.

Be careful with objects and arrays that are passed by reference, it could make your programming
easier but it may be the source of why your program is not working as expected.

IB Computer Science Practice Questions

1. Dice Rolls
Create an array named diceRolls. This array has 100 integer elements. Initialize each element with a random number representing the roll of a die. The die has the possible rolls of 1, 2, 3, 4, 5, or 6.

  • Count how many of each possible roll are contained in the array. That is, how many 1’s are in the array, how many 2s, etc.
  • Display the results on the screen as a horizontal bar graph using “*”


2. Names & Addresses
You are going to be storing names and addresses of people in 2 separate arrays. To make testing easier, define all the values inside your program (don’t ask the user to input them). Have 5 different names and corresponding addresses. Write a menu program that allows the user to choose the following options:

  • Option #1 – User enters a name from the keyboard and your program displays the address
  • Option #2 – User enters a name from the keyboard and a new address for that name and your program updates the data
  • Option #3 – Display the entire list
  • Option #4 – Quit
    Your program should keep looping and reload the menu until the user chooses to quit.

3. Statistics
Generate an array of 40 random decimal numbers. Write methods to accomplish the following tasks.

  • Display a percentage of the list. Accept the array, and the percentage as an argument to the method.
  • Average Value. Accept the array as an argument and return the average.
  • The location in the array of the maximum value. Accept the array as an argument and return the index
  • The smallest TWO numbers. Accept the array as an argument and return an array with 2 elements representing the two smallest numbers.
  • Another option of getting the two smallest numbers back is to send a second array as an argument and then use how java passes arrays by reference to get the data back into the main program. Try both ways.

4. Rotating an Array
Write a method that can shift an array by N spaces. A positive number will shift the array down. Values that go off the end of the array will wrap back to the beginning. The user should be able to enter positive or negative values for the shift. Output the array before the shift and after the shift.

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