• No results found

1 Hour, Closed Notes, Browser open to Java API docs is OK

N/A
N/A
Protected

Academic year: 2021

Share "1 Hour, Closed Notes, Browser open to Java API docs is OK"

Copied!
7
0
0

Loading.... (view fulltext now)

Full text

(1)

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)

(2)

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();

} }

(3)

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.

(4)

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.

(5)

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.

(6)

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

(7)

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.

References

Related documents

In accordance with Article 719 of the Belgian Companies Code, the boards of directors of KBC Group NV, a naamloze vennootschap (company with limited liability), with its

Development of an interim profile (IP) method was driven by a business need to rapidly measure an organization’s software engineering process maturity between organizational

We find that households that experienced a land dispute before the first-stage land reg- istration was implemented are more likely to be interested in a second-stage

Agriculture is a very important sector that we need to take care of since it gives benefits to human kinds and civilization. There are many challenges need to face to

2.6 Achieving pedagogical “equality” by instructional differentiation 19 3 Theory: self-regulation of learning in multilevel instructional contexts 23 3.1 Cognitive learning,

6.4 Filters—Filters shall be used whenever the contrast reductions caused by low energy, scattered radiation, or the extent of undercut (edge burn-off) occurring on

As inter-speaker variability among these the two groups was minimal, ranging from 0% to 2% of lack of concord in the 21-40 group and from 41% to 46% in the 71+ generation, we

With an established user community, the remit for ARCCA (Advanced Research Computing @ Cardiff) will be to engage with less traditional computational research groups to help assist