To Lecture Notes

IT 313 -- Oct 14, 2019

Review Exercises

  1. What is wrong with these class names?
    main  getBarCode  POSTNET
    
    Ans: Class names should be spelled with upper camel casing:
    Main  BarCode  Postnet or PostNet
    
  2. What do these symbols mean?
    *  /  .  ( )  [ ]  { }  &  ?
    
    Ans: * means multiplication, block comment: /* ... */ or JavaDoc comment: /** ... */
    / means division, either integer division: 5 / 2 == 2 or floating point division: 5.0 / 2.0 == 2.5.
    . means member selection, for example, if p is a Person object, p.age if age is a public instance variable, or p.getAge( ), if getAge is the getter for age.
    ( ) is used in four cases: (a) grouping in an expression to change the order of operations, (b) numeric cast operator, (c) method definition, (d) method definition.
    [ ] means array access, for example: a[3] or array definition, for example:
    int[ ] a = {2, 3, 5, 7, 11};
    
    { } is used to delimit the body of a class, method, if statement, while loop, for loop, or switch statement. Curly braces are also used to specify the items in an array, see the previous item [ ].
    ? is the Elvis Operator (because it looks like Elvis Presley's wavy hair when the ? is turned sideways), more technically ? known as the tertiery condition operator. It is used like this
    condition ? value1 : value2
    
    If condition is true, value1 is returned, if condition is false, value2 is returned. For example:
    String s = (value >= 1000) ? "Large" : "Small";
    
  3. What is the difference between s == t and s.equals(t) for String objects?
    Ans: s.equals(t) returns true if s and t contain the same characters. s == t returns true if the reference variables s and t refer to the same object in memory.
  4. What does a cast do? Give an example.
    Ans: A cast converts one primitive numeric type to another. char variables are considered to be numeric, with value equal to the
    2 byte Unicode code for the character. For example:
    char c = 'A';
    int n = (char) c;
    
    n gets assigned the ASCII code of 'A', which is 65.
    double x = 1.9365;
    int n = (int) x;
    
    n is assigned the int value 1.
  5. What is an overloaded method? Give an example.
    Ans: Overloaded methods are methods in the same class that have the name name but different signatures. (The signature of a method is the list of its parameter types). A basic example is System.out.println. Here are some of its signatures:
    ( )  (int)  (char)  (String)  (boolean)  (Object)
    
  6. How does method overloading differ from method overriding?
    Ans: To override a method means to replace the base class method with a method in the derived class.
    Overloading means same method names, same class, different signatures.
    Overriding means same method names, different classes, same signatures.
  7. What do these Java keywords mean?
    this  extends  super  try  catch
    
    Ans: this is a reference variable that refers to the current class. If an instance variable and a parameter have the same name, use this to denote the instance variable:
    public Pet(String name, String animalType) {
        this.name = name;
        this.animalType = animalType;
    }
    
    extends is used when defining a class to indicate that a class B inherits from a class A (A is the base class; B is the derived class):
    public class B extends A {
        // body of class B goes here.
    }
    
    super is used to invoke a constructor from the base class, for example:
    super(name, gender, age);
    
    or invoke some other method from the base class:
    super.toString( );
    
    A try block contains code that might cause an exception; a catch block contains code that executes if an exception occurs. Use a catch block to prevent program crashes.
  8. What is an ArrayList collection? How is it different than a traditional array? Ans:
    1. The size of an array must be specified in advance; an array listcan start small and be dynamically increased as needed.
    2. In an array, an item must be added at a specific index; when an item is added arraylist to the middle, the items wi larger index are pushed down.
    3. One can't remove items from an array, only replace them; when an item is removed from an arraylist, the hole closes up automatically
  9. If col is an ArrayList collection defined as
     ArrayList<String> col = new ArrayList< >(20);
    col.add("dog");    col.add("cat");       col.add("mouse");
    col.add("rabbit"); col.add("goldfish");
    
    write statements to:
    1. set the string with index 2 to "gerbil"
      Ans: col.set(2, "gerbil");
    2. print the string with index 4.
      System.out.println(col.get(4));
  10. The input file pairs.txt contains integer value pairs, one pair per line:
    3,4
    4,5
    6,7
    7,8
    
    1. Define a Pair class, whose instance variables are x and y. Also define a constructor and instance methods getX, getY, setX, setY, toString.
    2. Write a Main method that reads data from the input file and uses it to populate an ArrayList collection containing Pair objects.
    Ans: Here are the Pair and Main classes.
  11. Predict the output of these Java classes.

Review for Midterm