To Lecture Notes

IT 313 -- Jan 8, 2020

Review Exercises

  1. How many bytes are needed to store a constant or variable for each of these Java primitive datatypes?
    int  double  char  boolean
    
    Ans:
    Datatype Bytes Wrapper Class
    int 4 Integer
    double 8 Double
    char 2 (unicode) Character
    boolean 1 Boolean

  2. How do these Java operators work?
    +  /  ++  --  [ ]  ( )  new  ?
    
    Ans:
    + can be the addition operator or the concatenation operator. Addition is used if both operands are numeric types; concatenation is used if at least one of the operands is String. This is an example of an overloaded operator that has different meanings, depending on the datatypes of the operands.

    / means integer division if both of its operands are int and floating point division if at least one of its operands is double. For example, 5 / 3 == 1, 5.0 / 3.0 == 1.6666667.

    n++ means increment n: add one to the value of n.

    n-- means decrement n: subtract one from the value of n.

    [ ] is the index operator: a[i] returns the item in the array a with index i.

    ( ) are parentheses, which have at least four meanings. Here are four meanings of ( ):
    1. Change order of operator precendence. y = (1 + x) * 2 means add one to x first, then multiply by 2. Normally, multiplication has a higher precedence than addition.
    2. Method invocation, for example System.out.println(x);
    3. Method definition, which we will see on Wednesday.
    4. Datatype cast, which changes one datatype to another, for example int a = (int) 'A';
  3. What are the Java names for these standard I/0 streams?
         Keyboard   Terminal Window
    What are the IntelliJ color codes for them? Ans:
    IO Stream Meaning Java Name IntelliJ Color Code
    Standard In Keyboard System.in Green
    Standard Out Screen Output System.out Black
    Standard Err Screen Prompt
    or Error Message
    System.err Red
  4. Look at the greeter1, greeter2, and greeter3 modules in the Greeter Example project.
  5. Why does Java need methods other than the startup (main) method?
    Ans: A large software project (and medium ones too) must be broken up into smaller methods for these reasons:
    1. Reusability:   Source code within a method can be called multiple times within a project or it can be called by programmers that did not write the code (e.g., System.out.println).
    2. Modularity:   If software is broken into separate modules such as methods, each method can be debugged and tested separately.
    3. Encapsulation:   After a method is debugged and tested, another programmer can use it as a black box, without understanding its internals. The person using the black box method need only know what parameters must be passed in and what sort of return value to expect.
  6. Create a Java program that reads from the keyboard a distance in inches, converts it to centimeters, and outputs the result. Ans: See the
    InchesToCm Example Project.
  7. How do you submit the IntelliJ project for Project 1? Ans: Consult the Project Submission Guidelines. Maximum penalty of 10% for not compling with these guidelines.
    The name for Project 1 should look be Proj1Smith, where Smith is your last name. Use IntelliJ to create a zip file of the project. Submit this zip file.
  8. Look at the gradecalculator and wordtonum modules in the TerminalIO Example.
  9. How do you check whether the String objects s and t contain the same characters?
    Ans: Use the String method call s.equals(t). This returns true or false, depending on whether the strings s and t contain the same characters.

Project 1

ComputeAverage Example