CS 120A   Fall 2001,  Instructor:  Jeffrey Horn             EXAM 1  11-8-01
 Take-Home    Due:  Wednesday, Nov. 14, 2001



INSTRUCTIONS:

Partial credit:  I am big on this.  Show your work.  I will give zero credit for a blank answer (I am forced to!) but on the other hand I will give you more than zero if you show ANY work at all!  You can always use more paper, and you can always CROSS OFF what you don't like, but don't throw anything away, or erase anything, that shows some thinking, whether correct or not!


PART I:  

Here is a class named Soldier:
class Soldier
          {
              private  int age;
              private String name;
              private String rank;
              private int serial_number;
              private History personal_history;
              private Weapon  side_arm;
              private Weapon  assault_weapon;

               public Soldier()
                     {
                        age = 18;
                        name = "Joe";
                        rank = "private";
                        serial_number = 123456789;
                        History = new History();
                        side_arm = new Weapon("colt45");
                      }

              public Soldier(String input_name)
                       {name = input_name;}

              public void have_a_birthday()  {age++;}

              public void change_name(String new_name);
                  { name = new_name;}

              public String get_name();
                   {
              . . .               //  Assume more methods!

                   }

Basic Java
  1. How many constructors does the class Soldier have?  Name it (them).
  2. In the above code, list ALL of the different classes that are being used (and don't include standard Java  data types like int or standard Java classes like String)
  3. A default constructor is a constructor that takes no arguments (i.e., no input parameters).  Circle the default constructor for Soldier above, if there is one.
  4. Must each class have a default constructor?  Why or why not?
  5. Can a class have more than one default constructor?
  6. Write the comment that should go at the beginning of the method "have_a_birthday()".    The comment should describe what the method does.
  7. Write a line or two of Java that declares a new variable "GI_Jane" to be of type Soldier
  8. Now write code that CREATES a new soldier, gives her the name "Demi", and assigns it to the variable "GI_Jane"

 

Arrays

  1. Write a line of code to declare an array named "thirdCorps" of objects of class Soldier.
     
     
  2. Write a line of code to initialize the ARRAY "thirdCorps" to be of approx. army corps size (you decide!).
     
     
  3. Now write code to put GI_Jane into the array in the fourth position.  Assume she has already been created and initialized above.

 


PART II:  

    1. Write code that goes through the "thirdCorps" array and increments the age of each soldier, from the first soldier (i.e., thirdCorps[0]) to the last soldier (assume there exist an integer variable, nint um_soldiers, that indicates how many soldiers have been created, initialized, and inserted into the array thirdCorps;  thus thirdCorps[0]..thirdCorps[num_soldiers-1]  has real, valid soldiers in it.

    2.  

       
       
       
       

    3. Write a new method for the Soldier class, called "get_age" that actually returns the soldier's current age.

    4.  

       
       
       
       
       

    5. Write a new method for the Soldier class, called "captured(int hours)", that prints out a soldier's private information, according to how long the soldier has been captured.  If "hours" is 0, then print only the name.  After 1 hour, print name and rank, after 2 hours, name, rank, and serial number, and after three hours, name, rank, serial number and entire personal history.   Use "System.out.println(s);" to print a string s to the console.  And assume that the class History has a public method called "printHistory()" that prints the entire history to the console.