1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.

Slides:



Advertisements
Similar presentations
Comp1004: Building Better Classes II Software Design Partly based on BlueJ Book – Chapter 7.
Advertisements

Copyright by Scott GrissomCh 7 Designing Classes Slide 1 Well Designed Classes (Ch 7) Applications are a collection of interacting classes Our Goal It.
Lesson-06 Designing classes
Lab 10: Bank Account II Transaction List, error checking & aggregation.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 4.0.
Improving structure with inheritance
Objects First With Java A Practical Introduction Using BlueJ Designing object-oriented programs How to write code in a way that is easily understandable,
Objects First with Java A Practical Introduction using BlueJ
Designing Classes How to write classes in a way that they are easily understandable, maintainable and reusable.
Well-behaved objects Improving your coding skills 1.0.
Grouping Objects 1 Introduction to Collections.
Design: Coupling and Cohesion How to write classes in a way that they are easily understandable, maintainable and reusable.
Make Sure You Know All This!. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
STREAM STREAM A software process The Mañana Principle The Mañana Principle Don’t try to everything at once! Static Methods Static Methods Stateless Utility.
CO320 Introduction to Object- Oriented Programming Michael Kölling 3.0.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
5.0 Objects First with Java A Practical Introduction using BlueJ Introduction to Computer Science I Instructor: Allyson Anderson.
Java Classes Using Java Classes Introduction to UML.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
The Java Programming Language
1 COS 260 DAY 2 Tony Gauvin. 2 Agenda Questions? Class roll call Blackboard Web Resources Objects and classes 1 st Mini quiz on chap1 terms and concepts.
1 COS 260 DAY 1 Tony Gauvin. 2 Agenda Class roll call Instructor Introduction Instructor’s Educational Philosophy Contract on Classroom Behavior Syllabus.
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
Chapter 7 Object-Oriented Design Concepts
Designing Classes 2 How to write classes in a way that they are easily understandable, maintainable and reusable.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
1 CSC 221: Computer Programming I Spring 2008 course overview  What did we set out to learn?  What did you actually learn?  Where do you go from here?
Designing Classes. Software changes Software is not like a novel that is written once and then remains unchanged. Software is extended, corrected, maintained,
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More.
1 COS 260 DAY 10 Tony Gauvin. 2 Agenda Questions? 4 th Mini quiz Today –Chapter 4 Assignment 2 Due Capstone Discussion Proposals Due Oct 15 No class on.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 COS 260 DAY 15 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz Today –Chapter 6 Assignment 4 posted –Due Nov 9 Capstone progress reports are due –Brief.
Objects First With Java A Practical Introduction Using BlueJ Designing classes How to write classes in a way that they are easily understandable, maintainable.
1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.
1 COS 260 DAY 21 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz corrected –Good results 9 Th Mini Quiz Today –40 min covering Chap 9 Assignment 5 Due.
1 COS 260 DAY 17 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz –Chapter 7 –Password “GoBengals” –40 min Assignment 4 posted –Due Nov 9 (one week) Capstone.
1 COS 260 DAY 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)
1 COS 260 DAY 18 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz Graded –Good results 8 th Mini Quiz –Chap 8  Next class Assignment 4 Due Assignment.
6.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
Review. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects and Classes Objects and Classes –State.
Objektorienterad programmering d2, förel. 8
Objects First with Java A Practical Introduction using BlueJ
CSC 221: Computer Programming I Spring 2010
CSC 221: Computer Programming I Fall 2005
COS 260 DAY 19 Tony Gauvin.
Objects First with Java
Objects First with Java
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 16 Tony Gauvin.
COS 260 DAY 10 Tony Gauvin.
COS 260 DAY 2 Tony Gauvin.
COS 260 DAY 18 Tony Gauvin.
COS 260 DAY 15 Tony Gauvin.
Objects First with Java A Practical Introduction using BlueJ
COS 260 DAY 23 Tony Gauvin.
Objektorienterad programmering d2, förel. 8
COS 260 DAY 14 Tony Gauvin.
COS 260 DAY 4 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
Objects First with Java A Practical Introduction using BlueJ
COS 260 DAY 6 Tony Gauvin.
Presentation transcript:

1 COS 260 DAY 14 Tony Gauvin

2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems are posted –Will be due Nov 9 Capstone Proposals Over Due Finish Discussion on Designing Classes

Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0

4 Main concepts to be covered Responsibility-driven design Coupling Cohesion Refactoring Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

5 Review Define –Coupling –Cohesion (Class and Method) –Encapsulation –Code Duplication –Reasonability Driven Design –Localizing change Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

6 Refactoring When classes are maintained, often code is added. Classes and methods tend to become longer. (bloat code) Every now and then, classes and methods should be refactored to maintain cohesion and low coupling. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

7 Refactoring and testing When refactoring code, separate the refactoring from making other changes. First do the refactoring only, without changing the functionality. Don’t add or take away features Test before and after refactoring to ensure that nothing was broken. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

8 Design questions Common questions: –How long should a class be? –How long should a method be? These can now be answered in terms of cohesion and coupling. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

9 Design guidelines A method is too long if it does more then one logical task. A class is too complex if it represents more than one logical entity. Note: these are guidelines - they still leave much open to the designer. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

10 Refactoring example Adding an Item to World of Zuul –Player can pick up or drop an item in a room –Leads to concept of a Player Class What would a Player class look like? –What properties (instance variables) –What behaviors (methods) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

11 Enumerated Types A language feature. Uses enum instead of class to introduce a type name. Their simplest use is to define a set of significant names. –Alternative to static int constants. –When the constants’ values would be arbitrary. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

12 A basic enumerated type Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public enum CommandWord { // A value for each command word, // plus one for unrecognised commands. GO, QUIT, HELP, UNKNOWN; } Each name represents an object of the enum type, e.g., CommandWord.HELP. Enum objects are not created directly. Enum definitions can also have fields, constructors and methods.

13 The Switch Statement switch (something) { case valueofsomething: java statement; break; case valueofsomething: java statement; break: default: java statement; break: } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling nutsandbolts/switch.html

14 Enumerated Type examples Extend the class CommandWords to a use an enumerated type Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

15 A more robust CommandWord Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public enum CommandWord { // A value for each command word along with its // corresponding user interface string. GO("go"), QUIT("quit"), HELP("help"), UNKNOWN("?"); // The command string. private String commandString; /** * Initialise with the corresponding command string. commandString The command string. */ CommandWord(String commandString) { this.commandString = commandString; } /** The command word as a string. */ public String toString() { return commandString; }

16 Class Methods Jus like class variables we can have Class methods by using the reserved word static A static method can be invoked without creating an instance of the class –Math.sqrt(16); Used for ‘utility” methods, Many are found in the java.lang.Math class Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

17 Another Useful Collection  Stacks A stack is a classic collection used to help solve many types of problems A stack is a linear collection whose elements are added in a last in, first out (LIFO) manner That is, the last element to be put on a stack is the first one to be removed Think of a stack of books, where you add and remove from the top, but can't reach into the middl e

18 Stack - Conceptual View

19 Stack Operations Some collections use particular terms for their operations These are the classic stack operations:

20 Stack Implementation private Stack pastMoves; pastMoves = new Stack<>; pastMoves.push(“east”); pastMoves.push(“north”); String lastMove = pastMoves.pop(); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

21 Executing without BlueJ Appendix E To run a java class as a stand alone program you need a “main” method with following signature public static void main(string[] args) { java statements; } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

22 To run Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling On a cmd line type  java ClassName

23 Create a stand alone jar file Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

24 Review Programs are continuously changed. It is important to make this change possible. Quality of code requires much more than just performing correct at one time. Code must be understandable and maintainable. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

25 Review Good quality code avoids duplication, displays high cohesion, low coupling. Coding style (commenting, naming, layout, etc.) is also important. There is a big difference in the amount of work required to change poorly structured and well structured code. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling