+ All Categories
Home > Documents > CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D...

CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D...

Date post: 03-Jul-2018
Category:
Upload: hoangminh
View: 218 times
Download: 0 times
Share this document with a friend
47
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, and others. CS 106A, Lecture 27 Final Exam Review 1
Transcript
Page 1: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

Thisdocumentiscopyright(C)StanfordComputerScienceandMartyStepp,licensedunderCreativeCommonsAttribution2.5License.Allrightsreserved.BasedonslidescreatedbyKeithSchwarz,MehranSahami,EricRoberts,StuartReges,andothers.

CS106A,Lecture27FinalExamReview1

Page 2: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

2

Plan for today•Announcements/Examlogistics•Graphics,Animation,Events•1DArrays•2DArrays•ArrayList

Page 3: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

3

Plan for today•Announcements/Examlogistics•Graphics,Animation,Events•1DArrays•2DArrays•ArrayList

Page 4: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

4

Final exam• Isthe finalexam cumulative?• What will betested on the finalexam?• What about allthis stuff you aren’t covering today?

– ExpressionsandVariables– JavaControlStatements– ConsolePrograms– Methods,parameters,returns– Randomness– Strings andchars– Scanners andfileprocessing– Memory

• Isthe finalexam going tobedifficult/curved?• Howcan Ipractice forthe final?

Midterm review sessionwas the recorded sectionon Fridayof Week 4

Page 5: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

5

Practicing for the final• Reviewconceptsyou’reunsureof• Reviewconceptsfrompreviousassignments• Dosectionproblems• Dopracticefinalunderrealconditions• codestepbystep.com

Page 6: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

6

Plan for today•Announcements/Examlogistics•Graphics,Animation,Events•1DArrays•2DArrays•ArrayList

Page 7: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

7

Graphics• LookatlectureslidesforlistsofdifferentGObject typesandtheirmethods

• Remember:thexandyofGRect,GOval,etc.Istheirupperleftcorner,butthexandyofGLabel isitsleftmostbaselinecoordinate.

• Rememberforlabels:getHeight()=getAscent()+getDescent().

Page 8: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

8

AnimationStandardformatforanimationcode:

while (condition) {update graphicsperform checkspause(PAUSE_TIME);

}

Page 9: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

9

Events• TwowaysforJavatorunyourcode:fromrun()andfromeventhandlers(mouseClicked,mouseMoved,actionPerformed,etc.)

• Eventhandlersmusthaveexactlythespecifiedsignatureotherwisetheywon’twork!

e.g.public void mouseClicked(MouseEvent e)

• Ifyouneedtomodifysomethinginaneventhandlerthatyouuseelsewhereinyourcode,itshouldbeaninstancevariable(e.g.paddleinBreakout!)

Page 10: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

10

Demo: Seeker

Page 11: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

11

Seeker• Redsquareisthetarget• Transparentsquareistheseeker• Theseekershouldmovetowardsandengulfthetarget• Canchangetargetlocationbyclickingonthescreen

Page 12: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

12

Instance variables

Page 13: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

13

run()public void run() {

initTarget();initSeeker();

// Always keep seeking the targetwhile (true) {

seek();pause(PAUSE_TIME);

}}

Page 14: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

14

run()public void run() {

initTarget();initSeeker();

// Always keep seeking the targetwhile (true) {

seek();pause(PAUSE_TIME);

}}

Page 15: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

15

initTarget()

Page 16: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

16

initSeeker()

Page 17: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

17

run()public void run() {

initTarget();initSeeker();

// Always keep seeking the targetwhile (true) {

seek();pause(PAUSE_TIME);

}}

Page 18: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

18

seek()

Page 19: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

19

moveAmount()

Page 20: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

20

mouseClicked()

Page 21: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

21

Plan for today•Announcements/Examlogistics•Graphics,Animation,Events•1DArrays•2DArrays•ArrayList

Page 22: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

22

1D Arrays• Anarray isafixed-lengthlistofasingletypeofthing.• Anarraycanstoreprimitives andobjects.• Youcannotcallmethodsonarraysi.e.nomyArray.contains().• GetthelengthbysayingmyArray.length.(Noparentheses!)• PrintarraywithArrays.toString(myArray),not println(myArray)!

[2, 4, 6, 8] [I@4ddced80

Page 23: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

23

Program traces• Localvariablesareseparate acrossmethods• Parametersarejustassignednamesbytheorderinwhichthey’repassed

• Drawchangestovariablesasyougothroughtheprogram• Objectsvsprimitivebehaviorforparameters

A.Prints [5,10,15]5B.Prints [5,10,15]7C.Prints [5,20,15]5D.Prints [5,20,15]7

Page 24: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

24

Trace

Page 25: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

25

Trace

Output:[4, 0, 2] 4 1[4, 0, 2] 3[8, 0, 4] 1 1[8, 0, 4] 6

Page 26: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

26

Extra 1D Array problemWritethe method int longestSortedSequence(int[] array)

e.g.int[] array = {3, 8, 10, 1, 9, 14, -3, 0, 14, 207, 56, 98, 12}

Sorted inthis casemeans nondecreasing,soasequence could contain duplicates:

e.g.int[] array = {17, 42, 3, 5, 5, 5, 8, 2, 4, 6, 1, 19}

Link:http://www.codestepbystep.com/problem/view/java/arrays/longestSortedSequence

3 3 4 2 1

2 3 25

Page 27: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

27

Plan for today•Announcements/Examlogistics•Graphics,Animation,Events•1DArrays•2DArrays•ArrayList

Page 28: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

28

2D Arrays = Arrays of Arrays!int[][] a = new int[3][4];

a[0][0] a[0][1] a[0][2] a[0][3]

a[1][0] a[1][1] a[1][2] a[1][3]

a[2][0] a[2][1] a[2][2] a[2][3]

Outerarray

Page 29: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

29

Chess• Knight:moves inan”L”-shape (two steps inone direction,one stepinaperpendicular direction)

Page 30: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

30

knightCanMove()boolean knightCanMove(String[][] board,

int startRow, int startCol,int endRow, int endCol)

• (startRow,startCol)mustcontainaknight• (endRow,endCol)mustbeempty• (endRow,endCol)mustbereachablefrom(startRow,startCol)inasinglemove

• Assumethat(startRow,startCol)and(endRow,endCol)arewithinboundsofarray

Page 31: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

31

knightCanMove()

0 1 2 3 4 5 6 7

0 ”king”

1 ”knight”

2

3 ”rook”

4

5

6

7

Page 32: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

32

0 1 2 3 4 5 6 7

0 ”king”

1 ”knight”

2

3 ”rook”

4

5

6

7

knightCanMove()

0 1 2 3 4 5 6 7

0 ”king”

1 ”knight”

2

3 ”rook”

4

5

6

7

knightCanMove(board, 2, 2, 3, 4)

Noknight at(2,2)

returns false

Page 33: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

33

0 1 2 3 4 5 6 7

0 ”king”

1 ”knight”

2

3 ”rook”

4

5

6

7

knightCanMove()

0 1 2 3 4 5 6 7

0 ”king”

1 ”knight”

2

3 ”rook”

4

5

6

7

knightCanMove(board, 1, 2, 0, 4)

Spaceoccupied

returns false

Page 34: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

34

knightCanMove()

0 1 2 3 4 5 6 7

0 ”king”

1 ”knight”

2

3 ”rook”

4

5

6

7

knightCanMove(board, 1, 2, 3, 2)

(1,2)to(3,2)isnotavalidmove

returns false

Page 35: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

35

knightCanMove()

0 1 2 3 4 5 6 7

0 ”king”

1 ”knight”

2

3 ”rook”

4

5

6

7

knightCanMove(board, 1, 2, 3, 3)

Knightisat(1,2)and(3,3)isemptyand(1,2)->(3,3)isavalidmove

returns true

Page 36: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

36

knightCanMove()// This method returns true if the starting square contains a knight,// the end square is empty, and the knight can legally move from the// start square to the end square.private boolean knightCanMove(String[][] board, int startRow,

int startCol, int endRow, int endCol) {

}

Page 37: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

37

knightCanMove()// This method returns true if the starting square contains a knight,// the end square is empty, and the knight can legally move from the// start square to the end square.private boolean knightCanMove(String[][] board, int startRow,

int startCol, int endRow, int endCol) {if (board[startRow][startCol].equals("knight")) {

}

}

Page 38: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

38

knightCanMove()// This method returns true if the starting square contains a knight,// the end square is empty, and the knight can legally move from the// start square to the end square.private boolean knightCanMove(String[][] board, int startRow,

int startCol, int endRow, int endCol) {if (board[startRow][startCol].equals("knight")) {

if (board[endRow][endCol].equals("")) {

}}

}

Page 39: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

39

knightCanMove()// This method returns true if the starting square contains a knight,// the end square is empty, and the knight can legally move from the// start square to the end square.private boolean knightCanMove(String[][] board, int startRow,

int startCol, int endRow, int endCol) {if (board[startRow][startCol].equals("knight")) {

if (board[endRow][endCol].equals("")) {int rowDifference = Math.abs(startRow - endRow);int colDifference = Math.abs(startCol - endCol);if ((rowDifference == 1 && colDifference == 2) ||

(rowDifference == 2 && colDifference == 1)) {return true;

}}

}

}

Page 40: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

40

knightCanMove()// This method returns true if the starting square contains a knight,// the end square is empty, and the knight can legally move from the// start square to the end square.private boolean knightCanMove(String[][] board, int startRow,

int startCol, int endRow, int endCol) {if (board[startRow][startCol].equals("knight")) {

if (board[endRow][endCol].equals("")) {int rowDifference = Math.abs(startRow - endRow);int colDifference = Math.abs(startCol - endCol);if ((rowDifference == 1 && colDifference == 2) ||

(rowDifference == 2 && colDifference == 1)) {return true;

}}

}return false;

}

Page 41: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

41

Plan for today•Announcements/Examlogistics•Graphics,Animation,Events•1DArrays•2DArrays•ArrayList

Page 42: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

42

ArrayList• AnArrayList isaflexible-lengthlistofasingletypeofthing.• AnArrayList canonlystoreobjects.

– Forprimitivesusee.g.ArrayList<Integer> insteadofArrayList<int>.(Integer isawrapperclassforint)

– Otherwrapperclasses:Double insteadofdouble,Character insteadofchar,Boolean insteadofboolean.

• AnArrayList hasavarietyofmethodsyoucanuselike.contains,.get,.add,.remove,.size,etc.

Page 43: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

43

Array vs ArrayList• Array

– Fixedsize– Efficient(notaconcerninthisclass)– Nomethods,canonlyusemyArray.length (noparentheses!)– Canstoreanyobjectorprimitive

• ArrayList– Expandable– LessefficientthanArray(notaconcerninthisclass)– Convenientmethodslike.add(),.remove(),.contains()– Cannotstoreprimitives,sousetheirwrapperclassesinstead

Page 44: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

44

deleteDuplicates()private void deleteDuplicates(ArrayList<String> list)

• Guaranteedthatlistisinsortedorder• {"be","be","is","not","or","question","that","the","to","to"}becomes{“be”,“is”,“not”,“or”,“question”,“that”,“the”,“to”}

• Solutionstrategy:– LoopthroughArrayList– Comparepairsofelements– Ifelement.equals(nextElement),removeelementfromthelist

Page 45: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

45

deleteDuplicates• LoopthroughArrayList• Comparepairsofelements• Ifelement.equals(nextElement),removeelementfromthelist

Page 46: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

46

deleteDuplicatesReverse• LoopthroughArrayList inreverse• Comparepairsofelements• Ifelement.equals(previousElement),removeelementfromthelist

Page 47: CS 106A, Lecture 27 Final Exam Review 1 - Stanford University · 2017-08-15 · •1D Arrays •2D Arrays •ArrayList. 3 ... 7 Graphics •Look at lecture slides for lists of different

47

Recap•Announcements/Examlogistics•Graphics,Animation,Events•1DArrays•2DArrays•ArrayList

Nexttime:FinalExamReview2


Recommended