Lecture 14 - March 1, 2007

 

Gave out decks of cards and went over programming assignment #2 which is due the Thursday after Spring break

Discussed overall desired structure of program

Discussed symbols on the UML diagram

Dealt out hands and  evaluated individual hands

Talked about how to sort - and the desirablility of creating a deck and then randomly interchanging two cards instead of trying to generate deck randomly

Discussed symbols on the UML diagram

            Card  has-a rank

            Card has-a suit

            Deck has-a Card

            Hand has-a Card

            Dealer uses Card and BridgeHand

For example: driver calls Deal method to get a Card and gives the Card to BridgeHand

 

Went over all of the answers to Exam 1

 

Talked briefly about  problems observed by Professor Harris regarding program 1

Here are her comments about desired structure specificatly  of program 1 but the principles

Are applicable to all programs.

 

A constructor to initialize data (see preprocessor)

A getFileNames method which will do the prompting for file names and instantiate Scanner and PrintWriter objects.

If a preprocessor is used to count the number of input tokens, it would be a separate method where the counts would be used to instantiate the arrays.

A readFromFile method which would do the input file processing.  This is where the Scanner would “useDelimiters” and where the echo of the data would be as it is stored into the arrays.

A writeFile method which would output the array contents to the two output files.  Two write file methods could be used…one to write the Strings and one to write the integers.  If it did not take in a parameter with the “label” for the output, then this might have to be done separately.

A sortArray method which would sort each of the arrays.  Or two sortArray methods could be used, one for the String array and the other for the int array.

A getMessage method would return a String which indicates the name of the input files and the names of the output files.

 

Each of these could be called independently.  You might have the constructor call getFileNames since to be useful, this application needs files.  Otherwise, I would do no cascading of method calls…each method can be run independently and should be called in turn by the driver.

 

 

Here  is an enumerated type example similar to the one I ran in class showing additional ways of printing out the enumerated type values

public enum   Creature { CAT, DOG, SNAKE}

 

public  class Testy

{

  public static void main (String [] args)

{

    System.out.println (Creature.CAT);

    System.out.println (Creature.CAT.name);

}

}

 

 

Here are the "for each" examples I mentioned in class put didn't bring up on the screen

// A variety of answers submitted for "for each" question on Exam
// All of them compile and run.
// Not all of them did what the question asked for

public class TestForEach2
{
  
public static void main (String [] args)
   {
    Fruit myFruit; 
// declaration
        
    myFruit = Fruit.APPLE; 
// initialization - irrelevant in this program
    String fruitColor;  //  not required but used by example 2  below


// example 1:   prints fruits not their colors
    for (Fruit all:  Fruit.values() )
      System.out.println (all);
    System.out.println ();

// example 2:       

        for (Fruit f:  Fruit.values() )
        {
           fruitColor = f.getColor();
           System.out.println (fruitColor);
        }
        System.out.println ();

// example 3:  prints Fruits not colors 
    for (Fruit f:  Fruit.values() )
      System.out.println (f);
    System.out.println ();

// example 4:
   for (Fruit f:  Fruit.values() )
   {
          System.out.print (f +
" ");
              System.out.print (f.getColor() +
" ");
              System.out.print (f.getPrice() +
" ");
              System.out.println();
   }
   System.out.println (); 


// example 5:
     for (Fruit f:  Fruit.values() )
        System.out.println (f.getColor());
     System.out.println ();

// example 6:       
         for (Fruit f:  Fruit.values() )
           System.out.println (f +
" is the color " + f.getColor());
         System.out.println ();         
   }
// end Main
} // end class