Variable, Assignments, Expressions

You will need to download the Console class into the directory where you are working.

Learning Exercises

  1. Variable Names

    Which of the following are valid variable names? Of the valid names, which would be good style?

    1. last_name
    2. <height>
    3. lower-case
    4. 2point
    5. point2
    6. matchResult
    7. FirstName
    8. AGE

  2. Expressions

    Write down the values of the following expressions and their type.

    1. 2 + 3 * 4
    2. 7 / 2
    3. 7.0 / 2
    4. 7 % 2
    5. 6 % 2
    6. 2 * 3 + 4
    7. Console.readInt()
    8. new Robot()
    9. 8

    Note: The percentage symbol, %, is the modulus operator. It produces the integer remainder. E.g. 12 % 5 is two.

  3. Declaration Statements

    Write a statement which will define an integer called sum and assign it a value of 30.

  4. Use of the Declaration Statements

    Place the statement from the above exercise in a program, multiply the variable sum by two and print it out.

  5. Assignment Statement

    What will the following code fragment print when run

       int i = 5;
       System.out.println(i);
       i = 15;
       System.out.println(i);
    

  6. More Assignments

    What will the following code fragment print when run

       int sum = 5;
       sum = sum + 1;
       System.out.println(sum);
       sum = sum + 1;
       System.out.println(sum);
       sum = sum * 10;
       System.out.println(sum);
    

  7. Assignment Errors

    What mistakes are there in the following code fragment? If this fragment was in a proper method definition, would it compile?

       int i;
       System.out.println(i);
       int i = 15
       System.out.println(i);
    

  8. What's the matter here

    Is there anything wrong with the following code?

       double x = 15.0;
       x = x * 10;
       System.out.println(x);
    

    If not, what does it do?

  9. Get the Average

    Write a program which will read in three numbers and print the average value of the numbers. Try out the program. Does it produce the answers you expect?

    A sample session with the user might look like

    $ java Average
    Enter three numbers              
    5
    10
    15
    The average is 10

    Note you should Console.readInt() to read an int, and Console.readDouble() if you want to read a double. You will need three variables to hold the three numbers, and three assignment statements to place the numbers into the variables. You can then perform the calculation and print out the result. You could look at the Temperature program which may provide some inspiration.

    Here are some comments about the process of creating the program.

  10. Height in Light

    Write a program that reads in a person's height in centimeters and converts it to light years. One light year is the distance that light travels in a year. The speed of light is 2.99793e8 meters per second.

  11. Strange Numbers

    Stick the following code in a method and see what happens.

       System.out.print("1/2 = ");
       System.out.println(1/2);
    
       System.out.print("1/2.0 = ");
       System.out.println(1/2.0);
    
       System.out.print("1.0/3.0 = ");
       System.out.println(1.0/3.0);
    
       System.out.print("0.000000001 = ");
       System.out.println(0.000000001);
    
       double x = 0.1;
       System.out.print("x + 0.1 = ");
       System.out.println(x + 0.1);
    
       System.out.print("x + 0.2 = ");
       System.out.println(x + 0.2);
    
       System.out.print("x + 0.7 = ");
       System.out.println(x + 0.7);
    

    Explain the results.

    Well, how can you explain that! The computer wrong! Impossible! In fact, it's not impossible, it's down to the fact that the computer uses binary numbers and decimal numbers are not always accurately represented by binary decimals. In particular, 0.1 is a repeating binary number. Here is an explanation.

  12. Optional Problem

    Write a program that reads in an integer and breaks it into a sequence of individual digits and then prints each digit on a line of its own. For example, the input 16384 will produce the output
    $ java Splitter
    Enter a five digit number: 16384
    The  number split is
    1
    6
    3
    8
    4
    You may assume that the integer has 5 digits. Hint: You can use integer arithmetic and divide the number appropriately (e.g. what will you get if you divide the number by 1000?). Integer division can be used to remove digits to the right. Appropriate use of subtraction or the modulus operator may be used to remove digits to the left.