F21SF Software Engineering Foundations - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

F21SF Software Engineering Foundations

Description:

Finding the Car which goes the furthest //assume first can go furthest ... Car furthestCar = carList.get(0); //for each car, compare with the 'furthest' one ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 22
Provided by: bpal8
Category:

less

Transcript and Presenter's Notes

Title: F21SF Software Engineering Foundations


1
F21SFSoftware Engineering Foundations
  • L10 More on Arrays
  • arrays Cars, 2 D, operations
  • ArrayList
  • StringBuffer class
  • (nothing to do with arrays!)

Monica Farrow EM G30 email
monica_at_macs.hw.ac.uk Material available on Vision
and my website
2
An array of Cars
  • Arrays can store any data type, so lets create
    an array of 3 Cars
  • private Car allCars
  • private static final int NUM_CARS 3
  • allCars new Car NUM_CARS
  • allCars0 new Car("Ford Ka",
  • 40, 33.6, Jo Low")
  • allCars1 new Car("Mercedes Benz E280",
  • 80,22, Tom Grey")
  • allCars2 new Car("VW Sharan",
  • 60, 29, Tim White")

3
Finding the Car which goes the furthest
  • //assume first can go furthest
  • Car furthestCar allCars0
  • //for each car, compare with the 'furthest' one
  • //Should this index start at 0 or 1?
  • for (int index1 indexltNUM_CARS index )
  • if (furthestCar.estimateDistance() lt
  • allCarsindex.estimateDistance())
  • furthestCar allCarsindex

4
The main method
  • Now we can understand the parameter in the main
    method
  • public static void main (String args)
  • The parameter is an array of Strings
  • We probably wont use the parameter much if at
    all in this module
  • However, it is there, and could be used for
    providing runtime data

5
main with parameter
  • public class Greetings
  • public static void main (String arg)
  • System.out.println(Hello

  • args0 args1)
  • gtjavac Greetings.java
  • gtjava Greetings Sarah Jane
  • Hello SarahJane

6
Multiple arrays
  • You can have arrays of many dimensions. The next
    example shows 2D arrays
  • Consider the game of noughts and crosses

7
2 D arrays notation
  • Declare a 2D array with the rows first, then the
    columns
  • int grid new char 33

0 1 2
8
2D arrays contd
  • Each element needs to have the row and the column
    specified
  • grid20 X
  • Use nested loops to traverse complete array
  • for(int row0 rowlt3 row)
  • for (int col 0 collt3 col)
  • myGridrowcol ' '
  • Use meaningful indexes e.g. row, col rather than
    i and j.

9
Arrays (summary)
  • An array in java is a special kind of object,
    with a unique syntax
  • An array is a fixed-length sequence of values of
    the same type
  • Arrays are good for
  • Fixed length sequences
  • Finding or altering elements according to their
    index position
  • An array is a kind of list. Lists are good for
  • Holding elements in order (ideally order of
    adding)
  • Processing each element in turn, by traversing
    the list

10
Problems with arrays
  • An array is created with a fixed size.
  • If our list is variable in size,
  • We need to make sure that there is room to add
    items
  • We need to keep a note of how many items are in
    the array, and alter it if the number of items
    changes
  • Adding and deleting items from the middle of the
    array is time-consuming
  • You shouldnt usually leave gaps in the array, so
    all the elements which come after the deleted
    item have to be shuffled down, one by one
  • Likewise to add in the middle, all the elements
    which will come after the inserted element have
    to shuffled up, one by one (starting at the end)

11
Arrays contd
  • Using arrays involves a block of code for other
    manipulations such as
  • Deleting an item
  • Sorting the array
  • Searching the array for a value
  • Deleting an item

12
ArrayList
  • The ArrayList class in the Java Collections
    Framework stores items in an array and provides
    methods for adding, removing and getting
  • You can use reliable methods for these operations
  • The size of the array is automatically enlarged
    if necessary
  • There is a size() method so you dont have to
    keep track of how many items you have.
  • It is in the java.util package
  • Use an import statement.

13
Generic types
  • lt..gt encloses the class of objects to be stored
    in the collection
  • private ArrayList ltCargt carList
  • Checks at compile time that classes match
  • Whatever goes into this ArrayList must be a Car.
  • Whatever comes out will be a Car.
  • New in java 5. Earlier versions of java did not
    allow the programmer to specify the content type
    of an ArrayList . Generic types are an
    improvement.

14
Creating an ArrayList of Cars
  • We have added an ArrayList of cars to the
    CarComparison class used in L6
  • In the constructor, an empty list is created

import java.util.ArrayList public class
CarComparison private ArrayList ltCargt
carList public CarComparison() carList
new ArrayListltCargt ()
15
Populating the arraylist
  • We use the ArrayList add method to provide Cars
    to be added to the list

public void createCarList() Car myCar new
Car("Ford Ka", 40, 33.6) carList.add(myCar)
for (int i 1 i lt3 i) Car anotherCar
this.getCarFromUser() carList.add(anotherCa
r)
16
Traversing the array
  • Note the use of the ArrayList get method, using
    the required index.
  • There is a special for-each loop for
    collections
  • for (Object-type object-name collection-name)
  • For each item in the collection, perform the loop
    body, referring to the object as object-name

//initially assume first can go furthest Car
furthestCar carList.get(0) //for each car,
compare with the 'furthest' one for (Car
currentCar carList) if (furthestCar.estimat
eDistance() lt currentCar.estimateDista
nce()) furthestCar currentCar
17
Why Arrays
  • Arrays
  • Some programming languages only provide arrays as
    a form of list
  • Fast for accessing elements directly
  • Slow for adding and deleting elements if we want
    to keep the list in a sorted order
  • There are lots of alternative ways of storing
    lists
  • More about all this later in the module

18
String Implementation
  • String is a class
  • Strings are objects not variable
  • Once created the characters of a String object
    cannot be changed
  • String wStr"The weather"
  • wStr wStr" is cold"
  • wStr is actually detached from its first
    instance. A new instance is created leaving the
    old instance 'dereferenced
  • This is not very efficient.

wStr
wStr
The weather
The weather is cold
19
The StringBuffer class - optional
  • The StringBuffer class can be used in preference
    to the String class in applications requiring
    frequent changes to the sequence of characters
  • To create a StringBuffer object containing some
    text
  • StringBuffer textBuf new StringBuffer(mytext
    )
  • Or with no text
  • StringBuffer textBuf new StringBuffer()

20
The StringBuffer class - optional
  • To add some text
  • textBuf.append(more text)
  • To return the String that is the same as the
    StringBuffer
  • String text textBuf.toString()

21
Familiar methods in the StringBuffer class
  • StringBuffer has most of the methods familiar
    from the String class
  • subString, charAt, length
  • and they have similar behaviour
  • indexOf only has version for String parameter
  • And some StringBuffer methods
  • append, replace, insert, delete
Write a Comment
User Comments (0)
About PowerShow.com