Fundamentals of Software Development 1Slide 1 Recap: Key ideas in BallWorlds so far Writing a class:Writing a class: –structure –process Writing methods:Writing.

Slides:



Advertisements
Similar presentations
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Advertisements

Fundamentals of Software Development 1Slide 1 Recap: Constructors, the new operator and the this object Three ideas:Three ideas: –How to write a constructor.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Fundamentals of Software Development 1Slide 1 Review: Why start with WordGames? You wrote your first lines of code in this course in WordGames.You wrote.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Fundamentals of Software Development 1Slide 1 Outline of this exercise Learning objectives:Learning objectives: –After this exercise, a student should.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Chapter 41 Defining Classes and Methods Chapter 4.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 11, Fri Jan
Fundamentals of Software Development 1Slide 1 Today’s Summary UML class diagrams – –Why classes are important – –UML class diagrams – relationships – –UML.
Fundamentals of Software Development 1Slide 1 One class can describe many different instancesOne class can describe many different instances –Two NameDroppers,
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Fundamentals of Software Development 1Slide 1 Method invocation versus definition To define (write) a method means to write its codeTo define (write) a.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 13, Wed Feb
Games and Simulations O-O Programming in Java The Walker School
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
Fundamentals of Software Development 1Slide 1 Why start with WordGames? You wrote your first lines of code in this course in WordGames.You wrote your first.
Fundamentals of Software Development 1Slide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Fundamentals of Software Development 1Slide 1 Outline of this exercise Learning objectives of the tutorial:Learning objectives of the tutorial: –After.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
 Implementing a class ◦ Implementing an interface ◦ Using documented stubs before coding ◦ Writing JUnit tests before coding ◦ Using fields CSSE 220 Object-Oriented.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Unified Modeling Language (UML)
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Review. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects and Classes Objects and Classes –State.
Fundamentals of Software Development 1Slide 1 Today’s Summary InterfacesInterfaces –What are they? –Why are they important? –How do they relate to WordGames?
KS3 Mathematics N5 Using Fractions
Recap: Key ideas in WordGames
Animate objects and threads
Recap: The design of Word Games
Component 1.6.
Summary prepared by Kirk Scott
Chapter 10 Programming Fundamentals with JavaScript
Topic: Python’s building blocks -> Variables, Values, and Types
More Sophisticated Behavior
Concepts of Object Oriented Programming
Building Java Programs
Unified Modeling Language
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Learning to Program D is for Digital.
Chapter 3 Syntax, Errors, and Debugging
Chapter 16 UML Class Diagrams.
Objects as a programming concept
Testing and Debugging.
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Session 2 What you already know about Java Reading and using an API
Classes.
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CompSci 230 Software Construction
Chapter 4: Writing Classes
Lesson 2: Building Blocks of Programming
Java Programming with BlueJ
Chapter 10 Programming Fundamentals with JavaScript
Chapter 18: Refining Analysis Relationships
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
CS 100: Roadmap to Computing
Implementing Non-Static Features
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Java objects: a first view
Fundamentals of Software Development 1
Classes, Objects and Methods
Review of Previous Lesson
CMSC201 Computer Science I for Majors Lecture 12 – Midterm Review
SPL – PS3 C++ Classes.
Presentation transcript:

Fundamentals of Software Development 1Slide 1 Recap: Key ideas in BallWorlds so far Writing a class:Writing a class: –structure –process Writing methods:Writing methods: –Using parametersparameters FieldsFields local variables\local variables\ –Scope and lifetime. –Invoking methods: The dot notationThe dot notation Import statements. JDK Help.Import statements. JDK Help. The next several slides review each of these ideas Interactions with other classes:Interactions with other classes: –UML class diagrams Writing them, reading themWriting them, reading them –X extends Y –X implements A, B, C –The distinction between a class anda class and an object that is an instance of that classan object that is an instance of that class Object Creation and ManipulaitonObject Creation and Manipulaiton –constructors –the new keyword –the this object

Fundamentals of Software Development 1Slide 2 Exercise to review these key concepts from BallWorlds so far Concepts Writing a class: structure and processWriting a class: structure and process Writing methodsWriting methods Interactions with other classesInteractions with other classes Constructors, the new keyword and the this objectConstructors, the new keyword and the this object Exercise: Stand up & walk far away from your computer Find a partner with whom you have NOT worked before and sit next to each other –Don’t sit at either of your computers The next slides include questions. As your instructor shows each slide: –One partner, answer the questions aloud –Other partner, make sure that the answers are clear and correct –Both partners: ask your instructor and/or assistants for more explanation if you are unsure about the answers or the concepts behind them! –Switch roles after each slide Questions about how to do this exercise?

Fundamentals of Software Development 1Slide 3 Recap: Writing a class – structure public class NameDropper extends StringTransformer implements StringTransformable { private String name; private String name; public NameDropper(String whatMyNameIs) { public NameDropper(String whatMyNameIs) { this.name = whatMyNameIs; this.name = whatMyNameIs; } public String transform(String whatToSay) { public String transform(String whatToSay) { return this.name + " says " + whatToSay; return this.name + " says " + whatToSay; }} Questions? Field(s): Data that is associated with the object Scope: rest of the class Lifetime: lifetime of the object Method(s): operations that the object can do Constructor(s): code that runs when the object is constructed Tell your partner: What are the 3 sections of a class? What is the role / purpose of each?

Fundamentals of Software Development 1Slide 4 By using documented stubs, for each method: You get its form (syntax) correct before working on its content (semantics) You write its specification before trying to implement it Questions? Recap: Writing a class – process Step 1: Add the class to the projectStep 1: Add the class to the project Step 2: Implement the classStep 2: Implement the class –Step 2a: Write the Javadoc specifications with stubs –Step 2b: Convert stubs to the actual code Obey Sun’s code conventionsObey Sun’s code conventions Step 3: Test the classStep 3: Test the class –For larger classes, repeat steps 2 and 3 several times, implementing more of the class each time Explain to your partner: What are documented stubs? Why is it helpful to use them?

Fundamentals of Software Development 1Slide 5 Recap: Interactions with other classes For each of the circled interactions, explain to your partner: What does it mean? Why is it useful / important? Questions?

Fundamentals of Software Development 1Slide 6 Recap: The distinction between a class and an object that is an instance of that class public class Mover extends Ball implements Animate, Drawable { private Point2D.Double position; private ManagerOfBalls manager;... public Mover(ManagerOfBalls mob) { this.manager = mob; } public Shape getShape() { return new Ellipse2D.Double( this.position.getX(),...); }... } ManagerOfBalls world1, world2; world1 = new World(); world2 = new World(); Ball ball1, ball2, ball3; ball1 = new Mover(world2); ball2 = new Mover(world2); ball3 = new Mover(world1); Briefly explain the portion of the Mover class shown to the left. Then explain each statement below in detail. What do they say about the title of this slide? This box defines a class called Mover The new Mover expressions in the statements above and to the right create instances of that class. Questions?

Fundamentals of Software Development 1Slide 7 Recap: Using parameters, fields, local variables; Scope and lifetime public class NameDropper extends StringTransformer implements StringTransformable { private String ; private String name; public NameDropper(String whatMyNameIs) { public NameDropper(String whatMyNameIs) { = whatMyNameIs; this.name = whatMyNameIs; } public String transform(String whatToSay) { public String transform(String whatToSay) { String middle = " says "; String middle = " says "; return + middle + whatToSay; return this.name + middle + whatToSay; }} What are the fields in the following? Parameters? Local variables? What is the scope of each? Lifetime? What does the this keyword mean? Why use it? How do you decide when to use a parameter? When to use a field versus a local variable? Use a field when you need data whose: Scope is the entire class Lifetime is the life of the instance Use a local variable when you need data whose: Scope is a single method Lifetime is the life of the method Use this to refer to this instance of the class. Questions?

Fundamentals of Software Development 1Slide 8 Recap: method invocation expressions Suppose that you have an object from one of the Java library classes (e.g. a JTextField object). How do you find out: What that object can do? What import statement is needed? Explain each of the expressions to the right. What appears before the dot? After the dot? Questions? Select JDK Help, index tab, look up the class, see its list of methods. Shortcut: Type the variable, then dot, and see what pops up. As above, but see the first line of the class description Questions? Always use the dot notation to invoke a method. For example: whatToSay.toUpperCase() this.position.setLocation(...) Math.random() this.die()

Fundamentals of Software Development 1Slide 9 Recap: Constructors, the new keyword and the this object public class MusicPanel extends JPanel implements Runnable { public MusicPanel(Color color) { this.setBackground(color); }... } Consider the MusicPanel class to the right. Write expression(s) that declare and construct a new blue MusicPanel called panel MusicPanel panel; panel = new MusicPanel(Color.BLUE); or equivalently: MusicPanel panel = new MusicPanel(Color.BLUE); Another solution: MusicPanel panel = new MusicPanel(new Color(0, 0, 255))); Color in RGB (RedGreenBlue) format (range 0-255) BLUE: Constant values usually appear in all CAPS, but in this case blue and Blue will also work!

Fundamentals of Software Development 1Slide 10 Recap: Constructors, the new keyword and the this object public class MusicPanel extends JPanel implements Runnable { public MusicPanel(Color color) { this.setBackground(color); }... } A Thread is an independent instruction- follower. From JDK Help: public Thread(Runnable target, String name) Allocates a new Thread object. Parameters: target - the object whose run method is called. name - the name of the new thread. Write an expression that constructs a new Thread whose target is panel and whose name is “Lola”. MusicPanel panel = new MusicPanel(Color.BLUE); new Thread(panel, “Lola”); Write an expression that, placed in the MusicPanel constructor, would construct a new Thread whose target is the MusicPanel being constructed and whose name is the MusicPanel’s color (as a String). From the previous slide new Thread(this, color.toString()); Questions?

Fundamentals of Software Development 1Slide 11 Questions? Questions on anything?Questions on anything? –Reminder: BallWorlds (all of it – parts 1 to 6) is due tomorrow You will have some time to work on it in class todayYou will have some time to work on it in class today Before you leave today:Before you leave today: –Tell your instructor where you are on BallWorlds –Arrange to get help as needed to complete it –Student assistants are available to you Sunday through Thursday evenings 7 to 9 p.m. in the CSSE lab F-217, as well as at other times