Chapter 4 Flashcards

1
Q

Class Libraries

A

Java calls it “libraries”, packages.

Grouping objects is a recurring requirement

The java utility package contains classes for doing this

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Requirement Group Objects

A

-The collection abstraction becomes a class

-The operations are methods of that class

-A particular collection like music collection would be an instance of that class

-Items stored in the instance are object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Collections

A

We specify:
-The type of collection: ArrayList
-The type of object it will contain:<String>
-Private
ArrayList<String>files;
we say "ArrayList of String"</String></String>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Importing

A

-How we gain access to lib classes
-Import statements must be placed before class distinctions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Creating an ArrayList Object

A

-In versions of Java prior to version 7: -files=new ArrayList<String>();</String>

-Java 7 introduced diamond notation:
-Files=new ArrayList<>();

The type of parameter can be inferred from the variable being assigned to a convenience

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Features of the Collection

A

-Increases it capacity as necessary.
-Keeps a private count:
*size() accessor
-Keeps objects in order
-Details of how this is done is hidden

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Using the Collection

A

public class MusicOrganizer & private ArrayList<String>files;
...</String>

public void add file (string filename) & files.add(filename):
*For adding a new file

public int setnumberofFiles() &
return files.sizes():
*For returning of files (Delegation)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Generic Classes

A

-Collections are known as parameterized or generic types

-ArrayLIst implements list functionality:
*add, set, size, etc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

One type parameter says what we want a list of:

A

-ArrayList<Person>
-ArrayList<TicketMachine>
-Etc</TicketMachine></Person>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Index Number

A

-Position of an object in a collection = index

Starts at 0 & last size is -1

-Remove method can change the index value of remaining objects to fill in the gap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The General Utility of Indices

A

Using integers to index collections has a general utility
-‘next’ is: index +1
-‘previous’ is: index -1
-last’ is: list.size() -1

‘The first three’ are the items @ indices 0,1,2…
We could also think about accessing items in sequence:0,1,2….

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Iteration Fundamentals

A

-We often want to perform some actions an arbitrary number of times
-Most programming languages include loop statement to make this possible
-Java has several sorts of loop statements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

For- each loop pseudo code

A

I.e
for(ElementType element: collection) (loop body)

  • for each element in collection, do the things in the loop body
  • Type of loop variable must be the same as the declared element type of the collection we’re going to use
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Critique of for-each loop

A
  • only use for processing whole collection - definite iteration
  • can’t stop part way
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

For- Each loop- search tasks are indefinite

A
  • can’t predict, how many places we will have to look
  • although there may be an absolute limit (like checking every possible location)
  • infinite loops are also possible- through error or the nature of the task
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

While loop syntax

A

while(the keys are missing){
look in the next place;
}
or
while(not the keys have been found){
look in the next place;
}

17
Q

While loop

A
  • uses a boolean condition to decide rather or not to continue
18
Q

For-each vs while loop

A

For-each:
- easier to write
- safer, guaranteed to stop

while:
- Don’t have to process whole collection
- doesn’t have to be used with a condition
- could be infinite

19
Q

Searching a collection

A
  • fundamental activity
  • applicable beyond collections
  • necessarily indefinite iteration
  • must code for both success & failure- exhausted search
  • each of the finishing criteria must make the loop’s condition false to stop loop
  • collection might be empty
  • for each loop inappropriate for searching
20
Q

Phishing a search

A
  • either there are no more files to check:
    index >= files.size()
  • or the item has been found
    found == true
    found
    ! searching
21
Q

Continuing a search

A
  • with a while loop we need to state the condition for continuing
  • so the loop’s condition will be opposite of that for finishing:
    index < files.size() && : found
    index < files.size() && searching
  • NB: ‘or’ becomes ‘and’ when inverting everything
22
Q

Moving away from string

A
  • our collection of string objects for music tracks is limited
  • no separate ID of artist, title, etc
23
Q

Index vs iteration

A

Ways to iterate over a collection
- for- each loop - use if we want to process every element
- while loop - if we want to stop partway through - and for repetition that doesn’t involve a collection
- iterator object - to stop partway through - often used with collections where indexed access is not very efficient or possible - use to remove from a collection

24
Q

Iterator & iterator()

A
  • collections have an iterator() method
  • this returns an Iterator object
  • Iterator <E> has 3 methods:
    1. boolean hasNext()
    2. E next()
    3. void remove()</E>
  • remove method for Iterator :
    it.remove();
25
Q

null

A
  • used with object types
  • used to indicate, ‘no object’
  • test to see if an object holds the null value:
    if(highestBid == null)…
  • used to indicate ‘no bid yet’
26
Q

anonymous objects

A
  • objects are often created and handled on elsewhere immediately:
    Lot furtherLot = new Lot(nextLotNumber, description);
    lots.add(furtherLot);
    we don’t really need furtherLot:
    lots.add(new Lot(…));
27
Q

Chaining method calls

A
  • each object in the chain is called on the object returned from the previous method call in the chain
  • methods often return objects
  • we often immediately call a method on the returned object
    Bid bid = lot.getHighestBid();
    Person bidder = bid.getBidder();
  • we can use the anonymous object concept and chain method calls
    lot.getHighestBid().getBidder()