1 / 47

COP3502 Programming Fundamentals for CIS Majors 1

COP3502 Programming Fundamentals for CIS Majors 1. Instructor: Parisa Rashidi. Chapter 7: Multi-Dimensional Arrays. // Combine declaration and creation in one // single statement double [][] myTable = new double [10 ][10]; // Alternative syntax

ivana
Download Presentation

COP3502 Programming Fundamentals for CIS Majors 1

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. COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi

  2. Chapter 7:Multi-Dimensional Arrays

  3. // Combine declaration and creation in one // single statement double[][] myTable= newdouble[10][10]; // Alternative syntax doublemyTable[][] = newdouble[10][10]; //you might skip specifying the 2nd dimension doublemyTable[][] = newdouble[10][]; • Declaring and creating 2-dimensional array

  4. matrix[2][1] =7; • 2-dimensional array illustration • Accessing elements 0 1 2 3 4 0 1 2 3 4

  5. What is nums.length? • 5 • What is nums[0].length? • 4 int [][] nums = new int[5][4]; nums

  6. int[][] matrix2 = { {1, 2, 3}, {10, 3, 0}, {10, 7, 80} }; 0 1 2 • 2-dimensional array illustration • Initialization 0 1 2 matrix2

  7. int[][] matrix = { {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5} }; • Rows might have different lengths matrix

  8. for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { // use matrix[row][column] } } • Loops and 2D arrays

  9. In Java, you can create n-dimensional arrays for any integer n. • Generalization of 2D case • Example double[][][] scores = new double[10][5][2];

  10. What is the output of the following code?

  11. int[][] array = new int[5][6]; int x = {1, 2}; x array

  12. array[0] = x; array[0][1]? x array

  13. Chapter 8: Objects & Classes

  14. object • An entity in the real world that can be distinctly identified. • Astudent, a desk, a circle, a button, and even a loan can all be viewed as objects. • has a unique identity, state, and behaviors. • State = a set of datafields (also known as properties) with their current values. • Behavior = a set of methods

  15. UML class diagram

  16. Constructors are a special kind of methods that are invoked to construct objects. Constructor with no argument Circle() { } Circle(doublenewRadius) { radius = newRadius; } Constructor with argument

  17. Constructors must have the same name as the class itself. • Constructors do not have a return type—not even void. • Constructors are invoked using the new operator when an object is created. • Constructors play the role of initializing objects.

  18. A class may be declared without constructors. • In this case, a no-arg constructor with an empty body is implicitly declared in the class. • This constructor, called a default constructor, is provided automatically only if no constructors are explicitly declared in the class.

  19. Circle myCircle = new Circle(); • Declaring & creating objects in a single step • Two steps create declare //step 1: declare Circle myCircle; //step 2: create myCircle= new Circle();

  20. myCircle.radius • Referencing the object’s data fields: • Invoking the object’s method: myCircle.getArea()

  21. The default value of a data field is • nullfor a reference type • 0for a numeric type • falsefor a booleantype • '\u0000' for a char type • However, Java assigns no default value to a local variable inside a method.

  22. Static methods are not tied to a specific object. • Static variables are shared by all the instances of the class. • Static constants are final variables shared by all the instances of the class. • All declared using static keyword

  23. Static fields or methods can be used from instance or static methods. • Instance fields or methods can be only used from instance methods. • So: a variable or method that does not depend on a specific instance of the class, should be specified as static.

  24. Package access (default in Java) • The class, variable, or method can be accessed by any class in the same package. • public • The class, data, or method is visible to any class in any package. • private • The data or methods can be accessed only by the declaring class.

  25. The get and set methods are used to read and modify private properties. • Data encapsulation

  26. What is wrong?

  27. What is wrong?

  28. What is wrong?

  29. What is wrong?

  30. What is wrong?

  31. Suppose that the class Foo is defined in (a). Let f be an instance of Foo. Which of the statements in (b) are correct?

  32. Question on the next slide …

  33. What is the output?

  34. Chapter 10:Thinking in Objects

  35. For a class to be immutable, it must • Mark all data fields private • Provide no mutator methods • Provide no accessor methods that would return a reference to a mutable data field object.

  36. For a class to be immutable, it must • Mark all data fields private • Provide no mutator methods • Provide no accessor methods that would return a reference to a mutable data field object.

  37. The scope of instance and static data fields is the entire class. • They can be declared anywhere inside a class. • The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. • A local variable must be initialized explicitly before it can be used.

  38. Example

  39. If a local variable has the same name as a class’s variable, the local variable takes precedence and the class’s variable with the same name is hidden.

  40. Example

  41. Using thisto reference hidden fields

  42. Use thisto call overloaded constructor

  43. Composition is a special case of the “aggregation” relationship. • Aggregation models “has-a” relationships. • The owner object is called an aggregating object. • The subject object is called an aggregated object.

  44. UML composition & aggregation notation

  45. Is the following class immutable?

  46. What is the output?

More Related