To gain experience using recursion and recursive data structures.

Similar documents
CS 134 Programming Exercise 7:

Programming Exercise. Scribbler

To gain experience using GUI components and listeners.

To gain experience using arrays, manipulating image data, and using inheritance.

To gain experience using arrays, manipulating image data, and using inheritance.

You Can Make a Difference! Due April 11/12 (Implementation plans due in class on 4/9)

CS 51 Laboratory # 12

CS 134 Programming Exercise 2:

CS 134 Programming Exercise 3:

CS 134 Programming Exercise 1:

Lab 3. A Multi-Message Reader

1. Write code to answer each of the following questions. For this question, it is not necessary for you to define constants or write comments.

Programming Project 1

CS 134 Programming Exercise 9:

CS 051 Homework Laboratory #2

Lecture 15 CSE11 Fall 2013 Arrays

Lab 5 Classy Chat. XMPP Client Implementation --- Part 2 Due Oct. 19 at 11PM

Agenda. Container and Component

Scope. Dragging Algorithm Revisited. Tuesday, October 28, October 28, 2008

CS 134 Midterm Fall 2012

CSCI 204 Introduction to Computer Science II

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

Java Programming Lecture 6

Topic Notes: Java and Objectdraw Basics

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

Readings for This Lecture

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

CS 201 Advanced Object-Oriented Programming Lab 5 - Sudoku, Part 1 Due: March 3/4, 11:30 PM

Programming Languages and Techniques (CIS120)

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

Lab 5 TOC to Me. Class Structure. IM Client Implementation --- Part 2 Due: 23:00 Monday 13 October. CS 150, Profs. Lawson and Szajda Fall 2008

CS 170 Java Programming 1. Week 10: Loops and Arrays

CS Programming Exercise:

CMPSCI 187 / Spring 2015 Hangman

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

CMSC 150 Lab 8, Part II: Little PhotoShop of Horrors, Part Deux 10 Nov 2015

Lab 5 TOC to Me. IM Client Implementation --- Part 2

CSE115 Lab 4 Fall 2016

CS 106 Winter Lab 05: User Interfaces

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

CMPSCI 187 / Spring 2015 Implementing Sets Using Linked Lists

Example: Building a Java GUI

Java Classes (Java: An Eventful Approach, Ch. 6),

Example: Building a Java GUI

Programming Assignment 7 (100. Points)

Class 16: The Swing Event Model

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

Programming Languages and Techniques (CIS120)

CS Problem Solving and Object-Oriented Programming

Using Blackboard Drive to upload/manage content in Blackboard Learn

CSE115 Lab 9 Fall 2016

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

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

RECURSION (CONTINUED)

Part I: Learn Common Graphics Components

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

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CS2 Practical 1 CS2A 22/09/2004

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

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

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

CS Exam 3 - Spring 2010

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Programming Assignment 2 (PA2) - DraggingEmoji & ShortLongWords

Assignment 7: Due Wednesday May 11 at 6pm UPDATES on Monday May 9

Human-Computer Interaction IS4300

CS2 Practical 2 CS2Ah

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

Window Interfaces Using Swing. Chapter 12

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

CS180 Recitation. More about Objects and Methods

CMPSCI 187 / Spring 2015 Hanoi

Introduction. Introduction

Announcements for the Class

Lab 1: Silver Dollar Game 1 CSCI 2101B Fall 2018

Express Yourself. Writing Your Own Classes

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

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

PREPARING FOR PRELIM 1

CS 10, Fall 2015, Professor Prasad Jayanti

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

Midterm Exam 2 CS 455, Spring 2011

Phase 2: Due 11/28/18: Additional code to add, move and remove vertices in response to user mouse clicks on the screen.

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!

Initial Coding Guidelines

[ 8 marks ] Demonstration of Programming Concepts

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class

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

CoSc Lab # 6 (The Model) Due Date: Part I, Experiment classtime, Tuesday, Nov 6 th, 2018.

CS 209 Spring, 2006 Lab 8: GUI Development Instructor: J.G. Neal

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

ECSE-323 Digital System Design. Lab #1 Using the Altera Quartus II Software Fall 2008

CoSc Lab # 5 (The Controller)

Lecture 9. Lecture

Assignment 2. Application Development

Programming Languages and Techniques (CIS120)

CS 455 Final Exam Fall 2016 [Bono] December 13, 2016

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

Transcription:

Lab 6 Handout 8 CSCI 134: Fall, 2017 Scribbler Objective To gain experience using recursion and recursive data structures. Note 1: You may work with a partner on this lab. If you do, please turn in only one copy of your completed program. Note 2: Be sure to select Show Terminal from BlueJ s View menu. This will help you identify run-time errors in your program. Note 3: After you have downloaded the starter code, be sure to open the project from within BlueJ. Do not open the project by clicking on bluej.pkg. The Scenario This week, you will implement a program we call Scribbler. You have seen a simple version of this program in class as a demo. In lab, we would like you to extend the program from class to include the ability to move or color any of the scribbles that have been drawn rather than just the last one. We encourage you to come to lab with a design for steps 1 and 2 in the How to Proceed section, but it is not required. See the handouts web page to obtain a copy of the starter folder. There is also a working version of the Scribbler on the handouts page. You may want to visit the web page after you read the handout so that you can experiment with the program to clarify any details that are not clear to you. The Scribbler program has three modes: Draw mode, Move mode, and Color mode. The program starts in Draw mode. Draw mode, Move mode, and Color mode are selected by pressing buttons, and the color used by Color mode is selected by choosing a color from a JComboBox filled with color names. The modes behave as follows: 1

Draw mode: Drag to draw a new scribble on the canvas. Move mode: Reposition a scribble by pressing the mouse on it and dragging. Color mode: Click on any scribble to change its color to the color selected in the color menu. The program also has an Erase Last button that will erase the most recently drawn scribble. Pressing the button repeatedly will erase scribbles in the reverse order of which they were drawn. How to Proceed The starter for this lab is a working Scribbler, but it has only the Draw mode and a simplified Move mode that is capable of moving only the most recently drawn scribble. You will need to add code that will manage your scribbles to allow the various modes and the Erase last button to work correctly. There are a number of step-by-step approaches you could take to solve this problem, but it is important that you have a plan, and that you add functionality one step at a time. Here is one possible ordering of the tasks. We recommend that you develop and test your program incrementally make sure you have a working implementation at each step before moving on to the next. 1. Implement a simplified Color mode. This is similar to Move mode, except that you set the color of the scribble that contains the mouse point instead of moving it. Among other things, you will have to add a setcolor method to the ScribbleInterface interface, and the NonEmptyScribble and EmptyScribble classes. Initially, you will only be able to set the color of the most recently drawn scribble. 2. Implement a simplified Erase mode. Here, you respond to the Erase last button s actionperformed event by deleting the most recently drawn scribble from the canvas. For now, you will only be able to erase the most recently drawn scribble. A second button press will do nothing. 3. Since you need to keep track of a number of scribbles, you will need to define classes representing a collection of scribbles. The interface for these classes will be called ScribbleCollectionInterface, and the classes implementing that interface will be named EmptyScribbleCollection and NonEmptyScribbleCollection. The interface and classes will be similar to the stacks used in WoodStack and ball lists in the ChainReaction. Just as we do not know how many line segments will make up a scribble when we start to draw one, we will not know how many scribbles will be drawn and stored in a scribble collection. In your Scribbler class you will need a variable with type ScribbleCollectionInterface that will be initialized with an object created from EmptyScribbleCollection. You will add new scribbles to it using the constructor from NonEmptyScribbleCollection as they are drawn. Consider carefully what methods your scribble collection needs in order to support the functionality of the four modes. We have provided the skeleton of a ScribbleCollectionInterface interface, and NonEmptyScribbleCollection and EmptyScribbleCollection classes. Draw mode needs to add a new scribble to the scribble collection after the dragging is done. The Erase last button needs to remove the most recently drawn scribble from the canvas and remove it from the scribble collection. This means there is now a new most recently drawn scribble, and a second press would remove that one, and so on. Be careful in the case when there are no scribbles left. Move mode and Color mode need to determine if a mouse click takes place on any of the scribbles on the canvas, not just the most recently drawn. This requires that your scribble collection classes be able to search through all of the scribbles until either a scribble is located that contains the click point, or it is determined that none of the existing scribbles contain the point. Once you have determined which scribble contains the click point, you can move or color that scribble as you did in the simplified versions of these modes. 2

Submitting Your Work Once you have saved your work in BlueJ, please perform the following steps to submit your assignment: First, return to the Finder. You can do this by clicking on the smiling Macintosh icon in your dock. From the Go menu at the top of the screen, select Connect to Server.... For the server address, type in afp://guest@fuji and click Connect. A dialog box will pop up. Connect as a guest by selecting the Guest option. A selection box should appear. Select Courses and click OK. You should now see a Finder window with a cs134 folder. Open this folder by double clicking on it. You should now see the drop-off folders for the three lab sections. Drag your Lab6IDnnnn folder into the appropriate lab section drop-off folder. When you do this, the Mac will warn you that you will not be able to look at this folder. That is fine. Just click OK. Log off of the computer before you leave. You can submit your work up to 11 p.m. on Wednesday if you re in the Monday afternoon lab; up to 6 p.m. on Thursday if you re in the Monday night lab; and up to 9 p.m. on Thursday if you re in the Tuesday morning lab. If you submit and later discover that your submission was flawed, you can submit again. We will grade the latest submission made before your lab deadline. The Mac will not let you submit again unless you change the name of your folder slightly. It does this to prevent another student from accidentally overwriting one of your submissions. Just add something to the folder name (like the word revised ) and the re-submission will work fine. Grading Guidelines As always, we will evaluate your program for both style and correctness. Here are some specific items to keep in mind and focus on while writing your program: Style Descriptive comments Good names Good use of constants, no under- or over-use Appropriate formatting Design Good use of boolean expressions and conditionals, avoid redundancy Not doing more work than necessary Appropriate methods in NonEmptyScribbleCollection Appropriate recursive structure in NonEmptyScribbleCollection Sparing use of null (can you avoid using null entirely?) Correctness Switching among modes correctly Draw mode adds correctly to the scribble collection Move mode works correctly Color mode works correctly Erase button works correctly No NullPointerExceptions (check via Terminal window) 3

Sketch of classes provided for this program We are providing several classes and interfaces to help you get started. We include here printouts of the main program and the classes and the interface for a single scribble. These are very similar to the Scribbler examples from lecture. Here is the code from the basic Scribbler class. You may need to add additional methods to it: * A very simple drawing program. public class Scribbler extends WindowController implements ActionListener { // User modes for what operation is selected. // We are using ints rather than boolean to allow for extension to // other modes private static final int DRAWING = 1; private static final int MOVING = 2; private static final int COLORING = 3; // the current scribble private ScribbleInterface currentscribble; // the collection of scribbles private ScribbleCollectionInterface scribbles; // stores last point for drawing or dragging private Location lastpoint; // whether the most recent scribble has been selected for moving private boolean draggingscribble; // buttons that allow user to select modes private JButton setdraw, setmove, seterase, setcolor; // Choice menu to select color private JComboBox choosecolor; // new color for scribble private Color newcolor; // label indicating current mode private JLabel modelabel; // the current mode -- drawing mode by default private int chosenaction = DRAWING; // create and hook up the user interface components public void begin() { setdraw = new JButton("Draw"); setmove = new JButton("Move"); setcolor = new JButton("Color"); JPanel buttonpanel = new JPanel(); 4

buttonpanel.add(setdraw); buttonpanel.add(setmove); buttonpanel.add(setcolor); choosecolor = new JComboBox(); choosecolor.additem("red"); choosecolor.additem("blue"); choosecolor.additem("green"); choosecolor.additem("yellow"); seterase = new JButton("Erase last"); JPanel choicepanel = new JPanel(); choicepanel.add(seterase); choicepanel.add(choosecolor); JPanel controlpanel = new JPanel(new GridLayout(3,1)); modelabel = new JLabel("Current mode: drawing"); controlpanel.add(modelabel); controlpanel.add(buttonpanel); controlpanel.add(choicepanel); Container contentpane = this.getcontentpane(); contentpane.add(controlpanel, BorderLayout.SOUTH); // add listeners setdraw.addactionlistener(this); setmove.addactionlistener(this); seterase.addactionlistener(this); setcolor.addactionlistener(this); // make the current scribble empty currentscribble = new EmptyScribble(); this.validate(); * If in drawing mode then start with empty scribble. * If in moving mode then prepare to move. public void onmousepress(location point) { if (chosenaction == DRAWING) { // start with an empty scribble for drawing currentscribble = new EmptyScribble(); else if (chosenaction == MOVING) { // check if user clicked on current scribble draggingscribble = currentscribble.contains(point); // remember point of press for drawing or moving lastpoint = point; 5

* If in drawing mode, add a new segment to scribble. * If in moving mode then move it. public void onmousedrag(location point) { if (chosenaction == DRAWING) { // add new line segment to current scribble Line newsegment = new Line(lastPoint, point, canvas); currentscribble = new NonEmptyScribble(newSegment, currentscribble); else if (chosenaction == MOVING) { if (draggingscribble) { // move current scribble currentscribble.move(point.getx() - lastpoint.getx(), point.gety() - lastpoint.gety()); // update for next move or draw lastpoint = point; public void onmouserelease(location point) { // Add code when have collection of scribbles * Set mode according to JButton pressed. public void actionperformed(actionevent e) { if (e.getsource() == setdraw) { chosenaction = DRAWING; modelabel.settext("current mode: drawing"); else if (e.getsource() == setmove) { chosenaction = MOVING; modelabel.settext("current mode: moving"); 6

Here is the starting code for ScribbleInterface: public interface ScribbleInterface { // returns whether point is contained in scribble public boolean contains(location point); // move scribble by dx in x-direction and dy in y-direction public void move(double xoffset, double yoffset); Here is the starting code for EmptyScribble: * Class representing an empty scribble public class EmptyScribble implements ScribbleInterface { public EmptyScribble() { // point is never in an empty scribble! public boolean contains(location point) { return false; // nothing to move, so do nothing! public void move(double xoffset, double yoffset) { 7

Here is the starting code for NonEmptyScribble: public class NonEmptyScribble implements ScribbleInterface { // an edge line of the scribble private Line first; // the rest of the scribble private ScribbleInterface rest; public NonEmptyScribble(Line segment, ScribbleInterface therest) { first = segment; rest = therest; * Returns true iff the scribble contains the point. public boolean contains(location point) { return (first.contains(point) rest.contains(point)); * Move the scribble by xoffset in the x direction * and yoffset in the y direction public void move(double xoffset, double yoffset) { first.move(xoffset, yoffset); rest.move(xoffset, yoffset); The starter code for the collection interface and classes is omitted here. 8