question of the day you overhear a boy & his mother talking: mom:what is 25 + 8? boy:...

29
Question of the Day You overhear a boy & his mother talking: Mom: What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy: Simple. It's 40. Mom: Excellent! Now what's 40 + 23? Boy: Boring. The answer is 37. Mom: Perfect! Once you see how the boy is dressed it all makes sense. What is the boy wearing?

Upload: milton-watkins

Post on 04-Jan-2016

215 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Question of the Day

You overhear a boy & his mother talking:Mom: What is 25 + 8?Boy: That's easy, 33.Mom: Good. What's 33 + 7?Boy: Simple. It's 40.Mom: Excellent! Now what's 40 + 23?Boy: Boring. The answer is 37.Mom: Perfect!

Once you see how the boy is dressed it all makes sense. What is the boy wearing?

Page 2: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Question of the Day

You overhear a boy & his mother talking:Mom: What is 25 + 8?Boy: That's easy, 33.Mom: Good. What's 33 + 7?Boy: Simple. It's 40.Mom: Excellent! Now what's 40 + 23?Boy: Boring. The answer is 37.Mom: Perfect!

Once you see how the boy is dressed it all makes sense. What is the boy wearing?

A football uniform

Page 3: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

LECTURE 3:REFERENCES & ENUM

Page 4: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Announcements

If you need more review of Java… I have lots of good resources – talk to me Use “Additional Help” link on webpage

Weekly assignments problems due before class Remove rust from summer and get back

into coding Problems designed to help learn new

material, too

Page 5: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Data Types

8+1 primitive data types Examples: boolean, byte, char, int, double, String*

Only types that work with Java operators Operators include: +, -, %, &&, ||, >=, <, !

Primitives used natively by computers Means that using them is very quick Implies greater support within the language

Page 6: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Primitive Types

Primitive variables are simple to use Each variable is a “xerox” holding a

value Assignment copies value Update variable being assigned only

Page 7: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Java Enumerations

enum a fancy type of Java class Includes fields, methods & constructors like

any class But constructors must be private… … so cannot create new instances of an enum

Specifies predefined list of instances Instances declared at start of enum Instances accessed as EnumName.instanceName

Represent known, finite data with an enum 3 Musketeers, 7 Dwarfs, 4 Card Suits, …

Page 8: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Enumeration Methods

enum instances always include these methods public String name()

Returns String equal to actual instances' name public int ordinal()

Position in listing where instance was declared

Can also find all enumerated instances public static EnumName[] values()

Returns array of values enumerated by EnumName

Page 9: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Java Enumerations

Must be written in file EnumName.javapublic enum EnumName {

Instance0, Instance1, Instance2;}

Can be used in methods elsewhere:EnumName var = EnumName.Instance0;System.err.println(var.name());if (var.ordinal() > 1) { System.err.println(“Got Instance2!”);}EnumName[] arr = EnumName.values();

Page 10: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Quick Quiz

Should you be taking notes? Let’s see…

1. What are the 3 methods defined by each enum?

2. What do each of these methods do?3. Can you use ever compare an enum

using “>”?

Page 11: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Classes

Real world needs more than primitives Additional types defined by classes in

Java Classes usually begin with:public class ClassNameGoesHere { By convention, name starts with capital

letter Use interior capitals to highlight words in

name Should be in file named ClassNameGoesHere.java

ClassNameGoesHere becomes type to use

Page 12: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Creating Instances

Use instances of a class Keyword new creates instances:public class Kitty { ... }new Kitty(...);

Page 13: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Reference Variables

Variables of class type are references Must assign variable to instance before

use Variable is null when not referring to any

instance Work similar to a remote control

Reference is not object, but refers to objectTV telly = new TV(...);

Page 14: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Reference Variables

Variables of class type are references Must assign variable to instance before

use Variable is null when not referring to any

instance Work similar to a remote control

Reference is not object, but refers to objectTV telly = new TV(...);

telly

Page 15: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Reference Variables

Variables of class type are references Must assign variable to instance before

use Variable is null when not referring to any

instance Work similar to a remote control

Reference is not object, but refers to objectTV telly = new TV(...);

telly

Page 16: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Reference Variables

Variables of class type are references Must assign variable to instance before

use Variable is null when not referring to any

instance Work similar to a remote control

Reference is not object, but refers to objectTV telly = new TV(...);

telly telly

Page 17: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Using Instances

Use instances of a class Each instance is uniqueKitty cat = new Kitty(...);

cat

Page 18: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Using Instances

Use instances of a class Each instance is uniqueKitty cat = new Kitty(...);Kitty tiger = new Kitty(...);

cat tiger

Page 19: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Using Instances

Use instances of a class Each instance is uniqueKitty cat = new Kitty(...);Kitty tiger = new Kitty(...);cat = new Kitty(...);

cat tiger

Page 20: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Using Instances

Use instances of a class Each instance is uniqueKitty cat = new Kitty(...);Kitty tiger = new Kitty(...);cat = new Kitty(...);

cat tiger

Page 21: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Using Instances

Use instances of a class Each instance is uniqueKitty cat = new Kitty(...);Kitty tiger = new Kitty(...);cat = new Kitty(...);

cat tiger

Page 22: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Working With References

Assignments alias references Only way to create instance using new Refer to same instance when aliased

Page 23: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Working With References

Assignments alias references Only way to create instance using new Refer to same instance when aliased

Kitty cat, tiger;

cat

tiger

Page 24: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Working With References

Assignments alias references Only way to create instance using new Refer to same instance when aliased

Kitty cat, tiger;cat = new Kitty(...);

cat

tiger

Page 25: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Working With References

Assignments alias references Only way to create instance using new Refer to same instance when aliased

Kitty cat, tiger;cat = new Kitty(...);tiger = new Kitty(...); cat

tiger

Page 26: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Working With References

Assignments alias references Only way to create instance using new Refer to same instance when aliased

Kitty cat, tiger;cat = new Kitty(...);tiger = new Kitty(...);tiger = cat;

cat

tiger

Page 27: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Aliased variables refer to same instance Single instance get changes to aliased variables Aliased variables see all changes that are made But, assignments only affect the one variable

Kitty cat, tiger, kat;cat = new Kitty(...);tiger = new Kitty(...);kat = tiger;tiger = cat;cat = kat;tiger = null;

Aliasing

cat

tiger

kat

Page 28: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

Your Turn

Get into your groups and complete activity

Page 29: Question of the Day You overhear a boy & his mother talking: Mom:What is 25 + 8? Boy: That's easy, 33. Mom: Good. What's 33 + 7? Boy:Simple. It's 40. Mom:Excellent!

For Next Lecture

Reading from AF Chapters 8 & 9 for Wednesday What is the advantages of using arrays? When & why can it suck to use arrays? Are arrays like primitives or references?

There is weekly assignment problem on Angel Due before Wednesday’s lecture (via e-mail) Get back into the swing of writing Java code