UTM CSC207: Midterm Examination October 28, 2011 Student Number: Last Name: First Name: This 110 minute exam consists of 6 double sided pages. You are allowed two 8 1 2 11 double sided aid sheets. 1. [10 Marks: Medium] Class Balloon has constructor Balloon(int capacity, String color) which constructs an empty Balloon with the given capacity and color. Other Balloon methods include inflate(int amount) and deflate(int amount). If a Balloon is over inflated its ispopped() method returns true. Complete the class below class UseBalloon { // Create static method bunchofballoons(int count, String color). This method returns // an array of count Balloons with capacities 0,1,2,...,count-1, all with the given color. // Create static method inflateall which takes an array of Balloons and inflates // them all by a specified amount. Remember, if a is an array, a.length is its length. // YOUR CODE GOES HERE 1
public static void main(string [] args){ // Create two Balloons, a "red" one with capacity 10 and a "green" one with // capacity 20. Take turns inflating each of them a few times and cause one to pop. // Finally, ask each Balloon if they are popped, printing out an approprite message. // Use methods bunchofballoons to create an array of 20 Balloons. Use method inflateall // to inflate all Balloons in the array by 10 units. // YOUR CODE GOES HERE 2
2. [10 Marks: Medium] Write the classes Statistic, SumStatistic, AverageStatistic and MaxStatistic described below: IMPORTANT: Your classes should behave as in the UseStatistics class in the Appendix to this midterm. A Statistic has a single value (it is a floating point number). You can ask an instance for the value by calling getvalue. You can update the value by calling addvalue, for example, addvalue(3.4). For a Statistic, addvalue replaces the Statistics value with the new value. The default constructor for a Statistic sets value to 0. SumStatistic is a Statistic. The value of a SumStatistic is the sum of all values added to it. AverageStatistic is a Statistic. The value of an AverageStatistic is the average of all the values added to it. AverageStatistic should count the number of numbers added to it. It should also keep track of the sum of the numbers added to it. MaxStatistic is a Statistic. The value of a MaxStatistic is the maximum of all values added to it. 3
(continued...) 4
3. [15 Marks: Medium] In the Appendix to this exam, you will find some sample UML Class diagrams as well as interface StateHolder and classes ClickState, FirstClickState, SecondClickState and GUIController. (a) In the space below, draw the UML Class diagram for these 5 classes. 5
(b) Describe (from the users point of view) what happens when they run GUIController. Draw the GUI and describe what happens when the user plays with it. 6
(c) Briefly describe how the code actually works. 7
(Extra Space) 8
(Extra Space) 9
Appendix... /** Demonstrate the use of the Statistics classes **/ /** Note: The output is written below the calls to test **/ public class UseStatistics { public static void main(string [] args){ Statistic stat=new Statistic(); UseStatistics.test(stat); // Prints... // 0.0 1.0 2.0 3.0 4.0 5.0 SumStatistic sumstat=new SumStatistic(); UseStatistics.test(sumStat); // Prints... // 0.0 1.0 3.0 6.0 10.0 15.0 MaxStatistic maxstat=new MaxStatistic(); UseStatistics.test(maxStat); // Prints... // 0.0 1.0 2.0 3.0 4.0 5.0 AverageStatistic avgstat=new AverageStatistic(); UseStatistics.test(avgStat); // Prints... (sum/count=average) // 0.0/1=0.0 1.0/2=0.5 3.0/3=1.0 6.0/4=1.5 10.0/5=2.0 15.0/6=2.5 System.out.println(""+avgStat.getSum()); // Prints: 15.0 System.out.println(""+avgStat.getCount()); // Prints: 6 System.out.println(""+avgStat.getValue()); // Prints: 2.5 public static void test(statistic s){ for(int i=0;i<=5;i++){ s.addvalue(i); System.out.print(s+" "); System.out.println(); 10
Appendix (continued)... Some sample UML Class diagrams. In the second diagram, + indicates public, - indicates private. /** ----------- StateHolder --------------------------- **/ public interface StateHolder { public void setstate(clickstate s); public void announce(string first, String second); /** ----------- ClickState --------------------------- **/ public class ClickState { public void handleclick(stateholder s, String which){ /** ----------- FirstClickState --------------------------- **/ public class FirstClickState extends ClickState { public void handleclick(stateholder s, String which) { s.setstate(new SecondClickState(which)); /** ----------- SecondClickState --------------------------- **/ public class SecondClickState extends ClickState { private String firstclick; public SecondClickState(String firstclick){ this.firstclick=firstclick; public void handleclick(stateholder s, String secondclick) { s.announce(firstclick, secondclick); s.setstate(new FirstClickState()); 11
Appendix (continued)... /** ------------ GUIController ----------------------- **/ // Lots of imports omitted public class GUIController implements ActionListener, StateHolder { private JLabel answer = new JLabel("No Button presses yet!", JLabel.CENTER); private ClickState cs=new FirstClickState(); GUIController() { JFrame f = new JFrame("Second Click Frame"); f.setdefaultcloseoperation(jframe.exit_on_close); Container c = f.getcontentpane(); c.setlayout(new FlowLayout()); for (int i = 0; i < 5; i++) { JButton b = new JButton("" + i); b.addactionlistener(this); c.add(b); c.add(answer); f.pack(); f.setvisible(true); public static void main(string[] args) { javax.swing.swingutilities.invokelater(new Runnable() { public void run() { GUIController gui = new GUIController(); ); public void actionperformed(actionevent arg0) { // Note: arg0.getactioncommand() returns the String // label on the JButton (in this case) cs.handleclick(this, arg0.getactioncommand()); public void setstate(clickstate s) { this.cs=s; public void announce(string first, String second) { this.answer.settext(first+" then "+second); 12