MSc IT - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

MSc IT

Description:

Java allows you to group classes together in a collection called a package ... We have used packages ... catch (IOException e) {} //nothing done with ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 25
Provided by: ingr96
Category:
Tags: ioexception | msc

less

Transcript and Presenter's Notes

Title: MSc IT


1
MSc IT Computing Object Oriented Programming
  • Java
  • Lecture 2

2
Topics Covered
  • Packages
  • Javadoc
  • Scope
  • Review
  • Phase test 1 - Formative

3
Packages
4
What is a Package?
  • Java allows you to group classes together in a
    collection called a package
  • Packages are used to
  • organise work
  • separate your work from libraries provided by
    others
  • most importantly to avoid naming clashes

5
Using Packages
  • We have used packages throughout our programming
  • importing classes from java.awt, java.util, etc
  • We have also used java.lang, but without an
    import statement
  • that is assumed
  • An import statement merely allows us to use a
    class name without its package name
  • e.g. use Hashtable rather than java.util.hashtable

6
Importing a Package
  • We may import an entire package makes all the
    classes defined there available
  • import java.util.
  • Or separate classes
  • import java.util.Hashtable
  • There is no overhead at compile or run time in
    using .
  • the only possible problem would be when two
    classes from different packages have the same
    name
  • then only the classes required are imported
  • if more than one with the same name are actually
    needed, the full names are used

7
Naming Packages
  • All our own work so far has gone into the default
    package
  • Packages are named hierarchically
  • all java packages start with java.
  • except swing, which retains the javax.
    (extension) package name
  • from when it was an extension
  • Sun recommends that using your companys domain
    name, reversed, as a package prefix
  • e.g. uk.ac.dmu.mypackage

8
  • The first line of a file specifies the package to
    which its classes belong
  • Eg the first line of the Class file for
    GregorianCalender.java is
  • package java.util
  • Therefore the first line of a file creating
    mypackage would be
  • package uk.ac.dmu.mypackage

9
Locating Packages
  • The default package has no package path
  • if the current directory (.) is part of the class
    path
  • for practical use when developing programs, it
    should be
  • then the classes without a package declaration in
    the current directory automatically become part
    of the default package
  • JDK stores packages in subdirectories or ZIP or
    JAR files
  • e.g all files belonging to java.util are in a
    subdirectory java\util (or java/util on UNIX)
  • Subdirectories need not branch off the root, they
    can branch off at any point in the class path
  • e.g if the java packages are in K\JDK.1.2.1\BIN
    then java.util will be found in
    K\JDK1.2.1\BIN\JAVA\UTIL

10
Compiling
  • When the java compiler searches for a class it
    firstly examines all files in the default
    package, those are in all directories directly on
    the class path
  • It then examines the imported packages, by
    concatenating the class file name and package
    names with all the directories on the class path
  • e.g. suppose a class path of k\jdk1.2.1\bin\
  • there are imports of java.util and java.awt and
    the compiler is looking for a class called
    Example.class

11
  • It searches all files in the bin directory, then
    the current directory (.) for Example.class
  • Then it looks for
  • k\jdk1.2.1\bin\java\util\Example.class
  • k\jdk1.2.1\bin\java\awt\Example.class
  • .\java\util\Example.class
  • .\java\awt\Example.class
  • The java.lang package is always searched.
  • You must be sure that you place the class file of
    any package you write in a suitable subdirectory
    from the class path
  • e.g. \uk\ac\dmu\mypackage
  • In practise you will work in the default package
    until your code assumes its final form
  • If you distribute your application, dont forget
    to install the accompanying package

12
JavaDoc
13
What is Javadoc
  • Accurate documentation of code is essential
  • The javadoc utility produces documentation in the
    same form as the API documentation
  • examines the source code
  • for classes, methods and / ./
  • the API documentation is produced by javadoc
  • Output is produced as HTML files
  • Code should always be self documenting
  • separate documentation will tend to be forgotten
    as code is updated
  • use of javadoc ensures that the documentation can
    be read separately

14
Using Javadoc
  • Comments are placed within / /, and placed
    before the feature described
  • Comment the following features
  • Package
  • Public class
  • Public interface
  • Public and protected methods
  • Public and protected variables and constants
  • Comments consist of free form text starting with
    a summary statement
  • Some HTML tags may be included, such as ltBgt lt\Bgt
    for bold
  • dont use headings

15
  • You may then place tags such as
  • _at_see packagenamemethodname(parameterlist)
  • note separates the class name from the method
    name
  •  
  • /
  • A class providing keyboard reader facilities
  • _at_version 1
  • _at_author Susan
  • /

16
  • Method comments include tags
  • _at_param
  • _at_return
  • _at_throws
  •  
  • /
  • method reads from keyboard and returns a String
  • _at_return String
  • /

17
  • To run javadoc you may work in the directory
    containing the code and specify the directory for
    the results
  • javadoc d docDirectory nameOfPackage
  • To document a single class in the current
    directory
  • javadoc KeyboardInput.java
  • See online documentation for more details
  • See KeyboardInput.java example

18
Scope
19
Scope
  • Identifiers are only valid within the block of
    code in which they are created
  • Class variables are global
  • ie valid in all methods within the class
  • Method variables are valid only within the method
  • Loop Control Variables (eg for() loops) are only
    valid within the loop
  • Correct code indentation assists in understanding
    scope

20
Eg KeyboardInput.java
  • private BufferedReader in new BufferedReader
    (new InputStreamReader(System.in))
  • in is a global identifier
  • public synchronized boolean readBoolean()
  • String input ""
  • try
  • input in.readLine()
  • catch (IOException e) //nothing done with
    error!
  • boolean val
  • val (Boolean.valueOf(input)).booleanValue()
  • return val
  • input val are local to the readBoolean() method
  • Reused within the other methods

21
Review
22
We have covered
  • Statements
  • Classes
  • Objects Instantiation
  • Loops
  • for(), while(), do while()
  • Arrays
  • 1- and 2- dimensional
  • Using UML
  • Constructors Overloading

23
References Exercises
  • References
  • http//java.sun.com/j2se/javadoc/writingdoccomment
    s/index.html
  • http//tinf2.vub.ac.be/dvermeir/java/other_doc/Ja
    vaPackages.html
  • Exercises
  • Read through Chapter 3 of Java Gently (much of
    this will be revision, but there is a useful
    section on boolean expressions)
  • Do problem 3.3 on page 109, and problem 3.18 on
    page 112
  • If you complete all of this, try any (or all) of
    the remaining problems at the end of Chapter 3

24
Summary
  • Packages
  • organise work
  • separate your work from libraries provided by
    others
  • most importantly to avoid naming clashes
  • Javadoc
  • produces documentation in HTML format
  • easy to update/maintain
  • Scope
  • validity of identifiers
Write a Comment
User Comments (0)
About PowerShow.com