1 / 19

Object Oriented Programming

Object Oriented Programming. Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr. 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]

venusq
Download Presentation

Object Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

  2. Recap • Assignment 4 • Flow Control • Conditions / Decisions • Loops

  3. Today • Assignment 04 • Arrays – Strings • Thinking in Java – Chapter 4,5

  4. 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?

  5. 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

  6. 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

  7. .length • Java arrays are objects. • Slightly slower. • Lots of convenience • Each array has a variable “.length“. This variable remembers the storage capacity of the array. • Very convenient in setting up loops int index = 0; while ( index < intArray.length ) { System.out.println (intArray[index++]); }

  8. 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(); }

  9. 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]

  10. 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} };

  11. 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);

  12. 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.

  13. 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

  14. 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.

  15. StringBuffer methods • Some examples: • public int charAt(int index) • public StringBuffer append(“anytype” t) • public StringBuffer insert(int offset, “anytype” t) • public deleteCharAt(int index) • public delete(int start, int end) • public StringBuffer subString(int start, int end) • public String reverse() • …

  16. 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"

  17. 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

  18. 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.

  19. Next week • Preparation: Thinking in Java Chapter 6

More Related