CS 134 Programming Exercise 2:

Similar documents
CS 051 Homework Laboratory #2

CS 134 Programming Exercise 3:

CS 134 Programming Exercise 1:

Topic Notes: Java and Objectdraw Basics

Programming Project 1

To gain experience using GUI components and listeners.

CS Programming Exercise:

CS Problem Solving and Object-Oriented Programming

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

To gain experience using recursion and recursive data structures.

CS112 - INTRODUCTION TO PROGRAMMING. Programming Project #2 - Due Monday, March 10, at the start of class

Unit 21 - Creating a Navigation Bar in Macromedia Fireworks

Using Microsoft Word. Tables

CS 134 Programming Exercise 9:

The Fundamentals. Document Basics

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

CS 134 Programming Exercise 7:

Portrait Editor. to Portrait Flow

FILE ORGANIZATION. GETTING STARTED PAGE 02 Prerequisites What You Will Learn

Creating Vector Shapes Week 2 Assignment 1. Illustrator Defaults

= 3 + (5*4) + (1/2)*(4/2)^2.

CISC 181 Lab 2 (100 pts) Due: March 4 at midnight (This is a two-week lab)

INTRODUCTION... 1 UNDERSTANDING CELLS... 2 CELL CONTENT... 4

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

Programming Assignment 3 ( 100 Points ) START EARLY!

Practice Midterm Examination #1

Using Dreamweaver. 4 Creating a Template. Logo. Page Heading. Home About Us Gallery Ordering Contact Us Links. Page content in this area

In this exercise you will be creating the graphics for the index page of a Website for children about reptiles.

On the Web sun.com/aboutsun/comm_invest STAROFFICE 8 DRAW

This Tutorial is for Word 2007 but 2003 instructions are included in [brackets] after of each step.

Template Tidbits. Q How do I get the places I can enter copy to show up? (Highlight Fields Bar)

Photoshop tutorial: Final Product in Photoshop:

InDesign Part II. Create a Library by selecting File, New, Library. Save the library with a unique file name.

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

Assignment #1: and Karel the Robot Karel problems due: 3:15pm on Friday, October 4th due: 11:59pm on Sunday, October 6th

Graphing on Excel. Open Excel (2013). The first screen you will see looks like this (it varies slightly, depending on the version):

Midterm Exam, October 24th, 2000 Tuesday, October 24th, Human-Computer Interaction IT 113, 2 credits First trimester, both modules 2000/2001

ITEC185. Introduction to Digital Media

Programming Assignment 2 (PA2) - DraggingEmoji & ShortLongWords

CS 134 Test Program #2

Locate it inside of your Class/DreamWeaver folders and open it up.

Programming Assignment 4 ( 100 Points )

Text Generator Due Sunday, September 17, 2017

EXCEL BASICS: MICROSOFT OFFICE 2010

Animations involving numbers

Adobe photoshop Using Masks for Illustration Effects

Binary Trees Due Sunday March 16, 2014

Week 5 Creating a Calendar. About Tables. Making a Calendar From a Table Template. Week 5 Word 2010

Using Flash Animation Basics

Chapter 9 Getting Started with Impress

Read every line of the exam sheet before programming!

Lesson 1 Introduction to PowerPoint

Assignment #1: /Survey and Karel the Robot Karel problems due: 1:30pm on Friday, October 7th

Using Adobe Photoshop

Mehran Sahami Handout #7 CS 106A September 24, 2014

Using Windows 7 Explorer By Len Nasman, Bristol Village Computer Club

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

Using Microsoft Excel

DOING MORE WITH EXCEL: MICROSOFT OFFICE 2013

SNOWFLAKES PHOTO BORDER - PHOTOSHOP CS6 / CC

Lab 3. A Multi-Message Reader

IN DESIGN. A review of the overview

University of California at Berkeley College of Engineering Department of Electrical Engineering and Computer Science

MAKING TABLES WITH WORD BASIC INSTRUCTIONS. Setting the Page Orientation. Inserting the Basic Table. Daily Schedule

University of California at Berkeley College of Engineering Department of Electrical Engineering and Computer Science. EECS 150 Spring 2000

HOW TO. In this section, you will find. miscellaneous handouts that explain. HOW TO do various things.

Classes. Integer Division. Thursday, October 23, March 13, 2008

Impress Guide. Chapter 1 Introducing Impress

Section 5. Pictures. By the end of this Section you should be able to:

Lab 2. CSE 3, Summer 2010 In this lab you will learn about file structures and advanced features of Microsoft Word.

MAPLOGIC CORPORATION. GIS Software Solutions. Getting Started. With MapLogic Layout Manager

Integrating Word with Excel

ADOBE PHOTOSHOP Using Masks for Illustration Effects

Spring CS Homework 3 p. 1. CS Homework 3

ORB Education Quality Teaching Resources

How to Create Greeting Cards using LibreOffice Draw

Excel 2013 Beyond TheBasics

SETTING UP A. chapter

1 Introduction to Using Excel Spreadsheets

EXCEL BASICS: PROJECTS

A QUICK TOUR OF ADOBE ILLUSTRATOR CC (2018 RELEASE)

Windows XP. A Quick Tour of Windows XP Features

Laboratory 1: Eclipse and Karel the Robot

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

CSE 143: Computer Programming II Winter 2019 HW6: AnagramSolver (due Thursday, Feb 28, :30pm)

Introduction to MS Word XP 2002: An Overview

CS Multimedia and Communications REMEMBER TO BRING YOUR MEMORY STICK TO EVERY LAB! Lab 02: Introduction to Photoshop Part 1

Introduction to Powerpoint. Create it. Opening a presentation

Interactive Tourist Map

EXCEL BASICS: MICROSOFT OFFICE 2007

CISC 181 Lab 2 (100 pts) Due: March 7 at midnight (This is a two-week lab)

Using Adobe Photoshop

IGCSE ICT Section 16 Presentation Authoring

Creating Interactive PDF Forms

DOING MORE WITH WORD: MICROSOFT OFFICE 2013

Using Microsoft Excel

HAPPY HOLIDAYS PHOTO BORDER

This Tutorial is for Word 2007 but 2003 instructions are included in [brackets] after of each step.

ADOBE DREAMWEAVER CS4 BASICS

STAROFFICE 8 SUMMARY PROJECT

Transcription:

CS 134 Programming Exercise 2: Dirty Laundry Objective: To gain experience using conditionals. The Scenario. One thing some students have to figure out for the first time when they come to college is how to wash their own clothes. By the time many students have figured it out, all their underwear is pink and T-shirts and other light-colored items are streaked with lots of interesting colors. In the hopes of helping next year s first-year students adjust more easily to college, we would like you to write a laundry sorting simulator. The Approach. It is usually a good practice to develop programs incrementally. Start with a simplified version of the full problem. Plan, implement, and test a program for this simplified version. Then, add more functionality until you have solved the full problem. To encourage this approach, we have divided this lab into two parts. For the first, we will describe a laundry sorter with a very simple interface. You should plan, implement, and test a program for this problem first. Then, in the second part of the assignment we will ask you to add additional functionality. You should approach each of our two parts in this step-wise fashion. For example, in the first part you might begin by just writing the instructions to construct the necessary graphical objects without worrying about any of the mouse event handling. Once your program can draw the picture, you can move on to figure out how to handle events. Part 1: The simulator should begin with three wash baskets on the screen (for our purposes they can just be rectangles or squares). One is labeled whites, one darks, and the last colors. An image showing the kind of display we have in mind appears below. When the simulation begins, a color swatch will appear on the screen. The user ( laundry trainee ) should then click in the corresponding basket. If the user is correct, the program should randomly select a new color for the next item and display it on the screen. For this part of the assignment you may just select among the three colors Color.white, Color.red, and Color.black when creating items of clothing. If the user clicks on an incorrect basket, the original item remains in position for another try. In the version of this handout found on the course web site, the picture of the program window shown above is replaced by a working version of the simple laundry sorter. You may want to visit the web page after you read Part 1 so that you can experiment with the program to clarify any details of the interface that are not clear to you. 1

A Warning! One odd feature of the simple interface that may bother you a bit is a result of the fact that the program selects laundry items randomly. Because the selection is truly random it sometimes picks the same color twice in a row. When this happens and you click on the correct basket for the first item you will get the feeling that the program ignored you. Even though it has actually displayed a new item, the new item looks just like the old one, so you may think nothing changed. Don t let this trick you into thinking that your version of the program (or ours) isn t working correctly. The more advanced interface in Part 2 includes counters in the display that make it clearer whether the user succeeded. Creating a Project For this lab, you will create a project from scratch inside Eclipse: 1. Open the File Menu. Select New and then Project. 2. Select Java, then select Java Project, and then Next. 3. Enter Landry as the Project name. 4. Select the Libraries tab. 5. Click Add variable. 6. Select OBJECTDRAW from the list and click OK. 7. Click Finish. You now have an empty project to which you should add a Java file for your class: 1. Click on Laundry in the Package Explorer panel on the left side of the Eclipse window. 2. Open the File Menu. Select New and then Class. 3. Enter Laundry as the Name of the class. 4. Enter WindowController as the Superclass. 5. Click Finish. 6. You should now have a Laundry.java file inside the Laundry project. Be sure to modify the file so that it starts with import objectdraw.*; Later, when you start working the colors, you will also need to import java.awt.*. Design of Part 1. You will need to design an extension of the WindowController class which will display the wash baskets and the item to be sorted. Begin by laying out where all the items go on some graph paper. (We recommend using a window that has height and width of 400 pixels.) The picture should look more or less like the one above. When the program begins, place all the wash baskets (with labels) on the screen. Then, add the item of clothing that is to be sorted. For simplicity you might as well always make the first item have color white. The item should actually consist of two rectangles, a FilledRect which is the appropriate color and a FramedRect which is the same size, but lays on top of the filled rectangle to form a border (otherwise it will be awfully difficult to see a white item!) 2

Think Constants! When you lay out the wash baskets and item, define constants (private static final...) for all the relevant information. This makes it easier to change the layout and also makes your program much, much easier to read (presuming you give constants good names). Constant names are by convention written with all capital letters and underscores, e.g. THIS IS A CONSTANT. Your constants may be (and often should be) more complex objects like Locations. You can initialize constants with the results of a constructor: private static final Location SOME_LOCN = new Location(100,200); Remember that you may NOT have constants whose definition uses canvas (e.g., no FramedRect constants). The widths and heights of wash baskets and the item to be sorted, coordinates of the upper left corner of each of these, etc., are all good candidates for constants. After writing the code to draw the initial display, it would be good to run it to see if it does what you expect. As with last week, you must create a configuration to run your program. These instructions are basically the same as last week, except that you will use a different name and different dimensions. Open the Run menu. Select Run... Select Java Applet from the Configurations list. Click the New button at the bottom left. Make sure that the Name field at the top, the Project, and the Applet class all say Laundry. Click on the Parameters tab. Enter 400 for the width and 400 for the height. Click the Apply button and then the Run button. Identifying the Correct Basket Once you have done the layout and figured out how to generate new items, all you have to do is to write the code for the method onmouseclick. Because you may be generating the item in one method (begin) and checking to see if the user clicked in the appropriate basket in a different method (the onmouseclick method), you will need to associate some information with an instance variable that will enable onmouseclick to determine which is the correct basket. An appropriate way to do this is to use an instance variable of type FramedRect. When you generate a new item (in either begin or onmouseclick), you will associate the new variable with the rectangle/basket in which an item of its color should be placed. That way when the user clicks on a basket, onmouseclick can simply check to see if the rectangle currently associated with the instance variable contains the point where the mouse was clicked. Then, onmouseclick will either select a new color for the item (if the user was correct) or wait until the user clicks again (if incorrect). Recycling Because your program only uses one laundry item at a time, you might as well just recycle it reusing the same rectangle for each laundry item. Simply change its color rather than creating a new rectangle. In general it is a good strategy to reuse objects rather than creating new ones when possible, as this generally uses less time and does not clutter memory. Picking a random color To pick a random color, use the RandomIntGenerator class as described in the textbook (Section 2.9). Since there are 3 colors to select from, you should create your random number generator to return random numbers in the range of 1 to 3 and associate each of those values with one of the colors the laundry may have (Color.white, Color.black, and Color.red). 3

Part 2: Once you get the basic version working, we would like you to jazz it up a bit. Here are the extensions we would like you to make: 1. Add labels (Text items) at the bottom of the picture showing the number of correct and incorrect placements. This makes it clearer when the student succeeds in placing the item correctly. They should read something like correct = nn, incorrect = mm. The value in the first Text item will be formed by concatenating the string correct = with an integer instance variable which keeps track of the number of correct answers. The other is similar. 2. Users should drag the items to the correct laundry basket rather than just clicking on the basket. Recall from the example in class that you will need an instance variable to label the last mouse position before the drag so you can determine how far to drag the item. If the user presses the mouse button down outside the laundry item, it should not increase the correct or the incorrect counter. 3. Assign the item a randomly generated color by randomly choosing integers rednum, greennum, and bluenum between 0 and 255 for each of the red, blue, and green components of the color. You can create a color from those components by writing new Color(redNum,greenNum,blueNum)). Now define criteria for determining where each color should go. The simplest criterion is based on the sum of the three color components. If the sum of the component numbers is less than 230, decide it is dark, if it is greater than 600, decide it is white. Otherwise it is colored. After you get the program working you might want to experiment with other criteria. We will let you figure out most of the details of how to add the features for the more advanced versions. Note that for the second enhancement drop the onmouseclick method in favor of using the three methods: onmousepress for when the user first clicks on the item - though remember that they might miss. onmousedrag to do the actual dragging. onmouserelease check to see if they ve dropped it in the right place when the mouse is released. In the version of this handout found on the course web site, there is a working version of the final laundry sorter. You may want to visit the web page so that you can experiment with the program to clarify any details of the interface that are not clear to you. Extra credit We would like you to focus on getting the lab working, writing good comments, and using good style in your programming. If you have done all that and would like to do more, here is an opportunity to earn extra credit. As described above, if the user drops their laundry outside of all baskets, it will count as an incorrect sorting. For extra credit, you can be nicer to the user. If he/she misses all the baskets, let him/her try again without increasing either the correct or incorrect counters. Grading guidelines As with last week s lab, your grade will have components based on style and on correctness. Please note that each week we will likely be adding new style guidelines that we will be looking for in your programs. Final remarks Try to complete the basic version of the lab before attempting the more advanced features. Just work on adding one feature at a time, testing each thoroughly before working on the next. Turning in your program Your program is due at 11p.m. on the evening two days after your lab (Wednesday if you are in the Monday lab, Thursday if you are in the Tuesday lab). As last time, you need to export your project before copying it to the dropoff Folder: 4

Table 1: Grading Guidelines Value Feature Style, Design, and Efficiency (10 pts total) 2 pts. appropriate comments 2 pts. good variable names 2 pts. good use of constants 2 pts. appropriate formatting 1 pt. does not generate new objects unnecessarily 1 pts. design issues Correctness (10 pts) 2 pts. generates a new color swatch only if previous one placed correctly 2 pts. swatch displayed in the correct initial position; returns to original location if incorrectly sorted 2 pts. updates # correct and # incorrect appropriately 2 pts. drags swatch properly (-1 pt if use clicking instead of dragging) 2 pts. appropriate behavior if user does unexpected things like starting to drag outside the laundry item Extra credit (1 pt) 1 pt. does not update # correct or # incorrect sorted if user misses all baskets. First, return to Eclipse and make sure you included your name and lab in a comment at the start of the program. Next, click on Laundry in the Package Explorer panel on the left side of the Eclipse window. Now, select Export from the File menu. Select File system in the dialog box and click next. Make sure there is a checkmark (not just a line) in the box next to Laundry in the left list. Click the Browse button. Open the menu next to From and select Home. Click the New Folder button. A good folder name is one that identifies you and the lab you are working on, such as JaneDoeLab2 or JaneDoeLaundry, replacing JaneDoe with your name. Click the Create button. Then click the Choose button. Then click Finish. Quit Eclipse. Open a Finder window and go to your Home. You should see the folder you just created. Open a second Finder window by double-clicking the icon near the right side of the dock that looks like the western hemisphere on top of a disk drive. This will open a folder on the cortland file server. Double-click the cs134 folder nested within the window. Within the cs134 folder you should see several Dropoff folders. There is one for each lab section. Drag your new folder into the dropoff folder for your lab. 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. Good luck and have fun! 5