27
CS 121 Week 10 - Monday

Week 10 - Monday. What did we talk about last time? Method overloading Lab 9

Embed Size (px)

Citation preview

Page 1: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

CS 121Week 10 - Monday

Page 2: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Last time

What did we talk about last time? Method overloading Lab 9

Page 3: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Questions?

Page 4: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Project 4

Page 5: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Last Chance for Method and StdDraw Practice (for a while)

Page 6: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Roulette Write a complete program that simulates betting on a roulette

wheel You start with $1000 in your bank Each turn, you bet a dollar amount and either red (r) or black (b) Include a method with the following prototype:

public static boolean spin(char guess) This method randomly picks a number between 1 and 38 (where

37 is 0 and 38 is 00) and determines whether or not your guess (r or b) was correct

Remember, there are 38 numbers on an American roulette wheel: 1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36 are

red 2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35

are black 0 and 00 are green (neither red nor black)

Page 7: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Types in Java

Page 8: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Java types There are two classifications of types in Java Primitive types:

int double boolean char

Object types: String arrays An infinite number of others…

Page 9: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Characteristics of primitive types

Primitive types: Have a fixed amount of storage Think of them as a box designed to

hold a particular kind of data Have basic operations used to

manipulate them int, double (+, -, *, /, %) boolean (||, &&, ^, !)

Page 10: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Characteristics of object typesObject types Hold arbitrarily complex kinds of data of any

kind Do not have a prespecified amount of

storage Think of them as arrows pointing to some

concrete thing that holds primitive data Use methods for interaction instead of

operators For example, String objects use length(), charAt(), etc.

Page 11: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

References

Page 12: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Reference types are different Variables that hold object types are

called references Unfortunately, I have lied to you:

References do not work the same as primitive variables

Up to this point, we have tried to ignore this difference

A primitive variable holds a value A reference variable merely points to

the location of the object

Page 13: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

How does this affect you? Picture a ham… Imagine that this ham is actually a

Java object You may want a reference of type Ham to point at this ham

Let’s call it ham1ham1

Page 14: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

How many hams?

Now, what if we have another Ham reference called ham2

What happens if we set ham2 to have the same value as ham1 using the following code?

ham1Ham ham2 = ham1;

ham2

Page 15: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

There is only one ham!

When you assign an object reference to another reference, you only change the thing it points to

This is different from primitive types When you do an assignment with

primitive types, you actually get a copy

int x = 37;int y = x;

y37

x37

Page 16: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Reference vs. primitive variables Since reference variables are only

pointers to real objects, an object can have more than one name

These names are called aliases If the object is changed, it doesn’t

matter which reference was used to change it

Page 17: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Ham solo

Thus, if we tell ham2 to take a bite away, it will affect the ham pointed at by ham1

Remember, they are the same ham

ham1ham2.bite();

ham2

Page 18: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Remember that primitives make copies We have ints x and y, both with

value 37 If we change x, it only affects x If we change y, it only affects yint x = 37;int y = x;x++;y--;

y37

x3738 36

Page 19: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Creating New Objects

Page 20: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

A reference is just an arrow If you declare a lot of references, you

have not created any objects, just lots of arrowsEggplant aubergine;

DumpTruck truck1;Idea thought;

aubergine truck1 thought

Page 21: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Where do those arrows point? When you first declare a reference

variable, those arrows point to nullnull is a Java keyword that just

means nothingness If you try to do something with null,

thinking it is a real object, you can break your program

Page 22: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Constructors

To make those arrows point at a new object, you must call a constructor

A constructor is a kind of method that creates an object

Some constructors allow you to specify certain attributes of the object you are creating

The default constructor does not let you specify anything

Page 23: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Invoking the constructor

To call a constructor, you use the new keyword with the name of the class followed by parentheses:

Perhaps there is a Ham constructor that lets you take a double that is the number of pounds that the ham weighs:

Ham ham1 = new Ham(); //default constructor

Ham ham2 = new Ham( 4.2 ); //weight constructor

Page 24: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Strings are funny

The objects you are most familiar with by now are Strings

They break a few rules: It is possible to create them without

explicitly using a constructor They can be combined using the +

operator You can still create a String object

using a constructor:String s = new String("Help me!");

Page 25: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Upcoming

Page 26: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Next time…

More on classes and objects Start review if there's time

Page 27: Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9

Reminders

Exam 2 is next Monday Review is on Wednesday and Friday Start working on Project 4