Upload
brianne-neal
View
237
Download
0
Embed Size (px)
Citation preview
Conversion
Recall that C = (5/9)(F – 32) C is the temperature in degrees Celsius F is the temperature in degrees
Fahrenheit Write a method that takes a double
value in Fahrenheit and returns the answer in Celsius
Right triangle
Write a method with the following header:public static void rightTriangle( double a, double b, double c)
This method uses the Pythagorean equation (a2 + b2 = c2) to find the lengths of the sides of a right triangle
This method will compute the value for a, b, or c if the other two values are known
A user calls this method with the unknown value set to be zero If no values are zero, check to make sure that the answer is
correct If exactly one value is zero, compute it, and print the answer If two or more values are zero, print that it is impossible to
determine the lengths
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)
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…
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 (||, &&, ^, !)
Characteristics of object types
Object 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.
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
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 ham1
ham1
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
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;
y
37
x
37
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
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
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 y
int x = 37;int y = x;x++;y--;
y
37
x
3738 36
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
Where do those arrows point?
When you first declare a reference variable, those arrows point to null
null 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
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
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
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!");