hfj chapter 5 review don’t forget: you can copy- paste this slide into other presentations, and...

28

Upload: jonas-small

Post on 16-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

HFJ CHAPTER 5 REVIEW

Poll: What is the purpose of writing prepcode?

Poll: What is test code?

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

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)

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

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.

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

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

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

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

Poll: Which of these is not a loop?

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

For loops

While loops

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

//repeated codei++;

}

For loops

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

//repeated code}

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]);}

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

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);}

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);}

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);}

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);}

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]);}

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);}

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

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++;

}

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++;

}

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);}

}}