Transcript COS260Day14

COS 260 DAY 14
Tony Gauvin
1
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
2
Designing classes
How to write classes in a way that
they are easily understandable,
maintainable and reusable
5.0
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
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
A basic enumerated type
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.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
12
The Switch Statement
switch (something) {
case valueofsomething:
java statement;
break;
case valueofsomething:
java statement;
break:
default:
java statement;
break:
}
https://docs.oracle.com/javase/tutorial/java/
nutsandbolts/switch.html
Objects First with
Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
13
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
14
A more robust CommandWord
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;
}
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
15
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
16
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
17
Stack - Conceptual View
https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
18
Stack Operations
• Some collections use particular terms
for their operations
• These are the classic stack
operations:
19
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
20
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
21
To run
On a cmd line type  java ClassName
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
22
Create a stand alone jar file
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
23
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
24
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
25