Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [10pts] Consider the following program: Identify each of the following as either primitive, an array-reference or a class-reference:
    args      __________________
    subj1     __________________
    sc        __________________
    nm        __________________
    wt        __________________
    wtInGrams __________________
    r         __________________    __________________
    r.weight  __________________
    import java.util.*;
    public class HWEX1
      public static Rat read()
        Scanner sc = new Scanner(;
        String nm =;
        double wt = sc.nextDouble();
        double wtInGrams = wt*28.3495;
        Rat r = new Rat(); = nm;
        r.weight = wtInGrams;
        return r;
      public static void main(String[] args)
        Rat subj1 = read();
    class Rat
      String name;
      double weight;
  2. [5pts] Continuing from the previous example, how many instances of class Rat are created when this program is run? How many instances of class String are created when the function read() is called, assuming the user inputs name & weight properly. Explain your answers! [Note: "instance" is a technical term. Read the notes!]
  3. [5pts] Consider the following file
    public class HWEX2
      public static int fact(int n)
        return n < 2 ? 1 : n*fact(n-1);
      public static void main(String[] args)
        for(int i = 1; i < 10; i++) 
          System.out.println(i + ": " + fact(i));
    How would you call the function fact to compute 15 factorial from the main function of another .java file (assume it is If you run the program from (i.e. give the command java HW), how does the JVM know where to find the code for the fact function?
  4. [80Pts]

    Create a file entitled and put this class definition in it:

    public class Mid 
      public String alpha;
      public String firstName;
      public String lastName;
      public int company;

    Then write a program entitled with the following attributes:

    An example run:

    ~/$ java HW3
    How many mids? 3
    Alpha? 160006
    First name? George
    Last name? Finklehoffer
    Company? 3
    Alpha? 160012
    First name? John
    Last name? Jingleheimer-Smith
    Company? 4
    Alpha? 160018
    First name? Sterling
    Last name? Hotchkiss
    Company? 3
    What company would you like to print out? 3
    160006 Finklehoffer George 3
    160018 Hotchkiss Sterling 3

    Your input and output should be exactly as it is above.

Turn In This sheet (filled in with answers!), a printout (using codeprint) of your code, and a screenshot of it running on the above input. Note: you can print both source code files at once with codeprint like this: codeprint