CS 122 Winter 2001    instr. Jeff Horn                            YOUR NAME ____________________

Quiz 1 (take-home)  March 6, 2001, due March 14, 2001
 

     QUESTION 1  Given the following class, answer the questions:
 

         class Ewok
                  {
                      public static HowManyEwoksSoFar = 0;
                      private int health;
                      private String name;
 
                      public Ewok()
                                 { health = 100; HowManyEwoksSoFar++;}
                      public void change_Name(String new_name)
                                 {  name = new_name; }
                      public void hit(int how_hard)
                                  {  health -= how_hard;}
                      public void heal(int how_much)
                                  {  health += how_much;}
                      public boolean undead()
                                   { return (health < 0); }
                               }
 
  1. Write Java to declare an instance of the class Ewok, then CREATE an instance by calling the constructor, then give it a name.

  2.  
  3. Now write code to kill your Ewok and make it undead.  Include code that will PROVE it undead by asking it and printing the answer along with the name, to the system console, as in "Ewok  <name> is now an undead walking the forests of Endor."

  4.  
  5. Write code to resurrect your Ewok, with a different name.

  6.  
  7. Prove that your Ewok is a resurrection by writing the Java code for finding out, and printing out, the number of Ewoks ever created.  What should this number be?

  8.  
  9. Write me the Java code for printing the health of your Ewok, or if you can't, then tell me why, and change the code so you CNA do it (e.g., write new method).

  10.  

     
     
     


    QUESTION 2    Given an initially unsorted array:

         58
                     21
                                43
                                           12
                                                       8
                                                                  89
                                                                             91
                                                                                        104
                                                                                                    93
                                                                                                                4
                                                                                                                           17
                                                                                                                                      33
     
     

                         class  SelectSort
                                 {
                                      private int array[100];
                                       int lastElementIndex;
                                      public void initialize()
                                                     {  .... }
                                      private Swap(int first, int second)
                                                    {  int temp;
                                                        temp = array[first];
                                                        array[first] = array[second];
                                                        array[second] = temp;
                                                      }
                                      private  int MaxInRange(int left, int right)
                                                  {   int maxIndex = left;
                                                     for(int i = left+1; i <= right; i++)
                                                            if( array[maxIndex]  < array[i])
                                                                  maxIndex = i;
                                                     return maxIndex;
                                                   }

                                       public Sort()
                                                   {
                                                      for(right = lastElementIndex; right >= 0, right--)
                                                            Swap(right, MaxInRange(0,right) );
                                                     }
                                       }


         1.What would "MaxInRange" return the first time it is called?
     

           2.What would the array look like after three iterations through the main sort loop?
     

           3.If the array were already sorted at the beginning, how many swaps would be made by our selection sort?