![Page 1: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/1.jpg)
COP3502 Programming Fundamentals for CIS Majors 1
Instructor: Parisa Rashidi
![Page 2: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/2.jpg)
Chapter 7:Multi-Dimensional
Arrays
![Page 3: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/3.jpg)
Declaring and creating 2-dimensional array
2D Array
// Combine declaration and creation in one // single statementdouble[][] myTable = new double[10][10];
// Alternative syntaxdouble myTable[][] = new double[10][10];
//you might skip specifying the 2nd dimensiondouble myTable[][] = new double[10][];
![Page 4: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/4.jpg)
2-dimensional array illustration Accessing elements
2D Array
0 0 0 0 0
0 0 0 0 0
0 7 0 0 0
0 0 0 0 0
0 0 0 0 0
0 1 2 3 4
0
1
2
3
4
matrix[2][1] =7;
![Page 5: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/5.jpg)
What is nums.length? 5
What is nums[0].length? 4
2D Array
int [][] nums = new int[5][4];
nums
![Page 6: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/6.jpg)
2-dimensional array illustration Initialization
2D Array
1 2 3
10 3 0
10 7 80
0 1 2
0
1
2
int[][] matrix2 = { {1, 2, 3}, {10, 3, 0}, {10, 7, 80}};
matrix2
1 2 3
10 3 0
10 7 80
![Page 7: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/7.jpg)
Rows might have different lengths
Ragged Array
int[][] matrix = { {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5}};
matrix
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
![Page 8: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/8.jpg)
Loops and 2D arrays
Loops & 2D Array
for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column+
+) { // use matrix[row][column] }}
1 2 3
10 3 0
10 7 80
![Page 9: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/9.jpg)
In Java, you can create n-dimensional arrays for any integer n. Generalization of 2D case Example
Multi-dimensional array
double[][][] scores = new double[10][5][2];
![Page 10: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/10.jpg)
What is the output of the following code?
Q
![Page 11: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/11.jpg)
Q/A
0 0 0 0 0 0array
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
int[][] array = new int[5][6];int x = {1, 2};
x 1 2
![Page 12: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/12.jpg)
Q/A
array
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
array[0] = x;
x 1 2
0 0 0 0 0 0
array[0][1]?
![Page 13: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/13.jpg)
Chapter 8: Objects & Classes
![Page 14: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/14.jpg)
object An entity in the real world that can be
distinctly identified. A student, 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 data fields (also known as
properties) with their current values. Behavior = a set of methods
Object
![Page 15: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/15.jpg)
Class Example
class Circle { /** The radius of this circle */ double radius = 1.0; /** Construct a circle object */ Circle() { } /** Construct a circle object */ Circle(double newRadius) { radius = newRadius; } /** Return the area of this circle */ double getArea() { return radius * radius * 3.14159; }
}
Data field
Method
Constructors
![Page 16: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/16.jpg)
UML class diagram
UML Notation
Circle
radius: double Circle()
Circle(newRadius: double)
getArea(): double
circle1: Circle radius = 1.0
Class name
Data fields
Constructors and methods
circle2: Circle radius = 25
circle3: Circle radius = 125
UML Class Diagram
UML notation for objects
![Page 17: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/17.jpg)
Constructors are a special kind of methods that are invoked to construct objects.
Constructor
Circle(double newRadius) { radius = newRadius;}
Constructor with no
argument
Constructor with
argument
Circle() {}
![Page 18: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/18.jpg)
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.
Constructor
![Page 19: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/19.jpg)
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.
Constructor
![Page 20: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/20.jpg)
Declaring & creating objects in a single step
Two steps
Creating Objects
Circle myCircle = new Circle();
declare create
//step 1: declareCircle myCircle;//step 2: createmyCircle = new Circle();
![Page 21: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/21.jpg)
Referencing the object’s data fields:
Invoking the object’s method:
Access
myCircle.radius
myCircle.getArea()
![Page 22: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/22.jpg)
The default value of a data field is null for a reference type 0 for a numeric type false for a boolean type '\u0000' for a char type
However, Java assigns no default value to a local variable inside a method.
Default Values
![Page 23: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/23.jpg)
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
![Page 24: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/24.jpg)
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.
Static vs. Instance
![Page 25: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/25.jpg)
1. Package access (default in Java)
The class, variable, or method can be accessed by any class in the same package.
2. public
The class, data, or method is visible to any class in any package.
3. private
The data or methods can be accessed only by the declaring class.
Accessibility
![Page 26: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/26.jpg)
The get and set methods are used to read and modify private properties. Data encapsulation
get/set
Circle
-radius: double
-numberOfObjects: int
+Circle()
+Circle(radius: double)
+getRadius(): double
+setRadius(radius: double): void
+getNumberOfObject(): int
+getArea(): double
The radius of this circle (default: 1.0).
The number of circle objects created.
Constructs a default circle object.
Constructs a circle object with the specified radius.
Returns the radius of this circle.
Sets a new radius for this circle.
Returns the number of circle objects created.
Returns the area of this circle.
![Page 27: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/27.jpg)
What is wrong?
Q
![Page 28: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/28.jpg)
What is wrong?
Q
![Page 29: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/29.jpg)
What is wrong?
Q
![Page 30: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/30.jpg)
What is wrong?
Q
![Page 31: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/31.jpg)
What is wrong?
Q
![Page 32: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/32.jpg)
Suppose that the class Foo is defined in (a). Let f be an instance of Foo. Which of the statements in (b) are correct?
Q
![Page 33: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/33.jpg)
Question on the next slide …
Q
![Page 34: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/34.jpg)
What is the output?
Q
![Page 35: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/35.jpg)
Chapter 10:Thinking in Objects
![Page 36: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/36.jpg)
For a class to be immutable, it must 1. Mark all data fields private 2. Provide no mutator methods 3. Provide no accessor methods that
would return a reference to a mutable data field object.
Immutable
![Page 37: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/37.jpg)
For a class to be immutable, it must 1. Mark all data fields private 2. Provide no mutator methods 3. Provide no accessor methods that
would return a reference to a mutable data field object.
Immutable
![Page 38: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/38.jpg)
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.
Variable Scope
![Page 39: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/39.jpg)
Example
Variable Scope
![Page 40: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/40.jpg)
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.
Variable Scope
![Page 41: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/41.jpg)
Example
Variable Scope
![Page 42: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/42.jpg)
Using this to reference hidden fields
this
![Page 43: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/43.jpg)
Use this to call overloaded constructor
this
![Page 44: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/44.jpg)
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.
Aggregation
![Page 45: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/45.jpg)
UML composition & aggregation notation
UML
![Page 46: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/46.jpg)
Is the following class immutable?
Q
![Page 47: COP3502 Programming Fundamentals for CIS Majors 1](https://reader035.vdocuments.us/reader035/viewer/2022062802/568144f7550346895db1c5b5/html5/thumbnails/47.jpg)
What is the output?
Q