470 likes | 591 Views
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
E N D
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi
// 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
matrix[2][1] =7; • 2-dimensional array illustration • Accessing elements 0 1 2 3 4 0 1 2 3 4
What is nums.length? • 5 • What is nums[0].length? • 4 int [][] nums = new int[5][4]; nums
int[][] matrix2 = { {1, 2, 3}, {10, 3, 0}, {10, 7, 80} }; 0 1 2 • 2-dimensional array illustration • Initialization 0 1 2 matrix2
int[][] matrix = { {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5} }; • Rows might have different lengths matrix
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
In Java, you can create n-dimensional arrays for any integer n. • Generalization of 2D case • Example double[][][] scores = new double[10][5][2];
int[][] array = new int[5][6]; int x = {1, 2}; x array
array[0] = x; array[0][1]? x array
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
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
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.
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.
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();
myCircle.radius • Referencing the object’s data fields: • Invoking the object’s method: myCircle.getArea()
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.
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
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.
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.
The get and set methods are used to read and modify private properties. • Data encapsulation
Suppose that the class Foo is defined in (a). Let f be an instance of Foo. Which of the statements in (b) are correct?
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.
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.
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.
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.
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.