CS 3331 Advanced Object-Oriented Programming Final Exam

Similar documents
CS 3331 Advanced Object-Oriented Programming. Final Exam

CS 3331 Advanced Object-Oriented Programming Exam 1

Java. GUI building with the AWT

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

CHAPTER 1: A GENERAL INTRODUCTION TO PROGRAMMING 1

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

CS 180 Fall 2006 Exam II

CS Exam 1 Review Suggestions

Chapter 12 Advanced GUIs and Graphics

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

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

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Java Programming Lecture 6

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

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

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

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

John Cowell. Essential Java Fast. How to write object oriented software for the Internet. with 64 figures. Jp Springer

Sri Vidya College of Engineering & Technology

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

Graphical User Interfaces (GUIs)

Name Section. CS 21a Introduction to Computing I 1 st Semester Final Exam

Introduction to the JAVA UI classes Advanced HCI IAT351

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

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

CS 251 Intermediate Programming GUIs: Components and Layout

VALLIAMMAI ENGINEERING COLLEGE

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

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

CHAPTER 2. Java Overview

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

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

Applet. 1. init (): called once by the applet containers when an applet is loaded for execution.

15CS45 : OBJECT ORIENTED CONCEPTS

Midterm assessment - MAKEUP Fall 2010

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

Overview. Applets. A Java GUI inside your browser! Important methods Drawing images Playing audio Getting input parameters Double buffering

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Contents. iii Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1998, Revision B

G51PRG: Introduction to Programming Second semester Applets and graphics

Introduction to Java Programming

Introduction. Introduction

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

CS11 Java. Fall Lecture 4

Graphical User Interfaces. Swing. Jose Jesus García Rueda

CS 4330/5390: Mobile Application Development Exam 1

Part 3: Graphical User Interface (GUI) & Java Applets

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

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

Introduction to Java Programming

SELF-STUDY. Glossary


Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

Sample Examination Paper Programming and Software Development

Example: Building a Java GUI

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

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

Example: Building a Java GUI

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Packages: Putting Classes Together

Chapter 13. Applets and HTML. HTML Applets. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 1

Essential Series. Springer-Verlag London Ltd.

Come & Join Us at VUSTUDENTS.net

Murach s Beginning Java with Eclipse

7. Program Frameworks

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

Laying Out Components. What is Widget Layout?

Core JAVA Training Syllabus FEE: RS. 8000/-

RAIK 183H Examination 2 Solution. November 10, 2014

Java Applets / Flash

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

Information Technology for Industrial Engineers 15 November ISE 582: Information Technology for Industrial Engineers

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

To gain experience using GUI components and listeners.

Do not turn to the next page until the start of the exam.

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

Mobile MOUSe JAVA2 FOR PROGRAMMERS ONLINE COURSE OUTLINE

RAIK 183H Examination 2 Solution. November 11, 2013

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

No SVN checkout today. Object-Oriented Design

The Islamic University Gaza Department of Electrical & Computer Engineering. Midterm Exam Spring 2012 Computer Programming II (Java) ECOM 2324

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

Core Java Syllabus. Pre-requisite / Target Audience: C language skills (Good to Have)

Interfaces. Abstract Classes. A Generic Animator. A Generic Animator (cont'd)

Basicsof. JavaGUI and SWING

Window Interfaces Using Swing Objects

1.00 Lecture 14. Lecture Preview

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Swing from A to Z Using Focus in Swing, Part 2. Preface

Window Interfaces Using Swing Objects

40) Class can be inherited and instantiated with the package 41) Can be accessible anywhere in the package and only up to sub classes outside the

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

Graphic User Interfaces. - GUI concepts - Swing - AWT

CSCE3193: Programming Paradigms

H212 Introduction to Software Systems Honors

A Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p.

Transcription:

Fall 2015 (Thursday, December 3) Name: CS 3331 Advanced Object-Oriented Programming Final Exam This test has 5 questions and pages numbered 1 through 10. Reminders This test is closed-notes and closed-book. However, you may bring one page (8.5 X 11) of notes (both sides). Your notes must be your own, and they must be hand-written and turned in with your test. This test is to be done individually, and you are not to exchange or share your notes with others during the test. If you need more space, use the back of a page. Note when you do that on the front. This test is timed. Your test will not be graded if you try to take more than the time allowed. Therefore, before you begin, please take a moment to look over the entire test so that you can budget your time. Clarity is important; if your writing or code is sloppy or hard to read, you will lose points. Correct syntax also makes some difference. There are 110 points all. 1. Short answers (30 points) (a) (2 points) A is a schematic description of a design solution to a recurring problem in software design. It is generic and reusable but doesn t have to be implemented in the same way. (b) (2 points) The design pattern defines a skeleton algorithm by deferring some steps to subclasses. It allows the subclasses to redefine certain steps of the algorithm, often called hook methods or hot spots. (c) (2 points) The design pattern defines a family of algorithms by encapsulating each member of the family and making the members interchangeable. It should be used when many related classes differ only in their behavior or when different variants of an algorithm is needed. (d) (2 points) This design pattern should be used to access the contents of a collection without exposing its internal representation, to support multiple traversal of a collection, and to provide a uniform interface for traversing different collections. i. Template method ii. Strategy iii. Iterator iv. Abstract factory

(e) (2 points) The Java GUI framework consists of several categories of classes. The main categories are the followings except for: i. GUI components (or widgets) ii. Layout managers iii. Events and event listeners iv. Graphics and imaging classes v. Collection classes (f) (4 points) The design of Java GUI framework uses several design patterns, including: It uses the design pattern to compose GUI components into tree structures to represent part-whole hierarchies. Each container has a layout manager, that handles the layout of the components contained in the container. The design of the layout managers uses the design pattern. (g) (2 points) This layout manager arranges as many as five components in five positions identified as North, South, East, West, and Center. Which layout manager is described? i. BorderLayout ii. FlowLayout iii. GridLayout iv. GridBagLayout (h) (2 points) Which of the followings is not the naming convention used throughout AWT and Swing? i. The names of event classes end with the suffix Event. ii. For event class Xyz Event, the associated listener interface is usually named Xyz Listener. iii. There is always an associated adapter class for a listener interface Xyz Listener, and its name is Xyz Adapter. iv. For a listener that implements interface Xyz Listener, the name of the method to register the listener to its source is addxyz Listener. (i) (2 points) There are several different implementations of the List interface, each providing different time and space complexity for various operations. The implementation (class) is preferred if a list is dynamic or volatile in that it grows and shrinks a lot. (j) (2 points) The design of the Java IO classes uses the design pattern to enhance, or provide add-on features to, the basic IO capability through object composition.

(k) (2 points) All the following statements about a server socket are true except for: i. It waits for connection requests from clients. ii. It listens at a specific port. The port number is necessary to distinguish different servers running on the same host. iii. It is an instance of the java.net.serversocket class. iv. It can be used to send and receive data. v. It must be running on the server host before its clients initiate contact. vi. The accept() method of a server socket blocks the caller until a request is received. (l) (2 points) A client socket is an instance of the java.net.socket class and has a pair of an InputStream object and an OutputStream object for receiving and sending data. There are two ways to obtain a client socket: on the client side, it can be created using the constructor Socket(String, port), and on the server side, it is returned by the () method of the ServerSocket class. (m) (4 points) Threads can be created and declared in one of two ways: by directly extending the java.lang. class, and by implementing the java.lang. interface. (n) (2 points) Synchronization ensures the mutual exclusion of two or more threads in the critical regions, but it does not address cooperation among threads. The most common technique or pattern for supporting thread cooperation is known as in which a method call and a calling thread are suspended until a precondition (acting as a guard) is satisfied. 2. (5 points) If the synchronized modifier is removed from the following code snippet, what difference does it make? Be specific about your answer. public synchronized void put(object e) { if (!isfull()) { back = (back + 1) % size; rep[back] = e; count++;

3. (10 points) Consider the following socket-based game server class, named GameServer, which is supposed to support multiple clients. There is a serious problem in supporting multiple clients. Explain the problem, and rewrite the code to fix it. import java.net.*; public class GameServer { /** Launch this server. */ public start() { try { ServerSocket server = new ServerSocket(8000); while (true) { Socket socket = server.accept(); serveclient(socket); catch (IOException e) { e.printstacktrace(); /** Serve the client on the given socket. */ private void serveclient(socket socket) {...

4. (total 20 points) You are to define a class named Book and ordering among its instances. A book may have many attributes, but here you will consider only two: the title (of type String) and the publication year (of type int). Below you will define two different orderings for books so that they can be sorted in different orders. (a) (10 points) Define a Book class that implements a natural ordering among its instances. The natural order of books is based on the ascending order of the title and the publication year. A book, b 1, precedes another book, b 2, if the title of b 1 precedes that of b 2 in the lexicographic order. If both have the same title, the book published earlier precedes the other. public class Book implements { // <--- YOUR CODE HERE! private String title; private int year; public Book(String title, int year) { this.title = title; this.year = year; public String title() { return title; public int year() { return year; // end of class Product

(b) (10 points) Define a comparator class, named YearComparator, for the Book class to order books based on their publication years. A book published earlier precedes a book published later. If two books are published in the same year, the lexicographic order of their titles determines the order.

5. (total 35 points) You are to write a simple applet named PhotoAlbumApplet that can display a list of digital images (see Figure 1 below). The applet provides two control buttons to view previous and next images. Your applet should download digital images from the same directory where the applet is located, and the images are assumed to be stored in the files named Picture1.jpg, Picture2.jpg,..., PictureN.jpg, where N is given by an applet parameter numberofphotos. PhotoAlbumApplet + PhotoAlbumApplet() + init(): void - configuregui(): void - parseint(string): int imageviewer 1 ImageViewer - images[*]: Image + ImageViewer() + paing(graphics): void + addimage(image): void + viewnext(): void + viewprev(): void Figure 1: Sample screenshot (left) and detailed design (right) (a) (20 points) Define the PhotoAlbumApplet class by completing the partial code given below. Do not add any more fields or methods. You can use Image getimage(url, String) method of the Applet class to download an image, e.g., getimage(getcodebase(), "Picture1.jpg"). import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PhotoAlbumApplet extends java.applet.applet { /** A special panel that can display a list of images. */ private final ImageViewer imageviewer = new ImageViewer(); /** Parse a given string and return an int value. */ private int parseint(string s) { int result = 0; if (s!= null) { try { result = Integer.parseInt(s); catch (NumberFormatException e) { return result; /** Create a new instance. */ public PhotoAlbumApplet() { configuregui(); // MORE CODE ON THE NEXT PAGE.

/** Configure GUI consisting of an image viewer and two buttons. */ private void configuregui() { setlayout(new BorderLayout()); add(imageviewer, BorderLayout.CENTER); JButton prevbutton = new JButton("<Prev"); JButton nextbutton = new JButton("Next>"); // (Hint: refer to the ImageViewer class in part (b) of this question.) /** Downloads pictures and add them to the image viewer. */ @Override public void init() { // (Hint: refer to part (b) of this question.)

(b) (15 points) Define the PhotoViwer class used by the PhotoAlbumApplet class by completing its partial code given below. It is a special JPanel class that can contain a list of images and display them one at a time. import java.awt.*; import javax.swing.*; import java.util.list; import java.util.arraylist; /** A special panel that can display a list of images, one at a time. */ class ImageViewer extends JPanel { /** The list of images to display. */ private final List<Image> images = new ArrayList<>(); /** The 0-based index of the current image to display. */ private int index = 0; /** Create a new instance. */ public ImageViewer() { super(true); // enable double-buffering /** Overridden here to paint the current image. */ @Override public void paint(graphics g) { if (images.size() > 0) { Dimension d = getsize(); Image img = images.get(index); g.drawimage(img, 0, 0, d.width, d.height, Color.WHITE, this); /** Add the given image to this viewer. */ public void addimage(image image) { // MORE CODE ON THE NEXT PAGE.

/** Display the next image. If there is no next image, display the first * one. */ public void viewnext() { /** Display the previous image. If there is no previous image, display * the last one. */ public void viewprev() {