CSE1325 OO Programming Sample Test

July 9, 2013

 

Name:   Key/Notes                                                                            UTA ID: 1000                               

 

Instructions:

1.     Read all of the instructions for each question and answer what is asked.  Do not write down random stuff if you donÕt know the answer.

2.     The test is worth 100 points and there will be 10 points extra credit available.

3.     If you have a question during the test, raise your hand and ask the proctor.  You may or may not get an answer, but you wonÕt know unless you ask.

 

1.a.      Write part of a Java class that contains data fields to represent the following information for you favorite game.  Name the game; indicate how often you enjoy the game (use some numerical representation and explain its meaning); indicate the general category of the game as spectator sport, computer/video game, card game, participant sport, board game, or other;  indicate how long you typically participate on any one occasion; include a field for your favorite player or team name; and include a data field for an object of type Rules that would contain the rules of the game.  You do not have to define the Rules class.  Follow the standard conventions for representing your data.      {6 points}

 

public enum GameCategory {spectatorSport, computerVideoGame, cardGame, participantSport, boardGame, other}

 

public class FavoriteGame

{

      private String gameName;

      private int enjoyHowOften;  //number of times per week

      private GameCategory gameType;

      private double avgGameLength;  //average game length in fractions of hours

      private String favorite;

      private Rules gameRules;

 

}


 

1.b.      Write methods for your favorite game class to allow the user to enter values for each of the following data fields: 

game name;

how often you enjoy the game;

and the general category of the game;

The methods should error check the values as appropriate.  (If you need more space, use the bottom of the front page.)                                                                                                                {6 points}

public boolean setGameName(String name)

{

      gameName = name;

      return true;

}

 

public boolean setEnjoyHowOften(int often)

{

      if (often <= 0)

         {     return false;     }

      enjoyHowOften = often;

      return true;

}

/*

public enum GameCategory {spectatorSport, computerVideoGame, cardGame, participantSport, boardGame, other}

*/

public boolean setGameType(String gmtyp)  //input can come in a variety of forms

{

      if (gmtyp.equalsIgnoreCase( "spectatorsport")     // selection must be based on input type

      {     gameType = GameCategory.SPECTATORSPORT;  }

      else if (gmtyp.equalsIgnoreCase( "computervideogame")

      {     gameType = GameCategory.computerVideoGame,;  }

      else if (gmtyp.equalsIgnoreCase( "cardgame")

      {     gameType = GameCategory.CArdGame,;  }

      else if (gmtyp.equalsIgnoreCase( "participantsport")

      {     gameType = GameCategory.participantSport,;  }

      else if (gmtyp.equalsIgnoreCase( "boardgame")

      {     gameType = GameCategory.BOArdGame,;  }

      else if (gmtyp.equalsIgnoreCase( "other")

      {     gameType = GameCategory.OTHER,;  }

      else

      {     return false;   }

      return true;

}

1.c.      Define a class Leisure that contains a collection of game items.  Write one method that iterates through the collection to print all the games that are part of your leisure activities and your average participation time for that game.  (Assume that a toString function is NOT available for the game class.)                                                                                                                                                             {7 pts}

 

public class Leisure

{

      private ArrayList<FavoriteGame> myGames;

      public Leisure()

      {

         myGames = new ArrayList<FavoriteGame>;

      }

      public boolean addGame( FavoriteGame mine )

      {   myGames.add(mine);  

          return true;

      }

      public FavoriteGame getGame()

      {   return myGames[0];    }

 

      public void displayAllGamesLength()

      {

            for (FavoriteGame fg: myGames)

            {

                  System.out.println( gameName + " Avg playing time is "+ avgGameLength );

            }

      }

 

 

 

 

 

Short answer question

2.   List and describe at least two benefits of using library classes in Java.  Then give one example of library classes you have used this semester including class name and what is in the class.    {6 pts}

 

Benefit 1

Benefit 2

Name of library class

Contents of library class