213500 Programmeren 1
6 september 2010
HOORCOLLEGE 2: INTERACTIE EN CONDITIES
213500 PROGRAMMEREN 1
6 SEPTEMBER 2009
Software Systems - Programming 1
Programming
class diagrams and Java classes, parameters201300071-1B Module 2: Software Systems
15 November 2013
Week 1• Values, conditions• Classes, objects
Week 2• Specifications• Testing
Week 3• Abstraction,
inheritance
Week 4• Interfaces, abstract
classes• Arrays
Week 5• Collections• Generic structures
Week 6• Exceptions• Stream I/O• GUIs
Week 7• Concurrency• Networking
Week 8• Security
Week 9/10
Project
Software Systems - Programming 2
OVERVIEW PROGRAMMING LINE
Software Systems - Programming 3
PROGRAM DESIGN
A program must be designed before code is implemented
Example: Hotel Information System
System to store guests of a hotel, including the name and in which room they stay
(In general and sufficient for now) nouns indicate the relevant concepts, in this example:
Guest
Hotel
Room
Software Systems - Programming 4
PROGRAMMA ONTWERP
Ultimately program consists of objects that represent specific hotels, rooms and guests (instances of these concepts)
Examples
‘Hotel Fawlty Towers’
‘Room 101’, ‘Room 102’, etc.
‘Major Gowen’, ‘Miss Tibbs’, etc.
First generalise these objects by defining classes for them.
Hotel Room Guest
PROGRAM DESIGN
Define relations (associations) between the concepts (classes)
Hotel has Rooms Room belongs to a Hotel
Guest occupies a Room Room has (zero or one) Guest
Software Systems - Programming 5
one-to-one
one-to-many1..*
Hotel Room Guest0..11..*
PROGRAM DESIGN
Define attributes and features of each concept
Java terminology:
Attribute corresponds to field
Feature corresponds to method
Software Systems - Programming 6
HotelString nameString addresscheckin(String)
Roomint numberGuest getGuest()
GuestString nameRoom getRoom()
0..11..*
attributes
features
PROGRAM DESIGN
Define attributes and features of each concept
Software Systems - Programming 7
Hotel-String name-String address+checkin(String)
Room-int number+Guest getGuest()
Guest-String name+Room getRoom()
0..11..*
attributes
features
Visibility:-: private, can only be used by class itself+: public, can be used by every class
UML class diagrams:standard notation for classes, their
attributes and features and their relations
Software Systems - Programming 8
REMARKS
Program design is no complete system
Parts like the user interface are missing
Concepts, attributes and features are incomplete
Further development in phases
Design is no program
Gives a specifiation and structure
Not executable: details are missing
Next step: implement (in Java)
There may be more than one (good) design!
CLASSES AND INSTANCES
At runtime there are objects in the system
Class is a recipe for creating objects
Objects instances of a class
Software Systems - Programming 9
HotelString nameString addresscheckin(String)
Hotel h = new Hotel(“Fawlty Towers”, “Torquay”);
HotelString nameString address
Calls the constructor of the class.
“Fawlty Towers”“Torquay”
AttributesInstanceVariables
CLASSES AND INSTANCES
Strings are reference values
Variables store reference to String object
Software Systems - Programming 10
Hotelnameaddresscheckin(String)
HotelString nameString address
Stringvalueint length
“Fawlty Towers”13
Stringvalueint length
“Torquay”7
CLASSES AND INSTANCES
Example: Hotel 'Fawlty Towers', rooms 101 and 102 occupied by guest Major Gowen
HotelString nameString addressRoom room1Room room2
Roomint numberGuest guest
Roomint numberGuest guest
GuestnameRoom room
101
102null
“Fawlty Towers”
“Torquay”
“Majow Gowen”
Primitive values are stored in the variables.
Reference can be null (point to no object).
Software Systems - Programming 11
STATIC ATTRIBUTES
Values of static attributes stored in class variables
Software Systems - Programming 12
HotelString nameString addressdouble vat
checkin(String)
h1:HotelString nameString address
Static attributes are underlined
in UML.
Hoteldouble vat 0.06
h2:HotelString nameString address
ClassAttributes
ClassVariables
VARIABLES
Software Systems - Programming 13
class Hotel {
static double vat;
String name;
String address;
public double getBill(String guestName) {
Room room;
room = this.getRoom(guestName);
return room.getPrice() * (1 + Hotel.vat);
}
}
class variable:once per class
instance variables:once per object
local variables:once per method
execution
VARIABLES
Software Systems - Programming 14
class Hotel {
static double vat;
String name;
String address;
public double getBill(String guestName) {
Room room;
room = this.getRoom(guestName);
return room.getPrice() * (1 + Hotel.vat);
}
}this:
similar to local variable, refers toobject on which this method was called
formal parameter:similar to local
variable,value provided at
method call
Hotel h = ...h.getBill(“Major Gowen”);
Calls a method on object h and passes argument“Major Gowen”.
Software Systems - Programming 15
NAMING
In general
Identifiers consist of letters, digits and underscore “_”
Must start with letter
Conventions
Classes start with upper case letter (Guest)
Attributes / features start with lower case letter (name)
New word within identifier starts with upper case (getName)
Constants use all upper case and underscore to separate words (MAX_VALUE)
Use meaningful names no foo, var, i1, etc.
Cannot use keywords as identifiers
class, if, int, etc.
Software Systems - Programming 16
COMMENTS
Text for documenting the code
One-line: starts with // reaches till the end of the line
Multi-line: Between /* and */
Useful for programmer
Increases comprehensibility
Especially when working in a team
Improves maintainability
Is required in this course!
Software Systems - Programming 17
COMMENTS: JAVADOC
Special kind of comments for documenting how to use a class
Multi-line between /** and */
Start with textual description.
Use tags to document specific information
In front of
Class definition
important tags: @author
Method definition
important tags: @param, @return
Field definition
Software Systems - Programming 18
PACKAGES
Group related classes
Consists of identifiers separated by dots (use lower case letters)
Example: ss.week1.hotel
Must match folder hierarchy
Example: ss\week1\hotel\Guest.java
Package declaration in the first line of the file
package ss.week1.hotel;
Namespace: avoid name clashes between independently developed classes
Fully qualified class names include the containing package
Example: ss.week1.hotel.Guest
Details: section 2.8 in the book
Software Systems - Programming 19
JAVA FILES
package pname;
import <classes-from-other-packages>;
/** JavaDoc documentation of the class. */
public class CName {
/** JavaDoc documentation of the attribute. */
<field declarations>
/** JavaDoc documentation of the feature. */
<method declarations>
}
Details of import: section 2.9 of the book
For exampleJava packagesjava.util.Date;java.util.*;
Software Systems - Programming 20
DEFINITION OF A CLASS
package ss.week1.hotel;
/**
* Manages a hotel.
* @author John Cleese
* @version 2.0
*/
public class Hotel {
...
}
Javadoc-comment
namespace
Javadoc-tag for author of class
Visibility (for now only public for classes)
Javadoc-tag for class version
general description
Software Systems - Programming 21
DEFINITION OF AN ATTRIBUTE
/** * Stored the hotel’s name. */private String name;
/** * Constant representing a 1 star rating.
*/public static final String RATING_ONE_STAR = “*”;
Visibility (for now only public and private for attributes and features)
Field’s type
Class variable instead of instance variable
Value will not change during execution
Software Systems - Programming 22
DEFINITION OF A QUERY
/** * Returns the number of the room * occupied by the specified guest. * @param name the guest’s name * @return the room number */public int getRoomNumber(String name ) {
Room room = getRoom(name);
return room.getNumber();
}
Javadoc-tag for documenting the parameter “name”
formal parameter list, may be empty
result type
statement for returning the query result
Javadoc-tag for documenting the result value
Software Systems - Programming 23
DEFINITION OF A COMMAND
/**
* Finds a free room and
* occupies it with the guest.
* @param name the guest’s name
*/
public void checkin(String name) {
...
return;
}
No result value, therefore the result type is “void”.
Terminates the command without returning a value.
Software Systems - Programming 24
DEFINITION OF A CONSTRUCTOR
/**
* Creates a new Hotel.
* @param theName name of the hotel to create
* @param theAddress address of the hotel to create
*/
public Hotel(String theName, String theAddress ) {
this.name = theName;
this.address = theAddress;
}
Remarks
Constructor creates instances of the class and initialises the attributes
Name of the constructor is the same as the class name
Constructor has no result type
Classes represent concepts
Objects are instances of classes
Class diagrams can specify their attributes, features and relations
Class variables, instance variables, local variables, formal parameters
JavaDoc to document classes, attributes and features
Packages to group classes
Software Systems - Programming 25
MAIN POINTS