Java Review Fundamentals of Computer Science
Link to Head First pdf File https://zimslifeintcs.files.wordpress.com/2011/12/h ead-first-java-2nd-edition.pdf
Outline Data Types Arrays Boolean Expressions Conditionals: if-else switch Iteration: while and for loops Object Oriented Design and Programming State Behavior Constructors Accessors and Mutators Other Important Methods API Static Methods http://www.hark.com/clips/sfpbhcvhzh-jeopardy-theme
Data Types Is the following set of statements legal? Why or why not? If not legal, what would make it work? int j, k; double x = 2.0; j = x;
Data Types Is the following set of statements legal? Why or why not? If not legal, what would make it work? int j, k; double x = 2.0; j = x; Not legal you cannot assign a data item that takes up more space in memory (a double) to one that takes up less space (an int). You could type cast x to an integer so that the last line becomes: j = (int) x; This would cause x to be truncated to an integer and would now be legal.
Arrays Let s say you have a set of Duck objects you want to keep track of in an array. There are 7 Ducks. How do you declare the array? Is it ready to use as is, or do you need to do additional work?
Arrays Let s say you have a set of Duck objects you want to keep track of in an array. There are 7 of these Duck objects. How do you declare the array? Is it ready to use as is, or do you need to do additional work? Duck [] ducks = new Duck[7]; for (int i=0; i<7; i++) ducks[i] = new Duck(); The first line initializes the array to hold 7 Ducks, but each Duck object in the array is not yet usable until it has been allocated space on the heap. The for loop allocates that space by calling the Duck constructor for each one in the array.
Arrays How many dimensions can an array have?
Arrays How many dimensions can an array have? As many as you need.
Boolean Expressions What is the result of the following boolean expression? (What is the value of d after this runs?) int a = 4; int b = 6; boolean c = true; boolean d; d = (a < b) &&!c
Boolean Expressions What is the result of the following boolean expression? (What is the value of d after this runs?) int a = 4; int b = 6; boolean c = true; boolean d; d = (a < b) &&!c false (a < b) is true!c is false (a<b) &&!c is equal to true && false which is false
Boolean Expressions How about this one? (What is the value of d after this runs?) int a = 4; int b = 6; boolean c = true; boolean d; d = ((a < b) &&!c) (c && (b < a) c
Boolean Expressions How about this one? (What is the value of d after this runs?) int a = 4; int b = 6; boolean c = true; boolean d; d = ((a < b) &&!c) (c && (b < a) c true (a < b) is true,!c is false so first part is false, c is true, b<a is false, c && (b<a) is false, c is true false or false or true is true
Boolean Expressions Bonus Question: Which one is more efficient and why? int a = 4; int b = 6; boolean c = true; boolean d; // First one d = ((a < b) &&!c) (c && (b < a) c // Second one d = c ((a < b) &&!c) (c && (b < a)
Boolean Expressions Bonus Question: Which one is more efficient and why? int a = 4; int b = 6; boolean c = true; boolean d; // First one d = ((a < b) &&!c) (c && (b < a) c // Second one d = c ((a < b) &&!c) (c && (b < a) The second one, because when we use the double operations (&& instead of &, and instead of ), Java does lazy evaluation. In the second one, as soon as c evaluates to true. other stuff, Java knows the whole thing will be true and doesn t go any further.
Conditionals What is the output of this code? String s = yes ; if (s == yes ) System.out.println( Yes it is. ); else System.out.println( No I don t think so );
Conditionals What is the output of this code? String s = yes ; if (s == yes ) System.out.println( Yes it is. ); else System.out.println( No I don t think so ); OK, trick question you should not use == to compare strings. The if statement should have read if (s.equals( yes )) The answer is that you really don t know what the output will be. The == may compare like you expect, or it might not.
Conditionals What is the output of this code? String s = yes ; int a = 4; if (s.equals( yes )) System.out.println( Yes it is. ); if (a == 3) System.out.println( 3 ); else System.out.println( No I don t think so );
Conditionals What is the output of this code? String s = yes ; int a = 4; if (s.equals( yes )) System.out.println( Yes it is. ); if (a == 3) System.out.println( 3 ); else System.out.println( No I don t think so ); Yes it is. No I don t think so Java doesn t care about how you ve indented the code. An else always goes with the next closest if, in this case, the else goes with if (a == 3)
What is the output of this code? Conditionals final int NORTH = 0; final int SOUTH = 1; final int EAST = 2; final int WEST = 3; int direction = 0; switch (direction) { case NORTH: y--; System.out.println("Walking north"); case SOUTH: y++; System.out.println("Walking south"); case EAST: x++; System.out.println("Walking east"); case WEST: x--; System.out.println( Walking west ); }
What is the output of this code? final int NORTH = 0; final int SOUTH = 1; final int EAST = 2; final int WEST = 3; int direction = 0; switch (direction) { case NORTH: y--; } Conditionals Walking north Walking south Walking east Walking west System.out.println("Walking north"); case SOUTH: y++; System.out.println("Walking south"); case EAST: x++; System.out.println("Walking east"); case WEST: x--; System.out.println( Walking west ); Without a break statement in each of the cases, Java simply executes the next one (and next one and ) until it reaches a break or the end of the switch statement.
Conditionals What is the difference between a conditional, like if or switch and a loop statement like for or while?
Conditionals What is the difference between a conditional, like if or switch and a loop statement like for or while? Conditional statements choose between options they make a decision. Once. The purpose of a loop statement is to iterate to perform a set of actions multiple times.
Iteration / Loops How many times does the following loop execute? (What is the output of this code?) int counter = 0; int a = 10; while (a > 0){ a -= 2; counter++; } System.out.println(counter);
Iteration / Loops How many times does the following loop execute? (What is the output of this code?) int counter = 0; int a = 10; while (a > 0){ a -= 2; counter++; } System.out.println(counter); 5 On each iteration, 2 is subtracted from a, and the loop ends when a is less than or equal to 0, so it goes through 5 times.
Iteration / Loops Bonus question: How many times does the following loop execute? (What is the output of this code?) int counter = 0; int a = 10; while (a > 0){ counter++; } System.out.println(counter);
Iteration / Loops Bonus question: How many times does the following loop execute? (What is the output of this code?) int counter = 0; int a = 10; while (a > 0){ counter++; } System.out.println(counter); Well it never ends, so nothing is ever printed out. The condition on which the while loop is based (a > 0) is never changed. It starts out as true, and this loop will run forever (an infinite loop).
Iteration / Loops How many times does the following loop execute? (What is the output of this code?) int counter = 0; int a = 10; do { a -= 2; counter++; } while (a > 0); System.out.println(counter);
Iteration / Loops How many times does the following loop execute? (What is the output of this code?) int counter = 0; int a = 10; do { a -= 2; counter++; } while (a > 0); System.out.println(counter); 5 In this case, the do while loop iterates exactly the same amount of times the while loop would have. The difference is that if a had been less than 0 to start, the loop body would have executed once.
Iteration / Loops When would you use a for loop instead of a while loop? Can a for loop always be written as (translated to) a while loop? Can a while loop always be written as (translated to) a for loop?
Iteration / Loops When would you use a for loop instead of a while loop? Can a for loop always be written as (translated to) a while loop? Can a while loop always be written as (translated to) a for loop? Use a for loop when you know how many times you need to iterate. Use a while loop when you want to iterate as long as a condition holds true. A for loop can always be rewritten as a while loop, but a while loop cannot always be rewritten as a for loop.
Classes and Objects What is the difference between a class and an object?
Classes and Objects What is the difference between a class and an object? A class is a blueprint for a type of thing. An object is an instantiation (or instance, or example) of that class. A class defines the structure, but there is no memory allocated until an object (or more than one object) is constructed.
Classes and Objects What is a constructor method? What is it s main purpose?
Classes and Objects What is a constructor method? What is it s main purpose? A constructor is a method that has the same name as the class. It can be overloaded and have parameters, and its main purpose is to create an object of type class, that is it allocates the memory for an object on the heap. A constructor may initialize instance variables associated with the object and perform other initialization actions as well.
Classes and Objects What is a method? What is it s main purpose?
Classes and Objects What is a method? What is it s main purpose? In Java, all methods live within a class. The purpose of a method is to isolate the code to perform a single function so that code is not duplicated. Taken together, all the methods within a class define its behavior.
Classes and Objects Regarding an object, what defines its state?
Classes and Objects Regarding an object, what defines its state? The values of its instance variables.
Methods In the following method, what is its signature, what are it parameters, what is its return type, and what information would be included in its API description? public int AddThese(int a, int b) { return a + b; }
Methods In the following method, what is its signature, what are it parameters, what is its return type, and what information would be included in its API description? public int AddThese(int a, int b) { return a + b; } It s signature is its name, and the number and type of its parameters. a and b are its parameters, and it returns an integer. All of these things (name, parameters, return type) plus a description of what the method does would be included in its API description.
Methods What is a mutator method? What is an accessor method? Why do we use these instead of accessing instance variables directly?
Methods What is a mutator method? What is an accessor method? Why do we use these instead of accessing instance variables directly? A mutator method is a setter method, and it changes the value of an instance variable. An accessor method is a getter, and it retrieves the value of an instance variable. We use these to protect the data in instance variables from direct external change we can use these methods to ensure that instance variables are not changed to illegal values.
Static Methods and Variables What does it mean for a method or variable to be static?
Static Methods and Variables What does it mean for a method or variable to be static? It means that there is only one method or variable for the entire class, no matter how many objects have been created. An entire class can be static (for example, the Math class in Java) which means that you can only have one instance of that class.
Summary Data Types Arrays Boolean Expressions Conditionals: if-else switch Iteration: while and for loops Object Oriented Design and Programming State Behavior Constructors Accessors and Mutators Other Important Methods API Static Methods