java-overview-intermediate

advertisement
Intermediate Review
Shlomo Hershkop 2007
1
Intermediate Review






References
Basic inheritance
Time classes
Date Classes
File input/output
Packages
Shlomo Hershkop 2007
2
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
Shlomo Hershkop 2007
3
Basic Example


int sum = 100
int arr[];
SUM
arr
Shlomo Hershkop 2007
100
X
4
Next



int sum = 100
int arr[];
arr = new int[10];
Shlomo Hershkop 2007
SUM
100
arr
5
Question





What happens on the last line ?
SUM
int arr[];
arr = new int[10];
arr
arr[0] = 12;
arr = new int[5];
Shlomo Hershkop 2007
100
6
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)
Shlomo Hershkop 2007
7
References

So a primitive variable is mapped to a location in memory



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

x
int x;

miniVan mycar;
mycar = new miniVan(….)
234
mycar
ref
Shlomo Hershkop 2007
Honda
Odyssey
2000
Red
8
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
y
mycar
234
234
ref
oCar
Honda
Odyssey
2000
Red
ref
Shlomo Hershkop 2007
9
Who cares ?

So what is the difference ??
Shlomo Hershkop 2007
10
Difference !


Messing with x, won’t affect y
Messing with class reference will change
both objects
Shlomo Hershkop 2007
11
Difference II


oCar.year = 2005;
Surprise!
x
y
mycar
234
234
ref
oCar
Honda
Odyssey
2005
Red
ref
Shlomo Hershkop 2007
12
Why?


If its such a bad idea, why have it at all?
Any ideas why we would want to create
object using references?
Shlomo Hershkop 2007
13
Advantage


If the class is huge
Don’t want to keep copying all the
member variables if I plan on only reading
it
Shlomo Hershkop 2007
14
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 ?


Shlomo Hershkop 2007
15
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


Shlomo Hershkop 2007
16
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
Shlomo Hershkop 2007
17
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 
Shlomo Hershkop 2007
18
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
Shlomo Hershkop 2007
19
Eclipse
1.
2.
3.
4.
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
1.
2.
Check off that you want comments
Check off you want a main
Shlomo Hershkop 2007
20
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);
}
}
Shlomo Hershkop 2007
21
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 ?
Shlomo Hershkop 2007
22
Basic parts

Board – tic tac toe board



Move


What kind of method would you need here ?
Although it’s a 3 by 3 board, lets keep it general
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
Shlomo Hershkop 2007
23
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
Shlomo Hershkop 2007
24
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;
Shlomo Hershkop 2007
25




Board.X
Board.O
Don’t need to instantiate to use it
Makes it easier to speak a common
language when using the class
Shlomo Hershkop 2007
26
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
Shlomo Hershkop 2007
27
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
Shlomo Hershkop 2007
28
Next

Lets code the move class

Very basic

Need method to get the user’s next move


Need to ask board if game is done


Assume move is a move from each user
Add another method to the board game
If user enters bad move…what do you want to do ?
Shlomo Hershkop 2007
29
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 ?
Shlomo Hershkop 2007
30
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 ?
Shlomo Hershkop 2007
31
Yay!

Ok you have a working game

Test it out on all your friends 
Shlomo Hershkop 2007
32
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
Shlomo Hershkop 2007
33
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
Shlomo Hershkop 2007
34
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
Shlomo Hershkop 2007
35
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)
Shlomo Hershkop 2007
36

Hope you had fun learning this!
Shlomo Hershkop 2007
37
Download