CSE115 Lab 4 Fall 2016

Similar documents
CSE115 Lab 2 Fall 2016

CSE115 Lab 9 Fall 2016

Basic Input and Output

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

CS 134 Programming Exercise 7:

CSCI 1301: Introduction to Computing and Programming Spring 2019 Lab 10 Classes and Methods

CS Programming Exercise:

Announcements - Grades UBLearns grades just updated Monday, March 28 th (11:00am). Please check grades.

1. Complete these exercises to practice creating user functions in small sketches.

CS 134 Programming Exercise 3:

Basic Input and Output

Chapter 7 Applets. Answers

CS 134 Programming Exercise 1:

EECS 1001 and EECS 1030M, lab 01 conflict

CISC 1600 Lecture 3.1 Introduction to Processing

CS Problem Solving and Object-Oriented Programming

AP CS Unit 12: Drawing and Mouse Events

Topic Notes: Java and Objectdraw Basics

Class Meeting 05 (Lecture 04) Objectives for this class meeting. Conduct vote on basic style of game for class project

Recommended Group Brainstorm (NO computers during this time)

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

Slide 1 CS 170 Java Programming 1

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

Programming Assignment 4 ( 100 Points )

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

Basic Input and Output

To gain experience using GUI components and listeners.

CSE 113 A. Announcements - Lab

Assignment 3: Inheritance

Programming Assignment 2 ( 100 Points )

Programming Assignment 1: CS11TurtleGraphics

Programming Assignment 2 (PA2) - DraggingEmoji & ShortLongWords

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

1 Getting started with Processing

Basic Input and Output

Using the API: Introductory Graphics Java Programming 1 Lesson 8

More about GUIs GEEN163

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

PowerPoint Quick-Start. Table of Contents. How to Get Help. Information Technology

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

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Drawing Geometrical Objects. Graphic courtesy of Eric Roberts

Programming: You will have 6 files all need to be located in the dir. named PA4:

1 Getting started with Processing

TWO-DIMENSIONAL FIGURES

Unit 10: Data Structures CS 101, Fall 2018

CS 134 Programming Exercise 2:

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

Java Programming. Computer Science 112

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

Before submitting the file project4.py, check carefully that the header above is correctly completed:

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

Graphics and Painting

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

CS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017

@Override public void start(stage primarystage) throws Exception { Group root = new Group(); Scene scene = new Scene(root);

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

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

CS 134 Programming Exercise 9:

CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM

Assignment 2. Application Development

Before We Begin. Class Implementation II. Introduction to Computer Science II. Overview (1): Administrative Details (1):

Programming Project 1

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

To gain experience using recursion and recursive data structures.

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

Programming Karel the Robot

Garfield AP CS. Graphics

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

CISC 1600, Lab 3.1: Processing

You must pass the final exam to pass the course.

CPS122 - OBJECT-ORIENTED SOFTWARE DEVELOPMENT

Practice Midterm 1. Problem Points Score TOTAL 50

Using Graphics. Building Java Programs Supplement 3G

Programming Assignment 8 ( 100 Points )

Exercise 0.1 (Review from lectures no submission required)

King Abdulaziz University Faculty of Computing and Information Technology Computer Science Department

CS 134 Test Program #2

Expressions, Statements, and Control Structures

CS 051 Homework Laboratory #2

Chapter 6 Introduction to Defining Classes

CISC 1600, Lab 2.1: Processing

Topic 8 graphics. -mgrimes, Graphics problem report 134

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

References. Date Due: Monday, October 5 th, 5:00pm

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

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

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims

CSE 11 Midterm Fall 2008

CPS122 - OBJECT-ORIENTED SOFTWARE DEVELOPMENT

Writeup for first project of CMSC 420: Data Structures Section 0102, Summer Theme: Threaded AVL Trees

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Practice Final Examination #2

Appendix F: Java Graphics

Spring CS Homework 3 p. 1. CS Homework 3

Graphics JFrame. import java.awt.*; import javax.swing.*; public class XXX extends JFrame { public XXX() { ... main() public static

Java Interfaces Part 1 - Events Version 1.1

CSCI 161 Introduction to Computer Science

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

Transcription:

DUE DATES: Monday recitations: 9:00 PM on 10/09 Wednesday recitations: 9:00 PM on 10/11 Thursday recitations: 9:00 PM on 10/12 Friday recitations: 9:00 PM on 10/13 Saturday recitations: 9:00 PM on 10/14 Ready! We've been talking about instance variables (how to declare and intitialize them) and also mutator methods (which allow us to change the values of instance variables). In this lab you will get some practice writing a sequence of programs which let you draw pictures with lines of different colors and widths. If you are unsure how to do these things you should review your lecture notes, slides posted on the course website, and readings from the textbook. Before describing the required lab work we need to discuss a few points needed for this (and future) labs. IMPORTS Rather than always fully qualifying a class name from another package, you can "import" it. Importing a fully qualified name allows you to use it in an unqualified way throughout a file. This can make both writing and reading the code easier. Rather than writing this code: package part3; public class Farm { public Farm() { example1.barnyard b; b = new example1.barnyard(); example1.pig p1; p1 = new example1.pig(); b.addpig(p1); p1.start(); example1.pig p2; p2 = new example1.pig(); b.addpig(p2); p2.start(); we can use imports to make the names example1.barnyard and example1.pig available in their

unqualified forms, BarnYard and Pig: package part3; import example1.barnyard; import example1.pig; public class Farm { public Farm() { BarnYard b; b = new BarnYard(); Pig p1; p1 = new Pig(); b.addpig(p1); p1.start(); Pig p2; p2 = new Pig(); b.addpig(p2); p2.start(); STATIC VARIABLES Instances of the class java.awt.color represent colors. Some color objects are "pre-defined". References to these objects are stored in static variables of the Color class. A static variable is often public, and is associated with the class rather than an instance. For example, Color.BLUE is a static variable which holds a reference to a Color object representing the color blue. REMEMBER: do NOT label any of your variables as static. PUBLIC VARIABLES In this lab you will use instances of the class java.awt.point and java.awt.dimension. Point objects, which represent points on a plane, have two instance variables representing the x and y coordinates of the point. These instance variables are public rather than private. If the variable p is of type Point then p.x and p.y are the two instance variables. Dimension objects, which represent dimensions, have two instance variables representing the width and height of the dimension. These instance variables are public rather than private. If the variable dim is of type Dimension then dim.width and dim.height are the two instance variables. REMEMBER: do NOT label any of your instance variables public.

Set! 1. Log in 2. Start Eclipse 3. Switch to the CVS Repository Exploring perspective 4. Check out the CSE115-Lab4 project from the Labs repository 5. Switch to the Java perspective Go! For this lab we have written some code that creates a simple graphical user interface. You will be able to do this on your own soon enough, but we need to cover a few more topics first. For now our code will handle user inputs through the user interface (UI) and your job is to write the code that responds correctly to the user input. Part 1 Edit the definition of the Lab4 class in the part1 package so that it has an association relationship with the part1.support.canvas class. Establish the association via the constructor (the simple version we learned first in lecture). A Canvas object consists of a region on the screen on which we can draw. You will define the Lab4 class so that when a user releases the mouse button in the Canvas a circle with diameter 40 is drawn at the point where the user released the mouse button. The point determines the upper left corner of the bounding box of the cirlcle: When this happens the support code we wrote will attempt to call a method named 'drawcircle' on a Lab4 object. You must define this 'drawcircle' method. It must be a void method that has one java.awt.point parameter. This method needs to get a Graphics2D object from the Canvas object that Lab4 is associated with. Canvas defines a parameterless accessor method called getgraphics2d you need to call for this purpose. The Graphics2D object in turn defines a drawoval method that takes four values, x, y, w, and h. It draws an oval at (x,y) with a width of w and a height of h. Since we want to draw a circle with diameter 40 you need to make sure that the last two arguments of the drawoval method call are both 40. See the discussion above about how to extract the x and y coordinates from a java.awt.point. Once you have completed part 1 you can run it (use the Driver provided). When the window opens you should be able to draw thin black circles in the Canvas.

Part 2 In this part of the lab we will add the ability to change the color of the circle that's drawn. The support code now includes several buttons labelled with different colors. Clicking on a button will cause the support code to call a setcolor mutator method on Lab4. You must define this mutator, in addition to making some other changes in your drawcircle method. First edit the definition of the Lab4 class in the part2 package so that it does everything that the Lab4 class in part 1 did. Because selecting a color for a circle and drawing a circle are distinct activities, you must add code to Lab4 so that it remembers the last color selected until such time as the user draws a To do this you need to declare an instance variable of type java.awt.color in the Lab4 class. Initialize your instance variable to Color.GREEN in the constructor. Define the setcolor mutator method. Remember that this will be a public void method whose parameter list declares a variable of type java.awt.color. The body of the method will simply assign the value of the parameter to the instance variable. Finally, you need to modify the drawcircle method so that just before the circle is drawn the color of the Graphics2D object is set to the last selected color. Do this by calling the setcolor mutator method on that Graphics2D object. Note that the setcolor mutator method you define for Lab4 is different from the setcolor mutator method already defined in the Graphics2D object. Once you have completed part 2 you can run it (use the Driver provided). When the window opens you should be able to draw thin lines in various colors on the Canvas. Part3 In this part of the lab we will add the ability to change the diameter of the circle that's drawn. The support code in part 3 also includes several buttons labelled with different diameters. Clicking on a button will cause the support code to call a setdimension mutator method on Lab4. You must define this mutator, in addition to making some other changes in your drawcircle method. First edit the definition of the Lab4 class in the part3 package so that it does everything that the Lab4 class in part 2 did. Because selecting a diameter for a circle and drawing a circle are distinct activities, you must add code to Lab4 so that it remembers the last dimension selected until such time as the user draws a To do this you need do what you did in part 2 (to handle color), but now to handle a java.awt.dimension value. Initialize this instance variable to a new java.awt.dimension(80,80) value in the constructor, and add the setdimension mutator method.

Finally, you need to modify the drawcircle method so that the last two arguments of the drawoval method call are the width and height components of the java.awt.dimension object. 'width' and 'height' are public instance variables of the Dimension object, just like 'x' and 'y' are public instance variable of a Point object. (Remember, do NOT label any of your instance variable public!) Once you have completed part 3 you can run it (use the Driver provided). When the window opens you should be able to circle in various colors and diameters on the Canvas. Have fun get creative and make some cool pictures! Part4 In this part of the lab we will add the ability to change the width of the line used to draw circles. The support code in part 4 includes several buttons labelled with different line widths. Clicking on a button will cause the support code to call a setstroke mutator method on Lab4. You must define this mutator, in addition to making some other changes in your drawcircle method. First edit the definition of the Lab4 class in the part4 package so that it does everything that the Lab4 class in part 3 did. Because selecting a stroke width for a line and drawing a circle are distinct activities, you must add code to Lab4 so that it remembers the last stroke selected until such time as the user draws a To do this you need do what you did in part 2 (to handle color) and part 3 (to handle diameter), but now to handle a java.awt.basicstroke value. Initialize this instance variable to a new BasicStroke(5) value in the constructor, and add the setstroke mutator method. Finally, you need to modify the drawcircle method so that just before the circle is drawn not only is the color of the Graphics2D object set but also the stroke. Do this by calling the setstroke mutator method on that Graphics2D object, passing along the value stored in the new instance variable. Once you have completed part 4 you can run it (use the Driver provided). When the window opens you should be able to draw thin lines in various colors and thicknesses on the Canvas. Have fun get creative and make some cool pictures! Submitting your project to Web-CAT Make sure you submit your work on time; due dates are listed at the beginning of this lab description. This lab will be automatically graded by Web-CAT. You may submit as many times as you wish. Your last submission is the one that counts (so consider carefully whether you want to make any late submissions, as the late penalty is 20 points per day or portion thereof late).