Presentation is loading. Please wait.

Presentation is loading. Please wait.

Software Development Packages

Similar presentations


Presentation on theme: "Software Development Packages"— Presentation transcript:

1 Software Development Packages
Computer Science 209 Software Development Packages

2 What Is a Package? A package organizes related resources (interfaces and classes) into a conceptual unit Like a module in other languages Examples: java.lang (basic Java resources, no need to import) java.util (scanners, collections, arrays, random numbers) java.io (I/O, file processing) javax.swing, java.awt (GUIs)

3 Using a Package: Typical Imports
import java.util.Scanner; import java.util.Arrays; public class CircleArea{ public static void main(String[] args){ int[] numbers = new int[3]; Scanner input = new Scanner(System.in); for (int i = 0; i < numbers.length; i++) numbers[i] = input.nextInt(); Arrays.sort(numbers); for (int n : numbers) System.out.println(n); } Bring in specific classes only

4 Using a Package: Fully Qualified Imports
import java.util.Scanner; import static java.util.Arrays.sort; public class CircleArea{ public static void main(String[] args){ int[] numbers = new int[3]; Scanner input = new Scanner(System.in); for (int i = 0; i < numbers.length; i++) numbers[i] = input.nextInt(); sort(numbers); for (int n : numbers) System.out.println(n); } Bring in specific classes and static methods only

5 Using a Package: The Wildcard Gets All
import java.util.*; public class CircleArea{ public static void main(String[] args){ int[] numbers = new int[3]; Scanner input = new Scanner(System.in); for (int i = 0; i < numbers.length; i++) numbers[i] = input.nextInt(); Arrays.sort(numbers); for (int n : numbers) System.out.println(n); } Bring in all classes with wildcard

6 Using a Package: No Imports Needed
public class CircleArea{ public static void main(String[] args){ int[] numbers = new int[3]; java.util.Scanner input = new java.util.Scanner(System.in); for (int i = 0; i < numbers.length; i++) numbers[i] = input.nextInt(); java.util.Arrays.sort(numbers); for (int n : numbers) System.out.println(n); } Use fully qualified names with no imports

7 Packages and Files In Python, a module is typically a single file, which may define several related classes In Java, a package is a directory, which may include several related classes in source code files and byte code files

8 File Organization within a Package
Source files for classes should be in a directory whose name is the package name When byte code is generated, it is placed in a directory with the package’s name These directories should be just below the directories of the source or byte code files that use them

9 Example: The Student Class
studentexample StudentTester.java StudentTester.class student Student.java Student.class

10 Defining a Package Define Use package student; public class Student{
// Blah blah blah } Use import student.Student; public class StudentTester{ // Blah blah blah }

11 Creating a Package from the Terminal
Place the .java files in a directory whose name is the package name Attach to this directory and run javac *.java Place the package directory in the directory where your client program will be Run javac or java from this client program’s directory as needed

12 All Classes in One Package
student StudentTester.java StudentTester.class Student.java Student.class

13 Defining a Package Define Use package student; public class Student{
// Blah blah blah } Use package student; public class StudentTester{ // Blah blah blah }

14 Compile and Run > cd student > javac *.java > java student.StudentTester Need qualified name to locate main class within package


Download ppt "Software Development Packages"

Similar presentations


Ads by Google