Chapter 6 Flashcards

1
Q

What does the Java class library documentation include

A

The interface of the class:
-class name
-class description
-list of constructors and methods
-return values & parameters for constructors & methods
-a description of purpose of each constructor & method

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

The Java class library documentation does not include___

A

The implementation of the class:
-private fields
-private methods
-the bodies (source code) of methods

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

Using library classes

A
  • classes organized into packages
  • classes from the library must be imported using an import statement (except classes from the java.lang package)
  • can then be used like classes from the current project
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Methods from string

A
  • contains
  • ends with
  • index of
  • substring
  • to UpperCase
  • trim
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

T or F: strings are mutable

A

F: strings are immutable

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

Which is the correct way to modify a string?
input.toUpperCase();
or
input = input.toUpperCase();

A

2

strings cannot be modified
the correct one doesn’t modify but returns a new string

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

What’s the difference?
if(input == “bye”)
and
if(input.equals(“bye”))

A

1 tests identity

#2 tests equality - whether 2 objects contain the same state
Always use .equals for text equality

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

== vs .equals

A
  1. The main difference between the .equals() method and == operator is that one is a method, and the other is the operator.
  2. We can use == operators for reference comparison (address comparison) and .equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

using random

A
  • Class can be used to generate random numbers
    Random rand = new Random();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Parameterized classes (generic)

A
  • the documentation includes provision for a type parameter:
    ArrayList<E></E>
  • These type names reappear in the parameters & return types
    E get (int index)
    boolean add(Ee)
  • the types in the documentation are placeholders for the types we use in practice
    An Array List <Ticket> actually has methods:
    TicketMachine get(int index)
    boolean add(TicketMachine e)</Ticket>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Packages and import

A
  • single classes may be imported
    import java.util.ArrayList;
  • whole packages can be imported:
    import java.util.*;
  • importation does not involve source code insertion
  • Some classes are used so frequently that almost every class would import them like class string
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

maps

A
  • collections that contains pairs & values
  • pairs consist of a key and a value
  • lookup works by supplying a key & removing a value
  • ideal for one way lookup not reverse
    i.e. telephone book
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

List, map and set

A
  • alternative ways to group objects
  • varying implementations available
  • but hashMap is unrelated to Hashset
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Set

A

A collection that stores each individual element at most once
- does not maintain any specific order

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

set vs ArrayList

A
  • Identical to ArrayList except we used Hashset
  • A list will keep all elements entered in the desired order but set doesn’t
  • List provides access to elements by an index & can contain same element multiple times
  • each element in a set at most once
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Situations to use wrapper classes

A

int, boolean & char
- separate from objects
- their values are not instances of classes
- so not normally possible to add them to a collection
- Wrapper classes are the solution
- wrapper class for int = Integer
Integer iwrap = new Integer(ix);

17
Q

Autoboxing

A
  • performed automatically when a primitive type value is used in a context requiring a wrapper type
  • It makes storing primitive values on a object collection easier
  • applied whenever a primitive type value is passed as a parameter to a method that expects a wrapper type & when a primitive type value is stored in a wrapper type variable
18
Q

Unboxing

A
  • reverse of autoboxing, also automatic
  • applied when a wrapper type value is passed as a parameter to a method that expects a primitive type value & when stored in a primitive type variable
19
Q

Ways to use javadoc

A
  1. switching the pop-up selector @ the top right of the editor window from source code to documentation (or by using toggle documentation view from the editor’s tools menu)
  2. Use project documentation function from main windows tools menu to generate documentation for all classes in the project
20
Q

What should class documentation include?

A
  • class name
  • comment describing overall purpose & characteristics of the class
  • a version #
  • author’s names
  • documentation for each constructor & each method
21
Q

What should constructor & method documentation include?

A
  • method name
  • return type
  • parameter names & types
  • descriptor of purpose & function of the method
  • description of each parameter
  • description of value returned
22
Q

public vs private

A
  • public elements are accessible to objects of other classes- fields, constructors & methods
  • fields should not be public
  • private elements are accessible only to objects of the same class
  • only methods that are intended for other classes should be public
23
Q

information hiding

A
  • Data belonging to on object is hidden from other objects
  • know what an object can do, not how it does it
  • increases the level of independence
  • independence of modules is important for large systems & maintenance
  • private keyword enforces info hiding
24
Q

code completion

A
  • the BlueJ editor supports lookup of methods
  • use ctrl-space after a method-call dot to bring up a list of available methods
  • use return to select a highlighted method
25
Q

class variables

A
  • shared b/t all instances of the class
  • it belongs to the class & exists independent of any instances
  • designated by the static keyword
  • public static variables are accessed via the class name
26
Q

Constants

A
  • a variable, once set, can have its value fixed
  • designated by final keyword
    final int max = list.size();
  • final fields must be set in their declaration or the constructor
  • combining static & final is common
27
Q

class constants

A
  • static: class variable
  • final: constant
  • private static final int gravity = 3;
  • Public visibility is less of an issue with final fields
  • upper case names often used for class constants:
    public static final int BOILING_POINT = 100;
28
Q

class methods

A
  • class methods are conceptually related to class variables & use a related syntax (the static keyword)
    public static int getNumberOfDaysThisMonth()
29
Q

limitations of class methods

A
  1. can’t access any instances fields defined in the class
  2. can’t call an instance method from the class
30
Q

main method

A

public static void main (String args [])

  • to access program without BlueJ- user specifies class and Java will then invoke a method called Main in that class