1 CSC 221: Computer Programming I Fall 2005 graphics & design  Dot & DotRace revisited  Circle class implementation  adding Circle methods  static.

Slides:



Advertisements
Similar presentations
1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Object Oriented Concepts 2 Stewart Blakeway FML 213
Fall 2007ACS-1903 BlueJ Ron McFadyen Lab 1: Using BlueJ at UofW The purpose of this lab is to give you some experience with BlueJ. Over the course of the.
Enhancing classes Visibility modifiers and encapsulation revisited
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
1 CSC 221: Computer Programming I Fall 2004 Objects and classes: a first pass  software objects, classes, object-oriented design  BlueJ IDE, compilation.
1 CSC 222: Object-Oriented Programming Spring 2013 interacting objects  abstraction, modularization  internal vs. external method calls  expressions,
CS1101: Programming Methodology
CSC 212 – Data Structures Lecture 12: Java Review.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
OBJECTS AND CLASSES CITS1001. Concepts for this lecture class; object; instance method; parameter; signature data type multiple instances; state method.
1 CSC 221: Computer Programming I Fall 2004 variables and expressions (cont.)  counters and sums  expression evaluation, mixed expressions  int vs.
PART 3. You have been using static methods in your programs before and methods are the building blocks of procedural programming. In this part of the.
1 CSC 221: Computer Programming I Spring 2008 Class design & strings  design principles  cohesion & coupling  class example: graphical DotRace  static.
1 CSC 221: Computer Programming I Spring 2010 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
1 CSC 221: Computer Programming I Fall 2005 interacting objects  abstraction, modularization  internal vs. external method calls  primitives vs. objects.
1 CSC 221: Computer Programming I Fall 2004 interacting objects  abstraction, modularization  internal method calls  object creation, external method.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
1 CSC 222: Object-Oriented Programming Spring 2013 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
1 CSC 221: Computer Programming I Fall 2004 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
Session 7 Methods Strings Constructors this Inheritance.
1 CSC 221: Computer Programming I Fall 2009 interaction & repetition  modular design: dot races  constants, static fields  conditional repetition, while.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
1 CSC 221: Computer Programming I Fall 2009 Class design & strings  design principles  cohesion & coupling  example: graphical DotRace  objects vs.
1 CSC 221: Computer Programming I Fall 2005 Class design & strings  design principles  cohesion & coupling  class examples: Hurricane, TaxReturn  objects.
SEEM Java – Basic Introduction, Classes and Objects.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ) reading:
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
Lecture 3: More on Classes Textbook: Chapter 2 Recap: –Classes vs. Objects –Q: What comes first a class or an object? –Q: Do we need a class to create.
1 CSC 221: Computer Programming I Fall 2005 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CSC 222: Object-Oriented Programming Fall 2015
Topic: Classes and Objects
CSC 222: Object-Oriented Programming Spring 2017
EECE 310: Software Engineering
COMPSCI 107 Computer Science Fundamentals
CSC 222: Object-Oriented Programming Fall 2015
CSC 222: Object-Oriented Programming
Java Programming: Guided Learning with Early Objects
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 4: Writing classes
Interfaces and Constructors
Domain Classes Chapter 9.
תירגול 9 אובייקטים.
CSC 222: Object-Oriented Programming Spring 2012
Building Java Programs
Building Java Programs
CSC 221: Computer Programming I Fall 2009
Outline Anatomy of a Class Encapsulation Anatomy of a Method
How to organize and document your classes
CSC 221: Computer Programming I Spring 2008
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Building Java Programs
CSC 222: Object-Oriented Programming Spring 2012
Building Java Programs
CMPE212 – Reminders Assignment 2 due next Friday.
Presentation transcript:

1 CSC 221: Computer Programming I Fall 2005 graphics & design  Dot & DotRace revisited  Circle class implementation  adding Circle methods  static fields  Line class

2 Recall our Dot class in the final version:  a constant represented the maximum step size  the die field was static to avoid unnecessary duplication  an accessor method was added to access the dot color public class Dot { private static final int MAX_STEP = 5; private static Die die; private String dotColor; private int dotPosition; public Dot(String color) { die = new Die(MAX_STEP); dotColor = color; dotPosition= 0; } public int getPosition() { return dotPosition; } public String getColor() { return dotColor; } public void step() { dotPosition += die.roll(); } public void reset() { dotPosition = 0; } public void showPosition() { System.out.println(getColor() + ": " + getPosition()); }

3 Recall our DotRace class public class DotRace { private Dot redDot; private Dot blueDot; private int goalDistance; public DotRace(int goal) { redDot = new Dot("red"); blueDot = new Dot("blue"); goalDistance = goal; } public int getRedPosition() { return redDot.getPosition(); } public int getBluePosition() { return blueDot.getPosition(); } public int getGoalDistance() { return goalDistance; } public void step() { if (getRedPosition() >= getGoalDistance() && getBluePosition() >= getGoalDistance()) { System.out.println("The race is over: it's a tie."); } else if (getRedPosition() >= getGoalDistance()) { System.out.println("The race is over: RED wins!"); } else if (getBluePosition() >= getGoalDistance()) { System.out.println("The race is over: BLUE wins!"); } else { redDot.step(); blueDot.step(); } public void showStatus() { redDot.showPosition(); blueDot.showPosition(); } public void reset() { redDot.reset(); blueDot.reset(); }  added a finish line to the race  stored as a field the goal distance as a field and provided an accessor method  modified the step method to check if either Dot has finished

4 Adding graphics  we could utilize the Circle class to draw the dots  unfortunately, Circle only has methods for relative movements moveLeft() moveRight() moveUp() moveDown() moveHorizontal(dist) slowMoveHorizontal(dist) moveVertical(dist) slowMoveVertical(dist)  we need a Circle method for absolute movement (based on a Dot's position)  would also be useful to be able to access the circle diameter what if we wanted to display the dot race visually? let's explore the Circle class implementation

5 Circle implementation the Circle class has the following fields: private int diameter; private int xPosition; private int yPosition; private String color; private boolean isVisible; the constructor initializes those fields to define a default circle /** * Create a new circle at default position with default color. */ public Circle() { diameter = 30; xPosition = 20; yPosition = 60; color = "blue"; isVisible = false; }

6 Circle implementation (cont.) the low-level graphics methods are: /* * Draw the circle with current specifications on screen. */ private void draw() { if(isVisible) { Canvas canvas = Canvas.getCanvas(); canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition, diameter, diameter)); canvas.wait(10); } /* * Erase the circle on screen. */ private void erase() { if(isVisible) { Canvas canvas = Canvas.getCanvas(); canvas.erase(this); } these methods utilize a Canvas and call its methods to draw and erase the Circle how does Canvas work? LATER or maybe not (  abstraction)

7 Circle implementation (cont.) the other Circle methods build upon draw and erase /** * Make this circle visible. If it was already visible, do nothing. */ public void makeVisible() { isVisible = true; draw(); } /** * Move the circle horizontally by 'distance' pixels. */ public void moveHorizontal(int distance) { erase(); xPosition += distance; draw(); } /** * Move the circle a few pixels to the right. */ public void moveRight() { moveHorizontal(20); }...

8 Adding methods /** * Move the circle to a specific location on the canvas. xpos the new x-coordinate for the circle ypos the new y-coordinate for the circle */ public void moveTo(int xpos, int ypos) { erase(); xPosition = xpos; yPosition = ypos; draw(); } /** * Accessor for the circle diameter the diameter (in pixels) of the circle */ public int getDiameter() { return diameter; } to add new methods, we don't have to understand Canvas or even how draw/erase work  can abstract away those details and write code that builds upon the abstractions

9 Adding graphics due to our modular design, changing the display is easy  each Dot object will maintains and display its own Circle image  add Circle field  constructor creates the Circle and sets its color  showPosition moves the Circle (instead of displaying text) public class Dot { private static final int MAX_STEP = 5; private static Die die; private String dotColor; private int dotPosition; private Circle dotImage; public Dot(String color) { die = new Die(MAX_STEP); dotColor = color; dotPosition= 0; dotImage = new Circle(); dotImage.changeColor(color); } public int getPosition() { return dotPosition; } public void step() { dotPosition += die.roll(); } public void reset() { dotPosition = 0; } public void showPosition() { dotImage.moveTo(dotPosition, dotImage.getDiameter()); dotImage.makeVisible(); }

10 Graphical display note: no modifications are necessary in the DotRace class!!!  this shows the benefit of modularity  not only is modular code easier to write, it is easier to change/maintain  can isolate changes/updates to the class/object in question  to any other interacting classes, the methods look the same EXERCISE: make these modifications to the Dot class  add Circle field  create and change color in constructor  modify showPosition to move and display the circle

11 Better graphics the graphical display is better than text, but still primitive  dots are drawn on top of each other (same yPositions)  would be nicer to have the dots aligned vertically (different yPositions) PROBLEM: each dot maintains its own state & displays itself  thus, each dot will need to know what yPosition it should have  but yPosition depends on what order the dots are created in DotRace (e.g., 1 st dot has yPosition = diameter, 2 nd dot has yPosition = 2*diameter, …)  how do we create dots with different yPositions?

12 public class DotRace { private Dot redDot; private Dot blueDot; private Dot greenDot; public DotRace(int maxStep) { redDot = new Dot("red", 1); blueDot = new Dot("blue", 2); greenDot = new Dot("green", 3); }... } Option 1: parameters we could alter the Dot class constructor  takes an additional int that specifies the dot number  the dotNumber can be used to determine a unique yPosition  in DotRace, must pass in the number when creating each of the dots public class Dot { private static final int MAX_STEP = 5; private static Die die; private String dotColor; private int dotPosition; private Circle dotImage; private int dotNumber; public Dot(String color, int num) { die = new Die(MAX_STEP); dotColor = color; dotPosition= 0; dotImage = new Circle(); dotImage.changeColor(color); dotNumber = num; }... public void showPosition() { dotImage.moveTo(dotPosition, dotImage.getDiameter()*dotNumber); dotImage.makeVisible(); } this works, but is inelegant  why should DotRace have to worry about dot numbers?  the Dot class should be responsible

13 Option 2: a static field better solution: have each dot keep track of its own number  this requires a new dot to know how many dots have already been created  this can be accomplished with a static field  when the first object of that class is created, the field is initialized via the assignment  subsequent objects simply access/update the existing field public class Dot { private static final int MAX_STEP = 5; private static Die die; private String dotColor; private int dotPosition; private Circle dotImage; private static int nextAvailable = 1; private int dotNumber; public Dot(String color) { die = new Die(MAX_STEP); dotColor = color; dotPosition= 0; dotImage = new Circle(); dotImage.changeColor(color); dotNumber = nextAvailable; nextAvailable++; }... public void showPosition() { dotImage.moveTo(dotPosition, dotImage.getDiameter()*dotNumber); dotImage.makeVisible(); }

14 Drawing the finish line if we wanted to draw the finish line  would need to define a Line class (similar to Circle & Square)  the finish line belongs to the race, not individual dots  have a line field, draw it in the constructor public class DotRace { private Dot redDot; private Dot blueDot; private int goalDistance; private Line finishLine; public DotRace(int goal) { redDot = new Dot("red"); blueDot = new Dot("blue"); goalDistance = goal; finishLine = new Line(goalDistance, 0, goalDistance, 300); finishLine.makeVisible(); }.

15 Defining a Line class  start with a copy of Circle  what new fields are needed?  what fields are no longer needed?  do all the existing methods make sense?  how do the existing (and meaningful) methods need to be updated?  how is the low-level drawing & erasing done for lines? new Line2D.Double(x1, y1, x2, y2) note: a small change had to be made to Canvas to make it work for lines download the latest version