Intermediate Review - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

Intermediate Review

Description:

Create new primitive variable y, will result in another memory location and value copy ... Create another miniVan instance in the following will simply make it ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 38
Provided by: shl5
Category:

less

Transcript and Presenter's Notes

Title: Intermediate Review


1
Intermediate Review
2
Intermediate Review
  • References
  • Basic inheritance
  • Time classes
  • Date Classes
  • File input/output
  • Packages

3
Memory
  • One of the good things about java is that it
    abstracts memory away
  • You dont have to worry about how things are
    being put into memory
  • But you still need to be aware of how things are
    represented

4
Basic Example
  • int sum 100
  • int arr

100
SUM
X
arr
5
Next
  • int sum 100
  • int arr
  • arr new int10

100
SUM
arr
6
Question
  • What happens on the last line ?
  • int arr
  • arr new int10
  • arr0 12
  • arr new int5

100
SUM
arr
7
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)

8
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
9
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
10
Who cares ?
  • So what is the difference ??

11
Difference !
  • Messing with x, wont affect y
  • Messing with class reference will change both
    objects

12
Difference II
  • oCar.year 2005
  • Surprise!

x
234
HondaOdyssey2005Red
y
234
mycar
ref
oCar
ref
13
Why?
  • If its such a bad idea, why have it at all?
  • Any ideas why we would want to create object
    using references?

14
Advantage
  • If the class is huge
  • Dont want to keep copying all the member
    variables if I plan on only reading it

15
Back to Arrays
  • Can have array of length 0
  • not the same as null
  • int numbers
  • numbers new int0
  • numbers null
  • What is the difference here ?

16
Two dimensional arrays
  • You can create an array of any object, including
    arrays
  • Person bunch new Person10
  • int table new int1020
  • int t tableij
  • An array of an array is a two dimensional array

17
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

18
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 dont have to
    worry for example how they enter the information
  • In theory could be using hieroglyphics ?

19
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

20
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

21
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)

22
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 ?

23
Basic parts
  • Board tic tac toe board
  • What kind of method would you need here ?
  • Although its 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

24
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

25
Note please
  • The users in the game are represented by Xs and
    Os
  • No reason we cant use 1,2
  • If have a member , what does it mean ?
  • public static final int X 1
  • public static final int O 2

26
  • Board.X
  • Board.O
  • Dont need to instantiate to use it
  • Makes it easier to speak a common language when
    using the class

27
  • public class TicTacToepublic static final int
    EMPTY 0public static final int X 1public
    static final int O 2private final int SIZE
    3 //for 3x3
  • private int board
  • //ok lets add a constructor

28
coding
  • Add constructor
  • Add reset method
  • Add toString method
  • Use for loop
  • Need to translate from 1,2 to X,O
  • Dont hard code values, use your final statics

29
Next
  • Lets code the move class
  • Very basic
  • Need method to get the users 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 movewhat do you want to do ?

30
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 ?

31
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 ?

32
Yay!
  • Ok you have a working game
  • Test it out on all your friends ?

33
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

34
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

35
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

36
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)

37
  • Hope you had fun learning this!
Write a Comment
User Comments (0)
About PowerShow.com