From Juneday education
Jump to: navigation, search


This week's hand-in is based on the following chapters (and all preceding chapters):

Files to hand in

answer_weekly_04.zip containing some files and answer_weekly_04.txt (the latter should also contain your name!).


Browse the content of the jar-file

In this exercise you need the jar-file (systemet.jar) so you should begin by downloading the file from this page systemet.jar (see the "Download" button on that page, or use wget or curl to download the file directly from this URL).

As usual, we suggest that you do this, and the rest of this hand-in, in a fresh directory, e.g. "weekly04", to avoid polluting your home directory with a ton of files.

Now you should unpack the jar-file in a terminal using bash and unzip.

$ unzip systemet.jar

Note: if you get a message from bash saying command not found or similar it means that bash can't find a program called unzip. Make sure you have installed such a program. While the files are extracted from the zip file you'll see what files have been extracted.

Now, it's time to look at the content of the Product class.

Declaration - investigate the Product class

Open the Product class with your edit (e.g. atom se/itu/systemet/domain/Product.java) and identify the class declaration. What are the keywords in the class declaration? (the outer block of this source code is the class declaration - we are only interested in the declaration of this block, not what is inside the block in this question)

Expand using link to the right to see some hints.

Look at the following line:

public class Product {

Packages - investigate the systemet classes

What are the different packages for the classes and so called interfaces in this API?

Expand using link to the right to see some hints.

Look at the different directories created when extracting the jar-file. This can be done easily with the find command.

$ find . -type d

What is your best guess for why we chose these names for the packages?

Expand using link to the right to see some hints.

Look at the name of the directories where the files is located and see if you can get a hint from that. If a directory would contain the word network it does not seem to odd to think of the classes in that directory to deal with network connections of some kind.

Are the classes/interfaces in each package somehow related?

Main - investigate the systemet classes

What classes are inside the se.itu.systemet.main package?

Expand using link to the right to see some hints.

List the java source code files in that directory, using bash:

$ ls -al se/itu/systemet/main/*.java

Actually, now you are listing the Java source code files (which normally contains one class each).

Run ls again but this time filter on *.class. You should now see a heck of a lot more files now. Where do these class files come from?

If we start with the ones that have a dollar sign in their names, they actually are so called inner classes. We will not talk so much about inner classes in this course (we'll touch upon the matter in the Interfaces chapters though, but that's pretty far in the future!).

The class files without dollar signs in their names must come from somewhere. It is not that we forgot to include the corresponding Java source code files. We (ok, it was Rikard) declared some classes in the same source code file as TestSwing.java . The rule is "One top level public class per file". So you can have one public top level (not inner or nested) class in that file, and that class must be called the same as the source code file. But you can declare more top level classes in the same file if you don't use the public modifier in front of the class declaration.

This is not something we will focus on in this course, but it might be interesting to know. And it is actually considered poor style to define more than one top level class in the same file, so you are free to put "Shame on you, Rikard" in your answers file.

Before you proceed with the below you need to have the file sortiment.xml in a directory called resources (which should be created in the folder where the file systemet.jar is located), just like in previous weekly exercises.

Expand using link to the right to see some hints on downloading the sortiment.xml file.

Move the file to the newly created directory

$ mv sortiment.xml resources

Run the programs (classes) in that folder by using the command line java -Dsortiment-xml-file=resources/sortiment.xml -cp systemet.jar followed by the classname, i e java -Dsortiment-xml-file=resources/sortiment.xml -cp systemet.jar se.itu.systemet.main.TestSwing

Expand using link to the right to see some hints.

$ java -Dsortiment-xml-file=resources/sortiment.xml -cp systemet.jar se.itu.systemet.main.TestSwing
$ java -Dsortiment-xml-file=resources/sortiment.xml -cp systemet.jar se.itu.systemet.main.TestStreams
$ java -Dsortiment-xml-file=resources/sortiment.xml -cp systemet.jar se.itu.systemet.main.TestProductLine

Now try to execute the following class: se.itu.systemet.domain.Product.

Expand using link to the right to see some hints.

$ java -Dsortiment-xml-file=resources/sortiment.xml -cp systemet.jar se.itu.systemet.domain.Product

Most likely (nudge nudge) you will fail executing the class se.itu.systemet.domain.Product. Why? Does the Product class contain a main method? Let's draw some conclusion from this:

  • a class like Product which represents a product in our software (managing our stock etc). Such classes do not need, or even should not have, a main method. Fact is, it is common to have only one class with a main method.
  • only classes with a main method can be executed

Commenting - investigate the source code

Look at the source code for the TestSwing classes in the se.itu.systemet.main package. Can you understand the comments? Do they help you understand the code?

Old comment from 2017: For those who downloaded the JAR file before Wednesday September 20, you might have asked yourself "What frakkin' comments?". Rikard put hardly any comments in that file, and then asked you to try to understand them. He's a weirdo. He's since then put some comments for you to read in the file. Sorry about that. That's unfortunately how Rikard's brain works.

Declaring variables - investigate the source code

Look at the contents of the Product class (se.itu.systemet.domain.Product) in the source code. What statements are declaration of variables? Explain why you think we choose the various keywords for each variable declaration (we don't yet know about them all, but there is no harm guessing).

Creating the zip file to hand in

You should write your answers to the tasks above in the file answer_weekly_04.txt (which should also contain you name!). You should zip this file (to be consistent with the previous hand-ins).

It is possible to zip a single file (which if it is a large text file would make the file smaller). In order to zip the answer_weekly_04.txt file to answer_weekly_04.zip you simply issue the following:

$ zip answer_weekly_04.zip answer_weekly_04.txt

By now, you should have verified that you have the zip command installed in your system (we've use it in the previous weekly hand-ins).

As you see, the syntax for zipping (compressing one file and thus creating a zipped version of the file alongside the original file) is zip name_of_zip_file.zip file_or_files_to_compress_and_include as shown above.

It might seem unnecessary to compress a small text file to a zip file (it isn't even likely that the resulting zip file will be any smaller than the original) but we thought it could be interesting for you to see that you can zip a single file without a directory inside the zip file.

If you have a whole range of files you would like to package together, you can use zip for this purpose. As shown above, this works also for a single file (which might be compressed and also save space if the file is a text file which is big enough to compress).

Food for thought and some ideas for the future

We recommend that you also get familiar with the tar command and the gzip command, which often are used together as an alternative to zip. The tar command is typically used to group files and directories together, and the gzip command can be used to compress the resulting "tar file". Such a compressed tar file often has the file name like some_archive_name.tar.gz or even some_archive_name.tgz. To extract and uncompress such an archive, one uses a combination of gunzip and tar:

All in one command:

$ tar xvzf some_archive_name.tar.gz

The same but in two steps:

$ gunzip some_archive_name.tar.gz
$ tar x some_archive_name.tar

A difference from the way zip works, is that gunzip actually changes the original file - we start with some_archive_name.tar.gz and after using gunzip, we end up with some_archive_name.tar . Extracting the files from the tar archive, however, leaves you with the original tar file alongside the extracted files from the archive (pretty much like unzip leaves you with both the original zip file and the extracted contents).