cop3502 programming fundamentals for cis majors 1
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
COP3502 Programming Fundamentals for CIS Majors 1
Instructor: Parisa Rashidi
Chapter 7:Multi-Dimensional
Arrays
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][];
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;
What is nums.length? 5
What is nums[0].length? 4
2D Array
int [][] nums = new int[5][4];
nums
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
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
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
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];
What is the output of the following code?
Q
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
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]?
Chapter 8: Objects & Classes
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
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
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
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() {}
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
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
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();
Referencing the object’s data fields:
Invoking the object’s method:
Access
myCircle.radius
myCircle.getArea()
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
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
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
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
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.
What is wrong?
Q
What is wrong?
Q
What is wrong?
Q
What is wrong?
Q
What is wrong?
Q
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
Question on the next slide …
Q
What is the output?
Q
Chapter 10:Thinking in Objects
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
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
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
Example
Variable Scope
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
Example
Variable Scope
Using this to reference hidden fields
this
Use this to call overloaded constructor
this
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
UML composition & aggregation notation
UML
Is the following class immutable?
Q
What is the output?
Q