Title: Software Engineering Foundations
1Software Engineering Foundations
- 2
- A first
- Object-oriented program
Monica Farrow EM G30 email
monica_at_macs.hw.ac.uk Material available on Vision
and my website
2Topics this lecture
- A definition of UML
- Developing a first object-oriented program,
looking at - Data types
- Instance variables
- Constructors
- Methods
- Parameters
- Scope
3What is (the) UML?
- Unified Modeling Language
- A language for specifying, visualising and
constructing the descriptive diagrams and
documentation of software system - A family of mainly graphical notations
- a picture is worth 1000 words
- Helps in designing and describing software
systems, particularly object-oriented systems - Industry standard for object-oriented modelling
- This module includes an introduction to UML
- Initially UML diagrams will be used occasionally
where appropriate
4Websites
- There is also a lot of UML material on the web
- I recommend
- http//bdn.borland.com/article/0,1410,31863,00.htm
l - http//www.agilemodeling.com/essays/umlDiagrams.ht
m
5 A first object-oriented program
- Wed like a program to meet this
specificationFor any car, we know how much
fuel can be held in a full tank, and the average
miles per gallon. Wed like to know how far a car
might travel on a full tank. British
manufacturers provide the tank size in litres but
fuel consumption in miles per gallon! - Sample output
- A Ford Ka can travel 296 miles on a full tank
6Object Classes
- The idea of a class forms the basis of
object-oriented programming in Java - We model real world objects in a computer program
- Everything about one type of object is
encapsulated in the one place - Because a good program must be
- Readable, Maintainable, well-designed, Robust
- Should not contain duplicate code
7Program structure
- For a small problem, using objects makes the
program much larger than they would otherwise be - We are laying foundations for much larger
programs - The first time you see a class, it looks quite
complicated - Well keep reinforcing class design so that the
ideas sink in over time
8A Program may contain these classes
- A class containing the main method
- Essential. The program starts here. We will use
this in a class by itself. It could be used
within an object class. - Object classes that
- Either we write ourselves e.g. Person, Car, Name
- Or are supplied as part of the java API e.g. for
collections, for GUIs, for random numbers, etc. - Static classes (a later lecture)
- These are independent of an individual object
E.g. Math a collection of useful functions
(rounding, squaring..)
9Example class diagram
- Typically a program will have
- A main method alone in a class to start the
application - A class which handles the main functions of the
program, often interacting through a GUI or text
interface - Classes for distinct objects
- Classes for collections of similar objects
MainClass
Customer
Customer Collection
Process Transactions
Account
Account Collection
10Object-oriented solution
- With object-oriented design, we look at the
problem and see what objects we have, what
attributes the objects have and what operations
need to be performed on the object - One obvious object here is a Car
- Attributes could be the size of the tank, the
average mpg, the model - Operations find distance travelled on full
tank. - i.e. Tank size converted to gallons and
multiplied by avg mpg
11Todays program
- Will consist of
- a class containing a main method
- This is where the program starts
- It will simply create a Car object and obtain
that cars data to display results - A Car class containing everything about cars.
This is a typical class, although very small
12Objects and classes
- Wed like to produce a single template or class
which enables us to hold car details and find
distance information - One Car class
- This is the Source file that we write in java
- A text file named Car.java
- And one or more Car objects
- e.g. Ford Ka, tank size 40 litres, mpg 33.6
- E.g. Mercedes Benz E280, tank size 65 litres, mpg
22
13A class consists of
- Instance variables to describe the data about a
particular object - Eg. Model, tank size etc
- A constructor, to create the object and assign
values to the instance variables - E.g. tankSize 66
- Methods, to perform operations on the data.
Similar to functions and subroutines in other
languages - E.g. find out how far the car can travel
- E.g. tell us what the tank size is
14Class outline
- public class Classname
- //instance variables
- //constructor(s)
- //methods
-
- So a Car class starts
- public class Car
-
15Instance variables
- Instance variables have an access modifier, a
data type and a name (identifier) - The access modifier is usually private, meaning
that other classes cant use the variable
directly - The data type describes the data
- E.g. String for text, int for integer etc
- The identifier should be a meaningful name,
starting lower case by convention - Declare them at the start or the end of the
class. I prefer the start, because you can see
them easily - e.g. private String model
16Car class instance variables
- //model
- private String model
- //tank size in litres
- private int tankSize
-
- // miles covered per gallon
- //(manufacturer's figures)
- private double manfMPG
-
17CONSTRUCTOR
- First an object must be created
- The constructor has the SAME NAME AND CASE as
the class (starting with Upper Case) - The constructor should initialise the instance
variables (data) - either to suitable default values
- or from data is passed to the constructor as
parameters
18Creating an object
- We will create objects today in the main method,
using a statement that looks like this
19Executing this call to the Car constructor
- The line below calls the Car constructor and
provides the data for this car. - Car myCar new Car (Ford Ka, 40, 33.6)
- The Car constructor, in the Car.java class,
starts with a header like this
Class name, starts upper case
public Car (String model, int tank, double mpg)
Means that any other class can use the constructor
Type and name of parameters
20Car class - constructor
- The constructors task is to create the object,
usually assigning values to the instance
variables. - The body of the constructor consists of
statements - public Car(String model, int tank,
- double mpg)
-
- //store parameter details into
- //instance variables
- this.model model
- tankSize tank
- manfMPG mpg
21this
- In the constructor
- We initialise the instance variables, often from
a parameter value - One of the parameters has the same name as an
instance variable. We use this to refer to the
instance variable
22The program so far MainCar class
public class MainCar public static void main
(String args) //create a Car Car myCar
new Car(Ford Ka, 40, 33.6) //end main
method //end class
23The program so far Car class
public class Car //instance variables
private String model private int tankSize
private double manfMPG //constructor public
Car(String model, int tank,
double mpg)
this.model model tankSize tank manfMPG
mpg
24Running the program so far
- If we were to run the program as it is
- The program starts in the main method
- The first statement
- declares a variable of type Car and with name
myCar - assigns a value to myCar, using
- The value is a new Car object, created by
executing the Car constructor - Control passes to the Car constructor
- Each statement in the Car constructor is executed
in turn, assigning values to the instance
variables for an object named myCar - Control passes back to the next statement in the
main method - At the moment there isnt one!
25After using the constructor
26UML Sequence diagram
MainCar
main()
myCarCar
new Car(Ka,40,7.4)
27Using the Car class in the main method
- First we create an object of class Car, using the
constructor - Then we can use methods of the Car class
- To be able to print the line A Ford Ka can
travel 296 miles on a full tankwe need get the
name of the model and the number of estimated
miles. - The rest of the text is the same for all cars
- So the Car class must contain methods to provide
this information
28Using the Car class in the main method
- Assuming suitable methods exist in the Car class,
the main method will look like this - //create object
- Car myCar new Car ("Ford Ka", 40, 33.6)
- //get model
- String model myCar.getModel()
- //get estimated distance
- double distance myCar.estimateDistance()
- //print the details to standard output
- System.out.println("A " model " can travel "
distance " miles")
29Points to note on previous code
- When calling the constructor, we provide the same
types as specified in the constructor signature - To call a method from a different class, attach
the method name to an object with a period (full
stop) - E.g. myCar.getModel()
- The method runs, using the instance variables
that belong to myCar as opposed to any other
cars that might have been created - In the main method, we declare temporary local
variables of the correct type to store data in - We can concatenate Strings using the sign
30Whats in a method?
Access modifier
method name, starts lowercase
Return type
Any parameters in brackets (none)
public String getModel() return model
Return statement
One of the instance variables
Curly brackets enclose method body
31Car class methods to get info out
- //Return model
- public String getModel()
- return model
-
-
- //estimate distance car can travel
- public double estimateDistance()
-
- //there are 0.22 gallons per litre
- return tankSize manfMPG 0.22
-
32ARITHMETIC OPERATORS
- Operators are
- addition,
- - subtraction,
- multiplication,
- / division (different effect for integers/reals)
- remainder (only for integers, also called mod)
- , incrementation operators
- --, - decrementation operators
33What goes in methods
- Methods make the program do things
- Methods can, and frequently do, call other
methods - The code inside methods follows the basic ideas
for procedural programs - Sequence (simple statements)
- Selection (branching)
- Repetition (loops)
- Its your job, as a coder, to get things in the
right order
34Running a program with multiple classes
- Compile the main method
- Any associated classes will also be compiled
- Run the main method
- Each statement in the main method is executed in
turn, passing control to constructors and methods
when necessary.
35Sequence diagram
MainCar
Standard output
main()
myCarCar
new Car(Ka,40,7.4)
model getModel()
distance estimateDistance()
println(message incl model distance)
36Class diagram
MainCar main(..)
Car String model int tankSize double manfMPL
String getModel() double estimateDistance()
37Lab One
- See separate handout
- Familiarity with running a java program from the
command line - Familiarity with using an ide such as eclipse
- Run and experiment with the First and the CarMain
programs
38Next
- The next few lectures reinforce the ideas of
classes and objects, while also introducing
selection and repetition