Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Faster than copying the actual data
  • Show them how import are automatically done in eclipse
  • Will get zero results if any of the numbers have defaulted to zero
  • java-overview-interm..

    1. 1. Intermediate Review
    2. 2. Intermediate Review <ul><li>References </li></ul><ul><li>Basic inheritance </li></ul><ul><li>Time classes </li></ul><ul><li>Date Classes </li></ul><ul><li>File input/output </li></ul><ul><li>Packages </li></ul>
    3. 3. Memory <ul><li>One of the good things about java is that it abstracts memory away </li></ul><ul><li>You don’t have to worry about how things are being put into memory </li></ul><ul><li>But you still need to be aware of how things are represented </li></ul>
    4. 4. Basic Example <ul><li>int sum = 100 </li></ul><ul><li>int arr[]; </li></ul>100 X SUM arr
    5. 5. Next <ul><li>int sum = 100 </li></ul><ul><li>int arr[]; </li></ul><ul><li>arr = new int[10]; </li></ul>100 SUM arr
    6. 6. Question <ul><li>What happens on the last line ? </li></ul><ul><li>int arr[]; </li></ul><ul><li>arr = new int[10]; </li></ul><ul><li>arr[0] = 12; </li></ul><ul><li>arr = new int[5]; </li></ul>100 SUM arr
    7. 7. answer <ul><li>A new set of ints are allocated and WE LOSE all data in the old one </li></ul><ul><li>You need to copy over data (and use a temp array) </li></ul>
    8. 8. References <ul><li>So a primitive variable is mapped to a location in memory </li></ul><ul><ul><li>int x; x= 234; </li></ul></ul><ul><li>Class Objects are a little more complicated since they have member variables and methods </li></ul><ul><li>Memory references will point to a location which has been setup with the object you create </li></ul><ul><ul><li>miniVan mycar; </li></ul></ul><ul><ul><li>mycar = new miniVan(….) </li></ul></ul>234 ref x mycar Honda Odyssey 2000 Red
    9. 9. References <ul><li>Create new primitive variable y, will result in another memory location and value copy </li></ul><ul><li>int y = x; </li></ul><ul><li>Create another miniVan instance in the following will simply make it point to same place (unless new is used) </li></ul><ul><li>miniVan oCar = mycar; </li></ul>234 234 ref ref x mycar Honda Odyssey 2000 Red y oCar
    10. 10. Who cares ? <ul><li>So what is the difference ?? </li></ul>
    11. 11. Difference ! <ul><li>Messing with x, won’t affect y </li></ul><ul><li>Messing with class reference will change both objects </li></ul>
    12. 12. Difference II <ul><li>oCar.year = 2005; </li></ul><ul><li>Surprise! </li></ul>234 234 ref ref x mycar Honda Odyssey 2005 Red y oCar
    13. 13. Why? <ul><li>If its such a bad idea, why have it at all? </li></ul><ul><li>Any ideas why we would want to create object using references? </li></ul>
    14. 14. Advantage <ul><li>If the class is huge </li></ul><ul><li>Don’t want to keep copying all the member variables if I plan on only reading it </li></ul>
    15. 15. Back to Arrays <ul><li>Can have array of length 0; </li></ul><ul><li>not the same as null: </li></ul><ul><li>int numbers[]; </li></ul><ul><li>numbers = new int[0]; </li></ul><ul><li>numbers = null; </li></ul><ul><li>What is the difference here ? </li></ul>
    16. 16. Two dimensional arrays <ul><li>You can create an array of any object, including arrays </li></ul><ul><li>Person bunch[] = new Person[10]; </li></ul><ul><li>int[][] table = new int[10][20]; </li></ul><ul><li>int t = table[i][j]; </li></ul><ul><li>An array of an array is a two dimensional array </li></ul>
    17. 17. Before coding… <ul><li>Before we start to code one last thing </li></ul><ul><li>How to get user input ?? </li></ul><ul><li>Most languages give you access to something called STANDARD out/in </li></ul>
    18. 18. Standard IN/OUT <ul><li>Assume there is some way to talk to user </li></ul><ul><li>And get user input </li></ul><ul><li>Very low level </li></ul><ul><li>Want something a little higher so don’t have to worry for example how they enter the information </li></ul><ul><ul><li>In theory could be using hieroglyphics  </li></ul></ul>
    19. 19. Reading Input through scanners <ul><li>Construct Scanner </li></ul><ul><ul><li>from input stream (e.g. </li></ul></ul><ul><li>Scanner in = new Scanner( </li></ul><ul><li>nextInt, nextDouble reads next int or double </li></ul><ul><ul><li>int n = in.nextInt(); </li></ul></ul><ul><li>hasNextInt, hasNextDouble test whether next token is a number </li></ul><ul><li>next reads next string (delimited by whitespace) </li></ul><ul><li>nextLine reads next line </li></ul>
    20. 20. Eclipse <ul><li>Start Eclipse </li></ul><ul><li>Start a new project (right click in project explorer) </li></ul><ul><li>Double click, right click on default package and start a new class </li></ul><ul><li>Call it InputTester </li></ul><ul><ul><li>Check off that you want comments </li></ul></ul><ul><ul><li>Check off you want a main </li></ul></ul>
    21. 21. Code then run this <ul><li>public class InputTester </li></ul><ul><li>{ </li></ul><ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>Scanner in = new Scanner(; </li></ul><ul><li>System.out.print(&quot;How old are you?&quot;); </li></ul><ul><li>int age = in.nextInt(); </li></ul><ul><li>age++; </li></ul><ul><li>System.out.println(&quot;Next year, you'll be &quot; + age); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    22. 22. coding <ul><li>Let start to code </li></ul><ul><li>Tic tac toe game </li></ul><ul><ul><li>A program to allow two used to play a game </li></ul></ul><ul><li>Where to start ?? </li></ul><ul><ul><li>What objects can you think of ? </li></ul></ul>
    23. 23. Basic parts <ul><li>Board – tic tac toe board </li></ul><ul><ul><li>What kind of method would you need here ? </li></ul></ul><ul><ul><li>Although it’s a 3 by 3 board, lets keep it general </li></ul></ul><ul><li>Move </li></ul><ul><ul><li>The part which can ask for a move and check if legal and place into board </li></ul></ul><ul><li>Front end </li></ul><ul><ul><li>We would put main in here </li></ul></ul><ul><ul><li>It would start a game </li></ul></ul><ul><ul><li>When done ask if you want to play again </li></ul></ul>
    24. 24. Board class <ul><li>Would need a constructor </li></ul><ul><li>Reset method to set everything to blank </li></ul><ul><li>Place move to put a move into the board </li></ul><ul><li>Print out to see the board </li></ul>
    25. 25. Note please <ul><li>The users in the game are represented by X’s and O’s </li></ul><ul><li>No reason we can’t use 1,2 </li></ul><ul><li>If have a member , what does it mean ? </li></ul><ul><li>public static final int X = 1; </li></ul><ul><li>public static final int O = 2; </li></ul>
    26. 26. <ul><li>Board.X </li></ul><ul><li>Board.O </li></ul><ul><li>Don’t need to instantiate to use it </li></ul><ul><li>Makes it easier to speak a common language when using the class </li></ul>
    27. 27. <ul><li>public class TicTacToe{ public static final int EMPTY = 0; public static final int X = 1; public static final int O = 2; private final int SIZE = 3; //for 3x3 </li></ul><ul><li>private int[][] board; </li></ul><ul><li>//ok lets add a constructor </li></ul>
    28. 28. coding <ul><li>Add constructor </li></ul><ul><li>Add reset method </li></ul><ul><li>Add toString method </li></ul><ul><ul><li>Use for loop </li></ul></ul><ul><ul><li>Need to translate from 1,2 to X,O </li></ul></ul><ul><ul><ul><li>Don’t hard code values, use your final statics </li></ul></ul></ul>
    29. 29. Next <ul><li>Lets code the move class </li></ul><ul><li>Very basic </li></ul><ul><ul><li>Need method to get the user’s next move </li></ul></ul><ul><ul><ul><li>Assume move is a move from each user </li></ul></ul></ul><ul><ul><li>Need to ask board if game is done </li></ul></ul><ul><ul><ul><li>Add another method to the board game </li></ul></ul></ul><ul><ul><li>If user enters bad move…what do you want to do ? </li></ul></ul>
    30. 30. Finally <ul><li>Now lets code the front end with main </li></ul><ul><li>Create a class MainGameTTT </li></ul><ul><ul><li>Have a main in it </li></ul></ul><ul><ul><li>What do we need to do next ? </li></ul></ul>
    31. 31. Game logic <ul><li>Start a game </li></ul><ul><li>Loop </li></ul><ul><li>At the end ask user if they want to play another game ? </li></ul><ul><ul><li>This is a little tricky to loop….any ideas ? </li></ul></ul>
    32. 32. Yay! <ul><li>Ok you have a working game </li></ul><ul><li>Test it out on all your friends  </li></ul>
    33. 33. Multiple dimensions <ul><li>No reason cant create 4,5,6 dimension arrays </li></ul><ul><li>Gets hard to manage </li></ul><ul><li>Better idea: </li></ul><ul><ul><li>Think about another way of representing the data </li></ul></ul><ul><li>Often creating an object is a better approach </li></ul>
    34. 34. Arrays further <ul><li>Need to explicitly copy contents of arrays when resizing arrays with temp one </li></ul><ul><li>Better solution: </li></ul><ul><li>ArrayList </li></ul><ul><li>Vector </li></ul><ul><li>Full object versions of arrays </li></ul><ul><li>Capacity can grow over time </li></ul><ul><li>Useful methods bundles with them </li></ul>
    35. 35. code <ul><li>Create a new class with a main called VectorTest </li></ul><ul><li>Create a vector </li></ul><ul><li>Put in some stuff </li></ul><ul><li>Print them out </li></ul><ul><li>Replace something in the middle </li></ul><ul><li>Print it out </li></ul><ul><li>Clear the vector </li></ul><ul><li>Print it out </li></ul>
    36. 36. Default values <ul><li>Should be aware if you forget to set values </li></ul><ul><li>Might mess up your logic </li></ul><ul><ul><li>Think of multiplying a bunch of numbers and not setting one of them… </li></ul></ul><ul><li>Compiler/IDE will let you know if you forgot to set values (warning) </li></ul>
    37. 37. <ul><li>Hope you had fun learning this! </li></ul>