© 2004 Pearson Addison-Wesley. All rights reserved November 2, 2007 Class Relationships ComS 207: Programming I (in Java) Iowa State University, FALL 2007.

Slides:



Advertisements
Similar presentations
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Advertisements

Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
Chapter 4: Writing Classes
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Writing Classes in Java
Class Relationships. In systems with multiple classes, it can become difficult to keep track of relationships  e.g. the Student class requires the Course.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Day 7. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 7 Questions from last Class?? Problem set 1 Corrected  Good results 3.
Chapter 6 Object-Oriented Design. 6-2 Program Development The creation of software involves four basic activities:  establishing the requirements  creating.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
1 (c) elsaddik CSI 1102 Introduction to Software Design Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 5: Writing Classes.
Object Oriented Design and UML
Writing Classes (Chapter 4)
The this Reference The this reference, used inside a method, refers to the object through which the method is being executed Suppose the this reference.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
6-1 Object-Oriented Design Today we focuses on: –the this reference (Chapter 7) –the static modifier (Chapter 7) –method overloading (Chapter 7)
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Chapter 6 Object-Oriented Design Part 1. © 2004 Pearson Addison-Wesley. All rights reserved2/42 Object-Oriented Design Now we can extend our discussion.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Inheritance Reading for this Lecture: L&L 6.4 – 6.5.
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Static class members.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
© 2004 Pearson Addison-Wesley. All rights reserved October 31, 2007 Static Class Members ComS 207: Programming I (in Java) Iowa State University, FALL.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Outline Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
1 Chapter 4: Writing Classes  Chapter 4 focuses on: class definitions encapsulation and Java modifiers method declaration, invocation, and parameter passing.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
University of Turkish Aeronautical Association Computer Engineering Department CENG 112 COMPUTER PROGRAMMING 2 Tansel Dökeroglu, Ph.D.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Passing Objects to Methods
Object Oriented Programming
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Review for the Final Exam
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Chapter 4 Writing Classes.
Static Class Members March 29, 2006 ComS 207: Programming I (in Java)
Overriding Methods & Class Hierarchies
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object Oriented Programming Review
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
Chapter 4: Writing Classes
Presentation transcript:

© 2004 Pearson Addison-Wesley. All rights reserved November 2, 2007 Class Relationships ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor: Alexander Stoytchev

© 2004 Pearson Addison-Wesley. All rights reserved No Class on Monday Nov 5 Due to night exam last week (midterm 2)

© 2004 Pearson Addison-Wesley. All rights reserved Final Exam When:Thursday, December 13, 2007 Where:Curtiss Hall, room 127 (classroom) Time: 4:30-6:30pm Also see this page (for large classes like ComS 207): NOTE: The time listed on this page does **NOT** apply to ComS207!!!

© 2004 Pearson Addison-Wesley. All rights reserved Example: Sudoku_Solver.java

© 2004 Pearson Addison-Wesley. All rights reserved Solving Sudoku Puzzles With Recursion (

© 2004 Pearson Addison-Wesley. All rights reserved Rule #1: 1..9 must be in each row

© 2004 Pearson Addison-Wesley. All rights reserved Sample that satisfies rule #

© 2004 Pearson Addison-Wesley. All rights reserved Rule #2: 1..9 must be in each column

© 2004 Pearson Addison-Wesley. All rights reserved Sample that satisfies rules #1 and #

© 2004 Pearson Addison-Wesley. All rights reserved Rule #3: 1..9 must be in each 3x3 window

© 2004 Pearson Addison-Wesley. All rights reserved Sample that satisfies rules #1, #2, and #

© 2004 Pearson Addison-Wesley. All rights reserved Quick Review of Last Lecture

© 2004 Pearson Addison-Wesley. All rights reserved Visibility Modifiers publicprivate Variables Methods Provide services to clients Support other methods in the class Enforce encapsulation Violate encapsulation

© 2004 Pearson Addison-Wesley. All rights reserved The static Modifier We declare static methods and variables using the static modifier It associates the method or variable with the class rather than with an object of that class Static methods are sometimes called class methods and static variables are sometimes called class variables

© 2004 Pearson Addison-Wesley. All rights reserved Static Variables Normally, each object has its own data space, but if a variable is declared as static, only one copy of the variable exists private static float price; Memory space for a static variable is created when the class is first referenced All objects instantiated from the class share its static variables Changing the value of a static variable in one object changes it for all others

© 2004 Pearson Addison-Wesley. All rights reserved Classes A class can contain data declarations and method declarations int size; int weight; Data declarations Method declarations

© 2004 Pearson Addison-Wesley. All rights reserved Objects – instances of classes int size =5; int weight= 170; int size =10; int weight= 130; obj1obj2

© 2004 Pearson Addison-Wesley. All rights reserved Note that the variables can have different values in the two objects int size =5; int weight= 170; int size =10; int weight= 130; obj1obj2

© 2004 Pearson Addison-Wesley. All rights reserved Classes Things change if we declare a static variable static int size; int weight; Data declarations Method declarations

© 2004 Pearson Addison-Wesley. All rights reserved Objects – instances of a class with a static variable ‘size’ int weight= 170;int weight= 130; obj1obj2 static int size;

© 2004 Pearson Addison-Wesley. All rights reserved Objects – instances of classes Note that the variables can have different values in the two objects int size =5; int weight= 170; int size =10; int weight= 130;

© 2004 Pearson Addison-Wesley. All rights reserved Static Class Members The order of the modifiers can be interchanged, but by convention visibility modifiers come first Recall that the main method is static – it is invoked by the Java interpreter without creating an object Static methods cannot reference instance variables because instance variables don't exist until an object exists However, a static method can reference static variables or local variables

© 2004 Pearson Addison-Wesley. All rights reserved myMethod(); myMethodcompute Method Control Flow If the called method is in the same class, only the method name is needed

© 2004 Pearson Addison-Wesley. All rights reserved myMethod(); myMethodcompute Accessing Variables If the called method is in the same class, only the method name is needed int myVariable; myVariable=5; OK

© 2004 Pearson Addison-Wesley. All rights reserved myMethod(); myMethodcompute Accessing Variables Static methods cannot use non static class variables. int myVariable; myVariable=5; Error static

© 2004 Pearson Addison-Wesley. All rights reserved myMethod(); myMethodcompute Accessing Variables Static methods can use static class varables static int myVariable; myVariable=5; OK static

© 2004 Pearson Addison-Wesley. All rights reserved Static Class Members Recall that a static method is one that can be invoked through its class name For example, the methods of the Math class are static: result = Math.sqrt(25); Variables can be static as well Determining if a method or variable should be static is an important design decision

© 2004 Pearson Addison-Wesley. All rights reserved Static Methods class Helper { public static int cube (int num) { return num * num * num; } Because it is declared as static, the method can be invoked as value = Helper.cube(5);

© 2004 Pearson Addison-Wesley. All rights reserved Class Helper static cube static helpMe helpMe(); Helper.cube(); main Method Control Flow Static methods can only call other static methods within the same classs

Chapter 6 Section 6.4

© 2004 Pearson Addison-Wesley. All rights reserved Class Relationships Classes in a software system can have various types of relationships to each other Three of the most common relationships:  Dependency: A uses B  Aggregation: A has-a B  Inheritance: A is-a B

© 2004 Pearson Addison-Wesley. All rights reserved Dependency A dependency exists when one class relies on another in some way, usually by invoking the methods of the other We've seen dependencies in many previous examples We don't want numerous or complex dependencies among classes Nor do we want complex classes that don't depend on others A good design strikes the right balance

© 2004 Pearson Addison-Wesley. All rights reserved Dependency Example: Client-Server

© 2004 Pearson Addison-Wesley. All rights reserved Dependency Some dependencies occur between objects of the same class A method of the class may accept an object of the same class as a parameter For example, the concat method of the String class takes as a parameter another String object str3 = str1.concat(str2); This drives home the idea that the service is being requested from a particular object

© 2004 Pearson Addison-Wesley. All rights reserved concat(String s) Concatenation Example str1str2 concat(String s)

© 2004 Pearson Addison-Wesley. All rights reserved Dependency The following example defines a class called Rational to represent a rational number A rational number is a value that can be represented as the ratio of two integers Some methods of the Rational class accept another Rational object as a parameter See RationalTester.java (page 297)RationalTester.java See Rational.java (page 299)Rational.java

© 2004 Pearson Addison-Wesley. All rights reserved Aggregation An aggregate is an object that is made up of other objects Therefore aggregation is a has-a relationship  A car has a chassis  A student has an address

© 2004 Pearson Addison-Wesley. All rights reserved Aggregation Example: Components of a Car

© 2004 Pearson Addison-Wesley. All rights reserved Chasis

© 2004 Pearson Addison-Wesley. All rights reserved Tyres

© 2004 Pearson Addison-Wesley. All rights reserved Steering Wheel

© 2004 Pearson Addison-Wesley. All rights reserved What type of Steering Wheel?

© 2004 Pearson Addison-Wesley. All rights reserved Car Seat

© 2004 Pearson Addison-Wesley. All rights reserved Aggregation In software, an aggregate object contains references to other objects as instance data The aggregate object is defined in part by the objects that make it up This is a special kind of dependency – the aggregate usually relies on the objects that compose it

© 2004 Pearson Addison-Wesley. All rights reserved Aggregation Example: Copmonents of a Student

© 2004 Pearson Addison-Wesley. All rights reserved Student Home Address School Address First NameLast Name

© 2004 Pearson Addison-Wesley. All rights reserved john 21 Jump Street 800 Lancaster Ave. JohnSmith

© 2004 Pearson Addison-Wesley. All rights reserved marsha 123 Main Street 800 Lancaster Ave. MarshaJones

© 2004 Pearson Addison-Wesley. All rights reserved Aggregation in UML StudentBody + main (args : String[]) : void + toString() : String Student - firstName : String - lastName : String - homeAddress : Address - schoolAddress : Address + toString() : String - streetAddress : String - city : String - state : String - zipCode : long Address

© 2004 Pearson Addison-Wesley. All rights reserved Aggregation In the following example, a Student object is composed, in part, of Address objects A student has an address (in fact each student has two addresses) See StudentBody.java (page 304)StudentBody.java See Student.java (page 306)Student.java See Address.java (page 307)Address.java An aggregation association is shown in a UML class diagram using an open diamond at the aggregate end

© 2004 Pearson Addison-Wesley. All rights reserved THE END