Post on 26-Mar-2015
What have we learned so far?What have we learned so far? Week 1 Summary
There are things called classes that describe things we are interested in representing in software. There are things called objects that are realisations or instantiations or instances of classes. Only objects can be manipulated. The only things we can do to an object are
Inspect the state using a relational expression Alter the state using assignment
Ensuring objects have the correct initial state is absolutely crucial.
Weeks 2 and 3 Summary Inspection and alteration can be combined in three patterns (or
programming constructs) – sequence, selection and iteration For convenience a variety of selection and iteration mechanisms are
provided. Sophisticated computing solutions are created using objects that have
a large number of simple operations, or methods. Methods may require (parameter) data and may provide result
(return) data Methods can be categorised as functions or procedures. Frequently occurring utility objects (e.g. strings) do not need to be
built from scratch and are provided by Java with associated API (method headers) descriptions
Relational ExpressionsRelational Expressions Relational (or boolean) expressions are interesting
because when evaluated they produce one of only two possible results.
To evaluate a relational expression Java simply tests the truth of the expression and reports on the outcome.
Evaluating the test involves deciding whether it is TRUE or FALSE; Yes or No.
Relational expressions are sometimes called conditions.
Relational ExpressionsRelational Expressions The following binary operators are defined for
relational expressions == equal <= Less than or equal
!= not equal >= Greater than or equal
> Greater than < Less than
Note “double =” is used for equality to avoid ambiguity with the assignment operator.
Relational operators are not type specific.
Relational ExpressionsRelational Expressions
A “simple” relational expression involves one operator and two operands.
Examples of “simple” relational expressions4 < 3
WeeklyPay > AverageIndustrialWage
InterestRate <= 6.5
ApplicantsAge >= 18
LottoSlipNumber = = ChosenNumber
CursorOffScreen = CursorPosition > ScreenWidth
FoundIt = CurrentPageInBook = = PageToLookFor
Relational ExpressionsRelational Expressions The last two examples are important because
they highlight the fact that, just like other expressions, the result of a relational expression can be assigned to a variable.
This practice can be a useful documentation aid because it allows us to name conditions thereby making the program a little easier to read.
Relational ExpressionsRelational Expressions More powerful relational expressions are possible
using the Logical Operators AND and OR. In effect, the logical operators are used to “connect”
two or more simple relational expressions thereby forming one “composite” relational expression.
Java uses
&& for specifying AND connections
|| for specifying OR connections
Relational ExpressionsRelational Expressions
For example NumberOfPersons <= LiftCapacity && FloorSelected != CurrentFloor
Age >= 18 && Age <= 65
EntryPoints >= CourseCutOff || Age >= 24
AccountType == Current || AccountType == Deposit
The Logical Operators have some very important behavioural characteristics which we need to remember.
These characteristics are often described pictorially by means of a TRUTH TABLE.
Truth Table for ANDTruth Table for AND
The following truth table applies to the && operator
RelExp1 RelExp2 Result
T T T
F T F
T F F
F F F
Notice that BOTH sub-expressions must be true for the result of the composite expression to be true.
Truth Table for ANDTruth Table for AND
Truth Tables can easily be extended to describe composite expressions involving more than two sub-expressions.
NOTE, that no matter how many subexpressions are used the composite expression always produces a single result and this represents the result of the entire relational expression.
Truth Table for ORTruth Table for OR
The following truth table applies to the || operator
RelExp1 RelExp2 Result
T T T
F T T
T F T
F F F
Notice that when ANY ONE of the sub-expressions is true the result of the composite expression is true.
Truth Table for ORTruth Table for OR
Again, the Truth Table can easily be extended to describe composite expressions involving more than two sub-expressions
NOTE, that no matter how many subexpressions are used the composite expression always produces a single result and this represents the result of the entire relational expression.
Scaling UpScaling Up When we create an algorithm to solve a
particular problem we may find that the solution does not scale up for “larger” instances of the same problem.
For example, our solution for talking like a pirate is adequate (just about) for that particular instance of the problem but it would be completely inadequate, tedious and inefficient if the list had 10000 words or more!
Scaling UpScaling Up Ideally, we should try to develop solutions which lend
themselves to modification for larger problems of the same type.
To create programs like this we need some more tools. One of the major drawbacks of the pirate translator is
that we had to include an if for each word so each one was unique.
Treating each one as unique makes adding new ones problematic.
Scaling UpScaling Up The pirate talker is an example of the type of
situation we encounter quite frequently in programming environments.
Sometimes we would like to be able to treat a number of related items as a collection so that we would not have to explicitly reference each one.
At other times we would like to be able to treat them as separate and unique so that we can process them individually.
Scaling UpScaling Up
Strings are an example of this type of requirement but they have limitations We can only manipulate ‘portions’ of the string We can’t change individual characters (we can of
course inspect them but we can’t alter them) Alterations actually create new strings and discard
the old onesString s = “University” ;String t = “ of Limerick” ;s = s + t ;
ListsLists
A list is a collection of data items. A list has three important aspects
The data in the list The capacity of the list The current usage of the capacity
What kind of ‘things’ would we want to do to a list?
ListsLists
Add/Insert a new item Remove/Delete an existing item Modify an existing item Show the list contents
Sorted in some order? Find/Search the list for an item Browse the list