Class Design CSC 171 FALL 2004 LECTURE 11. READING Read Chapter 7 It’s abstract But it should help with project #1.

Slides:



Advertisements
Similar presentations
Chapter 8: Designing Classes
Advertisements

IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Designing Classes Chapter 8. Classes Collection of objects Objects are not actions Class names – Nouns Method names – Verbs What Makes a Good Class Represent.
Chapter 11 – Interfaces and Polymorphism. Chapter Goals Learn about interfaces Learn about interfaces Convert between class and interface references Convert.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 8 Designing Classes. Assignment Chapter 9 Review Exercises (Written)  R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13, 8.15, 8.19, 8.20 Due Friday,
Chapter 9 Designing Classes. Chapter Goals To learn how to choose appropriate classes to implement To understand the concepts of cohesion and coupling.
Chapter Goals To learn how to choose appropriate classes to implement
Chapter 3 Fundamental Data Types Goals: To understand integer and floating-point numbers To recognize the limitations of the int and double types and the.
Fall 2006CSC311: Data Structures1 Java Review. Fall 2006CSC311: Data Structures2 The Java Virtual Machine (JVM) Virtual machine -- the logical machine.
Chapter 7 Designing Classes Goals  To learn how to choose appropriate classes to implement  To understand the concepts of cohesion and coupling  To.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 7: More about Methods 1 Chapter 7 More about Methods.
Chapter 9 – Designing Classes. Chapter Goals  Learn to identify side effects  Know when to use method preconditions and postconditions  Go in depth.
CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Chapter 3 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the int and double types.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Week 12 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Chapter 3 Implementing Classes. Assignment Read 3.1 – 3.5 and take notes complete Self Check Exercises 1-10; Due September 24 th Read 3.6 – 3.8 and take.
Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data.
ICOM 4015: Advanced Programming Lecture 8 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Eight:
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain Name.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 8 – Designing Classes.
Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies – Extreme Programming Object-Oriented Design – CRC Cards - UML.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Eight: Designing Classes.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 Introduction to Classes and Objects Definitions Examples.
12 OBJECT-ORIENTED DESIGN CHAPTER
Class Design I Class Contracts Readings: 2 nd Ed: Section 9.5, Advanced Topic nd Ed: Section 8.5, Advanced Topic 8.2 Some ideas come from: “Practical.
Chapter 8: Designing Classes Accessors, Mutators, and Immutable Classes(8.3) Side Effects(8.4) Parameter Passing (Advanced Topic 8.1) Preconditions and.
Chapter 8 – Designing Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 9 – Designing Classes Goals Learning to design classes Learning to design classes Preconditions and postconditions for methods Preconditions.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Week 13 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 7 Designing Classes. Chapter Goals  To learn how to choose appropriate classes to implement  To understand the concepts of cohesion and coupling.
Fall 2006Adapded from Java Concepts Slides1 Designing Classes Advanced Programming ICOM 4015 Lecture 8 Reading: Java Concepts Chapter 8.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Chapter 9 – Designing Classes. Chapter Goals Discovering and learning to Design classes Discovering and learning to Design classes Preconditions and postconditions.
Chapter 3 Implementing Classes
Chapter 8 – Designing Classes
Objects as a programming concept
CSSE 220 Day 17 Introduction to your Vector Graphics project
Lecture 3 John Woodward.
Chapter 8 Designing Classes
Lecture 7 Designing Classes
Implementing Classes Yonglei Tao.
Chapter 11 – Interfaces and Polymorphism
Chapter Three - Implementing Classes
CSC 480 Software Engineering
Chapter 8 – Designing Classes
Introduction to Computer Science and Object-Oriented Programming
More on Classes and Objects
Chapter 8 – Designing Classes
JAVA CLASSES.
Information Hiding and Encapsulation Section 4.2
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Class Design CSC 171 FALL 2004 LECTURE 11

READING Read Chapter 7 It’s abstract But it should help with project #1

Design Methodology 1. Problem Definition 2. Requirements Analysis 3. Architecture 4. Construction 5. Testing 6. Future Improvements

Software Lifecycle

Classes and Objects Object oriented programs – Define classes of objects – Make specific object out of class definitions – Run by having the objects interact A class is a type of thing – Instructor An object is a specific thing – Ted An object is an instance of a class

How do we choose a class to implement? A single concept A “real world” thing – Student, car, monster An actor – StringTokenizer : “one who does” A utility – The “math” class.

Cohesion A “cohesive” class has a public interface closely related to the single concept that the class represents

Incohesive public class Purse { public Purse(){...} public void addNickels(int count){...} public void addDimes(int count){...} public void addQuarters(int count){...} public double getTotal(){...} public static final double NICKEL_VALUE =0.05; public static final double DIME_VALUE =0.1; public static final double QUARTER_VALUE =0.25;... }

Cohesive Concepts public class Coin { public Coin(double aValue,String aName){...} public double getValue(){...}... } public class Purse { public Purse(){...} public void add(Coin aCoin){...} public double getTotal(){...}... }

Coupling A class depends on another if it calls one of its methods Purse depends on Coin because it calls getValue on coins Coin does not depend on Purse High Coupling = many class dependencies Minimize coupling to minimize the impact of interface changes

Coupling & Dependency

Design Exercise Consider the following JAVA application What would make good “classes”

Accessor and Mutator Classes Accessor: does not change the state of the implicit parameter (e.g. getBalance) Mutator: changes the state of the implicit parameter (e.g. deposit ) Rule of thumb: Mutator should return void Immutable class: all methods are accessors (e.g. String)

Side Effects Side Effect: any observable change outside the implicit parameter Example: printing in method public void deposit(double amount){ if (amount < 0) System.out.println("Bad value");... }

Preconditions Document the parameters of a method Typical use: – To restrict the parameters of a method – To require that a method is only called when the object is in an appropriate state Method can do anything if called when precondition not fulfilled

Preconditions Method may throw exception if precondition violated if (amount < 0) throw new IllegalArgumentException(); balance = balance + amount;

Postconditions Condition that's true after a method has completed. Contract: If caller fulfills precondition, method must fulfill postcondition

Static Methods Every method must be in a class E.g. if all parameters are primitive class Numeric { public static boolean approxEqual(double x, double y) {...} } Call with class name instead of object: if (Numeric.approxEqual(a, b))... main is static--there aren't any objects yet Too many static methods are a sign of too little OO

Static Fields “Class variables” One field per class Minimize the use of static fields. Static final fields are ok. “class constants”

public class BankAccount {... private double balance; private int accountNumber; private static int lastAccountNumber; }

public BankAccount() { lastAssignedNumber++; // increment static field accountNumber = lastAssignedNumber; // set instance field }