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