The Coin Class

 

So just what exactly are we doing???

 

 

A class is needed so that objects can be created.  An object is a noun – it is something!  We can describe things by talking about what they look like (or represent) and what they can do.

 

Our coins can be of a particular type.  They might be a quarter or a penny.  While we all know how much a quarter is worth, we have to tell the computer, so we also need a field for the value of the coin.

 

As a result, we have two instance fields: itsName and itsValue. 

 

Those are the names of the fields, but all fields must be of a specific type.  The value of a coin, in dollars, is of type “double” which allows for decimal values.  The name of the coin is text, which we call “String” in Java.

 

 

Constructors

 

A constructor is necessary in a class when the class has instance fields.  The purpose of a constructor is simple: it initializes the instance fields, which simply means it gives them default values.

 

A class may have more than one constructor, so long as each one takes different parameters.  For instance, we may want to have a constructor that takes no parameters and, by default, just makes a penny.  We may also want to have one that takes one parameter of type “double” and expects the value of the coin, while another constructor may simply expect a parameter of type “String”, which would be the name of the coin.

 

They would look like this:

 

public Coin()

public Coin(String type)

public Coin(double value)

 

 

Testing if two strings are equal

 

If you want to see if two strings are equal in value (or any objects for that matter), we can not use “==” because this tests to see if they point to the same area in memory.  We must use the “.equals()” method of the String class.  To test if a parameter type is the word “quarter” you would say “type.equals(“quarter”) “.  If they have the same value, this boolean method will return true.


 

 

Printing an Object

 

System.out.println(something)” is a method that we use to print things to the “terminal window.”  We will use it typically to display a text message.  If you put an object as the parameter, it will print the object’s class name and address in memory.  If you want to print specific information about the object (perhaps you want to display the instance field data) you must include the “toString()” method in that object’s class.

 

In other words, say we want to be able to create a coin and print its name and value, we may include the following in an application program:

 

System.out.println(coin1)

 

For this to do what we want, the class that coin1 is an object of must have the toString method, which looks like the following:

 

public String toString()

{

            return (“This is a “ + itsName + “ with a value of $” + itsValue);

}

 

When you make the “System.out.println” call, it will automatically look for this method in the Coin class.  If it finds it, it will use it to print the customized message.  If it doesn’t, then all it will print is the object’s address.

 

 

Storing Objects

 

When you have to do something, you might write it down to remind yourself.  If you have several things to do, however, rather than have many post-its floating around, you would be better off writing a list. 

 

In Java, we can store objects conveniently in what is called an “ArrayList”.  Each object is put into a slot and can easily be removed or moved.  To create an ArrayList, you could write:

 

ArrayList handful = new ArrayList();

 

To add a coin to our ArrayList, we could write:

 

handful.add(new Coin());

 

By default, each time you add an object, it is appended to the end of the list.

In ArrayLists, the first object is actually in slot 0, with the second object in slot 1, third in slot 2, and so on.

 

To access the first object, you could write:

 

handful.get(0);

 

To access the seventh object, you could write:

 

handful.get(6);

 

ArrayLists are not naturally a part of the Java language.  As a result, we must “import” this class by using the following line of code at the top of any class that uses ArrayList:

 

import java.util.ArrayList;

 

 

Making things Random

 

Say you want to flip your coin.  You can either get a heads or a tails.  While we commonly expect a “50-50” chance of getting either one, that does not mean that you will get a heads every other time.  It means that, on the average, if we were to flip the coin “many many” times, it would come out to about half heads and half tails.  To simulate random occurrences in Java, we use the Random class:

 

import java.util.Random;  // Place this at the top of your classes that use it

 

Now that we have the Random class, we need to create a Random object that can generate random numbers for us:

 

Random rand = new Random();

 

There are two methods of the Random class we may find helpful:

 

nextInt(int n); // returns an integer from 0 up to (n-1)

nextDouble(); // returns a decimal value: 0 <= x < 1

 

An Example

 

Let’s use “rand.nextInt(2);”  This will either return the number 0 or the number 1.

 

We could say:

 

if(rand.nextInt(2) == 1) // let’s call “1” a value of heads

            numHeads++;

else

            numTails++;

 

 

Use the API!