|
Packages
|
|
|
In previous lessons, we were creating our classes either in
the same file or each in its own file. We also had to remember in which file a
class was. The file also had to bear the same name as the class, the main
class of the file, which was usually marked as public. Based on the rules of
operating systems, we could not have two files with the same name in the same
directory. This is convenient if/when you work alone.
|
In a group of programmers
where people work on the same project, each one may be allowed to create the
classes that he or she judges necessary. With people working in different
departments, different companies, or even different countries, it would not be a
surprise to end up with classes with similar names. This would not work and a large
maintenance job could take place to synchronize the classes. The solution to
such a messy scenario consist of using packages.
A package is a technique of organizing the classes to group
them in entities. Inside of such an entity, each class is created but must be
unique. By using different packages, a class in one package can bear the same
name as a class in another package. This is safe because the classes would
belong to different packages, which eliminates name conflicts.
Practical
Learning: Introducing Packages |
|
- Start NetBeans if necessary.
To start a new application, on the main menu, click File -> New
Project...
- In the Categories section, make sure Java is selected.
In the Projects section, make sure Java Application is selected.
Click Next
- Set the Project Name to Geometry5
- Uncheck Create Main Class
- Click Finish
To start a package, create a folder for the class(es) that
it will contain. If this is the first file, in Lesson 1, we learned how to
create the first folder of an application and how to save a file in it. Such a
project is said to use the default package. To create a package, follow these
rules:
- In the top section of the file that will use it, type package
followed by the name of the package, followed by a semi-colon. The name
follows the same rules of names in Java
- This line that contains package must
be the first line of code in the document. This means that before it, you can
include only either empty spaces or comments
- Save the file in a folder that holds the same name as the package. This
means that if you started the file with package Project1;, you must
save that file in a folder named Project1
In the document, you can type regular code as we have
learned in previous lessons. This means that, at a minimum, you can create a
class. In the same way, you can create classes that are based on other classes
or that implement an (some) interface(s).
Practical
Learning: Creating a Package |
|
- In the Projects window, under Geometry5, right-click Source Packages,
position the mouse on New, and click Java Package...
- Set the Name to CentralProcessing
- Click Finish
- Expand CentralProcessing if necessary by clicking its + button.
To create a class and put in a package, in the Projects window,
right-click CentralProcessing -> New -> Java Class...
- Set the Name to Central
- Click Finish
- Change the file as follows:
package CentralProcessing;
public class Central {
public static void main(String args[]) {
}
}
|
- To create another package, in the Projects window, under Geometry5,
right-click Source Packages -> New -> Java Package...
- Set the Name to Surfaces and press Enter
- To create a class to associate to the new package, in the Projects window,
right-click Surfaces -> New -> Java Class...
- Set the Name to Square and click Finish
- Change the file as follows:
package Surfaces;
public class Square {
protected double sd;
public Square() {
sd = 0.00;
}
public Square(double side) {
sd = (side <= 0) ? 0.00 : side;
}
public double getSide() {
return sd;
}
public void setSide(double side) {
sd = (side <= 0) ? 0.00 : side;
}
public double calculatePerimeter() {
return sd * 4;
}
public double calculateArea() {
return sd * sd;
}
}
|
- Save the file
After creating a class in a package, you can use it. You
have various options. To declare an instance of the class, use the name of the
class preceded by the name of the package. Here is an example:
File: \Geometry\Square.java |
package Geometry;
public class Square {
public double side;
public double calculateArea()
{
return side * side;
}
}
|
File: Exercise.java |
public class Exercise {
public static void main(String[] args) {
Geometry.Square sqr = new Geometry.Square();
sqr.side = 32.40;
System.out.println("\nSquare Characteristics");
System.out.printf("Side: %.2f", sqr.side);
}
}
|
Here is an example of compiling and executing the project:
C:\Exercise>javac Exercise.java
C:\Exercise>java Exercise
Square Characteristics
Side: 32.40
Practical
Learning: Using a Package |
|
- Click the Central.java tab and change its document as follows:
package CentralProcessing;
public class Central {
public static Surfaces.Square create() {
Surfaces.Square geometric = new Surfaces.Square(48.26);
return geometric;
}
public static void show(Surfaces.Square figure) {
System.out.println("\nSquare Characteristics");
System.out.printf("Side: %.3f\n", figure.getSide());
System.out.printf("Perimeter: %.3f\n", figure.calculatePerimeter());
System.out.printf("Area: %.3f\n", figure.calculateArea());
}
public static void main(String args[]) {
Surfaces.Square sqr = create();
show(sqr);
}
}
|
- To execute the application, press F6
- In the Run dialog box that appears, make sure CentralProcessing.Central is
selected and click OK
Square Characteristics
Side: 48.260
Perimeter: 193.040
Area: 2329.028
|
Using Various Packages and Various Classes
|
|
In the same way, you can use classes from other packages as
long as you precede them with the names of their parent packages. Here is an
example:
File: \Geometry\Square.java |
package Geometry;
public class Square {
public double side;
public double calculateArea()
{
return side * side;
}
}
|
File: \Figures\Square.java |
package Figures;
public class Square {
public double side;
public double calculatePerimeter()
{
return side * 4;
}
}
|
File: Exercise.java |
public class Exercise {
public static void main(String[] args) {
Geometry.Square sqr1 = new Geometry.Square();
sqr1.side = 32.40;
System.out.println("\nSquare Characteristics");
System.out.printf("Side: %.2f\n", sqr1.side);
System.out.printf("Area: %.2f\n", sqr1.calculateArea());
Figures.Square sqr2 = new Figures.Square();
sqr2.side = 68.25;
System.out.println("\nSquare Characteristics");
System.out.printf("Side: %.2f\n", sqr2.side);
System.out.printf("Perimeter: %.2f\n", sqr2.calculatePerimeter());
}
}
|
Here is an example of compiling and executing the project:
C:\Exercise>javac Exercise.java
C:\Exercise>java Exercise
Square Characteristics
Side: 32.40
Area: 1049.76
Square Characteristics
Side: 68.25
Perimeter: 273.00
Another option is to import the package. To do this, in the
file where you will need it, type import, followed by the name of the
package, followed by a period, followed by the name of the class you want to
use, and followed by a semi-colon. After typing it, to use the class, you can
omit preceding it with the name of the package. Here is an example:
import Geometry.Square;
public class Exercise {
public static void main(String[] args) {
Square sqr1 = new Square();
sqr1.side = 50.35;
System.out.println("\nSquare Characteristics");
System.out.printf("Side: %.2f\n", sqr1.side);
System.out.printf("Area: %.2f\n", sqr1.calculateArea());
}
}
Even if you import a package, you can still precede the
class with the name of the package. Here is an example:
import Geometry.Square;
public class Exercise {
public static void main(String[] args) {
Geometry.Square sqr = new Geometry.Square();
sqr.side = 50.35;
System.out.println("\nSquare Characteristics");
System.out.printf("Side: %.2f\n", sqr.side);
System.out.printf("Area: %.2f\n", sqr.calculateArea());
}
}
In the same way, you can import as many packages as you
want.
Practical
Learning: Using Various Packages |
|
- To create another package, in the Projects window, under Geometry5,
right-click Source Packages -> New -> Java Package...
- Set the Name to Rounds and click Finish
- To create a class to associate to the new package, in the Projects window,
right-click Rounds -> New -> Java Class...
- Set the Name to Circle and click Finish
- Change the file as follows:
package Rounds;
public class Circle {
protected double rad;
public Circle() {
rad = 0.00;
}
public Circle(double radius) {
rad = (radius <= 0) ? 0.00 : radius;
}
public double getRadius() {
return rad;
}
public void setRadius(double radius) {
rad = (radius <= 0) ? 0.00 : radius;
}
public double calculateDiameter() {
return rad * 2;
}
public double calculateCircumference() {
return calculateDiameter() * 3.14159;
}
public double calculataArea() {
return rad * rad * 3.14159;
}
}
|
- Click the Central.java tab and change its document as follows:
package CentralProcessing;
public class Central {
public static Surfaces.Square createSquare() {
Surfaces.Square geometric = new Surfaces.Square(48.26);
return geometric;
}
public static void show(Surfaces.Square figure) {
System.out.println("\nSquare Characteristics");
System.out.printf("Side: %.3f\n", figure.getSide());
System.out.printf("Perimeter: %.3f\n", figure.calculatePerimeter());
System.out.printf("Area: %.3f\n", figure.calculateArea());
}
public static Rounds.Circle createCircle() {
Rounds.Circle geo = new Rounds.Circle(48.26);
return geo;
}
public static void show(Rounds.Circle figure) {
System.out.println("\nCircle Characteristics");
System.out.printf("Radius: %.3f\n", figure.getRadius());
System.out.printf("Diameter: %.3f\n",
figure.calculateDiameter());
System.out.printf("Circumference: %.3f\n",
figure.calculateCircumference());
System.out.printf("Area: %.3f\n", figure.calculataArea());
}
public static void main(String args[]) {
Surfaces.Square sqr = createSquare();
show(sqr);
Rounds.Circle circular = createCircle();
show(circular);
}
}
|
- Execute the application
Square Characteristics
Side: 48.260
Perimeter: 193.040
Area: 2329.028
Circle Characteristics
Radius: 48.260
Diameter: 96.520
Circumference: 303.226
Area: 7316.850
|
- To use the import keyword, change the Central.java document as
follows:
package CentralProcessing;
import Surfaces.Square;
import Rounds.Circle;
public class Central {
public static Square createSquare() {
Square geometric = new Square(66.47);
return geometric;
}
public static void show(Square figure) {
System.out.println("\nSquare Characteristics");
System.out.printf("Side: %.3f\n", figure.getSide());
System.out.printf("Perimeter: %.3f\n", figure.calculatePerimeter());
System.out.printf("Area: %.3f\n", figure.calculateArea());
}
public static Circle createCircle() {
Circle geo = new Circle(122.40);
return geo;
}
public static void show(Circle figure) {
System.out.println("\nCircle Characteristics");
System.out.printf("Radius: %.3f\n", figure.getRadius());
System.out.printf("Diameter: %.3f\n",
figure.calculateDiameter());
System.out.printf("Circumference: %.3f\n",
figure.calculateCircumference());
System.out.printf("Area: %.3f\n", figure.calculataArea());
}
public static void main(String args[]) {
Square sqr = createSquare();
show(sqr);
Circle circular = createCircle();
show(circular);
}
}
|
- Execute the application to see the result:
Square Characteristics
Side: 66.470
Perimeter: 265.880
Area: 4418.261
Circle Characteristics
Radius: 122.400
Diameter: 244.800
Circumference: 769.061
Area: 47066.547
|
Using Various Classes of the Same Package
|
|
To add another class to an existing package, you can create
it in its own file but start the file with the package keyword followed by the
name of the intended package and a semi-colon. After creating the class, you can
use with any of the techniques we have applied so far. Here is an example:
File: \Geometry\Square.java |
package Geometry;
public class Square {
public double side;
public double calculateArea()
{
return side * side;
}
}
|
File: \Geometry\Rectangle.java |
package Geometry;
public class Rectangle {
public double length;
public double width;
public double calculateArea()
{
return length * width;
}
}
|
File: Exercise.java |
public class Exercise {
public static void main(String[] args) {
Geometry.Square sqr = new Geometry.Square();
sqr.side = 50.35;
System.out.println("\nSquare Characteristics");
System.out.printf("Side: %f\n", sqr.side);
System.out.printf("Area: %f\n", sqr.calculateArea());
Geometry.Rectangle rect = new Geometry.Rectangle();
rect.length = 124.602;
rect.width = 88.94;
System.out.println("\nRectangle Characteristics");
System.out.printf("Length: %f\n", rect.length);
System.out.printf("Width: %f\n", rect.width);
System.out.printf("Area: %f", rect.calculateArea());
}
}
|
Here is an example of compiling and executing the project:
C:\Exercise>javac Exercise.java
C:\Exercise>java Exercise
Square Characteristics
Side: 50.350000
Perimeter: 2535.122500
Rectangle Characteristics
Length: 124.602000
Width: 88.940000
Area: 11082.101880
Notice that both the Square and the Rectangle classes belong
to the same package named Geometry. We saw earlier that, when declaring a
variable for a class, instead of preceding the name of a class with its package,
you could import it. In the same way, you could import each one of its classes.
Here is an example:
File: \Geometry\Square.java |
package Geometry;
public class Square {
public double side;
public double calculateArea()
{
return side * side;
}
}
|
File: \Geometry\Rectangle.java |
package Geometry;
public class Rectangle {
public double length;
public double width;
public double calculateArea()
{
return length * width;
}
}
|
File: Exercise.java |
import Geometry.Square;
import Geometry.Rectangle;
public class Exercise {
public static void main(String[] args) {
Square sqr = new Square();
sqr.side = 62.48;
System.out.println("\nSquare Characteristics");
System.out.printf("Side: %f\n", sqr.side);
System.out.printf("Area: %f\n", sqr.calculateArea());
Rectangle rect = new Rectangle();
rect.length = 202.212;
rect.width = 144.62;
System.out.println("\nRectangle Characteristics");
System.out.printf("Length: %f\n", rect.length);
System.out.printf("Width: %f\n", rect.width);
System.out.printf("Area: %f\n", rect.calculateArea());
}
}
|
Here is an example of compiling and executing the project:
C:\Exercise>javac Exercise.java
C:\Exercise>java Exercise
Square Characteristics
Side: 62.480000
Area: 3903.750400
Rectangle Characteristics
Length: 202.212000
Width: 144.620000
Area: 29243.899440
Instead of importing each individual class, you can import
all classes that are part of a package. To do this, type import, followed by the
name of the package, followed by .*; Here is an example:
import Geometry.*;
public class Exercise {
public static void main(String[] args) {
Square sqr = new Square();
sqr.side = 48.62;
System.out.println("\nSquare Characteristics");
System.out.printf("Side: %f\n", sqr.side);
System.out.printf("Area: %f\n", sqr.calculateArea());
Rectangle rect = new Rectangle();
rect.length = 212.202;
rect.width = 62.144;
System.out.println("\nRectangle Characteristics");
System.out.printf("Length: %f\n", rect.length);
System.out.printf("Width: %f\n", rect.width);
System.out.printf("Area: %f", rect.calculateArea());
}
}
To assist you with application programming, you will use
many classes that have already been created. These classes were stored in
various packages. In the next sections and lessons, we will review some of the
most commonly used classes. Every time, we will mention in what package a class
is created.
Practical
Learning: Using Various Classes of the Same Package |
|
- To create another class for the Surfaces package, in the Projects window,
right-click Surfaces -> New -> Java Class...
- Set the Name to Rectangle and click Finish
- Change the file as follows:
package Surfaces;
public class Rectangle {
protected double len;
protected double hgt;
public Rectangle() {
len = 0.00;
hgt = 0.00;
}
public Rectangle(double length, double height) {
len = length;
hgt = height;
}
public double getLength() {
return len;
}
public void setLength(double length) {
len = (length <= 0) ? 0.00 : length;
}
public double getHeight() {
return hgt;
}
public void setHeight(double height) {
hgt = (height <= 0) ? 0.00 : height;
}
public double calculatePerimeter() {
return (len + hgt) * 2;
}
public double calculateArea() {
return len * hgt;
}
}
|
- Access the Central.java file and change it as follows:
package CentralProcessing;
import Surfaces.Square;
import Surfaces.Rectangle;
public class Central {
public static Square createSquare() {
Square geometric = new Square(228.259);
return geometric;
}
public static void show(Square figure) {
System.out.println("\nSquare Characteristics");
System.out.printf("Side: %.3f\n", figure.getSide());
System.out.printf("Perimeter: %.3f\n", figure.calculatePerimeter());
System.out.printf("Area: %.3f\n", figure.calculateArea());
}
public static Rectangle createRectangle() {
Rectangle recto = new Rectangle(94.509, 62.117);
return recto;
}
public static void show(Rectangle figure) {
System.out.println("\nRectangle Characteristics");
System.out.printf("Length: %.3f\n", figure.getLength());
System.out.printf("Height: %.3f\n", figure.getHeight());
System.out.printf("Perimeter: %.3f\n", figure.calculatePerimeter());
System.out.printf("Area: %.3f\n", figure.calculateArea());
}
public static void main(String args[]) {
Square sqr = createSquare();
show(sqr);
Rectangle rect = createRectangle();
show(rect);
}
}
|
- Execute the application to see the result:
Square Characteristics
Side: 228.259
Perimeter: 913.036
Area: 52102.171
Rectangle Characteristics
Length: 94.509
Height: 62.117
Perimeter: 313.252
Area: 5870.616
|
- To use the * operator, change the document as follows:
package CentralProcessing;
import Surfaces.*;
public class Central {
. . . No Change
}
|
|
|