Home

Packages

 

Packages Fundamentals

 

Introduction

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 LearningPractical Learning: Introducing Packages

  1. Start NetBeans if necessary.
    To start a new application, on the main menu, click File -> New Project...
  2. In the Categories section, make sure Java is selected.
    In the Projects section, make sure Java Application is selected.
    Click Next
     
    New Project
  3. Set the Project Name to Geometry5
  4. Uncheck Create Main Class
     
    New Java Application
  5. Click Finish

Creating a Package

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 LearningPractical Learning: Creating a Package

  1. In the Projects window, under Geometry5, right-click Source Packages, position the mouse on New, and click Java Package...
  2. Set the Name to CentralProcessing
     
    New Java Package
  3. Click Finish
  4. 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...
  5. Set the Name to Central
     
    New Java Class
  6. Click Finish
  7. Change the file as follows:
     
    package CentralProcessing;
    
    public class Central {
        public static void main(String args[]) {
    
        }
    }
  8. To create another package, in the Projects window, under Geometry5, right-click Source Packages -> New -> Java Package...
  9. Set the Name to Surfaces and press Enter
  10. To create a class to associate to the new package, in the Projects window, right-click Surfaces -> New -> Java Class...
  11. Set the Name to Square and click Finish
  12. 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;
        }
    }
  13. Save the file

Using a Package

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 LearningPractical Learning: Using a Package

  1. 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);
        }
    }
  2. To execute the application, press F6
     
    Run Project
  3. 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

 

Using Various Packages

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 LearningPractical Learning: Using Various Packages

  1. To create another package, in the Projects window, under Geometry5, right-click Source Packages -> New -> Java Package...
  2. Set the Name to Rounds and click Finish
  3. To create a class to associate to the new package, in the Projects window, right-click Rounds -> New -> Java Class...
  4. Set the Name to Circle and click Finish
  5. 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;
        }
    }
  6. 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);
        }
    }
  7. 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
  8. 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);
        }
    }
  9. 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 LearningPractical Learning: Using Various Classes of the Same Package

  1. To create another class for the Surfaces package, in the Projects window, right-click Surfaces -> New -> Java Class...
  2. Set the Name to Rectangle and click Finish
  3. 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;
        }
    }
  4. 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);
        }
    }
  5. 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
  6. To use the * operator, change the document as follows:
     
    package CentralProcessing;
    import Surfaces.*;
    
    public class Central {
        . . . No Change
    }
 
 

 

 

Packages and Inheritance

 

Introduction

Inheritance with packages primarily works like normal inheritance we studied in the previous two lessons. Before creating a class that is based on another class, obviously you must know in what package the parent class is created.

To create a class that is based on a class that belongs to a class in a certain package, after the extends keyword, you can precede the name of the parent class with the name of its package. Here is an example:

public class Cube extends Surfaces.Square {

}

Alternatively, you can use the import keyword and optionally omit preceding the parent class by its package. Here is an example:

import Surfaces;

public class Cube extends Square {

}

Practical LearningPractical Learning: Using a Package and Inheritance

  1. To create another package, in the Projects window, under Geometry5, right-click Source Packages -> New -> Java Package...
  2. Set the Name to Boxes and click Finish
  3. To create a class to associate to the new package, in the Projects window, right-click Boxes -> New -> Java Class...
  4. Set the Name to Cube and click Finish
  5. Change the file as follows:
     
    package Boxes;
    
    public class Cube extends Surfaces.Square {
    
        public double calculateTotalArea() {
            return sd * sd * 6;
        }
    
        public double calculateVolume() {
            return sd * sd * sd;
        }
    }
  6. Click the Central.java tab and change its document as follows:
     
    package CentralProcessing;
    import Surfaces.Square;
    import Boxes.Cube;
    
    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 Cube createCube() {
            Cube box = new Cube();
            box.setSide(45.0962117);
            return box;
        }
        
        public static void show(Cube figure) {
            System.out.println("\nCube Characteristics");
            System.out.printf("Side:   %.3f\n", figure.getSide());
            System.out.printf("Area:   %.3f\n", figure.calculateTotalArea());
            System.out.printf("Volume: %.3f\n", figure.calculateVolume());
        }
      
        public static void main(String args[]) {
            Square sqr = createSquare();
            show(sqr);
            
            Cube cb = createCube();
            show(cb);
        }
    }
  7. Execute the application
     
    Square Characteristics
    Side:      228.259
    Perimeter: 913.036
    Area:      52102.171
    
    Cube Characteristics
    Side:   45.096
    Area:   12202.010
    Volume: 91710.737

A Package Inside of Another

In the previous sections, we saw that, to start a package, you create a folder that holds the same name as the intended package. Then, we saw that, to create a class that belongs to the package, in the top section of the document, you type package followed by the name of the package and a semi-colon. We saw that you must save the file in a folder that holds the same name as its package. Here is an example:
File: C:\Geometry\Quadrilaterals\Trapezoid.java
package Quadrilaterals;

public class Trapezoid {
    public double side1;
    public double side2;
    public double side3;
    public double side4;

    public double calculatePerimeter() {
    	return side1 + side2 + side3 + side4;
    }
}

To better organize your classes, you can create one package inside of another. To create one package in an existing package, inside the folder of the parent package, create a sub-folder that holds the same name as the inside package. To create a class that belongs to the child package, in the top section of its document, type package, followed by the name of the parent package, followed by a period, followed by the name of the child package, followed by a semi-colon. Save that file in the sub-folder that holds the name of its package. Here is an example:

File: C:\Geometry\Quadrilaterals\Parallelograms\Square.java
package Quadrilaterals.Parallelograms;

public class Square {
    public double side;

    public double calculatePerimeter()
    {
	return side * 4;
    }
    
    public double calculateArea()
    {
	return side * side;
    }
}

After creating the class, you can use it. Once again, you have options. To declare an instance of the class, type the name of the parent package, followed by a period, followed by the name of the sub-package, followed by a period, and followed by the name of the class. Here is an example:

File: C:\Geometry\Exercise.java
public class Exercise {
    public static void main(String[] args) {
	Quadrilaterals.Parallelograms.Square sqr = 
	    new Quadrilaterals.Parallelograms.Square();

	sqr.side = 48.62;

        System.out.println("\nSquare Characteristics");
        System.out.printf("Side:      %.3f\n", sqr.side);
        System.out.printf("Perimeter: %.3f\n", sqr.calculatePerimeter());
        System.out.printf("Area:      %.3f\n", sqr.calculateArea());
    }
}

Instead of qualifying the class, you can use the import keyword. Here is an example:

File: C:\Exercise\Quadrilaterals\Square.java
import Quadrilaterals.Parallelograms.Square;

public class Exercise {
    public static void main(String[] args) {
	Square sqr = new Square();

	sqr.side = 118.384;

        System.out.println("\nSquare Characteristics");
        System.out.printf("Side:      %.3f\n", sqr.side);
        System.out.printf("Perimeter: %.3f\n", sqr.calculatePerimeter());
        System.out.printf("Area:      %.3f\n", sqr.calculateArea());
    }
}

In the same way, you can create a package A inside of another package B that itself is inside another package C, and so on.

You use these same rules to perform class inheritance.

Practical LearningPractical Learning: Introducing Packages

  1. To start a new application, on the main menu, click File -> New Project...
  2. In the Categories section, make sure Java is selected.
    In the Projects section, make sure Java Application is selected and click Next
  3. Set the Project Name to Geometry6
  4. Uncheck Create Main Class and click Finish
  5. To create another package, in the Projects window, under Geometry6, right-click Source Packages -> New -> Java Package...
  6. Set the Name to Triangles and press Enter
  7. To create a class to associate to the new package, in the Projects window, right-click Triangles -> New -> Java Class...
  8. Set the Name to Isosceles and click Finish
  9. To create another package, in the Projects window, under Geometry6, right-click Source Packages -> New -> Java Package...
  10. Set the Name to Quadrilaterals and press Enter
  11. To create a class to associate to the new package, in the Projects window, right-click Quadrilaterals -> New -> Java Class...
  12. Set the Name to Kite and click Finish
  13. Change the file as follows:
     
    package Quadrilaterals;
    import Triangles.Isosceles;
    
    public class Kite {
        protected Isosceles part1;
        protected Isosceles part2;
        
        public Kite() {
            part1 = new Isosceles();
            part2 = new Isosceles();
        }
        
        public Kite(Isosceles side1, Isosceles side2) {
            part1 = side1;
            part2 = side2;
        }
        
        public double calculatePerimeter() {
            return 0.00;
        }
    }
  14. To create another package, in the Projects window, under Geometry6, right-click Quadrilaterals, position the mouse on New, and create Java Package...
  15. Set the Name to Parallelograms and
     
    New Java Package
  16. Click Finish
  17. To create a class to associate to the new package, in the Projects window, right-click Quadrilaterals.Parallelograms -> New -> Java Class...
  18. Set the Name to Rectangle and click Finish
  19. Change the file as follows:
     
    package Quadrilaterals.Parallelograms;
    
    public class Rectangle {
        protected double len;
        protected double wid;
        
        public Rectangle() {
            len = 0.00;
    	wid = 0.00;
        }
    }
  20. To create another package, in the Projects window, under Geometry6, right-click Source Packages -> New -> Java Package...
  21. Set the Name to ApplicationManagement and press Enter
  22. To create a class to associate to the new package, in the Projects window, right-click ApplicationManagement -> New -> Java Class...
  23. Set the Name to Central and click Finish
  24. Change the document as follows:
     
    package ApplicationManagement;
    
    public class Central {
        public static void main(String args[]) {
            System.out.println("Desmonstration of Packages");
        }
    }
  25. To execute the application, press F6 
  26. In the dialog box that appears, make sure ApplicationManagement.Central is selected and click OK
 
   

Previous Copyright © 2009-2012 FunctionX, Inc. Next