145 Test 2

  1. (20%) Given the following class:
    public class Patient{
      private String name;
      private int age;
      private double weight;
    
      /** Gender of patient. 'M' for male and 'F' for female.*/
      private char gender;
      private double bloodPressure;
    
      /** These are true if the patient is allergic to the drug in question. */
      private boolean allergicToAbocin;
      private boolean allergicToBorocin;
      private boolean allergicToCitocin;
    
      public boolean canTakeAbocin() {};
      public boolean canTakeBorocin() {};
      public boolean cantakeCitocin() {};
      
    }
    
    implement the three functions canTakeAbocin, canTakeBorocin, canTakeCitocin making sure to obey all the following rules:
    1. A patient can only take a drug if he or she is not allergic to it.
    2. Only children (under 13) can take Abocin.
    3. Overweight children should not take Abocin. A child is overweight if his (or her) weight is greater than his age times 10.
    4. Only men or women under 60 can take Borocin.
    5. Men over 65 with bloodPressure greater than 10 should not take Borocin unless their weight is under 200 pounds.
    6. If a person can take Borocin then that person should not take Citocin.
    7. Otherwise (if none of these rules apply) then the patient can take any of the drugs.
    Solution:
    public boolean canTakeAbocin()
    {
      if (allergicToAbocin == false)
      {
        if (this.age < 13)
        {
          if (this.weight < (this.age * 10))
          {
            return true;
          }
        } 
      } 
      return false;
    } 
    public boolean canTakeBorocin()
    {
      if (allergicToBorocin == true)
        return false; 
      if(this.gender == ‘F’ && this.age >= 60)
        return false; 
      if(this.gender == ‘M’ && this.age > 65)
      {
        if(this.bloodpressure > 10)
        {
          if (this.weight >= 200)
            return false;
        }
      } 
      return true;
    } 
    
    public boolean canTakeCitocin()
    {
      if (allergicToCitocin == true || canTakeBorocin == true)
        return false; 
      return true;
    } 
    
    
    
  2. (40%) Answer the following:
    1. Can a class implement multiple interfaces? Yes
    2. How do you concatenate the chars 'a' 'l' 'e' 'x' to form the String "alex".
      String s = "";
      s += "a";
      s += "l";
      s += "e";
      s += "x";
    3. What is the final keyword used for? The final keyword is used to make a variable only take on one value. The first value is the last value that is ever assigned to the variable.
    4. What is a breakpoint and why might you need to set one? A breakpoint is a place that tells the debugger to stop execution of a program. This is very useful in debugging a programming by letting the programmer examine values of variables at that point and executing a program one line at a time.
    5. What is the difference between overloading and overrriding a method?
      • Overloading- There are several functions with the same name but have different parameters. The compiler chooses the correct one by which parameters are passed to the function.
      • Overriding- Overriding happens when a subclass and a superclass have the same function name with the same parameters. The one in the subclass overrides the one in the superclass and is executed when called.
    6. How do you prevent anyone from creating more than 4 instances of your class Stooge. Show your code.
      public class stooge{
        public static int num = 0; 
        public void stooge( )
        {
          if (num < 4)
          {
            super( );
            num++;
          }
          else
            //We actually want to throw an Exception here, but we have not covered
            // throw yet.
            System.out.println("There are too many stooge’s");
        }
      
    7. What does the assignment x = y do when both variables are of a primitive type. x will get the value of y.
    8. What does the assignment x = y do when both variables are of a non-primitive type. x will reference the same memory spot of y.
    9. What is the scope of an instance variable? The scope of an instance variable is the class itself.
    10. What is the scope of an argument variable? The scope of an argument variable is the method it is in.
  3. (40%) Write a function which takes as input a string that consists only of the digits from 0 to 9 and outputs a distribution (like a grade distribution) showing how many times each digit appears in the string. Specifically, the following main
      public static void main(String args[]){
        String s = "01223334444566";
        Distribution.doDistribution(s);
      }
    }
    
    should produce the following output:
    0:*
    1:*
    2:**
    3:***
    4:****
    5:*
    6:**
    7:
    8:
    9:
    
    Tip: The following for loop
     for (char c = '0'; c <= '9' ; c++)
    iterates over the characters '0', '1', '2', ... '9'. Also, check page 350 of the textbook for the needed String functions. Solution:
    public class Distribution{
      public static void doDistribution(String s){
        for (char c = '0'; c <= '9' ; c++){
          int index = 0;
          int count = 0;
          while ((index = s.indexOf(c,index)) != -1){
            count++;
            index++;
          }
          System.out.print(c + ":");
          for (;count > 0; count--){
            System.out.print("*");
          }
          System.out.println();
        }
      }
      
      public static void main(String args[]){
        String s = "01223334444566";
        Distribution.doDistribution(s);
      }
    }
    

Jose M Vidal
Last modified: Fri Nov 3 16:14:34 EST 2006