CS 120A Fall 2001, Instructor: Jeffrey Horn
EXAM 1 11-8-01
Take-Home Due: Wednesday, Nov. 14, 2001
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 SoldierBasic Java
{
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!}
- How many constructors does the class Soldier have? Name it (them).
- 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)
- 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.
- Must each class have a default constructor? Why or why not?
- Can a class have more than one default constructor?
- Write the comment that should go at the beginning of the method "have_a_birthday()". The comment should describe what the method does.
- Write a line or two of Java that declares a new variable "GI_Jane" to be of type Soldier
- Now write code that CREATES a new soldier, gives her the name "Demi", and assigns it to the variable "GI_Jane"
Arrays
- Write a line of code to declare an array named "thirdCorps" of objects of class Soldier.
- Write a line of code to initialize the ARRAY "thirdCorps" to be of approx. army corps size (you decide!).
- 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: