Lecture 5 - CS159 – Members in Classes
Login to Runestone:https://canvas.jmu.edu/courses/2071161/assignments/19788058�
Due this week: �Tonight: Practice Quiz1 11pm
Thu - 11pm Lab 3
Fri - 11pm Lab 4
- 11pm Quiz 2[canvas]
Sun - HW 2 11pm
Owning Object(this)
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?
Getters
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?
Comparing objects
public boolean equals(PictureFrame other)
{
return (this.width == other.width) && (this.height == other.height)
&& (this.matte == other.matte) && (this.stand == other.stand);
}
Python vs Java
String representation
What operators are being used?
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);
}
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?
Array Declarations
Write statements that declare and initialize variables with the following arrays (without using the ‘new’ keyword)
String Methods
Jshell strings
String str = "hello world";
str.charAt(8)
str.indexOf("wo")
str.length()
str.substring(4, 7)
str.toUpperCase()
Runestone activity
Parts of this activity are based on materials developed by David Bernstein.
</end>