Computing II
Tutorial 1
Prepared by: Ng Mee Mee
About Me
Name: Ng Mee Mee
Contact Number: 019-7644738
Email:
[email protected] Time available for telephone tutoring
From 9:00 am – 9:00 pm (Everyday)
Time NOT to call
After 9:00pm
Outlines
Introduction
Key Concepts of Unit 1
Marking Scheme and TMA1 support
Introduction to Wawasan Learn
Introduction to Computing II
TCC102 Computing II is a continuation of
TCC101 Computing I.
More advanced java programming concept is
introduced.
*Advanced Arrays ( Assignment 1-15%)
*OO features in classes ( Assignment 2-15%)
Basic input/output stream
*Graphical user interface (Assignment 3-20%)
Data Structures
Tutorial Schedule
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%
Final Exam 50%
100%
Course Materials
TCC 102 Computing II
Text Book
Malik, D S (2005) Java Programming: From
Problem Analysis to Program Design, Boston:
Thomson Course Technology. ISBN:0-619-
21608-5
WawasanLearn
Online Support (refer page 12)
Key Concepts of Unit 1
Apply simple arrays
Solve problems with multi-dimensional arrays
Apply searching algorithms on arrays
Linear search and binary search
Apply sorting algorithms
Insertion sort
Solve problem using recursion
TMA 1 (15%)
Individual Assignment
Submit hard copy of the TMA1 in word processed
(1.5 spacing) and must be labeled with your WOUID
and name.
Due on 26/8/07
Late submission
Over 7 days
Contact tutor before or on the due date
Over 8 – 14 days
Course Coordinator
Up to 21 days
Dean
Introducing Arrays
Array is a data structure that represents a collection of the
same types of data.
The entire array Each value has a numeric index
has a single name
0 1 2 3 4 5 6 7 8 9
scores 79 87 94 82 67 98 87 81 74 91
An array of size N is indexed from zero to N-1
This array holds 10 values that are indexed from 0 to 9
Arrays vs. Variables
A group of variable that a variable is a location
takes up more than one in memory where you
physical address, but store a value
has only one logical Each variable contains
address (name) one logical address and
Example: one physical address
int[ ] number= new Example:
int[10]; int number;
Note: the logical address is the name you give the physical
address, because it’s easier to remember and use in coding
Arrays
double[] myList = new double[10];
myList reference myList[0] 5.6
myList[1] 4.5 More than one
myList[2] 3.3 physical address
Array reference variable myList[3] 13.2
myList[4] 4
logical address myList[5] 34.33
myList[6] 34
Array element at myList[7] 45.45
index 5 myList[8] 99.993 Element value
myList[9] 11123
Variable
double list = 34;
list 34.0 value
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
Write a java program to calculate the
average marks of 5 students in a class
Write a java program to calculate the
average marks of 100 students in a class
Solution for Question 1
1. public class Question1 {
2.
3. public static void main(String[] args) {
4. double student1 = 67;
5. double student2 = 70;
6. double student3 = 30;
7. double student4 = 50;
8. double student5 = 78;
9.
10. double total = student1 + student2 + student3 + student4 + student5;
11. double average = total/5;
12.
13. System.out.println("the total mark of five students:" + total);
14. System.out.println("the average mark of five students:" + average);
15. }
16. }
Solution for Question 2
1. import javax.swing.*;
2. public class Question2
3. {
4. public static void main(String[ ] args)
5. {
6. String s1;
7. int i;
8. double total=0.00;
9. //int numbers[ ]; //declare the array
10. //numbers = new int[100]; //allocate the array
11. double students[ ] = new double[100];
12.
13. for(i = 0; i < students.length; i++) //enter the marks
14. {
15. s1 = JOptionPane.showInputDialog("Enter a mark:");
16. students[i] = Double.parseDouble(s1);
17. }
18. System.out.println("The average mark of 100 students ");
19. //display elements of arrays and the total
20. for(i = 0; i< students.length; i++)
21. {
22. System.out.print(" " + students[i]);
23. total = total + students[i];
24. }
25. System.out.print(" is " + total/students.length);
26. System.exit(0);
27. }
28. }
TMA 1 – Question 1a
Code to Read/Input Data into
Array
for (index = 0; index < sales.length;
index++)
sales[index] = console.nextDouble();
Code to Print/Output Array
for (index = 0; index < sales.length;
index++)
System.out.print(sales[index] + " ");
Passing Arrays to Methods vs.
Passing a Variable to Methods
For a parameter For a parameter
of an array type, of a primitive
the value of the type value, the
parameter actual value is
contains a passed.
reference to an changing the
array; this value of the
reference is local parameter
passed to the inside the method
method. does not affect
Any changes to the value of the
the array that variable outside
occur inside the the method.
method body will
Example: Passing Arrays to
Methods
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]);
}
}
}
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
m(x,y); //invoke m with argunment x and y
System.out.println("x is " + x);
System.out.println("y[0] is " + y[0]);
}
public static void m(int number, int[] numbers){
number=1001;//assign a new value to number
numbers[0]=5555;//assign a new value to numbers[0]
}
}
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)
Refer Malik textbook on page 570-577.
TMA 1 – Question 2a,2b & 2c
public class TwoDimensional{
public static void main(String[] args){
//define a constant for the array size
final int ARRAY_SIZE = 3;
int[][] array=new int[ARRAY_SIZE][ARRAY_SIZE];
//print the table
for(int row=0;row<array.length;row++){
for(int col=0;col<array[row].length;col++)
array[row][col] =row + col;
}
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();
}
}
}
Refer Malik textbook on page 570-577.
List Processing
List: A set of values of the same type.
Basic operations performed on a list:
Search list for given item.
Sort list.
Insert item in list.
Delete item from list.
Search
Necessary components to search a list:
Array containing the list.
Length of the list.
Item for which you are searching.
After search completed:
If item found, report “success” and return location
in array.
If item not found, report “failure.”
Sequential or Linear Search
Search the list for a given item starting with
the first element in the list
It continues comparing this item with the
elements in the list until either the item is
found, or the list has no more elements left to
compare with search item.
If a item is found, the linear
search returns the index of the
element in the array that matches
the item.
If no item is found, the search
returns 1.
Example: Linear Search
public class LinearSearch{
public static void main(String[ ] args){
int[ ] list={1,4,4,2,5,-3,6,2};
System.out.println(linearSearch(list,4));
System.out.println(linearSearch(list,-4));
System.out.println(linearSearch(list,-3));
}
public static int linearSearch(int[ ] list, int key){
for(int i=0; i <list.length; i++)
if(key==list[i])
return i;
return -1;
}
}
See Example 10.1 on page 613 (Malik text book)
TMA 1 – Question 3a
Refer Malik text book on page 610-614.
Binary Search
Can only be performed on a sorted list.
Search item is compared with middle
element of list.
If search item < middle element of list,
search is restricted to first half of the list.
If search item > middle element of list,
search is restricted to second half of the
list.
If search item = middle element, search is
complete.
Binary Search Algorithm
Determine whether 75 is in the list.
Example: Binary Search
public class BinarySearch{
public static void main(String[] args){
int[] list={0,10,20,30,40,50,60,70,80};
System.out.println(binarySearch(list,20));
System.out.println(binarySearch(list,60));
System.out.println(binarySearch(list,45));
}
public static int binarySearch(int[] list, int key){
int first=0;
int last=list.length-1;
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
Refer Malik text book on page 633-638
Insertion Sort
The insertion sort algorithm sorts the list by moving each element
to its proper place.
Insertion Sort
Insertion Sort
Insertion Sort
Insertion Sort
public static void insertionSort(int[] list,
int noOfElements)
{
int firstOutOfOrder, location;
int temp;
for (firstOutOfOrder = 1;
firstOutOfOrder < noOfElements;
firstOutOfOrder++)
if (list[firstOutOfOrder] < list[firstOutOfOrder - 1])
{
temp = list[firstOutOfOrder];
location = firstOutOfOrder;
do
{
list[location] = list[location - 1];
location--;
}
while(location > 0 && list[location - 1] > temp);
list[location] = temp;
}
} //end insertionSort
TMA 1 – Question 3c
Refer Malik text book on page 623-630
TMA 1 – Question 4a & 4b
Refer Malik text book 633-638