Copy-and-Paste Program

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

1/2 + 1/2  = 0 

It looks as though this is a mistake, but no: each 1/2 calls for integer division, resulting in an integer 0. The two zeros are added to get the final answer, zero.

Copy-and-Paste Program

If you really want to add one half to one half you should write 1.0/2.0 + 1.0/2.0 because now the decimal points make each number a double. Here is some Java code that illustrates these points:

class IntegerDivision
{
  public static void main ( String[] args )
  {
    System.out.println("The result is: " + (1/2 + 1/2) );
  }
}

Copy this program to a file IntegerDivision.java, compile, and run it. See a previous chapter for details on doing this. Make some changes to the program using your text editor then run it to see the effect.

Notice the parentheses around   (1/2 + 1/2). These are necessary so that the arithmetic is done first, then the result is converted to characters and appended to the string.

QUESTION 8:

What is the value of the expression 99/100 ?