Professional Documents
Culture Documents
Tutorial 1
Introduction
Key Concepts of Unit 1
Marking Scheme and TMA1 support
Introduction to Wawasan Learn
Introduction to Computing II
Tutorial 1 – 29/7/07
Tutorial 2 – 26/8/07 (TMA 1 due)
Tutorial 3 – 23/9/07 (TMA 2 due)
Tutorial 4 – 21/10/07(TMA 3 due)
Tutorial 5 – 18/11/07
*Please refer to your course material on page
10 for Suggested weekly reading
Course Assessment
Type Marks
TMA 1 15%
TMA 2 15%
TMA 3 20%
100%
Course Materials
0 1 2 3 4 5 6 7 8 9
scores 79 87 94 82 67 98 87 81 74 91
Note: the logical address is the name you give the physical
address, because it’s easier to remember and use in coding
Arrays
one physical
Variable’s name
address
logical address
Type of Arrays
Primitive
double[ ] myList = new double[10];
double[ ] myList = {1.9, 2.9, 3.4, 3.5};
int[ ] marks = new int[20];
char[ ] chars={'a','A','4','F','D','P'};
Non-primitive
String[ ] name = new String[5];
Item[ ] items = new Item[1000];
Integer Arrays Demo
public class ArrayDemo
{
public static void main(String[] args)
{
//declare an int array with 5 elements
int testScores[] = new int [5];
//populate the elements
testScores[0] = 75;
testScores[1] = 65;
testScores[2] = 80;
testScores[3] = 95;
testScores[4] = 75;
//display element contents
System.out.println("test score 1 = " + testScores[0]);
System.out.println("test score 2 = " + testScores[1]);
System.out.println("test score 3 = " + testScores[2]);
System.out.println("test score 4 = " + testScores[3]);
System.out.println("test score 5 = " + testScores[4]);
//compute average using length attributes
double average = 0;
for (int i = 0; i < testScores.length; i ++)
{
average += testScores[i];
}
average = average / 5;
System.out.println("average score is" + average);
}
}
String Arrays Demo
public class StringArray
{
public static void main(string[] args)
{
int i;
String names[];//declare the array
names = new String[4];//allocate the array
//String names[ ] = {“Evi”,”Yui”,”Anita”,”Kim”};
//assign values to each array element
names[0] = "Evi";
names[1] = "yui";
names[2] = "anita";
names[3] = "kim";
//display the names
for(i=0; i< names.length; i++)
System.out.println("names[" + i +"] is " + names[i]);
}
}
Problems
}
public static void printArray(double[ ] list){
for(int i=0; i < list.length; i++){
System.out.println(list[i]);
}
}
}
Example: Pass by Value
public class Test{
public static void main(String[] args){
int x =1; // x repersents an int values
int[] y = new int[10]; // y represents an array of int values
}
}
TMA 1 – Question 1b
public class ArrayLength{
public static void main(String[] args){
double[ ] myList=new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
printArray(myList); //valid call to method printArray
}
public static void printArray(double[ ] list){
for(int i=0; i < list.length; i++){
System.out.println(list[i]);
}
}
}
Two Dimensional Arrays
A one-dimensional array stores a list of elements
A two-dimensional array can be thought of as a table of
elements, with rows and columns
one two
dimension dimensions
Two Dimensional
Arrays,cont
To be precise, a two-dimensional array in Java is an array of
arrays
A two-dimensional array is declared by specifying the size of
each dimension separately:
int[][] scores = new int[12][50];
A two-dimensional array element is referenced using two
index values
value = scores[3][6]
The array stored in one row or column can be specified using
one index
TMA 1 – Question 1c (i)
0 1 2 3 4 0 1 2 3 4 0 1 2
0 0 0 1 2 3
1 1 1 4 5 6
2 2 7 2 7 8 9
3 3 3 10 11 12
4 4 int[][] array = {
{1, 2, 3},
matrix = new int[5][5]; matrix[2][1] = 7; {4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
rows columns
Example: Two Dimensional
Arrays
public class TwoDimensional{
public static void main(String[] args){
int[][] array={
{1,2,3},
{4,5,6},
{7,8,9},
{10,11,12,2,7}
};
//print the table
for(int row=0;row<array.length;row++){
for(int col=0;col<array[row].length;col++)
System.out.print(array[row][col] + "\t");
System.out.println();
}
System.out.println("the number of column in the 4th row of
array:" + array[3].length);
System.out.println("the number of rows in array:" +
array.length);
}
}
TMA 1 – Question 1c (ii)&(iii)
while(last>=first){
int mid=(first+last)/2;
if(key<list[mid])
last=mid-1;
else if(key==list[mid])
return mid;
else
first=mid+1;
}
return -first-1;
}
}
TMA 1 – Question 3b