Presentation is loading. Please wait.

Presentation is loading. Please wait.

Quick topix 11/7/13 CSE 1102 Fall 2013. previous statement Execute rest of program Is true? yes no This flowchart illustrates the logic of a _________.

Similar presentations


Presentation on theme: "Quick topix 11/7/13 CSE 1102 Fall 2013. previous statement Execute rest of program Is true? yes no This flowchart illustrates the logic of a _________."— Presentation transcript:

1 Quick topix 11/7/13 CSE 1102 Fall 2013

2 previous statement Execute rest of program Is true? yes no This flowchart illustrates the logic of a _________ loop A.Do-while B.For C.While D.If E.Loop dee

3 previous statement Execute rest of program Is true? yes no This flowchart illustrates the logic of a _________ loop A.Do-while B.For C.While D.If E.Loop dee

4 previous statement Execute rest of program Is true? yes no This flowchart illustrates the logic of a _________ loop A.Do-while B.For C.While D.If E.Loop dee

5 previous statement Execute rest of program Is true? yes no... while ( )... while (time > 0) { System.out.println(time); time = time – 1; }...

6 previous statement Execute rest of program Is true? yes no Alter... for ( ; ; )... for (int i = 0; i< _balls.length; i++) _balls[i].fill(aBetterBrush);...

7 previous statement Execute rest of program Is true? yes no... do while ( )... do { aCookieBag.add(new Cookie()); numCookies++; } while (numCookies < _myLunchNeeds);...

8 Mouse Adapters

9 Suppose we want to add a MouseListener or MouseMotionListener in JPanel, e.g., we have public void addMouseListener(MouseListener m); public interface MouseListener extends EventListener { void mouseClicked (MouseEvent e); void mouseEntered (MouseEvent e); void mouseExited (MouseEvent e); void mousePressed (MouseEvent e); void mouseReleased (MouseEvent e); } e.g., _myPanel.addMouseListener(new MyMouseListener(this));

10 Answer: MouseAdapter public interface MouseListener extends EventListener { void mouseClicked (MouseEvent e); void mouseEntered (MouseEvent e); void mouseExited (MouseEvent e); void mousePressed (MouseEvent e); void mouseReleased (MouseEvent e); } public class MouseAdapter implements MouseListener { public void mouseClicked (MouseEvent e){} public void mouseEntered (MouseEvent e){} public void mouseExited (MouseEvent e){} public void mousePressed (MouseEvent e) {} public void mouseReleased (MouseEvent e){} }

11 A.A subclass of an existing class is faster than a new class that implements the interface. B.The MouseAdapter code has been debugged. C.You don't have to define all of the methods D.There is no advantage if you need all of the methods in MouseListener E.More than one of the above are true What advantage is there in extending MouseAdapter over implementing MouseListener when defining a MouseListener class?

12 Arrays

13 Using an array Declare it: private Peg[] _pegs; _pegs Instantiate it: _pegs = new Peg[10]; Instantiate its elements: for (int i=0;i<5;i++) _pegs[i] = new Peg();

14 A._pegs.length causes a run-time error B._pegs.length is 0, and the number of Pegs in the array is 0. C._pegs.length is 10, and the number of Pegs in the array is 0. D._pegs.length is 10, and the number of Pegs in the array is 5. E._pegs.length is 5, and the number of Pegs in the array is 5. After the Declare step, which of the following is true? Declare it: private Peg[] _pegs;

15 A._pegs.length causes a run-time error B._pegs.length is 0, and the number of Pegs in the array is 0. C._pegs.length is 10, and the number of Pegs in the array is 0. D._pegs.length is 10, and the number of Pegs in the array is 5. E._pegs.length is 5, and the number of Pegs in the array is 5. After the Instantiate step, which of the following is true? Instantiate it: _pegs = new Peg[10];

16 A._pegs.length causes a run-time error B._pegs.length is 0, and the number of Pegs in the array is 0. C._pegs.length is 10, and the number of Pegs in the array is 0. D._pegs.length is 10, and the number of Pegs in the array is 5. E._pegs.length is 5, and the number of Pegs in the array is 5. After the Instantiate its elements step, which of the following is true? Instantiate its elements: for (int i=0;i<5;i++) _pegs[i] = new Peg();

17 Clicker questions 11/6/12 CSE 1102 Fall 2012

18 Why does SpeedListener have a SpeedSlider _slider set in its constructor? A.The SpeedListener can work with multiple sliders. B.The code is clearer, as this is hard to sort out C.We want to distinguish between the Accelerator and the SpeedSlider D.The SpeedListener needs to refer to its enclosing SpeedSlider. E.None of the above is true 1.public class SpeedSlider extends javax.swing.JSlider{ 2. private Accelerator _accelerator; 3. public SpeedSlider(int anOrientation, Accelerator anAccelerator, 4. int aMinSpeed, int aMaxSpeed, int aCurrentSpeed){ 5. super(anOrientation, aMinSpeed, aMaxSpeed, aCurrentSpeed); 6. _accelerator = anAccelerator; 7.... 8. this.addChangeListener(new SpeedListener(this)); 9. } 10. private class SpeedListener implements ChangeListener { 11. private SpeedSlider _slider; 12. public SpeedListener(SpeedSlider aSlider){ 13. _slider = aSlider; 14. } 15. public void stateChanged(ChangeEvent e) { 16. _accelerator.setValue(_slider.getValue()),_slider.getValue()); 17. } } }

19 A. You are already using the desired listener as an ActionListener for something else B. You want to associate the button actions with becoming deselected C. You want to reduce the number of messages sent to the listeners D. You want actions to be associated with both becoming selected and deselected E. All of the above are circumstances where ItemListener is a better choice If I add an ItemListener to a Button in a ButtonGroup, it will invoke itemStateChangeditemStateChanged(ItemEvent) in that listener whenever the button changes from selected to deselected or vice-versa.ItemEvent This can be preferable to using an ActionListener in some circumstances. Which of the following is not one of these circumstances?

20 _pegs In getting from left to right… A. _pegs.addToLength(5); B. _pegs = new Peg[10]; C. Peg temp = new Peg[10] D. _pegs[i] = temp[i]; E. temp[i] = _pegs[i]; Which line below will appear in the code?

21 _pegs temp Declare: Pegs[] temp; Instantiate array: temp = new Pegs[2 * _pegs.length]; Instantiate elements: for (int i=0;i<_pegs.length) temp[i] = _pegs[i]; Reassign variable: _pegs = temp;

22 A. The array _balls is unchanged B. The first element of _balls is a new Ball constructed using new Ball(Color.pink) and not the same as _moreBalls[0]; C. _balls[0] == _moreBalls[0]; D. 0, numberOfBalls – 1 E. _balls.length is undefined 1.public BallPanel extends javax.swing.Panel { 2.private Ball[] _balls; 3. private Ball[] _moreBalls; 4.public BallPanel(int numberOfBalls){ 5._balls = new Ball[numberOfBalls]; 6.for (int i=0;i<_balls.length;i++) 7._balls[i] = new Ball(); 8. _moreBalls = _balls; 9.} Suppose we instantiate BallPanel as follows: BallPanel myPanel = new BallPanel(5); Which of the following statements is true, after executing _moreBalls[0] = new Ball(Color.pink); in some other method of BallPanel ?


Download ppt "Quick topix 11/7/13 CSE 1102 Fall 2013. previous statement Execute rest of program Is true? yes no This flowchart illustrates the logic of a _________."

Similar presentations


Ads by Google