Change Contents of the Bubble
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Pre-Quiz3-Spring 2008

Part I: Grading TA


Part II: File I/O
Suppose you were given a String:

File input/output, File I/O, is sometimes hard to understand.

and were asked to break it into tokens based on different delimiters. Write down the tokens if we used the delimiter:

1. space, “ ”







2. comma, “,”







3. slash, “/”







4. lowercase letter o, “o”







Part III: Stacks and Queues
If given the following methods:

public void addToHead(Object obj) //adds object to the head of the list
   public void addToTail(Object obj) /adds object to the tail of the list
   public Object removeFromHead() //removes and returns object from the head
   public Object removeFromTail() //removes and returns object from the tail
   public Object peek() //returns but does not remove the head of the list


how would you write the following methods for stacks and queues? Remember that your choice for one method of the ADT affects your choice for the other. Also the removal methods above do not check if the ADT is not empty before attempting to remove; it is your job to account for this.

   public void enqueue(Object obj){




   } 

   public Object dequeue(){




   }

   public void push(Object obj){




   }

   public Object pop(){




   }


Circle the Stack methods.

enqueue

dequeue

push

pop

Circle the Queue methods.

enqueue

dequeue

push

pop


Part IV: Abstract Classes

public abstract class SuperHero{
  protected String name, nemesis, weapon;
  
  public abstract void fightCrime();
  
  public void speak(){
    System.out.println("My name is " + name + 
                       ". My nemesis is " + nemesis);
  }

  public String toString(){
    return "A superhero";
  }
}



public class NinjaTurtle extends SuperHero{
  
  public NinjaTurtle(String name, String weapon){
    this.name = name;
    this.weapon = weapon;
    nemesis = "Shredder";
  }
  
  public void fightCrime(){
    System.out.println("I kick butt with my " 
                         + "brothers using my weapon " 
                         + weapon);
  }
  
  public void speak(){
    super.speak();
    System.out.println("Cowabunga dudes!");
  }
  
  public String toString(){
    return "A ninja turtle";
  }
}


Consider the code provided above and answer the following questions:

1. Which of the following lines of code are valid (no errors or exceptions will occur)? Select all that apply.

a. SuperHero superhero1 = new NinjaTurtle("Leonardo", "ninjakens");
b. NinjaTurtle mike = new NinjaTurtle("Michelangelo", "nunchakus");
c. SuperHero superhero2 = new SuperHero();
d. NinjaTurtle donatello = new SuperHero();


2. Given that NinjaTurtle raphael = new NinjaTurtle("Raphael", "sais") what would be the output to the interactions pane as a result of the following lines:

a. raphael.fightCrime();



b. raphael.speak();



c. raphael.toString()




3. Given that SuperHero leonardo = new NinjaTurtle("Leonardo", "ninjakens"); what would be the output to the interactions pane as a result of the following lines:

a. leonardo.fightCrime();



b. leonardo.speak();



c. leonardo.toString()




4. If the SuperHero class was not an abstract class, would this line
   NinjaTurtle donatello = new SuperHero();

be valid? Why or why not?





Part V: Simulations
1. What is a simulation?




2. What the difference between a discrete event simulation and a continuous simulation?





Part VI: Using random number generators
Write a method that will return String representing a card from a deck of cards. Assume that there will the normal 52 cards and also two Jokers in the deck. Return the String in the format # of suit for cards 2-10 and then Jack of suit, Queen of suit, REMOVED of suit and Ace of suit. For a joker, return Joker. Some example Strings are "2 of Spades", "Jack of Hearts", "8 of Clubs", and "REMOVED of Diamonds".




















Joel is trying to figure out what shirt to wear today. Like any other Tech guy, Joel decides he’s just going to reach into his clean clothes pile and wear whatever random shirt he grabs on to first. The clean clothes pile contains 2 blue shirts, 3 gray shirts and 5 white shirts. However the pile also contains 3 pairs of pants. The pants he went to sleep in still seem okay so Joel does not need another pair. If Joel retrieves a pair of pants on his attempt, he will put the pants aside and try again. Write a method that will return String representing what random colored shirt Joel will wear today.


















Ask questions at Spring08-Pre-Quiz3-Questions

Links to this Page