Java the UML Way version 2000-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and.

Slides:



Advertisements
Similar presentations
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
CS0007: Introduction to Computer Programming Methods: Documentation, Reference Parameters, Modularization 2.
 Draft timetable has the same times as this semester: - ◦ Monday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm. ◦ Tuesday 9:00 am to 12:00 noon, 1:00 pm.
CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
1 Fall 2009ACS-1903 Methods – Ch 5 A design technique referred to as stepwise refinement (or divide and conquer, or functional decomposition) is used to.
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
Classes, methods, and conditional statements We’re past the basics. These are the roots.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Week101 APCS-AB: Java Miscellaneous Topics: Snippets we missed in Chapters 1-6 of book November 11, 2005.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Catie Welsh March 23,  Lab 6 due Friday by 1pm 2.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
A High Flying Overview CS139 – Fall 2006 How far we have come.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Classes - Intermediate
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Week91 APCS-A: Java Problem Solving November 2, 2005.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes and Objects.
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
Intro To Classes Review
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
CSC 113 Tutorial QUIZ I.
CHAPTER 6 GENERAL-PURPOSE METHODS
Presentation transcript:

Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7 Collaboration Between Objects Collaboration between objectspage 2 Collaboration in the renovation casepage 3-4 The getNoOfMeters() method with sequence diagrampage 5-6 The getTotalPrice() methodpage 7 Collaboration beween objects of the same classpage 8-9 The renovation case, a menu-driven programpage Several references to the same objectpage Passing argumentspage 19-20

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 2 Collaboration Between Objects In reality objects often collaborate to solve tasks. Examples: –The teacher sends the message “Complete this task” to a student. The student searches for help from other students, and searches on the Internet or in books to complete the task. –Elisabeth turns her CD player on. It has to collaborate with the CD to be able to make music. In programs, we get an object to collaborate with another object by sending messages to it. Example: We send the message “Play this CD!” to the myCDplayer object in this way: –myCDplayer.play(“Four Seasons”); For an object to send messages to other objects, it must have access to the other objects.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 3 Class Diagram, the Renovation Case An instance of the Flooring class has to collaborate with an instance of the Surface class to calculate the number of meters required to cover the surface. The Paint and Wallpaper objects have to collaborate with the Surface object to calculate the number of liters and rolls, respectively. Sorce code for the classes: Surface, page 86, Flooring and Wallpaper, page (Paint, solution to problem 2 p. 189.)

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 4 Message to the flooring: What will it cost to cover the surface with you? Collaboration, Examples class FlooringClient { public static void main(String[] args) { Surface theSurface = new Surface("Margaret's Floor", 5, 6); Flooring theFlooring = new Flooring("Fitted carpet", 24.50, 5); double noOfMeters = theFlooring.getNoOfMeters(theSurface); double price = theFlooring.getTotalPrice(theSurface); System.out.println("You need " + noOfMeters + " meters, price $" + price); } The client instantiates two objects Message to the flooring: How many meters do we need of you to cover the surface? Printout: You need 6.0 meters, price $147.0

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 5 The getNoOfMeters() Method public double getNoOfMeters(Surface aSurface) { double lengthSurface = aSurface.getLength(); double widthSurface = aSurface.getWidth(); int noOfWidths = (int)(lengthSurface / widthOfFlooring); double rest = lengthSurface % widthOfFlooring; if (rest >= limit) noOfWidths++; return noOfWidths * widthSurface; } The method call: double noOfMeters = theFlooring.getNoOfMeters(theSurface); A reference to the surface object is sent along with the message. (As an argument to the method.) In this way the method gets access to the client's surface object. And, because of that, the method may send messages to the surface object.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 6 The Collaboration is Shown by This Sequence Diagram Solve problem 1, page 189. time

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 7 The getTotalPrice() Method public double getTotalPrice(Surface aSurface) { return getNoOfMeters(aSurface) * price; } The method call: double price = theFlooring.getTotalPrice(theSurface);

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 8 Two Objects of the Same Class Collaborate Example: Comparing areas Surface surface1 = new Surface("A", 5, 4); Surface surface2 = new Surface("B", 4, 4); int result = surface1.compareAreas(surface2); if (result < 0) System.out.println(surface1.getName() + " is the smaller one."); else if (result > 0) System.out.println(surface2.getName() + " is the smaller one."); else System.out.println("The surfaces have the same area."); Here is the compareAreas() method in the Surface Class: public int compareAreas(Surface theOtherSurface) { final double precision = ; double area1 = getArea(); double area2 = theOtherSurface.getArea(); if (Math.abs(area2 - area1) < precision) return 0; else if (area1 < area2) return -1; else return 1; }

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 9 Two Surface-Objects Collaborate to Find the Larger Solve problem 3, page

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 10 A Menu-Driven Program We'll calculate the materials required and the price for renovating a surface (wall or floor). public static String[] alternatives = {"Wallpaper", "Paint", " Flooring ", "Exit"}; JOptionPane.showOptionDialog(null, "What do you choose:", "Renovation", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, alternatives, alternatives[0]); [option != exit] carry out the chosen task let the user do a menu choice [option == exit] let the user do a menu choice show instructions to the user

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 11 Which Classes Do We Need? Of course, the Surface, Flooring, Paint and Wallpaper classes. We need an object that can be responsible for carrying out the tasks in the activity diagram: –Display a short instruction to the user. –Input a menu choice from the user. –Carry out the chosen task We call this class ProjectChap7GUI. Do we need even more classes? Let us look at, for example, how the paint requirement has to be calculated: 1.Input data about the surface that will be painted (name, length, width). 2.Create an instance of the Surface class. 3.Input data about the paint that will be used (name, price, number of coats, number of square meters per liter). 4.Create an instance of the Paint class. 5.Ask the paint object how much paint is needed to cover the given surface and what it will cost. 6.Output the answers.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 12 The ReaderRenovationCase Class Points 1 and 3 generate much use of JOptionPane to input data  many code lines  loosing overview Points 1 and 2, and points 3 and 4, are general tasks that may be useful in other situations. We compose a new class with methods for inputting information and instantiating objects: ReaderRenovationCase ReaderRenovationCase readAndInstantiateSurface readAndInstantiateWallpaper readAndInstantiatePaint readAndInstantiateFlooring ProjectChap7 details:: ReaderRenovationCase alternativs[1..* ]: String showInstructions() doAChoice(): int carryOutTheRightThing( option: int)

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 13 Show the Source Code, Program Listings 7.2 and 7.3 (pages ) Solve problem 2, pp

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 14 Several References to the Same Object Collaboration between objects usually means that we have several references to the same object. It can be difficult to keep track of this if the references are in different methods or perhaps even in different classes. Which kinds of problems may arise? We’ll look at two concrete examples: –An access method gains access, via the parameter list, to an object that “belongs” to the client. –An instance variable refers to an object that it has received via the parameter list for a constructor or a mutation method. The object is created by the client.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 15 A Reference Type as a Parameter for an Access Method public double getNoOfMeters(Surface aSurface) { double lengthSurface = aSurface.getLength(); double widthSurface = aSurface.getWidth(); …… } ”AutumnBrown” theCarpet ”Anne’s floor” myFloor Flooring theCarpet = new Flooring("AutumnBrown", 23.50, 5.0); Surface myFloor = new Surface("Anne’s floor", 4.2, 3.5); double noOfMeters = theCarpet.getNoOfMeters(myFloor); What happens when the method getNoOfMeters() is invoked? Behind the scenes: Surface aSurface the method = myFloor client ; aSurface Here is a new reference to the floor-object

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 16 A Reference Type as a Parameter for an Access Method, continued What may happen inside getNoOfMeters()? If the Surface class is mutable, the method may change the data inside the Surface object, which is an object "belonging" to the client.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 17 An Instance Variable Refers to an Object that is Created by the Client The Surface class is expanded with information about the flooring which will be used. New constructor: public Surface(String initName, double initLength, double initWidth, Flooring initFlooring) { name = initName; length = initLength; width = initWidth; flooring = initFlooring; } The client program instantiates objects: Flooring theCarpet = new Flooring("AutumnBrown", 23.50, 5.0); Surface myFloor = new Surface("Anne’s floor", 4.2, 3.5, theCarpet); As an alternative, the constructor may create a copy of the flooring object: flooring = new Flooring(initFlooring.getName(), initFlooring.getPrice(), initFlooring.getWidth()); Now, the client and the myFloor object will each have one copy of the flooring object.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 18 Several References to the Same Object, Conclusion Keep the number of classes having access to a mutable object at a minimum – minimal coupling between classes. Consider using immutable classes. Instead of changing the object, a new object is made (The String class is a well known example.) Several classes may work with one copy each, but that seldom agree with the reality modeled. We have to know what we do, and we should document well. Solve problem 1, pp

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 19 Passing Arguments We’ve seen that a method can change an object that “belongs” to the client, if a reference to the object is an argument to the method. Can a method, in a similar fashion, change a variable of a primitive data type? As an example, we expand the Flooring class with the following method that change the price of the flooring: public void setPrice(double newPrice) { price = newPrice; newPrice = 0.0; } At the client side: Flooring theFlooring = new Flooring( "Margaret's flooring", 24.50, 5); String newPriceRead =JOptionPane. showInputDialog("New price: "); double newPrice = Double. parseDouble(newPriceRead); theFlooring.setPrice(newPrice); No changes on the client side.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 7, page 20 Summary, Argument Passing In a method call, the values for the arguments are always passed. The parameters are to be used as local variables, with initial values equal to the argument values. We differentiate between the following two cases: –The parameter is of a primitive data type: if the method changes the value for this variable, that doesn’t affect the argument at all. –The parameter is a reference: a copy of the argument means that the method and the client each have their own reference to the same object. If the method makes changes to the object, they will, of course, apply to the object that the argument points to, since that’s the same object we’re talking about. Solve all problems, page 204.