2018 spring cs61b dis2

pdf https://sp18.datastructur.es/materials/discussion/disc02.pdf

question one:
1.Name: Pikachu, Level: 100
2. https://cscircles.cemc.uwaterloo.ca/java_visualize/#code=public+
class+Pokemon+%7B%0A++++public+String+name%3B%0A++++public+int+
level%3B%0A%0A++++public+Pokemon(String+name,+int+level)+%7B%0A+++
+++++this.name+%3D+name%3B%0A++++++++this.level+%3D+level%3B%0A+++
+%7D%0A%0A++++public+static+void+main(String%5B%5D+args)+%7B%0A+++
+++++Pokemon+p+%3D+new+Pokemon(%22Pikachu%22,+17)%3B%0A++++++++int+
level+%3D+100%3B%0A++++++++change(p,+level)%3B%0A++++++++System.
out.println(%22Name%3A+%22+%2B+p.name+%2B+%22,+Level%3A+%22+%2B+p.
level)%3B%0A++++%7D%0A++++%0A++++public+static+void+change(Pokemon+
poke,+int+level)+%7B%0A++++++++poke.level+%3D+level%3B%0A+++++++
+level+%3D+50%3B%0A++++++++poke+%3D+new+Pokemon(%22Gengar%22,+1)
%3B%0A++++%7D%0A%7D&mode=display&curInstr=0
3.一个变量在chenge函数里面的临时变量。

question two:
Meow I'm Cream the cat!
Nyan I'm Tubbs the cat!
meow I'm Cream the cat!
NYAN I'm Tubbs the cat!

question three:

http://cscircles.cemc.uwaterloo.ca/java visualize/#code=public+class+
StringList+%7B%0A+++String+head%3B%0A+++StringList+tail%3B%0A+++
public+StringList(String+head,+StringList+tail)+%7B%0A++++++this.
head+%3D+head%3B%0A++++++this.tail%3Dtail%3B%0A+++%7D%0A+++public+
static+void+main(String%5B%5D+args)+%7B%0A+++StringList+L+%3D+new+
StringList(%22eat%22,+null)%3B%0A%09L+%3D+new+StringList(%22shouldn’
t%22,+L)%3B%0A%09L+%3D+new+StringList(%22you%22,+L)%3B%0A%09L+%3D+new+
StringList(%22sometimes%22,+L)%3B%0A%09StringList+M+%3D+L.tail%3B%0A%
09StringList+R+%3D+new+StringList(%22many%22,+null)%3B%0A%09R+%3D+new+
StringList(%22potatoes%22,+R)%3B%0A%09R.tail.tail+%3D+R%3B%0A%09M.tail.
tail.tail+%3D+R.tail%3B%0A%09L.tail.tail+%3D+L.tail.tail.tail%3B%0A%
09L+%3D+M.tail%3B%0A+++%7D%0A%7D%0A&mode=display&showStringsAsObjects=
&curInstr=52

question four:

    public static IntList square(IntList L){
       if (L == null) {
         return L;
       } 
       else {
         IntList rest = square(L.rest);
         IntList M = new IntList(L.first * L.first, rest);
         return M;
       }
     }

    public static IntList squareMutative(IntList L){
       IntList a = L
       while(a != NULL){
          a.first *= a.first;
          a.rest = a.rest;
        }
     }
posted @ 2022-05-12 15:21  天然气之子  阅读(48)  评论(0编辑  收藏  举报