Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming1 Programming class diagrams and Java classes, parameters B 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. HotelRoomGuest
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-many 1..* HotelRoomGuest *
PROGRAM DESIGN Define attributes and features of each concept Java terminology: Attribute corresponds to field Feature corresponds to method Software Systems - Programming 6 Hotel String name String address checkin(String) Room int number Guest getGuest() Guest String name Room getRoom() * 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() * 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 Hotel String name String address checkin(String) Hotel h = new Hotel(“Fawlty Towers”, “Torquay”); Hotel String name String address Calls the constructor of the class. “Fawlty Towers” “Torquay” Attributes Instance Variables
CLASSES AND INSTANCES Strings are reference values Variables store reference to String object Software Systems - Programming 10 Hotel name address checkin(String) Hotel String name String address String value int length “Fawlty Towers” 13 String value int length “Torquay” 7
CLASSES AND INSTANCES Example: Hotel 'Fawlty Towers', rooms 101 and 102 occupied by guest Major Gowen Hotel String name String address Room room1 Room room2 Room int number Guest guest Room int number Guest guest Guest name Room room null “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 Hotel String name String address double vat checkin(String) h1:Hotel String name String address Static attributes are underlined in UML. Hotel double vat 0.06 h2:Hotel String name String address Class Attributes Class Variables
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 to object 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 Method definition 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 ; /** JavaDoc documentation of the class. */ public class CName { /** JavaDoc documentation of the attribute. */ /** JavaDoc documentation of the feature. */ } Details of import : section 2.9 of the book For example Java packages java.util.Date; java.util.*;
Software Systems - Programming 20 DEFINITION OF A CLASS package ss.week1.hotel; /** * Manages a hotel. John Cleese 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. name the guest’s name 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. 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. theName name of the hotel to create 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