Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

14
Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal

Transcript of Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

Page 1: Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

Lecture 5 of Computer Science II

Arrays

Instructor: Mr.Ahmed Al Astal

Page 2: Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

Page 2

Using Arrays

ca b

Storing Game Entries in an Array

The first application we study is for storing entries in an array—in particular, high score entries for a video game.we have decided to store high score entries, which is a simple application that presents some important data structuring concepts that we will use for other implementations in this book.

what we want to include in a high score entry?

1. an integer representing the score itself, which we will call score.2. name of the person earning this score, which we will simply call name.

Page 3: Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

Page 3

Using Arrays

ca b

Storing Game Entries in an Array

An illustration of an array of length ten storing references to six GameEntry objects in the cells from index 0 to 5, with the rest being null references.

Page 4: Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

Page 4

Using Arrays

ca b

Insertion

One of the most common updates we might want to make to the entries array of high scores is to add a new game entry.

add(e): Insert game entry e into the collection of high scores. If the collection is full, then e is added only if its score is higher than the lowest score in the set, and in this case, e replaces the entry with the lowest score.

Preparing to add a new GameEntry object to the entries array. In order to make room for the new reference, we have to shift the references to game entries with smaller scores than the new one to the right by one cell.

Page 5: Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

Page 5

Using Arrays

ca b

Insertion

Once we have identified the place in the entries array where the new game entry, e, belongs, we add a reference to e at this position.

Adding a reference to a new GameEntry object to the entries array. The reference can now be inserted at index 2, since we have shifted all references to GameEntry objects with scores less than the new one to the right.

Page 6: Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

Page 6

Using Arrays

ca b

Object Removal

let us consider how we might remove a reference to a GameEntry object from the entries array.

remove(i): Remove and return the game entry e at index i in the entries array.•all objects previously stored at indices higher than i are "moved over" to fill in for the removed object

Page 7: Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

Page 7

Using Arrays

ca b

Sorting an Array

In this section, we study a way of starting with an array with objects that are out of order and putting them in order.

A Simple Insertion-Sort Algorithm

Insertion sort is the algorithm many people use when sorting a hand of cards.

Page 8: Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

Page 8

Using Arrays

ca b

A Simple Insertion-Sort Algorithm

High-level description of the insertion-sort algorithm.

Page 9: Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

This simple insertion–sort algorithm goes as follows.

1.We start with the first character in the array.

2.One character by itself is already sorted.

3.Then we consider the next character in the array. If it is smaller than the first, we swap them.

4.Next we consider the third character in the array. We swap it leftward until it is in its proper order with the first two characters.

5.We continue in this manner with the fourth, fifth integer, the sixth, and so on, until the whole array is sorted.

Page 10: Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

Page 10

Using Arrays

ca b

A Simple Insertion-Sort Algorithm

Intermediate-level description of the insertion-sort algorithm.

Page 11: Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

Page 11

Using Arrays

ca b

A Simple Insertion-Sort Algorithm

Execution of the insertion-sort algorithm on an array of eight characters.

Page 12: Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

Page 12

Using Arrays

ca b

java.util Methods for Arrays and Random Numbers

Java provides a number of built-in methods for performing common tasks on arrays, they are associated with the class, java.util.Arrays :

equals(A, B): Returns true if and only if the array A and the array B are equal.

fill(A,x): Stores element x into every cell of array A.

sort(A): Sorts the array A using the natural ordering of its elements.

toString(A): Returns a String representation of the array A.For example, the following string would be returned by the method toString called on an array of integers A = [4,5,2,3,5,7,10]:

[4, 5, 2, 3, 5, 7, 10]

Page 13: Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

Page 13

Using Arrays

ca b

Two-Dimensional Arrays and Positional Games

Arrays in Java are one-dimensional.

there is a way we can define two-dimensional arrays in Java—we can create a two-dimensional array as an array of arrays.

Such a two—dimensional array is sometimes also called a matrix. In Java, we declare a two—dimensional array as follows:

int[ ][ ] Y = new int[8][10];

Two-dimensional arrays have many applications to numerical analysis.

we explore an application of two- dimensional arrays for implementing a simple positional game.

Page 14: Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.

Page 14

Using Arrays

ca b

Tic-Tac-Toe Game

The basic idea is to use a two-dimensional array, board, to maintain the game board. Cells in this array store values that indicate if that cell is empty or stores an X or O.

A simple, complete Java class for playing Tic-Tac-Toe between two players is provides at page No. 134 ( The text Book)