Points Missed on Page page 1 of 8

Similar documents
Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Introduction. Introduction

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

Spring 2013 Homework Assignment #3. Due: 8 November 2012, 11:00pm Covers Chapters: 11, 13.

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

Systems Programming Graphical User Interfaces

CSE 11 Midterm Fall 2008

Part I: Learn Common Graphics Components

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

Top-Level Containers

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

Java for Interfaces and Networks (DT3010, HT10)

RAIK 183H Examination 2 Solution. November 10, 2014

Chapter 18. Advanced graphics programming

Give one example where you might wish to use a three dimensional array

Java for Interfaces and Networks (DT3029)

Packages: Putting Classes Together

MIT AITI Swing Event Model Lecture 17

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

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

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

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

CSE331 Fall 2014, Final Examination December 9, 2014 Please do not turn the page until 2:30. Rules:

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

RAIK 183H Examination 2 Solution. November 11, 2013

Introduction to Programming Using Java (98-388)

Practice Questions for Chapter 9

Window Interfaces Using Swing Objects

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

PROGRAMMING FUNDAMENTALS

Lecture 5: Methods CS2301

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

Project 1. LibraryTest.java. Yuji Shimojo CMSC 335

Window Interfaces Using Swing Objects

UTM CSC207: Midterm Examination October 28, 2011

1.00/ Introduction to Computers and Engineering Problem Solving. Quiz 2 / November 5, 2004

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

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

CS Exam 3 - Spring 2010

Graphical User Interfaces (GUIs)

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

User interfaces and Swing

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

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

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

A Foundation for Programming

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

University of Cape Town Department of Computer Science Computer Science CSC1017F

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

Lecture 10 Declarations and Scope

Midterm assessment - MAKEUP Fall 2010

Lec 3. Compilers, Debugging, Hello World, and Variables

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

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Computer Programming, I. Laboratory Manual. Final Exam Solution

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Swing/GUI Cheat Sheet

CS Exam 3 - Spring 2010

Practice Midterm 1. Problem Points Score TOTAL 50

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

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

Lecture 9 CSE11 Fall 2013 Active Objects

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.

Computer Science II (20082) Week 1: Review and Inheritance

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

I have neither given nor received any assistance in the taking of this exam.

Agenda. Container and Component

Final Exam CS 251, Intermediate Programming December 13, 2017

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

Window Interfaces Using Swing. Chapter 12

Java Help Files. by Peter Lavin. May 22, 2004

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

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

To gain experience using GUI components and listeners.

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

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!

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

Swing - JTextField. Adding a text field to the main window (with tooltips and all)

Chapter 4 Defining Classes I

Chapter 13 Lab Advanced GUI Applications

Java. GUI building with the AWT

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

Programming Language Concepts: Lecture 8

COS 126 Midterm 1 Written Exam Fall 2012

INTRODUCTION TO (GUIS)

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

CSE 143 Au04 Midterm 1 Page 1 of 9

AP CS Unit 11: Graphics and Events

Programming graphics

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

Graphical Interfaces

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

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

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

State Application Using MVC

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

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

Transcription:

Midterm II - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total. Name: ID: Problem #1 (8 points) Rewrite the following code segment using a for loop instead of a while loop (that is properly replace the while loop with a for loop so that running the rewritten code would yield the identical results as the original) Use the minimum number of statements possible. int i = 100; int k = 4; while (i >= 0 ) if ( (i % k) == 0 ) System.out.println( Divisible ); i = i 3; ans: int k=4; for ( int i=100; i >= 0; i =) if ( (i % k) == 0 ) System.out.println( Divisible ); Problem #2 ( 12 points) Assume the file ElectricalDevice.java has only the following contents public interface ElectricalDevice public boolean poweron(); public boolean powercontrol(int onoff); public static final int VOLTS = 120; Points Missed on Page page 1 of 8

(Problem 2 cont'd) Further suppose that the complete source of Kettle.java is as follows public class Kettle implements ElectricalDevice private int volts; private int powerstate = 0; public Kettle() volts = ElectricalDevice.VOLTS; public boolean poweron() if (powerstate!= 0) return true; else return false; (a) Kettle.java will not compile, What is the critical error? Kettle does not implement the powercontrol() method and therefore does not properly implement the Electrical device interface. (b) Is the following a legal declaration? Why or why not? ElectricalDevice [] mydevices = new ElectricalDevice[10]; Yes. This declares an array of references to objects that implement the ElectricalDevice interface. (c) Is the following a legal declaration? Why or why not? ElectricalDevice mydevice = new ElectricalDevice(); No. ElectricalDevice is an interface, not a class. The new operator can only be applied to classes. Points Missed on Page page 2 of 8

Problem #3 (20 points) Read the following program. Answer questions on the following page import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class Q3 implements Runnable, ActionListener, ChangeListener private JTextArea out; private JButton buttonreset; private JSlider myslider; private static final int MAX_INT=12; private String squares(int b) String rval=""; for (int i = 1; i<= b; i++) rval += "".format("% 4d", i*i); return rval; public void run() JFrame f = new JFrame("Squares"); f.setdefaultcloseoperation(jframe.exit_on_close); f.setlayout(new BorderLayout()); String sq = squares(max_int); out = new JTextArea(sq,1,4*(MAX_INT + 1)); out.setfont(new Font(Font.MONOSPACED,Font.PLAIN,12)); myslider = new JSlider(1, MAX_INT, MAX_INT); buttonreset = new JButton("Reset"); Container contentpane = f.getcontentpane(); contentpane.add( out, BorderLayout.CENTER ); contentpane.add (myslider, BorderLayout.NORTH); contentpane.add (buttonreset, BorderLayout.SOUTH); myslider.addchangelistener(this); f.pack(); f.setvisible(true); public void statechanged (ChangeEvent evt ) int j = myslider.getvalue(); out.settext(squares(j)); public void actionperformed (ActionEvent evt) out.settext(squares(max_int)); myslider.setvalue(max_int); public static void main(string[] args) Q3 q3= new Q3(); SwingUtilities.invokeLater(q3); Points Missed on Page page 3 of 8

Problem #3 (cont'd) (a) When the program first runs, which of the following is the correct screen output? Explain your reasoning. (i) (ii) (iii) (iv) (a) is the correct initial screen 1. The slider is the Border.NORTH location, and is initialized to the maximum value (12) 2. the text area in the Border.Center Location an is intialized with the string 1 4 9... 144 3. The reset button is placed in the Border.SOUTH location (b) What happens when the method ActionPerformed() is invoked? The slider is reset to it maximum value The text is reset to the string 1 4 9 16... 144 (c) What will the program do when the Reset button is pressed by the user? Explain why. Nothing. The Q3 instance is not registered as an ActionListener with the Reset button. That is, buttonreset.addactionlistener(this) does not appear in the code. Points Missed on Page page 4 of 8

Problem #4 (20 points) Circle T (indicating True) or F (indicating False) for each one of the following. T F A class can implement only one interface T F pause(100) causes a program to wait exactly 100 ms before resuming T F The onmouseclick() method used in this class is only invoked when clicking on objectdraw DrawingCanvas object. T F An activeobject run() method must invoke pause() T F In Program #4, the record() method defined in a CemeteryController is termed a callback. T F In Program #4, in order to use Tombstone objects in both TestCemetery and Cemetery the Tombstone must implement the CemeteryController interface. T F All for loops can be converted to while loops. T F A good programming habit is to declare instance variables with the private access modifier T F A good approach to solving a programming problem is to write all the code first and then debug the result. T F The BorderLayout() layout used in Swing can be applied only to JFrame objects T F The FlowLayout class defines five distinct areas in which a program can place graphical objects T F FilledOval ovals[10]; creates ten FilledOval objects named oval0, oval1, oval2,..., oval9; T F int [][] matrix = new int[10][20]; creates an array of integers with 200 elements T F If one declares int [][] matrix = new int[10][20]; then matrix.length evaluates to 200. T F All two-dimensional arrays in java are rectangular. T F If a method returns a value, it can only return a primitive type. T F The continue statement used inside of a loop body indicates that the program should immediately execute the first statement following the loop body. T F Array indices cannot be of double type T F for(i=0; i++ < 10;); is a valid for-loop. T F The this keyword means this class when used in the context addactionlistener(this); Points Missed on Page page 5 of 8

Problem #5 (20 points) Write a public method called with the following signature insertoval(filledoval ovals[], int idx) insertoval inserts an empty slot into the array ovals at index idx by properly moving any existing elements. It should only perform the operation if the last element of the ovals array is also empty. It should return true if the insert operation was successful, false otherwise. The method should not generate any exceptions for any combination of arguments. import objectdraw.*; public boolean insertoval(filledoval ovals[], int idx) // check valid parameters if (ovals == null) return false; if (idx < 0 idx >= ovals.length) return false; if (ovals[ovals.length 1]!= null) return false; for ( int i = ovals.length 1; i > idx; i ) ovals[i] = ovals[i 1]; ovals[idx] = null; return true; Points Missed on Page page 6 of 8

Problem #6 (15 points) More fun with for loops In each of the following, create a valid for( ; ; ) statement to achieve the desired results of the following two-line code segment: for ( ; ; ) System.out.println(i); example: print out integers from 1 to 10 ans: for (i = 1; i <= 10; i++) Assume that i already declared as int i; (a) print out k 2 for k = 11,13,15,17,19,21 (b) print out 3 k k=1,2,3,4,5 (c) print out every ninth integer starting at 9 and ending at 108 (includes 108) (d) print out the numbers from 1 to 41 that are NOT evenly divisible by 3 (e) print out the numbers 1,4,9,16,25,36,49,64,81,100 (a) for (int j = i = 11; (i=j*j) <= 21*21; j+=2) (b) for (i = 3; i <= 3*3*3*3*3; i*=3) (c) for (i = 9 ; i <= 108 ; i+=9) (d) for (i = 1; i <= 41 ; i += i % 3 ) (e) for (int j = i = 1; (i=j*j) <= 100; j++) (e) Alternative answer: for (int j = i = 1; (j+=2) <= 21; i += j) Points Missed on Page page 7 of 8

Problem #7 (5 points) public class NotQuite private int firstarg; public static void main(string[] args) NotQuite temp = new NotQuite(); firstarg = Integer.parseInt(args[0]); when compiling the following error is given: $ javac NotQuite.java NotQuite.java:7: error: non static variable firstarg cannot be referenced from a static context firstarg = Integer.parseInt(args[0]); ^ 1 error Why is this error occurring? What is the simplest fix you can make to the code to address the error? firstarg is instance variable and is only valid after a new NotQuite instance is created. Main is a static method and is invoked before any instance has been created. Hence firstarg is not available when the main method is invoked. The simplest fix is to declare firstarg with the static modifier to make it a class variable. Points Missed on Page page 8 of 8