25
1 COS 260 DAY 14 Tony Gauvin

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

Embed Size (px)

Citation preview

Page 1: 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

1

COS 260 DAY 14

Tony Gauvin

Page 2: 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

2

Agenda• Questions?• 6th 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

Page 3: 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

Designing classes

How to write classes in a way that they are easily

understandable, maintainable and reusable

5.0

Page 4: 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

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

Page 5: 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

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

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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

Page 9: 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

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

Page 10: 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

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

Page 11: 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

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

Page 12: 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

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.

Page 13: 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

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

Page 14: 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

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

Page 15: 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

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; }}

Page 16: 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

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

Page 17: 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

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 middle

Page 18: 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

18

Stack - Conceptual View

https://www.cs.usfca.edu/~galles/visualization/Algorithms.html

Page 19: 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

19

Stack Operations

• Some collections use particular terms for their operations

• These are the classic stack operations:

Page 20: 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

20

Stack Implementation

private Stack<String> 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

Page 21: 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

21

Executing without BlueJAppendix 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

Page 22: 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

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

Page 23: 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

23

Create a stand alone jar file

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

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

Page 25: 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

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