hfj chapter 5 review don’t forget: you can copy- paste this slide into other presentations, and...
Post on 16-Dec-2015
214 Views
Preview:
TRANSCRIPT
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)
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.
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
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
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]);}
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++;
}
top related