Midterm Exam

CSC 220

Spring 2005


Problems :



 1.  Class Card (attached below) describes a common playing card. It has two String data fields, face and suit. As its member methods, it has a constructor, a toString(), getFace(), getSuit() implementing the standard behavior of String converter and accessor methods respectively. The remaining equals(Card) member method compares two Cards: its accessing object and its parameter Card. They are equal if and only if their both components (face and suit) are identical.



// File : Card.java

public class Card {
    private String face;
    private String suit;

    public Card( String f, String s )
    {
        face = f;
        suit = s;
    }

    public String toString() { return face + " of " + suit; }
   
    public String getFace() { return face; }
    public String getSuit() { return suit; }

  public boolean equals( Card c ) {
    return ((face.equals(c.face)) && (suit.equals(c.suit)));
  }
}



Class PokerHand (attached below) describes a common five cards hand in a poker game. It has thirteen card face final and static data fields corresponding to standard card faces as well as four suit face final and static data fields corresponding to standard card suits. In addition, it has five non-static Card data fields (card1, card2, card3, card4, card5) representing the Cards of the actual hand. As its member methods, it has a :

(i)                  dealARandomCard() method that returns a Card object representing a randomly selected card from a standard 52 card deck,

(ii)                a constructor that creates a PokerHand object consisting of five different cards from the standard 52 card deck, 

(iii)               a toString() member method returning a String representation for a PokerHand object,

(iv)              a contains(Card) member method returning true if its accessing (this) PokerHand object contains the Card object passed to it as its parameter, and

(v)                an equals(PokerHand) member method returning true if its accessing (this) PokerHand object contains the same Cards as the PokerHand object passed to it as its parameter.

 

// File : PokerHand.java

import javax.swing.*;

public class PokerHand {

  // Card Faces
    private final static String ace = "Ace";
    private final static String deuce = "Deuce";

    private final static String three = "Three";
    private final static String four = "Four";
    private final static String five = "Five";
    private final static String six = "Six";
    private final static String seven = "Seven";
    private final static String eight = "Eight";
    private final static String nine = "Nine";
    private final static String ten = "Ten";
    private final static String jack = "Jack";
    private final static String queen = "Queen";
    private final static String king = "King";

  // Card Suits
    private final static String hearts = "Hearts";
    private final static String diamonds = "Diamonds";
    private final static String clubs = "Clubs";
    private final static String spades = "Spades";

    // Random five cards of a poker hand
    private Card card1, card2, card3, card4, card5;

  // dealARandomCard() method returns a Card object

  // randomly selected from a standard 52 card deck
  private Card dealARandomCard() {

   // YOUR CODE

     // (i)

 

    int randIndex = (int) (Math.random()*14 + 1);

    String randFace = null;

 

    switch (randIndex) {

 

    case 1  : case 11 : randFace = ace; break;

    case 2  : randFace = deuce; break;

    case 3  : randFace = three; break;

    case 4  : randFace = four; break;

    case 5  : randFace = five; break;

    case 6  : randFace = six; break;

    case 7  : randFace = seven; break;

    case 8  : randFace = eight; break;

    case 9  : randFace = nine; break;

    case 10 : randFace = ten; break;

    case 12 : randFace = jack; break;

    case 13 : randFace = queen; break;

    case 14 : randFace = king; break;

 

    }

   

    randIndex = (int) (Math.random()*4 + 1);

    String randSuit = null;

 

    switch (randIndex) {

 

    case 1  : randSuit = hearts; break;

    case 2  : randSuit = diamonds; break;

    case 3  : randSuit = clubs; break;

    case 4  : randSuit = spades; break;

 

    }

 

    return new Card(randFace, randSuit);

 

  } // dealARandomCard

  // Constructor that creates a PokerHand object consisting of 

  // five DIFFERENT cards from the standard 52 card deck
  public PokerHand() {

   // YOUR CODE
    // (ii)

 

    card1 = dealARandomCard();

 

    do {

      card2 = dealARandomCard();

    } while (card1.equals(card2));

 

    do {

      card3 = dealARandomCard();

    } while (card1.equals(card3) || card2.equals(card3));

 

    do {

      card4 = dealARandomCard();

    } while (card1.equals(card4) || card2.equals(card4) || card3.equals(card4));

 

    do {

      card5 = dealARandomCard();

    } while (card1.equals(card5) || card2.equals(card5) || card3.equals(card5) || card4.equals(card5));

 

  }


  public String toString() {

    return "\tCard 1 : " + card1 + "\n\tCard 2 : " + card2 + "\n\tCard 3 : " + card3 + "\n\tCard 4 : " + card4 + "\n\tCard 5 : " + card5 + ".";

  }

 

  // Member method returning true if its accessing (this) PokerHand

  // object contains the Card object passed to it as its parameter

  private boolean contains(Card card) {

 

   // YOUR CODE

    // (iii)

 

    if (

            (card.equals(card1)) ||

            (card.equals(card2)) ||

            (card.equals(card3)) ||

            (card.equals(card4)) ||

            (card.equals(card5))

       )

      return true;

    else

      return false;

  }

 

 

  // Member method returning true if its accessing (this) PokerHand object

  // contains the same Cards as the PokerHand object passed to it as its parameter

  public boolean equals(PokerHandDealer hand) {

    return (

                (hand.contains(card1)) &&

                (hand.contains(card2)) &&

                (hand.contains(card3)) &&

                (hand.contains(card4)) &&

                (hand.contains(card5))

               );

  }


} // PokerHand



    Add missing Java source code into the class PokerHand  methods (where requested by the presence of the comments // YOUR CODE) to get the corresponding methods to behave as follows :

    (i)    Design dealARandomCard() method that returns a Card object representing a randomly selected card from a standard 52 card deck.
    (ii)    Write the code for the constructor method that creates a PokerHand object consisting of five DIFFERENT cards from the standard 52 card deck using the services of the method from (i).
    (iii)   Write the code for the contains(Card card) member method returning true if its accessing (this) PokerHand object contains the Card object passed to it as its parameter.

 

   In addition, create an application class PokerHandApp that (by calling corresponding member methods) :
    (iv)    creates two DIFFERENT instances (objects) of the PokerHand class, the player1 and player2 object,

and
    (v)    displays both card hands in a message box as shown below.

// File : PokerHandApp.java

import javax.swing.*;

public class PokerHandApp {

   // YOUR CODE

  public static void main (String[] args) {

    // (iv)

    PokerHandDealer player1 = new PokerHandDealer();

    PokerHandDealer player2 = null;

    do {

      player2 = new PokerHandDealer();

    } while (player1.equals(player2));

    // (v)

    String message = "Player 1's hand\n" + player1 + "\n\nPlayer 2's hand\n" + player2;

    JOptionPane.showMessageDialog(null, message);

 }

} // PokerHandApp


(55 (=11+11+11+11+11)  points)


2.
        Find all syntax errors in the following JAVA class definition and correct them. Then, explain what its method main does. Please be brief and up to the point.

(I)

File : Puzzle.java

public class Puzzle

public static void main (String args) {

            String title = JOptionPane.showInputDialog(“Please, enter your title :”);

            String lastName = JOptionPane.showInputDialog(“Please, enter your last name :”);

            if (title.equalsIgnoreCase(“Mr.”) {

                        JOptionPane.showMessageDialog(“Good day Mr.  + lastName + “.”);

else if (title.equalsIgnoreCase(“Ms.”))

                        JOptionPane.showMessageDialog(“Nice seeing you again Ms. ” + lastName + “.”);

else if (title.equalsIgnoreCase(“Mrs.”))

                        JOptionPane.showMessageDialog(“Have a good day Mrs. ” + lastName + “.”);          

else if (title.equalsIgnoreCase(“Miss.”))

                                    JOptionPane.showMessageDialog(“Have a splendid day Miss. ” + lastName + “.”);

else

                        JOptionPane.showMessageDialog(“Very best of day M. ” + lastName + “.”)

} // main

} // Puzzle

 (20 points)


(II)

// File : Puzzle.java

import javax.swing.JOptionPane;

public class Puzzle {

public void main (String[] args)

            title = JOptionPane.showInputDialog(“Please, enter your title :”);

            lastName = JOptionPane.showInputDialog(“Please, enter your last name :”);

            if (title.equalsIgnoreCase(“Mr.”))

                        JOptionPane.showMessageDialog(“Good day Mr. ” + lastName + “.”);

} else if (title.equalsIgnoreCase(“Ms.”)

                        JOptionPane.showMessageDialog(“Nice seing you again Ms. ” + lastName + “.”)

else if (title.equalsIgnoreCase(“Mrs.”))

                        JOptionPane.showMessageDialog(“Have a good day Mrs. ” + lastName + “.”);          

else if (title.equalsIgnoreCase(“Miss.”))

                                    JOptionPane.showMessageDialog(“Have a splendid day Miss. ” + lastName + “.”);

else

                        JOptionPane.showMessageDialog(“Very best of day M. ” + lastName + .”);

} // Puzzle

(20 points)


// SOLUTION :

// File : Puzzle.java

import javax.swing.JOptionPane;

public class Puzzle {

public static void main (String args) {

            String title = JOptionPane.showInputDialog(“Please, enter your title :”);

            String lastName = JOptionPane.showInputDialog(“Please, enter your last name :”);

            if (title.equalsIgnoreCase(“Mr.”))

                        JOptionPane.showMessageDialog(null, “Good day Mr. “ + lastName + “.”);

else if (title.equalsIgnoreCase(“Ms.”))

                        JOptionPane.showMessageDialog(null, “Nice seeing you again Ms. ” + lastName + “.”);

else if (title.equalsIgnoreCase(“Mrs.”))

                        JOptionPane.showMessageDialog(null, “Have a good day Mrs. ” + lastName + “.”);  

else if (title.equalsIgnoreCase(“Miss.”))

                                    JOptionPane.showMessageDialog(null, “Have a splendid day Miss. ” + lastName + “.”);

else

                        JOptionPane.showMessageDialog(null, “Very best of day M. ” + lastName + “.”);

} // main

} // Puzzle

The method main prompts the user for his/her title and last name by displaying corresponding dialog boxes. After it reads the user’s answers, it displays a unique and distinct greeting for the user by distinguishing among the titles entered. The greeting is displayed in a message box and it includes the last name.

3.

(I)

    Explain briefly :
    (a)    Difference between static and non-static member methods of a class.

Static member methods of a class usually perform a general task not-tied to a particular object of the class. Therefore, they can be invoked using their class name dotted to their name (i.e. Math.sqrt(3.12);). No object is necessary to invoke them though this kind of invoking them is not prohibited. Non-static member methods of a class perform a object specific task and can be invoked only by using an accessing object dotted to their name (i.e. title.equals(“Mr.”);).

    (b)   Difference between static and non-static data fields of a class. 

Static data fields of a class usually are unique per class and shared by all objects of the class. Therefore, they can be invoked using their class name dotted to their name (i.e. Math.PI). No object is necessary to invoke them though this kind of invoking them is not prohibited. Non-static data fields of a class are unique and distinct for each object of the given class and belong only to their native object (i.e. player1.card1 vs. player2.card1).

(25 (=13+12)  points)

 (II)

(a)        List all Java primitive data types,

int, byte, short, long, unsigned, char, boolean, float, double

(b)        List all Java wrapper classes and how they relate to Java basic data types,

Integer is the wrapper class for int, byte, short, long, unsigned types. Character is the wrapper class for char type. Float is the wrapper class for Float, Double for double and Boolean for boolean data type, respectively.

    (c)    Briefly explain the rationale behind the idea of having wrapper classes in addition to the basic data types.  

Primitive data types are provided only with sets of applicable operations and relations. They do not possess additional conversion functionalities, neither can be used as objects. Wrapper classes provide a set of useful conversion methods (Integer.parseInt(String), Double.parseDouble(double), Character.toString(), Float.toString(), etc…). Their instances can also be used as objects and unlike with their corresponding primitive data types.

(25 (=8+8+9)  points)