CSE 142 Su 02 Homework 4

Similar documents
Interfaces. CSE 142, Summer 2002 Computer Programming 1.

Interfaces. Readings and References. Recall the discussion of ArrayLists. How can we manage lists of objects? Reading

ArrayLists. Readings and References. Collections in the Real World. How can we manage lists of objects? Reading. Other References

ArrayLists. CSE 142, Summer 2002 Computer Programming 1.

Express Yourself. Writing Your Own Classes

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Inheritance. CSE 142, Summer 2002 Computer Programming 1.

CSE 142 Sp02 Final Exam Version A Page 1 of 14

Com S 227 Spring 2018 Assignment points Due Date: Thursday, September 27, 11:59 pm (midnight) "Late" deadline: Friday, September 28, 11:59 pm

CSE 113 A. Announcements - Lab

Data Structures and Other Objects Using C++

CISC 1600 Lecture 3.1 Introduction to Processing

CS 170 Java Programming 1. Week 12: Creating Your Own Types

Summary. Recursion. Overall Assignment Description. Part 1: Recursively Searching Files and Directories

Exceptions. When do we need exception mechanism? When and why use exceptions? The purpose of exceptions

CS 134 Programming Exercise 3:

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

Fall 2013 Program/Homework Assignment #2 (100 points) -(Corrected Version)

CMSC 131A, Midterm 1 (Practice) Fall 2017

Load your files from the end of Lab A, since these will be your starting point.

CS 139 Practice Midterm Questions #2

Practice Midterm Examination #1

ISY00245 Principles of Programming. Module 7

Lines of Symmetry. Grade 3. Amy Hahn. Education 334: MW 8 9:20 a.m.

Class Libraries. Readings and References. Java fundamentals. Java class libraries and data structures. Reading. Other References

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

Be sure check the official clarification thread for corrections or updates to this document or to the distributed code.

Last class. -More on polymorphism -super -Introduction to interfaces

61A Lecture 3. Friday, September 5

CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017

Variables One More (but not the last) Time with feeling

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

2. Click on the Launch button and select Terminal. A window will appear which looks like:

CMSC 131A, Midterm 1 (Practice) SOLUTION. Fall 2017

Assignment 5: Part 1 (COMPLETE) Sprites on a Plane

Tips from the experts: How to waste a lot of time on this assignment

1

Chapter 4 Defining Classes I

Operating Systems (234123) Spring (Homework 3 Wet) Homework 3 Wet

Unit 1, Lesson 1: Moving in the Plane

4. Write sets of directions for how to check for direct variation. How to check for direct variation by analyzing the graph :

CS 2110 Fall Instructions. 1 Installing the code. Homework 4 Paint Program. 0.1 Grading, Partners, Academic Integrity, Help

6.001 Notes: Section 17.5

Drawing Geometrical Objects. Graphic courtesy of Eric Roberts

ECE 122. Engineering Problem Solving with Java

Direct Variations DIRECT AND INVERSE VARIATIONS 19. Name

Introduction to Java Unit 1. Using BlueJ to Write Programs

EXAM Computer Science 1 Part 1

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Turn in a printout of your code exercises stapled to your answers to the written exercises by 2:10 PM on Tuesday, January 18th.

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

Skill 1: Multiplying Polynomials

[ the academy_of_code] Senior Beginners

Lab 8. An Introduction to Objects

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Animations involving numbers

Introduction to Programming System Design CSCI 455x (4 Units)

To help you prepare for Problem 2, you are to write a simple Swing application which uses an anonymous inner class to control the application.

CS Problem Solving and Object-Oriented Programming

Programming Assignment 4 ( 100 Points )

Program 12 - Spring 2018

Example Program. public class ComputeArea {

CSE 131 Introduction to Computer Science Fall Exam II

TABLE OF CONTENTS. Worksheets Lesson 1 Worksheet Introduction to Geometry 41 Lesson 2 Worksheet Naming Plane and Solid Shapes.. 44

Programming Languages and Techniques (CIS120)

2. [20] Suppose we start declaring a Rectangle class as follows:

CMSC 201 Spring 2017 Homework 4 Lists (and Loops and Strings)

4. Java Project Design, Input Methods

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

Lesson 18: Animation. Computer Programming is Fun!

APCS Semester #1 Final Exam Practice Problems

Chapter 1 Getting Started

CS 106 Winter Lab 05: User Interfaces

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

INF 111 / CSE 121. Homework 3: Code Reading

CMSC 201 Fall 2016 Homework 6 Functions

CS108, Stanford Handout #3. HW1 CodeCamp

CS212 - COMPUTATIONAL STRUCTURES AND ALGORITHMS

COMP 105 Homework: Type Systems

Advanced Computer Programming

Programming Assignment 2 (PA2) - DraggingEmoji & ShortLongWords

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

Programming Exercise 7: Static Methods

ASSIGNMENT 4 Classes and Objects

CS 1301 Homework Robot Web Writer

Slide 1 CS 170 Java Programming 1

CS 351: Design of Large Programs.

Our second exam is Thursday, November 10. Note that it will not be possible to get all the homework submissions graded before the exam.

Practice Midterm Exam #1

Code Listing H-1. (Car.java) H-2 Appendix H Packages

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

CSE115 Lab 4 Fall 2016

Garbage Collection (1)

COMP102: Test 1 Model Solutions

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

CPS122 - OBJECT-ORIENTED SOFTWARE DEVELOPMENT

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

If the ball goes off either the right or left edge, turn the ball around. If x is greater than width or if x is less than zero, reverse speed.

Homework 09. Collecting Beepers

Lecture 10. Instructor: Craig Duckett

Transcription:

CSE 142 - Su 02 Homework 4 Assigned: Wednesday, July 17 Due: Wednesday, July 24, BEFORE MIDNIGHT ** General Comments about the Homework ** All homework is turned in electronically. Go to the class web site and use the link on the homework page to do the turnin. Don't be late! Late homeworks will not be accepted. This homework assignment has three parts: Practice Problems, Graded Problems and a Programming Project. Page 1 of 7

** Homework 4 Practice Problems ** Do not turn in the Practice Problems. 1. Write a method that will copy the objects stored in an ArrayList into a new list and return the new list. There are several ways to accomplish this task. The method header in each case is the same, but the implementation of the body is different. public ArrayList copylist(arraylist source) {... method body... a. Provide an implementation that creates a new ArrayList object, then copies each element from the source list to the new list using a loop, and then returns the new list to the caller. b. Look at the documentation for ArrayList in the java.util package. There is a constructor that takes a Collection and returns a new ArrayList containing all the elements of the original Collection. Since an ArrayList is a Collection (it implements the Collection interface) you can use this constructor to build a new ArrayList that is a copy of original list. Provide an implementation that does this and returns the new ArrayList to the caller. (It can be done in one line). 2. Write a method that will copy the objects stored in an ArrayList into a new list in reverse order and then return the new list to the caller. There are several ways to accomplish this task. The method header is: public ArrayList reverselist(arraylist source) {... method body... 3. When you have finished the requirements for the programming problem below, feel free to extend it. Take a look at class Polygon in the uwcse.graphics package. You can define arbitrarily complex outlines with that class instead of the simple Rectangles and Ovals. In the example I showed in class, the Car makes a noise when it is clicked on and when it hits a window edge. These are not required features. There is example code in class ShapeSlider that shows how to load and play sound files, if you are interested in this. Note that there are several import statements at the start of the source file that tell Java where to find all the classes it needs to deal with sounds. ShapeSlider also is an example of how to load and display images. There is an ImageShape class which you can use just like all the other Shapes (Oval, Line, etc). Images are not a required feature for this homework. Describe your extensions in extensions.txt if you make any changes beyond those called out in the requirements below. Page 2 of 7

** Homework 4 Graded Problems ** Turn in your answers to the graded problems in the text file answers.txt. Each of the following 5 questions is worth 2 points. 1. The midterm on Friday July 26 and the review session on Wednesday July 24th will NOT be in our regular classroom. a. What building and what room will we use for the midterm and the review session? b. Do you know where that building is located and how to find the room? 2. You have been working with classes that implement the Actor interface, which is defined in the uwcse animation package. There is another interface named Prop defined in that package too. This is the code for the Actor interface: package uwcse.animation; import uwcse.graphics.*; public interface Actor { /** Every Actor must implement some fundamental action. @param stage the stage on which the actor is performing. */ public void doaction(stage stage); /** Every Actor must be able to draw itself on a GWindow. */ public void addto(gwindow w); /** Every Actor must be able to remove itself from its GWindow. */ public void removefromwindow(); a. Refer to the documentation for Prop in the uwcse animation package. What are the methods that are defined in the Prop interface? b. Notice that there is considerable overlap between the Prop interface methods and the Actor interface methods. Rewrite the Actor interface so that it extends the Prop interface and does not re-specify the methods that are in both interfaces. (Don't worry about the package statement and import statements, just include them in the rewritten code exactly the way they are shown above.) 3. Assume we have a Student class which has the following two methods: public int getid() : Returns the student's ID number. public String getname() : Returns the student's name. We also have the following (rudimentary) QuizSection class definition. public class QuizSection { /** Construct a new QuizSection object. */ public QuizSection() { roster = new ArrayList(); /** store the currently registered students */ ArrayList roster; Page 3 of 7

a. Write a new method for the QuizSection class called addstudent that takes a Student object as a parameter and adds it to the roster. Be sure to include the javadoc comments that describe the method and its parameter. addstudent does not return a value. b. Write a new method called isenrolled which takes a student's ID as a parameter and returns true or false, depending on whether the student is on the roster. Be sure to include the javadoc comments that describe the method, its parameter, and the return value. 4. We have talked about the ArrayList class. It is one of several classes that implements the List interface. Look at the documentation for the List interface (it is part of the Java package java.util). Notice that in addition to the add(object o) method that I have talked about in class, there is another version of the add method defined that has different parameter variables: "void add(int index, Object element)". Write a new method called addstudentatfront for the QuizSection class in question 3 that takes a Student object as a parameter and adds it to the roster at the front of the list. Be sure to include the javadoc comments that describe the method and its parameter. addstudentatfront does not return a value. 5. Use the Java library documentation to answer the following questions. a. What methods are guaranteed to be implemented by any class that implements the Comparable interface defined in the java.lang package? b. What methods are guaranteed to be implemented by any class that implements the FilenameFilter interface defined in the java.io package? Page 4 of 7

** Homework 4 Programming Project ** In this project, you will do two software upgrades: 1. Change the Car class to display a multi-shape Car instead of the simple Rectangle it uses now. For this, you will use ArrayLists to keep track of all the Car parts. 2. You will upgrade the existing Car class from its current implementation as a plain Actor to a ClickableActor that recognizes when it is being clicked and reverses direction as a result. ++ List of classes ++ As provided to you, homework 4 is a complete and running program. The overall concept of the program is that it builds a set of Actors which can draw images on the graphics window, and then it loops continuously and calls the doaction() method of each Actor every time the clock ticks. This homework is a little different from earlier homeworks in that the program will loop forever until you click the green box in the upper left corner. The files involved in this program are the following. * Property files in subdirectory "cast" * As in homework 3, this directory holds one file for each element in your picture. See the homework 3 writeup for details on how the files are used, including directions on how to open them with a text editor. * Car.java * This is the class that you will modify. As delivered to you, there is one constructor and three methods. You should have a copy of Car.java in front of you while you read this description. The constructor is called when an object of this class is first created. The constructor gets values from the parameter variable pmap exactly the same way as OvalSlider did in homework 3. Refer to homework 3 for details, or read the existing code. Right now, the constructor just builds a Car shape that is a single Rectangle. Part of your job, described below, is to implement a fancier, multi-shape Car and use an ArrayList to keep track of all the Shapes that are part of the image. The doaction(stage stage) method is called by the animation framework over and over to implement the behavior of your Actors. Each time it is called, you can move the Car a little bit and do whatever else you have defined for this vehicle. Right now, the doaction method moves the Shape a little bit each clock tick, and bounces it off the walls of the window frame when it reaches an edge. There are several changes to be made to the doaction method. They are described below. The addto(gwindow w) method is called by the animation framework early on when the Actor is first created. You need to modify this method to add all the new Shapes. Page 5 of 7

The removefromwindow() method is called by the animation framework to remove an Actor from the stage. You need to modify this method to remove all the new Shapes. * Director.java * This class contains the main method that is used to start the program running. In BlueJ, you can right-click on the class definition square for Director, and then select "void main(arg)" It will ask you for an argument to pass to main. This argument is the name of the cast directory and it defaults to "cast". You can just click okay, and the program will start running. The main method creates a new Cast, Stage, and AssistantDirector, then asks the AssistantDirector to run the performance. You don't need to make any changes to the Director class. * the skel directory * This directory contains all the utility classes for this homework. You can look at this code for interest and information, but you do not need to change it for this assignment. ++ Project Requirements ++ 1. Download the cse142-hw4.zip file and unzip it. The project skeleton is in the directory hw142\hw4. Your task is to open the project using BlueJ and modify Car to meet the requirements below. 2. Start BlueJ and open the hw4 project. Open the Car source file and read it. You can see that it will compile and run, but it doesn't implement all the capabilities that are needed. 3. I have given you examples of the way to implement each of the required features. The use of ArrayLists was demonstrated in the upgraded PetSet that I gave you in class on Monday July 15. An implementation of a ClickableActor is provided in class ShapeSlider, included in homework 4. 4. The function of an Actor class like Car is to create and then control one or more Shapes that are shown in the graphics window. A basic Car is already implemented; your job is to implement more features as described below. 4a. Change the Car design so that the image is built from multiple Shapes. In the code provided to you, the Car is just one plain Rectangle. You should add details to provide a more exciting driving experience. Two wheels at minimum should be added to the Car's image. You should define an instance variable that is of type ArrayList. This will be the container for the Shapes that you create to be the car's image on screen, and will replace the variable "Shape blob" that is used to hold the simple Rectangle in the provided code. In the constructor, you should create new Shapes representing the parts of the car. At minimum, the car should have a body (represented by a Rectangle or other suitable shape) and two wheels (represented by Ovals). You should add all of the car part Shapes to the ArrayList you created earlier. You should pay attention to how you calculate the dimensions of the various parts of the car. The prop files can specify width and height values, and your Page 6 of 7

constructor should use these values to proportion the parts so that a car can be constructed in different sizes and still look okay. In doaction, you need to change the code so that instead of just moving a single Rectangle Shape, it loops through the entire ArrayList and does a moveby call for each of the Shapes in your Car. In addto, you need to modify the code to add all the Shapes in the fancy car, rather than just the single Rectangle that it has now. In removefromwindow, you need to modify the code to remove all the Shapes in the fancy car, rather than just the single Rectangle that it has now. When this is working correctly, each of the Cars will bounce off the sides of the window frame and all the parts will move together. Remember, you must use an ArrayList to store all the various Car parts. Although it would be possible to just define more variables like you did when you added a third pet to PetSet, we now have a better way and we need to use it. So define and use an ArrayList. 4b. The second major change is to upgrade the Car so that it is a ClickableActor, not just an Actor. A ClickableActor can take special action when the user clicks on it. The ClickableActor interface extends the Actor interface, and is defined in the file skel\clickableactor.java. In order to implement this, you need to make several changes. The line "public class Car implements Actor" should be changed to "implements ClickableActor". In the constructor, no changes are needed for the clickability. In doaction, no changes are needed for the clickability. You need to add two new methods in order to implement the new behavior called for by the ClickableActor interface. The method "boolean intersects(shape other)" is called to decide if some Shape intersects any of the Shapes that this object is displaying. Since your car object is the only object that knows what Shapes it is displaying, it is the only object that can answer this question. So you must implement this method so that it loops through all of the Shapes that are being used to represent the Car, and uses the intersects(other) method defined for all Shapes to decide if there is an intersection. If there is, this method returns "true", if not, it returns "false". Somewhere in this method you will have a loop containing a statement like this: if (((Shape)myShapes.get(i)).intersects(other)) {... The method "void doclickaction(stage stage)" is called whenever it is decided that your Car is the object that was clicked on. This method should implement a direction reversal immediately. The car goes the opposite direction from its current direction, just as though it had run into the edge of the window at the moment you clicked on it. Note that you don't actually move anything in this method, you just change the deltax and deltay values so that the next time doaction is called, the Car moves in the opposite direction. Page 7 of 7