Android 101, Class 2

It’s time to make our first Android application!

Course Material

Office Hours

If you have any questions, please feel free to reach out to the Facebook group. This forum gives you access to your fellow classmates, the TAs, and myself. In addition, I will hold Skype office hours on Thursday, August 16 from 7-8 PM to answer any questions that will help you get started. I will hold office hours again on Saturday from 4-6 PM to answer questions you may have when deeper in the coding assignment.

Homework Exercises

If you haven’t finished last night’s homework, please start there to get more familiar with the foundations of Java.

Now that we’ve laid the ground work for the calculator app, let’s make it work! Here’s what the calculator should do:

  1. Consecutive numbers should be appended to the current number (Example: 1 2 3 4 5 should display 12345).
  2. Create an enum that represents operation types that a calculator can perform (see below for more details).
  3. When an operation is performed, store the type.
  4. When an operation is performed, store the number displayed.
    • NOTE: We’re displaying as a String, but should store a number type.
    • int number = Integer.parseInt(“12345”)   // Gives the integer 12345
  5. When equal is pressed, calculate the result using the operation type and stored number.
  6. Division and modulus should perform integer calculations (Example: 7 / 3 = 2 and 7 % 3 = 1).
  7. When equal is pressed, update the display with the result.
  8. Pressing equal after a calculation is complete should show the last result (Example: 2 + 3 = 5 ==== 5, pressing equal repeatedly after the initial calculation will show as result of 5).
  9. The clear button should reset the calculator state.
  10. Users should be able to change their minds about what calculation to perform (Example: 2 * + / * 4 = 8, should calculate 2 * 4 because the last operation entered was multiplication).
  11. Calculations can “chain” or use the last calculated number in the new equation (Example: 2 + 3 = 5 + 2 = 7).

Extra Credit (End of Class Goals)

Users don’t always do what you expect them too! Handle the following unexpected user actions or exceptional states:

  • Detect division and modulus by 0. When this occurs, place the application in an error mode and display the string “NaN”. Only pressing the clear button or a number key clears this state.
  • User presses equals after starting the application.
  • User presses an operation before a number is entered.
  • User enters a number, presses an operation, then presses enter.
  • Prevent and detect integer overflow. To prevent, limit the number of digits a user can enter. When a calculation exceeds the maximum integer amount, place the calculator in an error mode and display the string “Error”. Only pressing the clear button or a number key clears this state.

Switches & Enums

Using switches and enums together can simplify the coding assignment. An enum is a handy data type that allows you to basically specify a name for an integer. Consider this example:

public enum Season { WINTER, SPRING, SUMMER, FALL }

You’d define this enum in Season.java or in your current class. Then, you create a field in your activity that stores a particular season.

Season season = Season.SUMMER;

This technique is especially useful when you want to do different things for different seasons. You could do this with normal if/else/else if blocks, but a switch statement is much cleaner:

switch (season)
{
    case SPRING:
        System.out.println("I love spring!");
        break;

    case SUMMER:
        System.out.println("I love summer!");
        break;

    case FALL:
        System.out.println("I love fall!");
        break;

    case WINTER:
        System.out.println("I love winter!");
        break;

    default:
        System.out.println("Not sure what season we're in!");
}