Tuesday June 13, 2000


Quiz 6


1. What is the symbol used in flowcharts to represent the start and end of an algorithm.

2. What is the symbol used to represent an if statement without an else?

3. True/False. A class is a concrete thing.

4. True/False. It is not necessary to place { and } around the body of a class definition.
































The Java Language.

Today we will begin to look at more specifics of the Java language.

A literal in Java is a stated value. For example 3 or 4.

Data Types

Java supports a set of data types that are called primitive data types. This is the because they are defined by the language. Later we will look at data types that we define.

  • Integers. Integers, or whole numbers, are stored in 4 bytes in memory. This means that valid integers are in the range from

    -2,147,483,648 to +2,147,483,647.

    In Java to declare an integer type we use the keyword int.

    If we need to store larger integers we can use the keyword long. Long integers are stored in 8 bytes so the valid long integers are in the range from

    -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.

    When we declare a long integer literal we append either an l or an L to the end of it. For example,

    10000l would be a long integer interal

    The use of the + sign is optional.

  • Real Number. Real numbers, or floating-point numbers, are stored in two peices in memory. The first piece is called the mantissa. The second piece is called the exponent. For example in the real number 0.437875 x 103, 0.437875 is the mantissa, and 3 is the exponent. Using four bytes to represent a real number gives a range from

    +/- 3.40282347 x 1038 to +/- 1.40239846 x 10-45.

    Recall that everything stored in a machine is in binary. Most decimal fractions do not convert exactly into binary fractions, so the representation of real numbers is not always accurate.

    The Java keyword to indicate a real number stored in 4 bytes is float.

    Sometimes 4 bytes is not enough. Java can stored much larger integers in 8 bytes. This gives a range from

    +/- 1.79769313486231570 x 10+308 to +/- 4.94065645841246544 x 10-324.

    The Java keyword to indicate a real number stored in 8 bytes is double.

    When declaring literal floats we must append the letter f or F to the end of the number. For example

    3.14f

    would indicate a float literal. By default a real number is considered to be a double, but if we want we can append a d or D to the end of it.

  • Characters. In Java, characters are represented in two bytes of memory using a 16 bit Unicode. You can find this Unicode on page 16 of your book.

    The Java keyword used to indicate a character is char.

    When declaring a character literal we surround it with single quotes. For example,

    'A'
    would be a character literal.

    There are a few other primitive data types but we will not discuss them now. We will talk about them later.

    Identifiers

    When writing programming langauges to manipulate values stored in memory, it is necessary for us to have a way to identifying these values.

    The rules for creating identifiers are
    1. We may use any combination of the alphabetical characters, uppercase and lowercase, the digits 0-9, the underscore character _, and the dollar sign $.

    2. Identifiers may start with any of these characters except a decimal digit.

    3. Java is case-sensitive.

    4. Java keywords cannot be used as identifiers.
    You will find a list of Java keywords on page 19 in your book.

    Some other conventions that we normally follow are that we don't start an identifier with a $. We also usually begin an identifier with a lower-case alphabetical character.

    We also use meaningful variable names. For example instead of using a variable i to run through a list of number we would use a descriptive variable name like counter.

    When writing programs we will make use of data that will vary or change and data that will stay the same. When we identify data that will vary we call that identifier a variable. When we identify data that will stay the same we call that identifier a constant.

    The syntax for declaring a variable is
    data-type identifier;
    data-type identifier-list;
    
    For example.

    int i;
    int j;
    int k;
    
    or
    int i,j,k;
    
    We may initialize a variable when we declare it.

    int i=3;
    
    The syntax for declaring a constant is
    final data-type identifier = literal;
    
    For example,
    final float pi = 3.14f;
    
    The Java keyword final means that the identifier is initialized to a value which will not change during the execution of a program.

    Arithmetic Operations

    There are several arithmetic operations we can use.

  • Unary operators. These are operators that have one operand and are used to represent positive or negative numbers.

  • Binary multiplicative operators.

    There are three of these operators.

    1. * - multiplication.

    2. / - division.

    3. % - remainder or modulus.
  • Binary additive operators.

    There are two of these.

    1. + - addition.

    2. - - subtraction.
    Many times it is necessary to try to have a different representation of our data. That is, if we have a float number, but are only interested in the whole number part, if we look at that number as an integer, then we get the whole number part. To accomplish this we have the casting operator.

    The syntax for a cast is
    (data-type) expression;
    
    For example,
    int i = (int)(3.14f);
    
    There is a precedence that Java follows when evaluating arithmetic expressions.

    1. First anything in parantheses will be evaluated.

    2. Next unary operators are evaluated.

    3. Next casts and multiplicative identifiers are evaluated.

    4. Next additive operators are evaluated.

    5. Next assignment operators are evaluated.
    Notice that the assignment operator is the one we use when we initialize the value of a variable when we declare it.

    Comparisons

    When performing conditional execution it is necessary to have a way to compare the values of identifiers or literals.

  • Equality. In order to test whether or not two variables have the same value or a variable has the same value as a literal we use the double equal sign, ==. For example,
    if (r==0)
       ...
    
    A common mistake made in programming is to use the single equals here. Is this a compile-time error, run-time error, or a logic error?

  • Greater Than or Greater Than or Equal to. To compare the value of two identifiers to see if one is larger than or larger than or equal to the other we use the symbols > and >=.

  • Less Than or Less Than or Equal to. To compare the value of two identifiers to see if one is larger than or equal to the other we use the symbols < and <=.

  • Not Equal to. To determine whether or not two variable are not equal we use the exclamation point followed by an = sign. The exclamation point can be used by itself as a unary negation operator.
    public class DecimaltoBinary {
       int original;
    
       public DecimaltoBinary(int decimal) {
          original = decimal;
       }
    
       public void convert() {
          int quotient;
          int remainder = 0;
    
          int num = (int)(Math.log(original)/Math.log(2));
    
          while (num>=1) {
             quotient = (int)(original/Math.pow(2,num));
             remainder = (int)(original - quotient*Math.pow(2,num));
             System.out.println(quotient);
             original -= quotient*Math.pow(2,num);
             num--;
          }
         
          System.out.println(remainder);
          
       }
    
       public static void main(String[] args) {
          DecimaltoBinary myConverter = new DecimaltoBinary(32);
          myConverter.convert();
       }
    }