Programlama ve Nesneler

Download Report

Transcript Programlama ve Nesneler

Object Oriented Programming
Lecture 5: Arrays and Strings
Mustafa Emre İlal
[email protected]
Recap
• Assignment 4
• Flow Control
– Conditions / Decisions
– Loops
Today
• Assignment 04
• Arrays – Strings
• Thinking in Java – Chapter 4,5
Arrays
• Need for an Array [group]
– To group elements of a particular type
– To apply a procedure to individual elements within a
larger group
• Example: assume we have 50 Circles
– We do not want to create 50 different variables.
– What would the code to find the sum of their areas look
like?
– Why 50? Why any pre-determined number? Why not
let the user determine the number?
Java Arrays
• The Array is an ordered group data elements of a
single data type (primitive or reference).
• Java arrays are objects (reference type)
• Arrays can be declared and initialized seperately
– Declaration is by specifying the data type
– Initialization requires the number of elements to be
stored.
• int[] intArray; //square brackets declare the array
• intArray = new int[10]; // memory for 10 int’s are
allocated and intArray points to the array
note the “new” statement
• int intArray[] = new int[10] // alternative –
same result
Index
• The value within the square brackets define the
order of the element of interest within the array.
(Its index)
• Java arrays start counting the index at 0. (not 1)
• intArray[2] = 7;
// the third element in the array
is 7
.length
• Java arrays are objects.
– Slightly slower.
– Lots of convenience
• Each array has a variable “.length“. This variable
remembers the storage capacity the array.
• Very convenient in setting up loops
int index = 0;
while ( index < intArray.length ) {
System.out.println (intArray[index++]);
}
Initialization of Elements
• Nothing to be done for primitives
• Object arrays are reference types
– A new object needs to be initialized at the reference
point.
Circle[] circleArray = new Circle[10];
for (int j=0; j<10; j++) {
circleArray[j] = new Circle();
}
Multi Dimensional Arrays
• Arrays do not have to represent a one dimensional
relationship (queues) between elements.
• Multi-dimensional structures (tables, time-series)
can also be abstracted
– Note that humans have difficulty in imagining beyond
three dimensions
Cell[][] spreadsheet = new Cell[25][50];
spreadsheet[row][column];
classroom[][][] university= new classroom[10][8][4];
university[building][floor][room]
Pre-known Data
• Alternative way to initialize an array involves
providing a listing of all the data to be stored.
int[] fibonacci = {0,1,1,2,3,5,8,13};
• For multi-dimensional arrays:
int[][] matrix= { {0,1,2},{1,0,1},{34,23,6} };
java.lang.Vector
• Array size is fixed once the array is initialized.
• When more data needs to be added than the size allows, a
new array of a larger size needs to be initialized and the
elements of the old array copied into the new one.
• Vector Class is a data structure that has no size limitation.
Vectors are utilized through the methods the class
provides. Some methods:
public Vector(int initialSize);
public Vector(); // create empty vector
public void addElement(Object obj);
public Object elementAt(int index);
public void insertElementAt(int index);
public void removeElementAt(int index);
public int indexOf(Object obj);
Character Arrays (String)
• Text is an array of characters (string)
• Java provides the String class for convenient
manipulation of these character arrays.
• Using the String class is almost as easy as using
primitive data types.
String
• Points to watch out for
– The content cannot be subjected to comparison by the
“==“ operator.
the .equals() method needs to be used.
firstString.equals(String secondString)
– String object does not allow altering the content
directly. Useful to use helper classes
StringBuffer
• String class is not designed to modify the
character contents (value) but to manage the
container
• StringBuffer class should be used to add to or strip
characters from the array.
StringBuffer methods
• Some examples:
–
–
–
–
–
–
–
–
public
public
public
public
public
public
public
…
int charAt(int index)
StringBuffer append(“anytype” t)
StringBuffer insert(int offset, “anytype” t)
deleteCharAt(int index)
delete(int start, int end)
StringBuffer subString(int start, int end)
String reverse()
preparation - chapter 4,5
• Chapter 4
– Constructor
– Overloading
– this() for calling one constructor from others
– static
– finalize() the method that is called before the object is deleted
• Chapter 5
– package *
– public – private – protected – "friendly"
Assignment 05
•
Add the following to the Circle class
– Variable for name
– Variable for color
•
Substantial changes in CircleApplication
– Create 100 circles
•
•
Each should have a name in the form of “Circle-XXX"
according to its order of creation.
Use Math.random() to assign the following properties
–
–
Color: Either Red , Green, Blue, Yellow, Puple, Cyan, Orange,
Brown, Black, or White (use an array for color list)
Coordinates for the center and the radius
Assignment 05
– Reports to be displayed:
•
•
•
•
•
List the circles in alphabetical order, displaying the properties
of each.
Sum of all areas and the average of all areas
The number of circles for each color
List of red circles sorted in order of size.
The farthest and closest circles (in terms of their centers) to a
randomly determined point.
Next week
• Preparation: Thinking in Java Chapter 6