1 of 15

Lecture 5 - CS159 – Members in Classes

Due this week: �Tonight: Practice Quiz1 11pm

Thu - 11pm Lab 3

Fri - 11pm Lab 4

- 11pm Quiz 2[canvas]

Sun - HW 2 11pm

2 of 15

Owning Object(this)

  • An Observation:
    • The . or dot operator requires an object or class identifier as the left-side operand
  • A Question:
    • What should the left-side operand be when writing the class (i.e., inside of the class definition)?
  • The Answer:
    • this (which is a reference to the owning object)

3 of 15

Example Attributes + Constructor

Why are some attributes static and some not?

What are the formal parameters of the constructor?

What local variables are used in the constructor?

What methods are invoked in the constructor and are they static or not?

4 of 15

Getters

  1. How do you refer to the owning object?
  2. Why isn't it necessary to do so?
  3. Could you do so anyway?

5 of 15

Private members

Which new methods are private?

What is true of private members?

Why include private methods?

Is it necessary to use this. when invoking private methods?

Which members are static and which are not?

6 of 15

Comparing objects

public boolean equals(PictureFrame other)

{

return (this.width == other.width) && (this.height == other.height)

&& (this.matte == other.matte) && (this.stand == other.stand);

}

  1. How many objects are being compared?
  2. How are they referred to?
  3. Is it necessary to use this.?
  4. Why are the attributes of other accessible?

7 of 15

Python vs Java

  • The Owning Object:
    • In Python, the owning object is often named self (though needn't be) and is the first parameter passed to a method
  • Class and Static Methods:
    • Python makes a distinction between class methods and static methods

8 of 15

String representation

  1. public String toString()
  2. {
  3. String result;
  4. result = "" + width + "x" + height;
  5. return result;
  6. }
  7. }�When might this method be useful?

What operators are being used?

9 of 15

Loop runs in JShell

int i;

�System.out.println("\nLoop 1A");

i = 1;

while (i <= 10) {

System.out.println(i);

i++;

}

System.out.println("\nLoop 1B");

i = 10;

while (i >= 1) {

System.out.println(i);

i--;

}

System.out.println("\nLoop 4A");

for (i = 1; i <= 10; i++) {

System.out.println(i);

}

10 of 15

Array Review

char[] letterArray = {'H', 'i'};

System.out.println(letterArray[0]); // outputs H

System.out.println(letterArray.length); // outputs 2

double[] numberArray = new double[365];

System.out.println(numberArray[0]); // outputs 0.0

System.out.println(numberArray.length); // outputs 365

a) What is the length of letterArray?

b) What is the length of numberArray?

c) What is the index of the element'i' in letterArray?

d) What is the index of the last element of numberArray?

11 of 15

Array Declarations

Write statements that declare and initialize variables with the following arrays (without using the ‘new’ keyword)

  1. 0 14 1024 127 3 5521
  2. 3.23 1.52 4.23 32.5 2.45 5.23 3.33

12 of 15

String Methods

13 of 15

Jshell strings

String str = "hello world";

str.charAt(8)

str.indexOf("wo")

str.length()

str.substring(4, 7)

str.toUpperCase()

14 of 15

Runestone activity

15 of 15

  • Acknowledgements

Parts of this activity are based on materials developed by David Bernstein.

</end>