F21SF Software Engineering Foundations - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

F21SF Software Engineering Foundations

Description:

'Mercedes Benz E280', 65, 22); double myDist = myCar.estimateDistance(); double yourDist ... 296 miles, which is 19 miles less than the Mercedes Benz E280. ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 31
Provided by: bpal8
Category:

less

Transcript and Presenter's Notes

Title: F21SF Software Engineering Foundations


1
F21SFSoftware Engineering Foundations
  • 4 Basics B
  • Constants, Math class, selection, boolean
  • Code available on web
  • Car CarMain classes printed today

Monica Farrow EM G30 email
monica_at_macs.hw.ac.uk Material available on Vision
and my website
2
Topic overview
  • Multiple objects. Comparing 2 cars and thinking
    about
  • static constants
  • Math class
  • selection
  • Boolean expressions

3
Multiple objects
  • In the Car class, we can create 2 cars and
    compare how far they can travel
  • Car myCar new Car("Ford Ka", 40, 33.6)
  • Car yourCar new Car
  • ("Mercedes Benz E280", 65, 22)
  • double myDist myCar.estimateDistance()
  • double yourDist
  • yourCar.estimateDistance()
  • double difference yourDist myDist
  • System.out.println ("Diff is "
    difference)

4
Multiple objects
  • One Car class, many objects
  • Each object has its own instance variables
  • Each object can use the classs methods

yourCar
model
Mercedes....
tankSize
65
manfMPG
22
5
Issues arising
  • Using raw numbers (0.22 for gallons per litre) is
    often bad style
  • If we dont know which car goes further, we might
    print out a negative difference
  • Wed like to print out values to one decimal
    point (covered in last lecture)

6
No magic numbers!
  • To convert the manufacturers MPG to MPL, we need
    to multiply by 0.22.
  • We might want to use the conversion factor
    elsewhere in the class
  • We should declare it with the instance variables,
    then it can be used anywhere in the class, and
    its obvious what the number is
  • The instance variables model, tank size and mpl
    will vary with each type of car
  • The conversion factor 0.22 is fixed and will
    never change for ANY car

7
Class Constants
  • Insert these lines just after the instance
    variables
  • //gallons per litre
  • private static final double GPL0.22
  • Use in the methods
  • return tankSize manfMPG GPL

Never changes during the program
Use capitals by convention
private static final double GPL 0.22
Independent of specific objects of the class -
The same for all Car objects
8
Math class
  • A collection of mathematical methods and
    constants
  • Rounding, power, absolute value, sin/cos, pi etc
    (The absolute value of a number is the numerical
    value, disregarding the sign. E.g. it is 4 for
    both 4 and -4)
  • It is available to all java programs
  • The complete specification can be found at
    http//java.sun.com/javase/6/docs/api/
  • You can find the method signatures and a brief
    explanation of the methods purpose

9
The API
  • Java itself is a small language
  • To program in Java, you need to use the Java API
    (Application Programming Interface)
  • The API is divided into packages
  • A package contains a group of related classes
  • Every class in the API is documented

10
Class documentation
  • The documentation will show the classs
    interface
  • the name of the class
  • a general description of the class and its use
  • a list of methods, with parameters and return
    types
  • a description of each method

11
Math abs method - documentation
  • Well use the abs method to disregard the sign
    when printing the difference in distance
  • Here is the java documentation on the method

Method name and link to slightly fuller
description
Parameters incl type
Indep of obj
Return type
Description
12
No implementation
  • In some cases, you might spot a variable, though
    almost all of them are private and not in the
    interface
  • You wont see any implementation
  • For example, you wont see how the method works
  • As a rule, you dont need to know this
  • You need to know what a method does (not how it
    does it)

13
Math abs method - using
  • We use it like this
  • double absDiff Math.abs(difference)String
    absString
  • String.format(.1f, absDiff)
  • Ststem.out.print(Difference is
  • absString)
  • Math methods are all static methods
  • i.e. independent of a particular object
  • No need to instantiate a Math object
  • Call using the name of the class before the
    period

14
Calling methods
  • Usually, methods are Instance methods associated
    with an object which use the instance variables
    belonging to that object. Call using the object
    name and a full stop (period).
  • myCar.estimateDistance() uses the value of
    tankSize associated with the myCar object
  • yourCar.estimateDistance() uses the value of
    tankSize associated with the yourCar object
  • Static methods typically take data from
    parameters and compute a value, like a maths
    function. They use no instance variables of any
    object of the class they are defined in. Just
    call using the name of the class
  • double squareRoot Math.sqrt(45)

15
static
  • static means that it is independent of
    particular objects of that class
  • It is used for class constants
  • It is used for the main method, which is not
    related to any objects
  • public static void main (String arg)
  • All methods in the Math class are static you
    cant make a Math object.
  • As the module proceeds, well meet more static
    constants and methods

16
Selection
  • To improve our message about different distances,
    we need to print different text depending on
    which car can go further.
  • Sample textThe Ford Ka can travel 296 miles,
    which is 19 miles less than the Mercedes Benz
    E280.
  • Algorithm (set of instructions) In English, for
    output
  • Output text for first car up to which is
  • Output the absolute difference and milesif
    first distance is lt second, output
    lessotherwise output moreOutput remaining
    text for second car.What is the logic error
    here?!
  • Is this the only possible algorithm?

17
If-else statement
  • The if-else statement allows you to execute
    alternative blocks of code depending on a
    condition
  • if (myDist lt yourDist)
  • diffMessage absString
  • " miles less than the "
  • else if (myDist gt yourDist)
  • diffMessage absString
  • " miles more than the "
  • else //equal
  • diffMessage "the same as the "

18
Other if statements
  • You can also just have if aloneSystem.out.prin
    tln("coin")if (coinNumber gt 1)
    System.out.println("s")
  • Or one if..else
  • if (coinNumber 1)
  • System.out.println("coin")
  • else //coinNumber gt1
  • System.out.println("coins")

19
More about if
  • Always use curly brackets to enclose the blocks
  • Like my last 2 examples, not like my first
  • You dont NEED to use them if the block has only
    one statement
  • The chances are that you might change your code
    to add a second statement and forget to add the
    curly brackets so start off with them!
  • The if statement evaluates the condition (which
    is given within the rounded brackets) to a
    boolean value, True or False

20
Final output
  • //note the insertion of spaces where necessary
  • System.out.println("The "
  • myCar.getModel()
  • " can travel " myDistString
  • " miles on a full tank, which is "
  • diffMessage yourCar.getModel() )

21
Testing the improved Car class
  • We need to make sure that the improvements work
    in all situations
  • So in the main method, we should
  • Run it with the existing code, where the first
    distance is lt the second
  • Run again, changing the 2nd car so that the
    distances are the same
  • Run again, changing the data so that the first
    distance is gt the second
  • This is messy! We should be able to do all the
    testing in one run. Improvements suggested in
    another lecture

22
Boolean expressions
  • These can become complicated. The basic
    operators are
  • lt (less than)
  • lt (less than or equal to)
  • gt (greater than)
  • gt (greater than or equal to)
  • (equal to)
  • ! (not equal to)
  • Note there are TWO equals signs. (The single
    is the assignment operator.)

23
More about booleans
  • Sometimes you need to make more than one
    comparison
  • E.g. a number is in the range 1 and 12
  • We cant say if (0ltnumlt12)
  • We have to say If the number is greater than 0
    AND ALSO less than or equal to 12
  • if (num gt 0 numlt12)
  • Comparison operators
  • Use for AND - both conditions must be true
  • Use for OR at least one condition must be
    true
  • Use ! for NOT condition must be false

24
boolean type
  • boolean is another of the primitive types in java
  • Variables can be declared of type boolean e.g.
  • boolean isEmployee trueboolean found false
  • Methods can return a boolean type e.g.
  • public boolean tankBigger(int tSize) return
    tankSize gt tSize
  • Use booleans rather than integer flags with
    value 0 or 1
  • if (found) is more readable than if (flag 1)

25
Boolean exercises
  • Write boolean expressions (which could go inside
    the brackets of an if statement), using the
    variables and methods on the previous 2 slides
  • num is not equal to 6
  • num is not in the range 1-12
  • found is true
  • found is false
  • myCar has a tank bigger than 50
  • myCar has a tank less than or equal to 50

26
Scope instance variables
  • Instance variables are declared within a class,
    not within a method
  • they can be used in the constructor and any of
    the methods in that class
  • If they are declared private, they are not
    accessible to any other classes
  • Other classes get at this data via get methods
  • This is the norm in OOP
  • We will some variations on this later in the
    module

27
Scope local variables
  • Variables which are used as useful storage within
    a method are called local variables
  • they can only be used within the method in which
    they are declared
  • The main method contains a number of local
    variables
  • The estimateDistance method could have been
    written with local variables. This is useful if
    you want to check values as you go along, either
    by printing them out or looking at them in a
    debugger.
  • double mpl manfMPG GPL
  • double distance tankSize mpl
  • return distance

28
Scope local variables
  • Parameters are like local variables
  • they can only be used inside the method
  • public Name(String fName, String mName, String
    lName)
  • firstName fName
  • middleName mName
  • lastName lName

29
Debugger
  • Instead of Run As Java Application you can run
    the eclipse debugger
  • First set a breakpoint by the first statement in
    the main method by right-clicking in the left
    hand margin next to the statement, and choose
    Toggle Breakpoint. You should now see a little
    circle in the margin
  • Then choose Debug As
  • The perspective changes
  • The code is displayed with a solid line over the
    statement with the breakpoint, it has stopped
    there
  • Continue by choosing RunStep-over or click the
    icon
  • You can see the variables and their values as
    they are created
  • There are buttons to Step Into other classes,
    resume and terminate
  • StepInto doesnt work for me at the moment

30
Debugger screenshot
variables
Control buttons
Write a Comment
User Comments (0)
About PowerShow.com