Top Banner
28

HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Dec 16, 2015

Download

Documents

Jonas Small
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.
Page 2: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

HFJ CHAPTER 5 REVIEW

Page 3: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Poll: What is the purpose of writing prepcode?

Page 4: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Poll: What is test code?

Page 5: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Prepcode and Testcode• Prep code – a form of pseudocode, to help you focus on

the logic without stressing about syntax• Prepcode describes what to do, not how to do it• Use prepcode to help design the test code

• Test code – a class or methods that will test the real code and validate that it’s doing the right thing• Write test code before you implement the methods

Page 6: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.
Page 7: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Developing a Class• Figure out what the class is supposed to do• List the instance variables and methods (class diagram)• Prep code• Test code• Implement the class/ write real code• Test using the test code• Debug and reimplement as needed• (Test with real users)

Page 8: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Poll: int numRooms = Integer.parseInt(input); ...

Page 9: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Converting a String to an int• Parse - Analyze (a sentence) into its component parts and

describe their syntactic roles.• In this case we analyze a String and convert into an int if

the String is an integer number.• After it is converted, we store it as an int.

Page 10: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Poll: int numRooms = Integer.parseInt(input); ...

Page 11: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Preconditions• Important Seen on the AP CS exam to reduce

complexity of writing methods• Preconditions are conditions that must be met in order for

the code to work.• Usually specify the type of inputs so that we don’t have to

deal with invalid inputs

Page 12: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Poll: int randomNum = (int)(Math.random() * 5)...

Page 13: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Type casting• Forces the expression after the type cast to become the

type specified• int randomNum = (int)(Math.random() * 5);

• (Math.random() * 5) generates a random double between 0 and 5• The (int) type cast cuts off the decimal part of the number• Left with 0, 1, 2, 3, or 4

Page 14: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Poll: Which of these is not a loop?

Page 15: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Poll: Which of these is not a part of a for lo...

Page 16: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

For loops

While loops

• int i = 0;while (i < 100){

//repeated codei++;

}

For loops

• for(int i = 0; i<100; i++){

//repeated code}

Page 17: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Hw Q7• int[] someNums = new int[6];

//someNums is filled with random ints

for(int i = 0; i<someNums.length; i++){

System.out.println(someNums[i]);}

Page 18: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Poll: Which of these is not part of a for-each...

Page 19: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

For-each loops

While loop

• int[] locationCells = new

int[100];int i = 0;while (i < 100){

System.out.println

(locationCells[i]); i++;}

For-each loop

• for(int cell : locationCells){ System.out.println(cell);}

Page 20: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

For-each loops

For loop

• for(int i = 0; i<100; i++){

System.out.println

(locationCells[i]);}

For-each loop

• for(int cell : locationCells){ System.out.println(cell);}

Page 21: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Hw Q8

For loop

• for(int i = 0; i<someNums.length; i++){

System.out.println

(someNums[i]);}

For-each loop

• for(int num : someNums){ System.out.println(num);}

Page 22: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Hw Q9

For loop

• for(int index = 0; index < dailyTemps.length; index++)

{System.out.println

(dailyTemps[index]);}

For-each loop

• for(int temp : dailyTemps){

System.out.println

(temp);}

Page 23: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Hw Q10 – for loop

While loop

• int index = 0;while(index<cities.length){

System.out.println

(cities[index]);index++;

}

For loop

• for(int index = 0; index < cities.length; index++){

System.out.println

(cities[index]);}

Page 24: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Hw Q10 – for-each loop

For loop

• for(int index = 0; index < cities.length; index++){

System.out.println

(cities[index]);}

For-each loop

• for(int city : cities){ System.out.println(city);}

Page 25: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

For loop vs. for-each loop

For loop

• Has an index useful for setting data that depends on the index

• Initialization, boolean test, and iteration expression are all in one line

For-each loop

• Easier to write when simply accessing data from an array

• Not much better than a while loop if not accessing array data

Page 26: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

For loop vs. for-each loop

For loop

• for(int i = 0; i<100; i++){

locationCells[i] = i;}

For-each loop

• int i = 0;for(int cell : locationCells){

cell = i;i++;

}

Page 27: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

While loop vs. for-each loop

While loop

• int i = 0;while(i < locationCells.length){

locationCells[i] = i;i++;

}

For-each loop

• int i = 0;for(int cell : locationCells){

cell = i;i++;

}

Page 28: HFJ CHAPTER 5 REVIEW Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll: What is the purpose of.

Game Lab Q2• public void printLocations()

{if (locationCells == null){

System.out.println(“Please set my locations”);

}else{

for(int loc : locationCells){

System.out.println(loc);}

}}