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

Similar documents
Chapter 13 Lab Advanced GUI Applications

RAIK 183H Examination 2 Solution. November 10, 2014

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

MIT AITI Swing Event Model Lecture 17

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Inheritance (cont) Abstract Classes

RAIK 183H Examination 2 Solution. November 11, 2013

CS 11 java track: lecture 3

Theory Test 3A. University of Cape Town ~ Department of Computer Science. Computer Science 1016S ~ For Official Use

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

import javax.swing.*; import java.awt.*; import java.awt.event.*;

Implementing Graphical User Interfaces

CS 180 Final Exam Review 12/(11, 12)/08

University of Cape Town Department of Computer Science Computer Science CSC1017F

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

Window Interfaces Using Swing Objects

CMP 326 Midterm Fall 2015

We are on the GUI fast track path

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

Object-Oriented Programming: Revision. Revision / Graphics / Subversion. Ewan Klein. Inf1 :: 2008/09

Swing from A to Z Some Simple Components. Preface

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

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

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

public static boolean isoutside(int min, int max, int value)

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

COURSE DESCRIPTION. John Lewis and William Loftus; Java: Software Solutions; Addison Wesley

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

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Window Interfaces Using Swing Objects

CS159. Nathan Sprague

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Rules and syntax for inheritance. The boring stuff

TeenCoder : Java Programming (ISBN )

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

Lecture 2: Java & Javadoc

Introduction to Programming Using Java (98-388)

Original GUIs. IntroGUI 1

SINGLE EVENT HANDLING

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

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

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Class, Variable, Constructor, Object, Method Questions

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

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

1 Shyam sir JAVA Notes

A Simple Text Editor Application

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Attempt FOUR questions Marking Scheme Time: 120 mins

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Inheritance and Polymorphism. CS180 Fall 2007

Inheritance (Part 5) Odds and ends

GUI Forms and Events, Part II

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall (total 7 pages)

More Swing. CS180 Recitation 12/(04,05)/08

CS Exam 1 Review Suggestions

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

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

C09: Interface and Abstract Class and Method

1 Method Signatures and Overloading (3 minutes, 2 points)

CS 302 Week 9. Jim Williams

1.00/ Introduction to Computers and Engineering Problem Solving. Quiz 2 / November 10, 2005

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CS415 Human Computer Interaction

CompuScholar, Inc. 9th - 12th grades

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

QUESTIONS FOR AVERAGE BLOOMERS

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

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Midterm assessment - MAKEUP Fall 2010

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

Language Features. 1. The primitive types int, double, and boolean are part of the AP

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

Final Exam CS 251, Intermediate Programming December 13, 2017

AP CS Unit 11: Graphics and Events

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Full file at Chapter 2 - Inheritance and Exception Handling

Review sheet for Final Exam (List of objectives for this course)

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

ITI Introduction to Computing II

OLLSCOIL NA héireann THE NATIONAL UNIVERSITY OF IRELAND COLÁISTE NA hollscoile, CORCAIGH UNIVERSITY COLLEGE, CORK. Summer Examination 2012

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

AP CS Unit 6: Inheritance Notes

Inheritance and Polymorphism

PASS4TEST IT 인증시험덤프전문사이트

1 Looping Constructs (4 minutes, 2 points)

CS 112 Introduction to Programming. (Spring 2012)

Transcription:

Topics for Exam 2: Queens College, CUNY Department of Computer Science CS 212 Object-Oriented Programming in Java Practice Exam 2 CS 212 Exam 2 Study Guide Linked Lists define a list node define a singly-linked list with head node and counter be able to traverse the list from beginning to end and, for example, find a given node, print the data in a given node. append a new node to the end of the list. insert a new node after a given node in the list Interfaces understand the difference between a Class and an Interface understand that classes are extended and interfaces are implemented Inheritance understand what methods and data items are inherited from a super class when they are declared public, private and protected. understand the use of super(), the default use of super() and how to work with multiple super() methods (with different signatures). understand the use of the this reference to use different constructors in the same class. understand the use of overridden methods (polymorphism) in a class hierarchy (for example, for a given object, which tostring method will be called. Abstract Classes and Methods understand what the implications are when a class or methods in a class are declared abstract (that is, an abstract class cannot be instantiated and must be extended to the point that no methods remain abstract.) Event-Driven Programming Creation of a GUI (JFrame) with JMenuBar, JMenu, and JMenuItem. Registration of JMenu items with an even handler (ActionListener) Creation of an ActionListener with an appropriate actionperformed method. Determine which menu item generated the event

CS 212 Practice Exam 2 Page 2 Question 1 Linked lists. Assuming that a singly-linked list with head node containing Social Security numbers (as in the Lecture on Dynamic Structures) been created correctly, write a method for the SSNList class will print each SSN in the list to the system console. Question 2- Interfaces. a. public Interface Exam2 { public int getanswer(int i, int j); public String getstring(string[] s, int i); public Class Exam2class implements Exam2 { What methods must be declared in Class Exam2Class? b. public Interface Exam2 { public int getanswer(int i, int j); public String getstring(string[] s, int i); public Class Exam2Super { public int getanswer(int p, int q) { return p+q; public Class Exam2class extends Exam2Super implements Exam2 { What methods must be declared in Class Exam2Class? Question 3 - Inheritance. Examine the code below and answer the following questions. a. Provide a get method to be inserted after line 13 for the value of species. b. Provide a set method for the instance variable population which does the same error checking as the constructor. c. Write a class FlyingAnimalException which is an extension of IllegalArgumentException. d. The instance variable wingbeatspersecond in the abstract class FlyingAnimal must be greater than zero. Complete the if statement on line 20 so that a FlyingAnimalException is thrown with the message "Wing beats must be greater than zero." if it is not.

CS 212 Practice Exam 2 Page 3 e. Provide the proper arguments for the super call in the single-argument constructor on line 32 so that the default values from the interface are used for the remaining parameters. public interface Flapping { public static final int DEFAULT_WING_BEATS = 30; public static final int DEFAULT_POPULATION = 10000; public int wingbeats (); public boolean flaps (); 1 public abstract class Animal { 2 String species; 3 int population; 4 5 public Animal (String s, int p) { 6 if (s == null s=="") 7 throw new InvalidAnimalException("Species must be given."); 8 if (p<0) 9 throw new InvalidAnimalException("Population cannot be negative"); 10 species = s; 11 population =p; System.out.println("New animal, species is "+species+", population "+population); 12 13 14 15 16 public abstract class FlyingAnimal extends Animal implements Flapping { 17 protected int wingbeatspersecond; 18 public FlyingAnimal (String name, int population, int w) { 19 super(name,population); 20 if (w<1) throw new FlyingAnimalException("Wing beats < 1"); 21 else 22 wingbeatspersecond = w; 23 24 public String tostring() { 25 return this.getclass().getname(); 26 27 28 29 public class Bird extends FlyingAnimal { 30 private boolean flapping = true; 31 public Bird (String species) { 32 super (species, Flapping.DEFAULT_WING_BEATS,Flapping.DEFAULT_POPULATION); 33 34 public Bird (String species, int population, int wingbeats, boolean f) { 35 super(species, population, wingbeats); System.out.println(this.toString()+" created, Species is "+ species+" wing beats = "+wingbeats); 36 37 38 public int wingbeats() { 39 return wingbeatspersecond; 40 41 public boolean flaps (){ 42 return flapping; 43 44 45 46 public class Bat extends FlyingAnimal { 47 private boolean flapping = false;

CS 212 Practice Exam 2 Page 4 48 public Bat (String species) { 49 super (species, Flapping.DEFAULT_WING_BEATS,Flapping.DEFAULT_POPULATION); 50 51 public Bat (String species, int population, int wingbeats, boolean f) { 52 super(species, population, wingbeats); System.out.println(this.getClass().getName()+" created, Species is "+ species+" wing beats = "+wingbeats); 53 54 55 public int wingbeats() { 56 return wingbeatspersecond; 57 58 public boolean flaps (){ 59 return flapping; 60 Question 4. (Continuing with the set of classes above) public class Main { public static void main(string[] args) { Bat igor=null; Bird robin; FlyingAnimal finch; Statement 1; What will be the output of the application above for each given value of Statement1. If no value is given, assume the statement is blank. If either statement will cause a compiler error, explain why. a. Statement1 is igor = new Bat("Vampire",1200,30); b. Statement1 is igor = new Bat(null,1200,30,false); c. Statement1 is robin = new Bird ("Robin",3000,50,true); d. Statement1 is robin = new Bird ("Robin"); e. Statement1 is igor = new Bat ("",20,25); f. Statement1 is finch = new FlyingAnimal ("Finch",5000,45);.

CS 212 Practice Exam 2 Page 5 Question 5. The following code is meant to create a GUI (JFrame) with one menu called Exam 2 which has two menu items, Pass and Fail. Write the proper code for each of the missing pieces in the box to the right. import java.awt.*; import javax.swing.*; public class MainWindow extends JFrame 1 { private JFrame mainwindow; private MenuBar 2 mainmenubar; private JMenu exam2 3 ; private MenuItem 4 mpass, mfail, mquit 5 ; private Exam2Listener 6 exam2ml; public static void main (String args[]){ MainWindow m = new MainWindow("Exam 2"); public MainWindow (String title) { mainwindow = new JFrame(title); mpass = new MenuItem ("Pass"); mfail = new MenuItem 7 ("Fail"); mquit = new MenuItem ("Quit"); exam2 = new JMenu ("Exam 2", /*tearoff =*/ false); exam2.add(mpass); exam2.add(mfail); exam2.addseparator(); exam2.add(mquit); mainmenubar = new JMenuBar(); mainmenubar.add(exam2 8 ); mainwindow.setmenubar(mainmenubar); exam2ml = new Exam2Listener(this); 9 mpass.addactionlistener(exam2ml); mfail.addactionlistener(exam2ml); 10 mquit.addactionlistener(exam2ml); Answers: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. mainwindow.setdefaultcloseoperation(exit_on_close); mainwindow.setsize(400,400); mainwindow.setlocation(100,100); mainwindow.setvisible(true); // constructor // class

CS 212 Practice Exam 2 Page 6 Question 6. The following code is a listener for the GUI in the question above. If the user chooses the menu item Pass, as message saying Hooray! Your passed! should be printed to the console. If the user chooses Fail, a message saying Too bad! You failed. should be printed to the console. If the user chooses Exit, the program should terminate. Write the proper code for each of the missing pieces in the box to the right. import java.awt.*; import java.awt.event.*; public class Exam2Listener implements ActionListener 1 { private MainWindow 2 mainframe; public Exam2Listener(MainWindow f) { mainframe = f; // constructor public void actionperformed(actionevent 3 e) { String chosenitem = ((MenuItem) e.getsource()).getlabel(); if (chosenitem.equals( "Pass" 4 )) { System.out.println("Hooray! You passed!"); // end if minputfile else if (chosenitem.equals("fail")) { System.out.println( "Too bad! You 5 failed." ); // end if minputfile else if ( chosenitem.equals("quit")) { System.exit(0); //if // // class Exam2Listener Answers: 1. 2. 3. 4. 5.