\documentclass[10pt]{article}

\usepackage{hyperref}
\usepackage[a4paper, total={6in, 8in}, top=.75in, bottom=1in]{geometry}

\usepackage{graphicx}
\usepackage{comment}
\usepackage{url}
\usepackage{courier}
\usepackage{color}
\usepackage{amssymb}

\usepackage[normalem]{ulem}
\useunder{\uline}{\ul}{}
\DeclareGraphicsExtensions{.pdf,.png,.jpg}
\usepackage{color}
\newcommand*\ShiftLeft{\ll}
\newcommand*\ShiftRight{\gg}
\newcommand*\BitOr{\mathrel{|}}
\newenvironment{ppl}{\fontfamily{courier}\selectfont}{\par}


\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\definecolor{pred}{rgb}{0.9,0,0}
\definecolor{pgrey}{rgb}{0.46,0.45,0.48}

\usepackage{listings}
\lstset{language=Java,
  showspaces=false,
  showtabs=false,
  breaklines=true,
  showstringspaces=false,
  breakatwhitespace=true,
  commentstyle=\color{pgreen},
  keywordstyle=\color{pblue},
  stringstyle=\color{pred},
  basicstyle=\ttfamily,
  moredelim=[il][\textcolor{pgrey}]{$$},
  moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%}
}

\setlength\parindent{0pt}
\begin{document}
\title{Building Blocks -- Packages, Chapter 4.5}
\author{Dr. Yvon Feaster}
\date{}
\maketitle

\section*{Packages}
Classes are important in Java. Large software systems often consist of thousands of classes.  Often you may have two classes with the same name.  Java provides us with a way to prevent naming conflicts, a way to control access to specific classes, namespace management, etc.  This mechanism is called Packages.  Examples of some common Java packages are java.lang (provides many fundamental classes) and java.io (provides input, output functionality).\\

You as a programmer can define your own package.  In essence, in doing this you are creating your own namespace.  \\

\paragraph{Creating a package}  Packages are file based.  In others words, if you are using packages, you should put `package' and the name of the package at the top of each file you want included in the package.  The package statement should be the first line of code in the source file.  You should only have one package statement in each source file.  If a package name is not included java will use an unnamed package. 

Let's look a couple examples\footnote{These examples come from \url{www.tutorialspoint.com}}:

Suppose we have an Animal class, such as:

\begin{lstlisting}
package animals;

interface Animal
{
   public void eat();
   public void travel();
}

Now we have an implementation  in the same package.

package animals;

public class MammalInt implements Animals
{

   public void eat()
   {
      System.out.println("Mammals eat");
   }
   
   public void travel()
   {
      System.out.println("Mammals travel");
   }
   
   public int noOfLegs()
   {
      return 0;
   }
   
   public static void main(String [] args)
   {
      MammalInt m = new MammalInt();
      m.eat();
      m.travel();
   }
   
}

You would put these two files in a sub-directory called animals, compile and run.
\end{lstlisting} 
If a class contains import statements, they must come after the package declaration.

\subsection*{Packages and the Directory Structure}
If you choose to use packages the source and class files must have the same directory structure as the package name.  As an example:  if your package name is cu.cpsc2150.checkers, then the file structure for your checkers game files should be .../cu/cpsc2150/checkers.  Often companies will use the reverse of their website as the structure.  Example:  if you had a class name Dell in file named Dell.java and your package name was com.apple.computers then your directory structure would be the following ..../com/apple/computers/ Dell.java.  BTW, I use a MAC so I use `/'.   The following table provides information on directory paths that are important to the java compiler.  

\begin{table}[h]
\centering

\begin{tabular}{|l|l|}
\hline
{\bf Path}                 & {\bf Description}                                                                                                                                                                                      \\ \hline
Source directory root      & \begin{tabular}[c]{@{}l@{}}The compiler will look here for the source files.  \\ The default is the current working directory.\end{tabular}                                                            \\ \hline
Destination directory root & \begin{tabular}[c]{@{}l@{}}The class files will be placed here.  This file can be specified\\ on the command line by using the -d option.  The default\\ is the current working directory.\end{tabular} \\ \hline
The CLASSPATH              & \begin{tabular}[c]{@{}l@{}}The compiler will look in this file for any pre-compiled class \\ files first.  This can be set by using the command line\\ -classpath option.\end{tabular}                 \\ \hline
\end{tabular}
\caption{Directory Paths}
\label{tab:directory_paths}
\end{table}


\end{document}


