Creating Packages.
Packages in Java allow a collection of classes to be grouped together under a single name. Other classes can make use of the package by referencing the package name.
To create a package the programmer must do two things:
The classpackage package-name; class class-name { ... }
class-name
is marked as part of the package
package-name
Things to remember:
public
are available to every
other class.
The snapshots are taken on tucotuco
.
You should be able to do the same thing in Cafe.
First we create a directory where we will keep the package:
Our package will be calledtucotuco.cs.indiana.edu% pwd /nfs/paca/home/user2/dgerman/A202/lab6 tucotuco.cs.indiana.edu% ls -l total 0 tucotuco.cs.indiana.edu% mkdir SimpleIO tucotuco.cs.indiana.edu% ls -l total 2 drwxr-xr-x 2 dgerman students 512 Feb 18 00:29 SimpleIO tucotuco.cs.indiana.edu% cd SimpleIO tucotuco.cs.indiana.edu% pwd /nfs/paca/home/user2/dgerman/A202/lab6/SimpleIO tucotuco.cs.indiana.edu%
SimpleIO
.
We will try to emulate the ccj
package, or the
BreezyGUI
package, in that we will be interested
in getting information from the user (as input) and printing
information to the user (as output).
Our package will be very small:
Reader
Writer
Reader
will define read
integer values
pause
method
Writer
will define write
integer values
write_line
s (strings of characters)
Reader.java
:
We then addtucotuco.cs.indiana.edu% pwd /nfs/paca/home/user2/dgerman/A202/lab6/SimpleIO tucotuco.cs.indiana.edu% vi Reader.java tucotuco.cs.indiana.edu% cat Reader.java package SimpleIO; import java.io.*; public class Reader { public static int read() throws IOException { BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in)); String value = stdin.readLine(); return Integer.parseInt(value); } public static String pause() throws IOException { System.out.println("Press Enter to continue..."); BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in)); return stdin.readLine(); } } tucotuco.cs.indiana.edu%
Writer.java
:
Then we compile the two files:tucotuco.cs.indiana.edu% pwd /nfs/paca/home/user2/dgerman/A202/lab6/SimpleIO tucotuco.cs.indiana.edu% vi Writer.java tucotuco.cs.indiana.edu% cat Writer.java package SimpleIO; public class Writer { public static void write(int value) { System.out.print(value); } public static void write_line(String line) { System.out.print(line); } } tucotuco.cs.indiana.edu%
And now we're ready to use the package.tucotuco.cs.indiana.edu% ls -l total 4 -rw-r--r-- 1 dgerman students 597 Feb 18 00:41 Reader.java -rw-r--r-- 1 dgerman students 205 Feb 18 00:44 Writer.java tucotuco.cs.indiana.edu% javac *.java tucotuco.cs.indiana.edu% ls -l total 8 -rw-r--r-- 1 dgerman students 779 Feb 18 00:45 Reader.class -rw-r--r-- 1 dgerman students 597 Feb 18 00:41 Reader.java -rw-r--r-- 1 dgerman students 510 Feb 18 00:45 Writer.class -rw-r--r-- 1 dgerman students 205 Feb 18 00:44 Writer.java tucotuco.cs.indiana.edu%
Using Packages
You've been using BreezyGUI
and ccj
.
It should be a piece of cake to use our SimpleIO
package.
Let's write a program that asks the user for two numbers then adds them up and prints the sum, to the great satisfaction of the user:
Ah! So an exception was thrown, as expected.tucotuco.cs.indiana.edu% pwd /nfs/paca/home/user2/dgerman/A202/lab6 tucotuco.cs.indiana.edu% vi Calculator.java tucotuco.cs.indiana.edu% cat Calculator.java import SimpleIO.*; import java.io.IOException; public class Calculator { public static void main(String[] args) throws IOException { while (true) { Writer.write_line("Give me an int: "); int n = Reader.read(); Writer.write_line("Give me another int: "); int m = Reader.read(); Writer.write_line(n + " + " + m + " = " + (n + m) + " "); Reader.pause(); } } } tucotuco.cs.indiana.edu% javac Calculator.java tucotuco.cs.indiana.edu% ls -l total 6 -rw-r--r-- 1 dgerman students 895 Feb 18 01:22 Calculator.class -rw-r--r-- 1 dgerman students 419 Feb 18 01:22 Calculator.java drwxr-xr-x 2 dgerman students 512 Feb 18 01:20 SimpleIO tucotuco.cs.indiana.edu% java Calculator Give me an int: 1 Give me another int: 1 1 + 1 = 2 Press Enter to continue... Give me an int: 10 Give me another int: 345 10 + 345 = 355 Press Enter to continue... Give me an int: 6 Give me another int: not an int see what happens... java.lang.NumberFormatException: not an int see what happens... at java.lang.Throwable.(Compiled Code) at java.lang.Exception. (Compiled Code) at java.lang.RuntimeException. (Compiled Code) at java.lang.IllegalArgumentException. (Compiled Code) at java.lang.NumberFormatException. (Compiled Code) at java.lang.Integer.parseInt(Compiled Code) at java.lang.Integer.parseInt(Compiled Code) at SimpleIO.Reader.read(Compiled Code) at Calculator.main(Compiled Code) tucotuco.cs.indiana.edu%
This represents an introduction to creating packages.
We'll get back to this when we do I/O and revisit exception handling.
Both BreezyGUI
and the ccj
package are similar to
SimpleIO
, except they are bigger.