Programming Project 1 is available—see link on class web page.
Hw1 is due next class: questions?
Using Objects
Strings are objects, so are Scanners and arrays.
Strings are Objects
Useful methods: pg. 162, 1121
Examples
String fruit = “apple”;
<picture of reference “fruit” in a little box, pointing to the string characters in another bigger box>
fruit.length() = 5, fruit.startsWith(“ap”) is true, fruit.toUpperCase() = “APPLE”. Here “APPLE” is another new String object created by the toUpperCase method.
Strings are special objects because they are immutable and can be constructed without using “new”. Also they can be +’d together:
Immutable = can’t be changed, so we create new strings out of old strings instead.
Strings have “state”, the chars inside them. Very simple.
We can have arrays of Strings, to save for later, configure, etc.
Ex. String suits[] = {“Clubs”, “Diamonds”, “Hearts”, “Spades”};
Suits[0] is “Clubs”, … suits[3] = “Spades”.
52 cards in a deck, numbered 0 to 51.
Cards 0-12 are clubs, 13-25 are Diamonds, 26-38 are Hearts, 39-51 are Spades
We can use integer division to compute the suit number from the card number-- division by 13 says how many 13’s are in a number…
Card 22 (a Diamond) has suit 22/13 = 1, and suits[1] = “Diamonds”
Card 32 (a Heart) has suit 32/13 = 2, and suits[2] = “Hearts”
In general, card x has suit x/13 named suits[x/13]; Useful in card games.
Do class exercise---
Scanners are Objects
Scanner: a more “normal” object—can be changed, needs new keyword to construct.
Pg. 165—
Scanner console = new Scanner(System.in);
// print prompt
Double loan = console.nextDouble();
// print prompt
Int years = console.nextInt();
…
Here console is an object that keeps track of what characters have been used already for values, and arranges with the OS to get more chars as needed.
<picture a little box named console (the reference) pointing to a bigger drawing of the machine inside Scanner object working on a stream of characters>
This process is illustrated on pp. 387-388
That kind of memory inside the object is called object state.
It is possible to ask the Scanner if there is more data to work on—
See pp. 335-338
Method on pg. 338, also in NumberGuess3, pg. 355
public static int getInt(Scanner console, String prompt)
checks for int coming in with console.hasNextInt(), keeps trying for an int…
Changing the working Radix in a Scanner.
Radix: number base. We usually use base 10, or “radix” 10. But hex numbers, base 16 are important in CS and IT. For example, addresses in memory are usually written in hex.
Hex digits: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f 16 digits, values 0 – 15.
Example of hex number: a1, a = 10, so the value of this is 10*16 + 1 = 161.
Another example, from Wikipedia: http://en.wikipedia.org/wiki/Hexadecimal
For example, the hexadecimal number 2AF3 is equal, in decimal, to (2 × 163) + (10 × 162) + (15 × 161) + (3 × 160), or 10995.
Note: you don’t have to remember how to do this for this class. The point here is how we can talk to our objects and get them to do our bidding.
The above program worked with decimal numbers, but we can switch the radix of a Scanner using the Scanner method useRadix.
input.useRadix(16); //switch the Scanner object to hex (can use any radix 2-36)
Then input.nextInt() can read “A1” or “a1” into an int (value 161). Note that an int is held in binary in memory, so it doesn’t care whether the human typed the original number in decimal or hex.
The Scanner object has inside it the “current” radix, an integer between 2 and 36. It starts out being 10, but can be changed by the useRadix() method. We added a little box inside the Scanner machine picture to show the stored radix.
We can “run” the Scanner via its methods. We can think of switching a Scanner to hex as a customization of it to our own purposes. Another customization possible is changing the delimiter for the tokens with useDelimiter.
That’s the object way.