To Lecture Notes

IT 313 -- Feb 4, 2020

Review Exercises

  1. Order these items by containment order from largest to smallest:
    Package  Method  Project  Class  Module 
    
    Ans: For most projects, the containment order looks like this:
    Project ⊃ Module ⊃ Package ⊃ Class ⊃ Module
    
    However, it is possible for packages to be divided over several modules.
  2. How do you specify that a class belongs to a package?
    Ans: You put this line at the top of the class file:
    package it313.ssmith;
    
  3. How do you use a class A that belongs to a package?
    Ans: You put this line at the top of the class to avoid typing it313.ssmith everytime you use the class A:
    import it313.ssmith.A;
    
  4. How do you use a class that belongs to a different module?
    Ans: Suppose that Main is the class that uses the class Greeter and Main is in module2. Also suppose that the Greeter class is in module1. Then for the Main class to use the Greeter class, you set up a dependency for module2 on module1. Here is how you set up the dependency in module2 on module1:
    1. ...
  5. What is a wrapper class? How are wrapper classes used with ArrayList collections?
    Look at the IntegerTest class in the InstanceMethodsExample project.
    Ans: a wrapper class object contains a primitive datatype value. Here is the table of primitive types and their corresponding wrapper class types:

    Primitive Type Wrapper Class
    int Integer
    double Double
    char Character
    booleanBoolean

  6. Create a Main class to run this main method:
    public static void main(String[ ] args) {
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);
        System.out.println("Quotient: " + (a / b));
    }
    
    Set up try/catch blocks to handle exceptions that may occur
    To let IntelliJ write the try/catch block, use
    Code >> Surround With... >> try/catch
    
    Ans: Here is the main method with a try/catch block added:
    public class Main {
        public static void main(String[ ] args) {
            try {
                int a = Integer.parseInt(args[0]);
                int b = Integer.parseInt(args[1]);
                System.out.println("Quotient: " + (a / b));
            }
            catch (NumberFormatException e) {
                System.out.println("Non-numeric input.");
                System.exit(1);
            }
            catch (ArithmeticException e) {
                System.out.println("Division by zero.");
                System.exit(2);
            }
        }
    }
    

Project 4

Review of Inheritance

Project 5