18 Jan 2012 [ 201 week2 ]

Up to now, we've been able to work mostly with primitive data types. Reference types are more complex because they (usually) are made up of several parts, possibly a large number that is not predetermined. Java provides a new keyword that is used to create data with reference types.

The only reference type that doesn't always require the new keyword is String, which Java handles specially. You can still create a String by using new String(), but it's discouraged.

We're going to talk about several useful classes, and a general group of datatypes which I'll call aggregates. These types are those made up of some collection of elements, including:

Exercises

We're going to create a simple calculator, and gradually add features.

Let's start with this simple version that just handles adding a single pair of numbers.

import java.util.Scanner;

public class Calc {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double x = input.nextDouble();
        double y = input.nextDouble();
        double sum = x + y;
        System.out.println(sum);
    }
}
  1. Improve this by printing a prompt, so that the input and output would look something like this:

    > 5 6
    11
    
  2. Add the ability to repeatedly enter pairs to add, until the user indicates he or she is finished. You can accomplish this by wrapping everything in a new kind of loop, a while loop, that just repeats until a condition is not met. This will work:

    while (input.hasNext()) {
        // ...
    }
    

    The hasNext method checks to see if the input has been closed, or is unavailable in any way. On Linux, you can type <ctrl-D> to indicate end-of-file (EOF), signaling that there's no more input coming. On Windows, typing <ctrl-Z> <enter> works (usually).

  3. Add the ability to type quit to exit instead of (or in addition to) typing a special key. So the input and output would look something like:

    > 5 10
    15.0
    > 100 200
    300.0
    > quit
    

    You'll have to change the way you read in the first input, and then check to see if quit was typed. Otherwise, you'll have to convert to a number, using Double.parseDouble. The Scanner.next method will read the next input token without trying to convert it to a number.

    A useful new keyword is break, which will exit the loop immediately when invoked.

  4. Add the ability to use other operations such as subtraction, multiplication, division, and exponentiation, so that your calculator session could look like:

    > 1 + 2
    3.0
    > 4 * 5
    20.0
    > quit
    
  5. Change the input so that it can tell if you type an integer or a floating point number, and use the appropriate type of arithmetic. So you could do:

    > 1.0 / 2
    0.5
    > 1 / 2
    0
    > quit
    
  6. Improve your calculator so that it can operate on integers of arbitrary size. Use the BigInteger class for this. You'll have to import it:

    import java.math.BigInteger;
    

    With this change, you should be able to perform operations like:

    > 12345 ** 6
    3539537889086624823140625
    > quit
    
  7. Add the ability to define variables, so you can type expressions like:

    > x = 5
    > x * 10
    50
    

    One way to accomplish this is to use a HashMap to save the variables you've defined. This works like:

    HashMap<String, Double> vars = new HashMap<>();
    vars.put("x", 5.0);
    

    The above allows you to save variables of type double. Think about how to handle that in the general case, when you might want to save variables of both integer and floating point type.

Readings

Chapter 3 ("Using Classes and Objects").

Some useful online reading for arrays includes this chapter outline, and this tutorial.