Download presentation

Presentation is loading. Please wait.

Published bySamantha Bennett Modified over 3 years ago

1
What have we learned so far? u Week 1 Summary u There are things called classes that describe things we are interested in representing in software. u There are things called objects that are realisations or instantiations or instances of classes. u Only objects can be manipulated. u The only things we can do to an object are u Inspect the state using a relational expression u Alter the state using assignment u Ensuring objects have the correct initial state is absolutely crucial. u Weeks 2 and 3 Summary u Inspection and alteration can be combined in three patterns (or programming constructs) – sequence, selection and iteration u For convenience a variety of selection and iteration mechanisms are provided. u Sophisticated computing solutions are created using objects that have a large number of simple operations, or methods. u Methods may require (parameter) data and may provide result (return) data u Methods can be categorised as functions or procedures. u 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

2
Relational Expressions u Relational (or boolean) expressions are interesting because when evaluated they produce one of only two possible results. u To evaluate a relational expression Java simply tests the truth of the expression and reports on the outcome. u Evaluating the test involves deciding whether it is TRUE or FALSE; Yes or No. u Relational expressions are sometimes called conditions.

3
Relational Expressions u The following binary operators are defined for relational expressions ==equal<=Less than or equal !=not equal>=Greater than or equal >Greater than

4
Relational Expressions u A simple relational expression involves one operator and two operands. u Examples of simple relational expressions 4 < 3 WeeklyPay > AverageIndustrialWage InterestRate <= 6.5 ApplicantsAge >= 18 LottoSlipNumber = = ChosenNumber CursorOffScreen = CursorPosition > ScreenWidth FoundIt = CurrentPageInBook = = PageToLookFor

5
Relational Expressions u 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. u This practice can be a useful documentation aid because it allows us to name conditions thereby making the program a little easier to read.

6
Relational Expressions u More powerful relational expressions are possible using the Logical Operators AND and OR. u In effect, the logical operators are used toconnect two or more simple relational expressions thereby forming one composite relational expression. u Java uses && for specifying AND connections || for specifying OR connections

7
Relational Expressions u For example NumberOfPersons <= LiftCapacity && FloorSelected != CurrentFloor Age >= 18 && Age <= 65 EntryPoints >= CourseCutOff || Age >= 24 AccountType == Current || AccountType == Deposit u The Logical Operators have some very important behavioural characteristics which we need to remember. u These characteristics are often described pictorially by means of a TRUTH TABLE.

8
Truth Table for AND u The following truth table applies to the && operator RelExp1RelExp2Result TTT FTF TFF FFF u Notice that BOTH sub-expressions must be true for the result of the composite expression to be true.

9
Truth Table for AND u Truth Tables can easily be extended to describe composite expressions involving more than two sub-expressions. u 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.

10
Truth Table for OR u The following truth table applies to the || operator RelExp1RelExp2Result TTT FTT TFT FFF u Notice that when ANY ONE of the sub- expressions is true the result of the composite expression is true.

11
Truth Table for OR u Again, the Truth Table can easily be extended to describe composite expressions involving more than two sub- expressions u 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.

12
Scaling Up u 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. u 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 words or more!

13
Scaling Up u Ideally, we should try to develop solutions which lend themselves to modification for larger problems of the same type. u To create programs like this we need some more tools. u 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. u Treating each one as unique makes adding new ones problematic.

14
Scaling Up u The pirate talker is an example of the type of situation we encounter quite frequently in programming environments. u 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. u At other times we would like to be able to treat them as separate and unique so that we can process them individually.

15
Scaling Up u Strings are an example of this type of requirement but they have limitations u We can only manipulate portions of the string u We cant change individual characters (we can of course inspect them but we cant alter them) u Alterations actually create new strings and discard the old ones String s = University ; String t = of Limerick ; s = s + t ;

16
Lists u A list is a collection of data items. u A list has three important aspects u The data in the list u The capacity of the list u The current usage of the capacity u What kind of things would we want to do to a list?

17
Lists u Add/Insert a new item u Remove/Delete an existing item u Modify an existing item u Show the list contents u Sorted in some order? u Find/Search the list for an item u Browse the list

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google