ANSWER KEY Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

Similar documents
SAMPLE EXAM Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

MIT AITI Swing Event Model Lecture 17

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

H212 Introduction to Software Systems Honors

Graphical Applications

SAMPLE Final Exam Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 17 December 2009

CS211 GUI Dynamics. Announcements. Motivation/Overview. Example Revisted

Final Exam Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 20 May 2010

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Part I: Learn Common Graphics Components

RAIK 183H Examination 2 Solution. November 10, 2014

Final Exam Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 17 December 2009

CLASSES AND OBJECTS. Fundamentals of Computer Science I

CS 134 Programming Exercise 7:

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

AP CS Unit 11: Graphics and Events

SINGLE EVENT HANDLING

Graphical User Interfaces 2

Second Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 11 November 2010

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

First Name: AITI 2004: Exam 2 July 19, 2004

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

Original GUIs. IntroGUI 1

Chapter 4 Writing Classes

Programming Languages and Techniques (CIS120)

RAIK 183H Examination 2 Solution. November 11, 2013

Graphical User Interfaces 2

SAMPLE EXAM Second Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 11 November 2010

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

Class 16: The Swing Event Model

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

Graphical User Interfaces 2

BASICS OF GRAPHICAL APPS

Java - Applets. public class Buttons extends Applet implements ActionListener

CSIS 10A Assignment 7 SOLUTIONS

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

First Name: AITI 2004: Exam 2 July 19, 2004

To gain experience using GUI components and listeners.

Adding Buttons to StyleOptions.java

CS Exam 3 - Spring 2010

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

Designing Classes Part 2

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

Programming Language Concepts: Lecture 8

Introduction. Introduction

Graphic User Interfaces. - GUI concepts - Swing - AWT

AP Computer Science Unit 13. Still More Graphics and Animation.

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

AP CS Unit 12: Drawing and Mouse Events

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Final Examination Semester 2 / Year 2010

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

Graphical User Interfaces

Advanced Java Unit 6: Review of Graphics and Events

CS180 Spring 2010 Exam 2 Solutions, 29 March, 2010 Prof. Chris Clifton

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference

CS108, Stanford Handout #22. Thread 3 GUI

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

First Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 7 October 2010

Lecture 9. Lecture

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

EXCEPTIONS & GUI. Errors are signals that things are beyond help. Review Session for. -Ankur Agarwal

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS 180 Fall 2006 Exam II

CLASSES AND OBJECTS. Fundamentals of Computer Science I

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Question 1. Show the steps that are involved in sorting the string SORTME using the quicksort algorithm given below.

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Graphical Interfaces

COMP Assignment #10 (Due: Monday, March 11:30pm)

Swing/GUI Cheat Sheet

CS 2113 Software Engineering

CS Exam 3 - Spring 2010

Graphical User Interfaces (GUIs)

Packages: Putting Classes Together

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015

Condi(onals and Loops

DM503 Programming B. Peter Schneider-Kamp.

SAMPLE EXAM Final Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 16 December 2010

CIS 120 Programming Languages and Techniques. Final Exam, May 3, 2011

CMP 326 Midterm Fall 2015

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Graphical Interfaces

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Computer Science 210: Data Structures. Intro to Java Graphics

Lab 10: Inheritance (I)

Transcription:

ANSWER KEY Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009 1. True or False: (a) T In Alice, there is an event that is processed as long as the world is running. (b) F In Alice, an event can execute its statements only once. (c) F An array is the only data structure available in Alice. (d) F In Alice, it is not possible to have lists of objects. (e) T A constructor is a method that creates a new object. (f) T A new class can be derived from an existing one. (g) F In Java, a variable can be used before it is declared. (h) F In Java, GUI components cannot contain other components. (i) T To generate a keyboard event, a component must have the keyboard focus. (j) T In Java, every event has a related listener interface. 2. (a) What is an event? Give an example. An event is something that happens. For example, when the mouse is dragged or a key is clicked are both examples of events. (b) What is a listener in Java? Give an example. A listener responds to events in Java. For example, when a key is pressed, an event is triggered that the KeyListener responds to. 3. Write the Alice code that would make the items in a list, called Ballerinas, turn in place 3 times and jump, in unison: 4. Answer the questions below, based on the following code: 1

What happens when the world starts? The method, my first method, is called. The cow swishes his tail back and forth for 10 seconds, a a sign counts up to 10. What does the other event do? When mootime is true, cow plays the moo sound. 5. Line up the Alice statements with the corresponding statement in Java: A II I world.drawcircle(x,y); II if ( count < 10 ) done = false; B C D IV III I III world.drawcircle(); IV if ( count < 10 ) done = false; else done = true; V name = Integer.toString(count); E V 2

6. To the right of each line of code, indicate the values stored in the variables immediately after those lines have been executed. If a variable is uninitialized, enter? in the box for that variable. (a) int x, y, z; String a, b; x = 5; (b) y = x/5; (c) a = "Hi"; (d) b = a+a; (e) z = x*x+1; (f) a = "23"; (g) y = Integer.parseInt(a); (h) x = y-1; (i) a = z % x; (j) x--; 5???? 5 1??? 5 1? Hi? 5 1? Hi HiHi 5 1 26 Hi HiHi 5 1 26 23 HiHi 5 23 26 23 HiHi 22 23 26 23 HiHi 22 23 26 4 HiHi 21 23 26 4 HiHi 3

7. Fill in the missing methods below: //***************************************************************************** // Circle.java Programming with Alice and Java // // Represents a circle with a particular size, color, and location. The // circle can be drawn filled or unfilled. //***************************************************************************** import java.awt.*; public class Circle private int radius; private Color color; private int x, y; // the circle s center point // Sets up the circle with the specified location, size, and color. public Circle(int xcenter, int ycenter, int size, Color circlecolor) x = xcenter; y = ycenter; radius = size; color = circlecolor; // Draws the circle, filled, in the specified graphics context. public void drawfilled(graphics gc) gc.setcolor(color); gc.filloval(x-radius, y-radius, radius*2, radius*2); 8. Write the constructor for the PushCounterPanel. The count should be initialized to 0, the button should have the title, Push me!, and should have an associated listener from the class, ButtonListener. Also, add the button and label so they appear on the panel and set the background color for the panel. 4

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class PushCounterPanel extends JPanel private int count; private JLabel label; private JButton push; // Sets up the interface on the panel. public PushCounterPanel() count = 0; push = new JButton("Push me!"); ButtonListener listener = new ButtonListener(this); push.addactionlistener(listener); label = new JLabel("Pushes: " + count); add(push); add(label); setbackground (Color.cyan); setpreferredsize (new Dimension(350, 60)); // Increments the counter and updates the label accordingly. public void incrementcount() count++; label.settext("pushes: " + count); 5

9. Write a complete Alice program that has 5 bugs moving in random directions. When a bug is clicked with the mouse, it disappears from view. world.my first method: Events: 10. Write a complete Java program that prints Hello World to the console: public class Hello public static void main(string args[]) System.out.println("Hello World"); 6