Chapter:Classes - Packages - Exercises

From Juneday education
Jump to: navigation, search

Exercises - package declaration

Create the following directory structure:

.
|-- bin
|   `-- org
|       `-- progund
`-- src
    `-- org
        `-- progund

You should remain in the directory signified by "." in above. Download the Game.java source code into src/org/progund (when we say "download a file into a directory", we actually mean that you should download a file and make sure you move it to that directory). Open the file and look at the package declaration. Remember that if there is a package declaration, it must be on the first line of the source code file.

Q1. What is the package name?

Expand using link to the right to see a proposed solution/answer.

org.progund

Q2. What is the full (qualified) name of the Game class?

Use cd to change directory down to src. Compile Game.java from this directory (you need to give javac the full path to the file Game.java!). Still in the src directory, list the contents of the relative path org/progund/.

Expand using link to the right to see a proposed solution/answer.

org.progund.Game

Q3. How many files is in that directory?

Expand using link to the right to see a proposed solution/answer.

src/org/progund/ now contains two files.

Q4. What is the names of those files?

Still in the src directory, run the program Game (Game has a main method!):

$ java org.progund.Game

Use the ls command to investigate what the file names are in the src/org/progund/ directory. Given that you should be located in the src you should exclude that directory from the path so the command to list should be:

Expand using link to the right to see a proposed solution/answer.

ls org/progund/ src/org/progund/Game.java and src/org/progund/Game.class

Q5. What is printed to the terminal?

Delete the class file you just created:

$ rm org/progund/Game.class

Change directory up one level:

$ cd ..

Now try this:

$ javac -d bin src/org/progund/Game.java

Note the flag -d bin! It means that javac should no longer put the class file in the same directory as the source code, but use the bin directory structure instead. Using the package declaration, javac knows that it should put the class file not only somewhere under bin but exactly in bin/org/progund.

But, what was printed to the terminal when you ran the program in the previous exercise?

Expand using link to the right to see a proposed solution/answer.

Game over!

Q6. How many files are now inside the src/org/progund directory? (use ls to find out)

Expand using link to the right to see a proposed solution/answer.

src/org/progund/ now contains one file, Game.java. You just deleted the other one, and you told javac to put the class file in the bin directory structure instead, using the flag -d which stands for "destination".

Q7. How many files are now inside the bin/org/progund/ directory?

Run the program inside bin/org/progund/Game.class For this, you need to use the class path flag -cp bin (since you haven't changed directory down to bin, java would never find the org directory without the -cp bin!).

Now, check how many files are in the bin/org/progund/ directory, using ls.

Expand using link to the right to see a proposed solution/answer.

To run the program from the directory above bin, you can do like this:

$ java -cp bin/ org.progund.Game

The -cp bin/ flag is needed, since you are in the directory above bin, and want Java to run a class in a package called org.progund. But since there are no directory org in your current directory, you need to help Java a little and give it a hint. So -cp bin/ means something like "Hey, before you start complaining about Class not found, go and look for the org.progund.Game class inside the bin directory!".

Now, to answer the question, how many files are in the bin/org/progund/ directory, you can simply use ls.

bin/org/progund/ now contains one file, Game.class. You told javac to put the class file in the bin directory structure, using the flag -d which stands for "destination". So this is the one file!

Q8. Did it work?

Expand using link to the right to see a proposed solution/answer.

Of course it worked? If not, please consult with your class mates and/or tutors.

Q9. Download the Student and StudentDemo classes and put them in a package

Create the following directory in your home directory:

$ cd
$ mkdir java-classes-packages

Go down in the directory:

$ cd java-classes-packages

Download the following java classes (source code):

$ wget https://raw.githubusercontent.com/progund/classes/master/java-classes-declaration/Student.java
$ wget https://raw.githubusercontent.com/progund/classes/master/java-classes-declaration/StudentDemo.java

Create the following directory structure in the new directory:

$ mkdir -p org/progund/education/{main,domain}

Verify that the directory structure worked:

$ tree --charset=ascii org/
org/
`-- progund
    `-- education
        |-- domain
        `-- main

4 directories, 0 files

Move Student.java to org/progund/education/domain/

$ mv Student.java org/progund/education/domain/

Move StudentDemo.java to org/progund/education/main/

$ mv StudentDemo.java org/progund/education/main/

Open and edit StudentDemo.java and add a package declaration matching the relative path org/progund/education/main/

Hint: the package should be named org.progund.education.main

Do the same for Student.java but matching the relative path org/progund/education/domain/

Hint: the package should be named org.progund.education.domain

Add an import statement to StudentDemo.java so that it can use the Student class from another package:

import org.progund.education.domain.Student;

Compile and run StudentDemo:

$ javac org/progund/education/main/StudentDemo.java && java org.progund.education.main.StudentDemo
Rikard Fröberg (46 years old)

Hints:

Don't cd down into the org/progund... directory tree. Remain in the directory where org/ is. In o rder to open e.g. org/progund/education/main/StudentDemo.java you don't need to cd to anywhere, you just give the full path to the file as argument to your editor, e.g. Atom:

$ atom org/progund/education/main/StudentDemo.java

In order to compile the same file, you don't need to cd anywhere, either! Do exactly the same as when opening the file, but with javac instead:

$ javac org/progund/education/main/StudentDemo.java

And, to run, you give the full name of the class as the argument to java. In order for this to work, you need to remain in the directory above org/ again, so no cd-ing anywhere!

$ java org.progund.education.main.StudentDemo

The class name including the package name, org.progund.education.main.StudentDemo includes the information to java where to find the class StudentDemo. The package org.progund.education.main will tell java to look for the relative path org/progund/education/main/ in which it will find the class file for the compiled Java file StudentDemo.java (the compiled class will be called StudentDemo.class).

So, stay put in the directory java-classes-packages where you created the org/ directory tree. This way, you can edit, compile and run from the same spot.

Links

Further reading

Where to go next

The next page is: Classes_-_main

« PreviousBook TOCNext »