Ch2 – Arithmetic Operators

This tutorial is specifically designed for DrJava’s Interactions pane. You are not doing “true” Java programming, but rather, you are using DrJava’s interactive compilation. If you read chapter one, you will recall the difference between an interpreted and a compiled language. ¬†DrJava allows you to simulate an interpreted language by entering Java statements and observing the results immediately.

  1. Open DrJava and expand the Interactions Pane.
  2. Type the following lines in the Interactions Pane and review the output.chapter_two_image_one
> int hour = 11;
> int minute = 59;
> System.out.print("Number of minutes since midnight: "); System.out.println(hour * 60 + minute);
Number of minutes since midnight: 719
  1. Type the following lines and review its output.
> System.out.print("Fraction of the hour that has passed: "); System.out.println(minute/60);
Fraction of the hour that has passed: 0
  1. Fix the output so that it prints an approximation. Note that in the next section, Floating-Point Numbers, you explore why step two is in error due to its being an integer (rounding error).
> System.out.print("Fraction of the hour that has passed: "); System.out.println(minute*100/60);
Fraction of the hour that has passed: 98

The following video by LearningLad provides another introduction to Java arithmetic operators.