CSE 143 Au04 Midterm 1 Page 1 of 9

Similar documents
CSE wi Final Exam 3/12/18 Sample Solution

CSE 331 Final Exam 6/5/17. Name UW ID#

CSE wi Final Exam 3/12/18. Name UW ID#

CSE 142 Su01 Midterm 2 Sample Solution page 1 of 8. Question 1. (4 points) Suppose we ve declared and initialized the following variables

CSE 331 Final Exam 6/7/16 Sample Solution

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

CSE 331 Final Exam 6/7/16

CSE 331 Final Exam 6/5/17 Sample Solution

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Inheritance & Polymorphism

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

CSE 331 Final Exam 3/16/15 Sample Solution

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

CSE 143 Sp03 Final Exam Sample Solution Page 1 of 13

Programming Languages and Techniques (CIS120)

CS 201, Fall 2016 Sep 28th Exam 1

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

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

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

CSE 142 Sp02 Final Exam Version A Page 1 of 14

CSE 331 Final Exam 12/14/15 Sample Solution. Question 1. (20 points, 1 each) Warmup. For each statement, circle T if it is true and F if it is false.

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

CS170 Introduction to Computer Science Midterm 2

Logistics. Final Exam on Friday at 3pm in CHEM 102

ITI Introduction to Computing II

MIT AITI Swing Event Model Lecture 17

AP CS Unit 11: Graphics and Events

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Class, Variable, Constructor, Object, Method Questions

Object Oriented Programming. Week 1 Part 3 Writing Java with Eclipse and JUnit

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

ITI Introduction to Computing II

H212 Introduction to Software Systems Honors

CSE 413 Winter 2001 Midterm Exam

CS 251, Intermediate Programming Midterm Exam October 9, 2013

CSE 143 Sp04 Final Exam Sample Solution Page 1 of 12

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

Department of Civil and Environmental Engineering, Spring Semester, ENCE 688R: Midterm Exam: 1 1/2 Hours, Open Book and Open Notes

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

Programming Languages and Techniques (CIS120)

Points Missed on Page page 1 of 8

Java Object Oriented Design. CSC207 Fall 2014

CSE 142 Wi03 Midterm 2 Sample Solution All Versions Page 1 of 6

Lecture 3. Lecture

CSE 331 Final Exam 12/9/13

CSE 143 Lecture 12 Inheritance

SUN Sun Certified Associate for the Java Platform.

Building Java Programs

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Inheritance and Testing Spring 2018 Exam Prep 4: February 11, 2019

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

public class Q extends P { private int num; private int i; P(int n) { public Q() { this.num = n;

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

Inheritance (cont.) Inheritance. Hierarchy of Classes. Inheritance (cont.)

Swing - JButton. Adding buttons to the main window

Example: Count of Points

Programming Languages and Techniques (CIS120)

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

Instance Members and Static Members

Department of Civil and Environmental Engineering, Spring Semester, ENCE 688R: Midterm Exam: 1 1/2 Hours, Open Book and Open Notes

COE318 Lecture Notes Week 8 (Oct 24, 2011)

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

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

CSE wi Midterm Exam 2/8/18. Name UW ID #

APCS Unit 5 Exam. Assuming all four classes have a default constructor, which of the following statements would result in an error from the compiler?

CS 11 java track: lecture 3

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

CS 112 Introduction to Programming

The Nervous Shapes Example

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

CSE 401 Final Exam. December 16, 2010

COMP-202 More Complex OOP

Accurate study guides, High passing rate! Testhorse provides update free of charge in one year!

Rules and syntax for inheritance. The boring stuff

CS 120 Fall 2008 Practice Final Exam v1.0m. Name: Model Solution. True/False Section, 20 points: 10 true/false, 2 points each

CSE wi Midterm Exam 2/8/18 Sample Solution

Object- Oriented Analysis, Design and Programming

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Java Swing Introduction

CSCE3193: Programming Paradigms

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 :) )

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

Class 16: The Swing Event Model

Administrivia. CMSC433, Fall 2001 Programming Language Technology and Paradigms. Administrivia (cont.) Project 1. Copy constructor.

L4,5: Java Overview II

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

CSE 331 Midterm Exam 11/9/15 Sample Solution

Please show all of your work.

Full file at Chapter 2 - Inheritance and Exception Handling

Java Magistère BFA

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

CS/120 Final Exam. Name

Practice Questions for Chapter 9

CS260 Intro to Java & Android 03.Java Language Basics

Questions Answer Key Questions Answer Key Questions Answer Key

1 Inheritance (8 minutes, 9 points)

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

COURSE 2 DESIGN PATTERNS

Person class. A class can be derived from an existing class by using the form

Transcription:

CSE 143 Au04 Midterm 1 Page 1 of 9 Question 1. (4 points) When we re modifying Java code, some of the things we do will change the coupling between the class we re working on and other classes that it interacts with. For each of the following possible changes, circle increase if the change increases the coupling between this class and other classes, circle decrease if it decreases the coupling, or circle no effect if the change has no effect on the coupling with other classes, whether or not it is a worthwhile change. (a) Change the declaration of an instance variable from public to private. increase coupling decrease coupling no effect (b) Change the name of an instance variable. increase coupling decrease coupling no effect (c) Move a method that is declared public and duplicated in several related classes to their common superclass. increase coupling decrease coupling no effect (d) Change the declaration of a method from private to protected. increase coupling decrease coupling no effect Question 2. (2 points) One of your colleagues is experimenting with Java 1.4 Swing and trying to create a simple window with a label in it. For some reason the code doesn t work. Here it is: import java.awt.*; import javax.swing.*; public class TestFrame { public static void main(string[] args) { JFrame window = new JFrame("window title"); JLabel label = new JLabel("label text"); window.add(label); window.pack(); window.setvisible(true); What s the problem? Show how to fix it. The problem is that you cannot add items directly to a JFrame, you need to use it s getcontentpane method instead. To fix it change the line that says window.add(label) to window.getcontentpane().add(label).

CSE 143 Au04 Midterm 1 Page 2 of 9 Question 3. (16 points) The obligatory inheritance question. Consider the following interfaces and classes. (You can remove this page from the exam for reference if you want to avoid having to flip back and forth while answering the questions on the next page.) public interface Red{ public void turnred(); public interface Blue{ public void turnblue(); public class A implements Red { public void one() { System.out.println( "A.1" ); public void two(){ one(); System.out.println( "A.2" ); public void turnred() { System.out.println("Nooooooooo!!"); public class B extends A implements Red, Blue { public void one( int x){ System.out.println( "B.1 " + x ); public void two() { System.out.println( "B.2" ); public void turnblue() { System.out.println("Aaaiiiiiiiieeeeeee!!"); public class C extends A { public void one() { System.out.println( "C.1" );

CSE 143 Au04 Midterm 1 Page 3 of 9 Question 3. (cont.) For each of the following, indicate what output is produced when the code is executed. If some sort of error occurs instead, give a brief explanation of the problem. (a) A a = new B(); a.two(); Prints B.2 (b) B b = new B(); b.one(4); Prints B.1 4 b.one(); Prints A.1 (c) Red r = new B(); r.turnred(); Prints Nooooooooo!! (d) Blue b = new Blue(); Error can t create instances of interfaces b.turnblue(); (e) C c = new A(); Error incompatible types in assignment c.two(); (f) A a = new B(); a.turnblue(); Error no turnblue method in class A (g) A a = new C(); a.two(); Prints C.1 A.2 (h) One of your colleagues wants to extend the class hierarchy to create a new class that combines the features of both class B and class C and adds a new method. Here s the code: public class D extends B, C { public void three() { System.out.println("D.3"); Will this work? Why or why not? No, a Java class can only extend one other class, not two.

CSE 143 Au04 Midterm 1 Page 4 of 9 Question 4. (2 points) Here is a small program that uses inheritance. public class Alpha { public void a() { System.out.println("Alpha.a"); public class Beta extends Alpha { public void b() { System.out.println("Beta.b"); Now we d like to add an instance variable to class Alpha along with a constructor to initialize it as follows: public class Alpha { private int n; public Alpha(int initialvalue) { n = initialvalue; public void a() { System.out.println("Alpha.a"); Unfortunately, after this change, these classes won t compile without errors. Why not? Class Beta does not contain a constructor so a default constructor is assumed which contains a call to super(). This works with the original version of Alpha, which contains no explicit constructor definitions, but once we add an explicit constructor to Alpha, the zero-argument constructor is no longer automatically supplied. So the call to super() in Beta fails because no zero-argument constructor is available.

CSE 143 Au04 Midterm 1 Page 5 of 9 Question 5. (4 points) The example code that uses Swing event handling to react to things like mouse and button events usually starts with the following lines of code: import java.awt.*; import java.awt.event.*; import javax.swing.*; (a) If we change the first line to import java.awt.color; will the code continue to compile with no other changes? If no, why not; if the answer is maybe or it depends, describe when the change would be ok and when it would cause a problem. This will work if Color is the only class from java.awt that is referenced in the code. If any other classes from java.awt are used, the program will not compile. (b) Suppose we leave the first line unchanged as import java.awt.*; In that case, can t we delete the second line that says import java.awt.event.*;? It looks like it is not needed. Is it? If so, why? The second import is needed if anything in java.awt.event is used in the code. The import java.awt.*; statement only imports things declared directly in java.awt, not things declared in subpackages. Question 6. (4 points) Suppose we re programming a computer solitaire (card) game using a Model-View-Controller design. For each of the following, identify which MVC component should implement the appropriate action by writing M, V, or C. C React to user mouse clicks on the screen M Determine if a move the user is trying to make is legal M Shuffle the card deck (i.e., randomize the order of the cards) V Show an animation of the deck being shuffled

CSE 143 Au04 Midterm 1 Page 6 of 9 The rest of the questions involve a simple version of the dinosaur world. The code describing the key parts of the simulation appears on this page and the next. You can remove these pages for reference if you want. public interface SimThing { public void action(); import java.util.*; public class SimModel { /** Add a new SimThing t to this model */ public void add(simthing t) {... /** Return a list of all SimThings currently in the model */ public List getthings() {... // rest omitted... The Food interface and simple Plant and Dinosaur classes are defined as follows: public interface Food { public double getcalories(); public class Plant implements Food, SimThing { private double x, y; // meters from the center of the safari private double calories; // energy value of this plant public Plant(double x, double y, double calories) { this.x = x; this.y = y; this.calories = calories; public double getx() { return x; public double gety() { return y; public double getcalories() { return calories; public void action() { // plants do nothing

CSE 143 Au04 Midterm 1 Page 7 of 9 public class Dinosaur implements Food, SimThing { private double x, y; // meters from the center of the safari private double calories; // current energy of this dinosaur; private SimModel world; // the model this dinosaur belongs to public Dinosaur(double x, double y, SimModel m) { this.x = x; this.y = y; this.calories = 0.0; this.world = m; public double getx() { return x; public double gety() { return y; public double getcalories() { return calories; public void eat(food lunch) { calories = calories + lunch.getcalories(); public void action() { // to be supplied /** return the distance from point (x1,y1) to point (x2,y2) */ public double distance(double x1, double y1, double x2, double y2) { double dx = x2 x1; double dy = y2 y1; return Math.sqrt(dx*dx + dy*dy); Notice that Dinosaurs and Plants are almost exactly the same, except that the number of calories in a Plant never changes, regardless of how many times it is eaten or how often its action() method is called, and a Plant doesn t eat anything.

CSE 143 Au04 Midterm 1 Page 8 of 9 Question 7. (8 points) Provide an implementation of the action() method for a Dinosaur object so that it eats all of the Plant objects within 100 meters of its current position. It should not eat other Dinosaurs, and it does not change the Plant objects that it eats. Notice that a Dinosaur has an instance variable that is a reference to the SimModel controlling the simulation, and you can get a list of all the SimThings in the simulation from there. Also, a distance method is provided that you can use to calculate the distance between two (x,y) points. For full credit, you must use an iterator to process the list of SimThings, not method get(i). Hint: you may not need all of the available space /** Perform this Dinosaur s action eat all Plants within * 100 meters */ public void action() { List things = world.getthings(); Iterator it = things.iterator(); while (it.hasnext()) { Object thing = it.next(); if (thing instanceof Plant) { Plant food = (Plant) thing; if (distance(x, y, food.getx(), food.gety()) <= 100.0) { eat(food);

CSE 143 Au04 Midterm 1 Page 9 of 9 Question 8. (16 points) Add tests to the following JUnit framework to test the following two things about your Dinosaur class: (i) Verify that the eat(food lunch) method properly updates the Dinosaur s energy level, and (ii) Verify that the action() method as described in the previous question properly eats all Plants, and only Plants, within 100 meters of the Dinosaur s location. You may include additional instance variables and methods (like setup) if you d like, but this is not required. import junit.framework.testcase; public class DinosaurTest extends TestCase { public void testeat() { Dinosaur dino = new Dinosaur(0.0, 0.0, null); double oldcalories = dino.getcalories(); Plant lunch = new Plant(0.0, 0.0, 100.0); dino.eat(lunch); assertequals(100.0, dino.getcalories() oldcalories, 0.0); public void testaction() { SimModel world = new SimModel(); world.add(new Plant(10.0, 10.0, 1.0); world.add(new Plant(200.0, 200.0, 2.0); Dinosaur fred = new Dinosaur(20.0, 20.0, world); fred.eat(new Plant(0.0, 0.0, 4.0)); world.add(fred); Dinosaur dino = new Dinosaur(0.0, 0.0, world); double oldcalories = dino.getcalories(); dino.action(); assertequals(1.0, dino.getcalories() oldcalories, 0.0); There are obviously a lot of different ways to test these methods. If your code did an adequate job it received the credit, even if it didn t look like the above code. A couple of specific notes: This version of testeat() verifies the difference between the dinosaur s calories before and after eating something so that it would continue to work even if the initial value of a dinosaur s calories was not 0.0. But it s ok to assume that a dinosaur has 0.0 calories when it is created. A minimal test for the action() method would be to create a model with at least one plant that is within range ( 100.0 meters), a plant that is out of range, and one dinosaur that is within range (and shouldn t be eaten). The calorie values for the different objects should be non-zero and different enough from each other so that the calories of the things eaten can t accidentally add up to the expected value.