Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 COS 260 DAY 14 Tony Gauvin

2 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

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

4 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 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 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 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 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 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 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 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 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 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 https://docs.oracle.com/javase/tutorial/java/ nutsandbolts/switch.html

14 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 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. * @param commandString The command string. */ CommandWord(String commandString) { this.commandString = commandString; } /** * @return The command word as a string. */ public String toString() { return commandString; }

16 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 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 18 Stack - Conceptual View https://www.cs.usfca.edu/~galles/visualization/Algorithms.html

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

20 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 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 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 23 Create a stand alone jar file Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

24 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 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


Download ppt "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."

Similar presentations


Ads by Google