CS61B srping 2018 disc02 https://sp18.datastructur.es/
- pass by what?
a. 下列程序 每一行怎么运行?
b. 画出box-and-pointer 指示图
c. 在第19行,level被赋值为50 ,level是什么?是Pokemon的实例变量?(instance variable)还是change方法的本地变量?(local variable?) 还是main方法里的 变量?还是其他什么东西?
1 public class Pokemon {
2 public String name;
3 public int level;
4
5 public Pokemon(String name, int level) {
6 this.name = name;
7 this.level = level;
8 }
9
10 public static void main(String[] args) {
11 Pokemon p = new Pokemon("Pikachu", 17);
12 int level = 100;
13 change(p, level);
14 System.out.println("Name: " + p.name + ", Level: " + p.level);
15 }
16
17 public static void change(Pokemon poke, int level) {
18 poke.level = level;
19 level = 50;
20 poke = new Pokemon("Gengar", 1);
21 }
22 }
- 静态方法和变量 Static Methods and Variables
public class Cat {
public String name;
public static String noise;
public Cat(String name, String noise) {
this.name = name;
this.noise = noise;
}
public void play() {
System.out.println(noise + " I'm " + name + " the cat!");
}
public static void anger() {
noise = noise.toUpperCase();
}
public static void calm() {
noise = noise.toLowerCase();
}
}
运行下列程序,写出每一次play方法被调用的结果
public static void main(String[] args) {
Cat a = new Cat("Cream", "Meow!");
Cat b = new Cat("Tubbs", "Nyan!");
a.play();
b.play();
Cat.anger();
a.calm();
a.play();
b.play();
}
- 链表的一些介绍Practice with Linked Lists
为下面程序的运行过程画出对应的 box-and-pointer 图,StringList类似IntList,有rest和first两个实例变量
StringList L = new StringList("eat", null);
L = new StringList("shouldn't", L);
L = new StringList("you", L);
L = new StringList("sometimes", L);
StringList M = L.rest;
StringList R = new StringList("many", null);
R = new StringList("potatoes", R);
R.rest.rest = R;
M.rest.rest.rest = R.rest;
L.rest.rest = L.rest.rest.rest;
L = M.rest;
4.实现下面两个方法 ,他们接受一个IntList L,返回一个把L中每个元素做平方的列表,第一个square方法要求不要改变输入列表,squareMutative要改变原列表。
public static IntList square(IntList L) {}
public static IntList squareMutative(IntList L) {}

浙公网安备 33010602011771号