CSCI 143 Exam 2 Name 1 Hour, Closed Notes, Browser open to Java API docs is OK A. Short Answer For questions 1 5 credit will only be given for a correct answer. Put each answer on the appropriate line. 1. [4] In the Java AWT, a WindowListener registered with a Frame can be used so that a click in the go away box of the frame s window will close the program. (T/F) 1. 2. [4] If an exception is thrown while a program is running then you can be certain that the programmer has made an error. (T/F) 2. 3. [4] In the Java AWT, a MouseListener object registered with a Button causes a click on that Button to activate the code in the mouseclicked method of the MouseListener object. (T/F) 3. 4. [4] When using GridLayout, one needs to specify the geographic location (i.e. North, South, etc.) of each Component added to the Container. (T/F) 4. 5. [4] It is ok for a class to define two methods with the same name, as in the code below: (T/F) 5. public void setmood() { System.out.println("I am a happy person"); public void setmood(string mood) { System.out.println("I am a "+mood+" person"); 1
B. Medium Answer Answer each of the following as directed, or using one or two sentences. Some partial credit may be given. 6. [8] A java.awt.button object has a processevent method as well as a processactionevent method. What is the difference between the two? 7. [8] Predict the output when the following Java program runs. public class Mystery { int y = 4; public int divide12(int x) throws Exception { if (x == 0) throw(new Exception()); else return(12/x); public void test() { try { System.out.println("12 divided by " + y + " is " + divide12(y)); catch(exception e) { System.out.println("The end"); return; y = y - 1; test(); public static void main(string[] args) { Mystery m = new Mystery(); m.test(); 2
C. Long Answer Solve each of the following in the space provided (use the back of the facing page if necessary). Partial credit may be given. 8. [8] I am working on a class that extends java.awt.frame. I want my Frame to have 10 circles each having a color chosen at random and each having a random diameter between 5 and 20 units. The circles are placed at random locations within the frame. Write the paint method. As a hint, the expression (int) ((30 * Math.random()) + 10 returns a random int between 10 and 40. Another hint is that you can generate a random color by a call to new Color(***, ***, ***) where each of the *** should be replaced by its own random int in the range 0-255. 9. [8] Now I want exactly the same functionality but I want to extend javax.swing.jframe instead of java.awt.frame. Explain what needs to change. 3
10. [8] We never did do any audio during this course. Until now!! If you decided you needed to add music to one of your AWT or Swing programs, how would you do it? Describe some steps you would take. What do you look for? Where do you look? How do you look? What kind of documentation do you hope to find? What kind of tutorial material do you expect to find? Describe your research process and a few, a very few, key discoveries. 4
11. [20] I want to design a Java program to play Poker. You do not need to know anything about Poker beyond what is given in this question. Poker is a game played with a standard deck of 52 cards played by a number of players sitting around a table. Also seated at the table is a dealer. Each player possesses a stack of chips (tokens of monetary value with which to bet). A game proceeds as follows: (a) Each player places one chip onto the table. This initial, compulsory bet is called the Ante. (b) The dealer deals two cards to each of the players. (These cards are dealt face down, and only the player is able to see those cards.) We call these the hidden cards. Each player has two, and nobody knows what hidden cards the other players hold. (c) A round of betting takes place. Each player in turn may choose to fold, call, or raise. (Do not worry about the meaning of those terms. Just be aware that they may involve placing some of the player s chips on the table.) (d) Now the dealer deals three cards face up on the table. These are the first three of what will eventually be five community cards. Each player will, eventually, generate their best 5-card poker hand by choosing five out of seven available cards. (Do not worry about what constitutes the best poker hand: Just think of it a 5- card subset chosen according to some strategy from the seven available.) For each player the seven available cards are their own two hidden cards and the eventual five community cards. (e) Before the fourth community card is dealt, there will be a round of betting, during which each player may fold, call or raise. (f) Then the dealer deals the fourth community card face up on the table where every player can see it. (g) Then another round of betting. (h) Then the fifth and final community card is dealt. (i) Then the final round of betting. (j) At the end of the final round of betting, any players who have not previously folded will show their hands and the best hand takes all of the chips that have been bet. You are not to write Java code for this question. Just design some classes, and think about where various variables and methods belong. This question is continued on the next page, where a table is provided in which you can enter your answers. 5
In the first column is a list of potential classes, each with a name that should suffice for you to understand its purpose. Space is provided for you to add more classes. Do so, and choose meaningful names. In the second column is the parent class. It may be that your classes extend known Java classes. This is where you write the name of the parent class. If you don t need inheritance, just extend java.lang.object. You should also use the second column to list any interfaces you want your class to implement. The third column lists some of the variables used to store the object s state. Choose meaningful names. The last column should list some of the public methods. Name them. There is no single correct answer to this question. Try to think through the early stages of solving the poker simulation problem and design a workable scenario. If you need more classes than I ve provided space for then make space. Class extends Variables public Methods Card java.awt.component boolean faceup; public void paint() Dealer Table Player 6
12. [4] In one or two sentences explain what is a Thread. 13. [4] I have a class called Frog that is to be used in a video game that I m designing. Write two headers that I can use for the class in order that any Frog object will be able to run as an independent thread. (a) public class Frog { (b) public class Frog { 14. [4] Whichever way I declare the class Frog, there is one method that I absolutely must define (in one case) and definitely should override (in the other case). What is that method? 15. [4] In my video game I also have a class LilyPad. It has a method public boolean isoccupied() that a Frog object can call to find out if there is already a frog on the LilyPad object, and a public void climbon(frog client) that a Frog object can call in order to place itself on this LilyPad object. As you know, it is not desirable for more than one frog to sit on the same lily pad, so within the Frog class we have code: LilyPad lily = new LilyPad();... if (lily.isoccupied()) { // do nothing else { lily.climbon(this); Clearly the intention is that no Frog object should ever attempt to climb on to a LilyPad object that another Frog object is already sitting on. Sadly, when I run my game, I find that occasionally a Frog will attempt to climb on to an already occupied LilyPad. Explain. 16. [4] Fix the problem. Write code for the LilyPad class to be called from the Frog class and explain why it will make it impossible for a frog to climb an occupied lilypad. 7