Chapter 6: Using the Java Library - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Chapter 6: Using the Java Library

Description:

Java ships with hundreds of pre-built classes. The Java library is known as the Java API. ... Egg b = new Egg(); myList.add(b); int theSize = myList.size(); // 2 ... – PowerPoint PPT presentation

Number of Views:194
Avg rating:3.0/5.0
Slides: 25
Provided by: systema181
Category:

less

Transcript and Presenter's Notes

Title: Chapter 6: Using the Java Library


1
Chapter 6 Using the Java Library
  • Phil FeibishIT 3883

2
Overview
  • Java ships with hundreds of pre-built classes.
  • The Java library is known as the Java API.
  • Continuation of the Sink-A-Dot-Com game from the
    previous chapter. You should understand the 3
    options provided to correct the bug in the
    chapter 6 version of the code.

3
Options to fix the bug
  • Create second array, then check 2nd array each
    time user makes a guess. (Clunky and
    inefficient)
  • Keep only 1 array, but change values of "hit"
    cells to -1. Still clunky and inefficient.
  • Delete cells as they are "hit" but arrays can't
    change size, so we have to continually recreate
    the array and copy all data into smaller, "new"
    array. Still a poor solution due to
    inflexibility of array structure.

4
What we need
  • We need a find a way to create an array which can
    shrink when we remove an element, easily
    determine if an item exists in the array, and
    allow you to retrieve data without knowing the
    index (position) of the data element.
  • Solution Use an ArrayList.

5
ArrayList objects
  • ArrayList is a class in the core Java Library.
  • Contains very useful methods

6
ArrayList sample code
  • ArrayList myList new ArrayList()
  • Egg s new Egg()
  • myList.add(s)
  • Egg b new Egg()
  • myList.add(b)
  • int theSize myList.size() // 2
  • boolean isIn myList.contains(s)// t
  • int idx myList.indexOf(b) // 1
  • boolean empty myList.isEmpty() //false
  • myList.remove(s)

7
Differences between Arrays and ArrayLists
  • Array objects do not have methods.
  • Can remove elements from ArrayList, but not from
    Arrays.
  • ArrayList can change size dynamically.
  • ArrayLists cannot hold primitives. (Workaround
    create objects which hold primitives.)
  • Arrays may be slightly more efficient than
    ArrayLists, but benefit is negligible.

8
Arrays vs. ArrayLists (p133)
  • A "plain old array" has to know its type and size
    at the time it is created.
  • To put an object into a regular array, you must
    assign it to a specific location.
  • ArrayLists are heterogeneous you can put
    different types of objects into a single
    ArrayList.
  • Objects in the ArrayList do NOT know their type
    when they come out of the ArrayList.

9
Type issues in ArrayLists
  • Everything comes out of the ArrayList as the
    generic type "Object", regardless of what type
    the object was when you put it in.
  • Objects in arrays "remember" what they are, which
    means you can assign an array element to a
    variable of the matching type.
  • Objects in an ArrayList need special treatment
    when they come out, having forgotten their type.

10
New and improved DotCom class
  • import java.util.ArrayList
  • public class DotCom
  • private ArrayList locationCells
  • public void setLocationCells(ArrayList loc)
  • locationCells loc
  • public String checkYourself (String userInput)
  • String result "miss"
  • int index locationCells.indexOf(userInput)
  • if (index 0)
  • locationCells.remove(index)
  • if (locationCells.isEmpty())
  • result "kill"
  • else
  • result "hit"
  • //close if
  • // close outer if
  • return result
  • // close method

11
Review Format of prepcode (p140)
  • Variable Declarations
  • Method Declarations
  • Method Implementations
  • Be sure to compare to UML diagrams!

12
Important!
  • Review the DotCom classes and implementation in
    chapter 6 (p138-146).
  • This exercise clearly demonstrates the
    development and implementation of a complete Java
    application.
  • You should understand every line of code in this
    example!

13
Boolean expressions (p147)
  • // and, short circuit operator
  • // or, short circuit operator
  • // and, non short circuit operator
  • // or, non short circuit operator
  • ! // not equals
  • ! // not example !slot1.soldOut()
  • Dont worry about precedence better to use
    parentheses to make your code clear.
  • Non short circuit operators generally used for
    bit operations.

14
Using the Java API
  • Classes are grouped into packages.
  • To use a class, you must know which package the
    class is in.
  • Examples
  • java.swing //GUI
  • java.util //utilities including ArrayList
  • java.lang //System, String, and Math

15
API naming conventions
  • You must know the full name of the class you want
    to use in your code
  • Format .
  • Example packagejava.util
  • class ArrayList
  • full name java.util.ArrayList

16
Identifying classes
  • You must tell the JVM where to find the classes
    you wish to use. Two ways to do this
  • IMPORT Put an import statement at the top of
    your source code file
  • import java.util.ArrayList
  • public class MyClass
  • TYPE Type the full name everywhere in your
    code. Must do this each and every time you use
    it
  • java.util.ArrayList list new java.util.ArrayList
    ()
  • public void go(java.util.ArrayList list)
  • public java.util.ArrayList foo . . .

17
Advantages of packages (p151)
  • Packages help the overall organization of a
    project or library. Avoid one huge pile of
    classes better to group into packages for
    specific functionality.
  • Packages help give you name-scoping to help
    prevent collisions. If you have a class with the
    same name as other code, you can identify your
    class by package name.
  • Packages provide security. Can specify that only
    classes in same package can access its methods.

18
java vs. javax
  • In early generations of Java, all API classes
    began with java.
  • Soon Sun created other packages (extensions) not
    included in standard library.
  • Two types
  • standard
  • not standard
  • Standard extensions named "javax."
  • But what happened when an extension was
    'promoted' to standard library? Name changes
    very painful for existing code

19
"Dumb Questions" (p 153)
  • Does an import make a class bigger? NO it just
    saves you some typing.
  • Why don't I have to import String or System
    class? These classes exist in java.lang which
    does not have to be imported. Classes in
    java.lang are very fundamental.
  • Do I have to put my own classes into packages?
    Ultimately, yes. But it is not necessary for
    small applications.

20
How to find API information?
  • Purchase a textbook / reference (O'Reilly for
    example)
  • Use the HTML API docs (best source, but you must
    download separately)
  • An API reference will provide
  • Class name
  • Package name
  • Class description
  • Methods included in the classes

21
Chapter 6 Bullet Points
  • ArrayList is a class in the Java API.
  • To put something in an ArrayList, use add().
  • To remove something, use remove().
  • To find out where something is (and if it is) in
    an ArrayList, use indexOf().
  • To find out in an ArrayList is empty, use
    isEmpty().
  • To get the size (number of elements), use the
    size() method.

22
Chapter 6 Bullet Points
  • To get the length (number of elements) in a
    regular old array, use the length variable.
  • An ArrayList resizes dynamically.
  • You can put any kind of object into an ArrayList.
    Elements in an array must match the declared
    type.
  • You can't put primitives directly into an
    ArrayList.
  • Classes are grouped into packages.

23
Chapter 6 Bullet Points
  • A class has a full name, which is a combination
    of the package name and the class name. Class
    ArrayList is really java.util.ArrayList
  • To use a class in a package other than java.lang,
    you must tell Java the full name of the class.
  • You use either an import statement at the top of
    your source code, or you can type the full name
    every place you use the class in your code.

24
Questions?
Write a Comment
User Comments (0)
About PowerShow.com