public class CS61BStudent {
public int idNumber;
public int grade;
public static String professor = "Hilfinger";
public CS61BStudent(int id) {
this.idNumber = id;
this.grade = 100;
}
public void watchLecture() {
// ...
}
public static String gradeToLetter(int grade) {
// ...
}
}
Static variables and functions belong to the class as a whole. The
professor is a shared characteristic of all CS61BStudents.
Instance variables and functions belong to a particular instance. Each
CS61BStudent has its own independent idNumber.
public class Avatar {
public static String electricity;
public String fluid;
public Avatar(String str1, String str2) {
Avatar.electricity = str1;
this.fluid = str2;
}
public static void main(String[] args) {
Avatar foo1 = new Avatar(“one”, “two”);
Avatar foo2 = new Avatar(“three”, “four”);
System.out.println(foo1.electricity + foo1.fluid);
foo1.electricity = “I declare ”;
foo1.fluid = “a thumb war”;
System.out.println(foo2.electricity + foo2.fluid);
}
}
What's printed after executing main?
public class Avatar {
public static String electricity;
public static String fluid;
public Avatar(String str1, String str2) {
Avatar.electricity = str1;
this.fluid = str2;
}
public static void main(String[] args) {
Avatar foo1 = new Avatar(“one”, “two”);
Avatar foo2 = new Avatar(“three”, “four”);
System.out.println(foo1.electricity + foo1.fluid);
foo1.electricity = “I declare ”;
foo1.fluid = “a thumb war”;
System.out.println(foo2.electricity + foo2.fluid);
}
}
Does this code compile after changing lines 2 and 3?
public class Avatar {
public static String electricity;
public String fluid;
// New method!
public static String getFluid() {
return fluid;
}
// ...
}
After adding this method, does the code compile?
public class Shock {
public static int bang;
public static Shock baby;
public Shock() { this.bang = 100; }
public Shock(int num) {
this.bang = num;
baby = starter();
this.bang += num;
}
public static Shock starter() {
Shock gear = new Shock();
return gear;
}
public static void shrink(Shock statik) {
statik.bang -= 1;
}
public static void main(String[] args) {
Shock gear = new Shock(200);
System.out.println(gear.bang);
shrink(gear);
shrink(starter());
System.out.println(gear.bang);
}
}
What's printed after executing main?
/** Destructively reverses A in place. */
public static void reverse(int[] A) {
}
Hint: element 0 swaps with element N-1, element
1 swaps with element N-2...
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 |
→
| 1 | 2 | 9 | 4 |
| 5 | 6 | 7 | 8 |
| 3 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 |
/** Destructively reverses the items along the diagonal in B */
public static void reverseDiagonal(int[][] B, int diagonal) {
}
Hint: diagonal 2 is a sequence of elements at indices [2][0],
[1][1], [0][2].
| 2 | 3 | 4 | 5 | 6 | 7 | 0 | 1 |
| 6 | |||||||
→
0
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 0 |
public static int[] overflow(int[] A, int i, int k) {
}
Use System.arraycopy to copy chunks of the input array!
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 |
→
| 1 | 5 | 9 | 13 |
| 2 | 6 | 10 | 14 |
| 3 | 7 | 11 | 15 |
| 4 | 8 | 12 | 16 |
public static int[] overflow(int[] A, int i, int k) {
}