Download EE2E1 Lecture 3 old

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
EE2E1. JAVA Programming
Lecture 3
Java Programs and Packages
Contents
Java source files and class files
 Packages
 public/package/private scope
 The CLASSPATH environment variable

Java source files and class files
Java progams execute on the Java Virtual
Machine (JVM) which is usually
implemented as an interpreter and executed
with the java command
 Java source code is compiled to bytecode by
the compiler executed with the javac
command

Java source file Test.java
public class Test{
public static void main(String[] args)
{ System.out.println(“Hi!”);}
}
Java compiler
Java class file
Test.class
Java bytecode
Hi!
Java interpreter
(JVM)

The name of the source file (Test.java)
matches the name of the public class (Test)
and produces a bytecode file Test.class

Only one public class per file is allowed
(see later - public/package/private scope)

A source file can contain any number of
classes and compilation produces a .class
file for each class in the source file
Packages

Java groups classes into packages

The standard Java library is distributed over a
number of packages including java.lang, java.util
indicating sub-packages

java.lang automatically imported into your
programs

Contains a lot of the basic classes we use
(String,System etc)

We can create our own packages using the
package keyword

In the following example, classes A and B are in
the package my_package
package my_package;
class A
{
// A’s public & private members
}
class B
{
// B’s public & private members
}
The default package

If a source file does not begin with the
package statement, the classes contained in
the source file reside in the default package

The java compiler automatically looks in
the default package to find classes (see
later)

It then searches all imported packages to
find the remaining classes (see later)
Importing packages

The import statement imports packages by
directing the compiler where to look

The import statement in a Java program
must occur before any class definition

Either the whole package or a sub-package
can be imported

Either all of the classes in a package can be
imported or just a particular class from the
package
import my_package.*;
// Imports all of the classes
class C
{
// Can refer directly to classes A & B
private A a;
private B b;
}
import my_package.A; // Imports class A only
class C
{
// Can refer directly to class A only
private A a;
private my_package.B b;
}
public/package/private scope

Scope is concerned with the visibility of program
elements such as classes and members

Both classes and class members (methods or
instance fields) can be defined with public,
package or private scope

A class with public scope means it is visible
outside its containing package

Allows Java to find a class (maybe with the
help of CLASSPATH (see later)) as it must be
in a file with the same name as the public class

A class member with public scope means it is
visible anywhere its class is visible

A class member with private scope means it is
visible only within its encapsulating class

A class/class member with package scope means
it is visible only inside its containing package
Example 1
package my_package;
class A
// package scope
{
// A’s public & private members
}
public class B
// public scope
{
// B’s public and private members
}

class A has no private/public label and thus
has, by default, package scope

class B has public scope

class A is visible only within the package
but class B is visible outside the package
also
package another_package;
import my_package.*;
class C
{
// C’s public & private members
// class C ‘knows’ about class B
private B b;
}
// OK – class B has public scope
package my_package;
class D
{
// D’s public & private members
// Class D ‘knows’ about classes A and B
private B b;
private A a;
}
// OK – class B has public scope
// OK – class A has package scope
Example 2
package my_package;
class A
{
int get() { return data; }
public A(int d) { data=d;}
private int data;
}
class B
{
void f()
{
A a=new A(d);
int d=a.get();
int d1=a.data;
}
}
// package scope
// public scope
// private scope
// OK A has package scope
// OK – get() has package scope
// Error! – data is private

Even though member function A() has public
scope, because class A only has package scope, the
class and hence all of its member functions are
only visible inside the package

If member functions are to be publicly visible,
the class itself has to have public scope

In general, classes and their members should be
given the narrowest possible scope appropriate
to their functionality
The CLASSPATH environment variable

The compiler and runtime interpreter know how to
find standard packages such as java.lang and
java.util

The CLASSPATH environment variable is used to
direct the compiler and interpreter to where
programmer defined imported packages can be
found


This then allows the public class in the file to
be found
The CLASSPATH environment variable is an
ordered list of directories and files

Example CLASSPATH’s

Windows
.;C:\java;C:\java\my_jar_file.jar

UNIX
.; /home/java; /home/java/my_jar_file.jar

A jar file is a Java archive file and is a collection
of java class files (with the .class extension)

‘.’ means the current directory. In some Java
implementations, this is part of the CLASSPATH
by default but normally has to be added

The above directories and jar files are searched in the order
they appear in the CLASSPATH list for imported packages

The CLASSPATH list can be overridden with the –
classpath option to the compiler :
(javac –classpath …)
and the interpreter
(java –classpath …)
All files in a package must be in a sub-directory that
matches the full package name
 Eg. all files in package my_package.sub-package are in
a sub-directory my_package/sub-package (Unix) which
branches off one of the directories in the CLASSPATH

// source file “my_package/A.java”
package my_package;
public class A
{
int get() { return data; }
public A(int d) { data=d;}
private int data;
}
// source file “TestA.java”
import my_package.A;
public class TestA
{
public static void main(String[] args){
A a=new A(100); }
}

class TestA is in the default package

Package/sub-package directory matches the
directory/sub-directory structure
Package
Default
package
Directory
Package
my_package
Sub-directory
Sub-package
. (current directory)
./my_package
Example

We can create a Polygon class which represents a
polygon as an array of points (vertices)

The Point class is imported as part of a
(programmer defined) geometrical package geom

A method perimeter() computes the polygon’s
perimiter

class PolygonTest contains a main method to test
the Polygon class
package geom;
public class Point
{
public Point(int x, int y)
{ xpos=x; ypos=y;}
public int getX()
{ return xpos;}
public int getY()
{ return ypos; }
private int xpos, ypos;
}
import geom.Point;
public class Polygon
{
public Polygon(int n, Point[] v)
{
numVertices=n; vertices=new Point[n];
System.arraycopy(v,0,vertices,0,n);
}
public double perimeter()
{
double pm=0; int nn;
for (int n=1; n<numVertices; n++)
{
int xv1= vertices[n-1].getX(); int yv1=vertices[n-1].getY();
nn=n%numVertices;
int xv=vertices[nn].getX(); int yv=vertices[nn].getY();
pm+=(Math.sqrt((xv1-xv)*(xv1-xxv)+(yv1-yv)*(yv1-yv)));
}
return pm;
}
private Point[] vertices;
private int numVertices;
}
import geom.Point;
public class PolygonTest
{
public static void main(String[] args)
{
Point[] pointArray={new Point(0,0), new Point(0,1),
new Point(1,1), new Point(1,0)};
Polygon square=new Polygon(4,pointArray);
double perim=square.perimeter();
System.out.println(“The perimeter is ” + perim);
}
}

class Point is in the geom package whereas classes
Polygon and PolygonTest are in the default package

File Point.java will reside in a sub-directory geom of the
current directory
Default Polygon.java
package PolygonTest.java
Sub-package
geom
Point.java
Directory ‘.’
Sub-directory geom

class Point is made public so it is visible outside of its
package

The geom package could be expanded to include other
basic geometrical entities (line, curve etc)
And finally….



When writing seriously large Java applications,
you will create many classes and several packages
 Related classes will be grouped into packages
When you use a Java API, you normally have to
set up the CLASSPATH environment variable so
that its classes can be imported
 Its important to understand the use of
CLASSPATH
However, for your lab exercises, keeping all of
your public classes in files with the same name as
the class in the current directory (default package)
will ensure all public classes are visible