TIG015 WEEKLY 2

From Juneday education
Jump to: navigation, search

About

This weekly hand-in is supposed to cover the topics taught in the following lectures/chapters:

If you do all the exercises it should be no problem to finish this simple hand-in.

Some words about the hand-in

In this, and many to come, hand-in, we will use a so called API (application programming interface) for accessing the product catalog (product line) of the Swedish Alcohol monopoly Systembolaget. The API makes all the products in the Systembolaget product line (catalog, assortment) available as Java objects called Products (a name for the type of objects which we have chosen). This makes it possible to deal with a Product from Systembolaget's actual catalog as if it were any regular kind of Java object such as java.lang.String.

When we program towards this API, we will be able to do all sorts of interesting stuff with the Systembolaget's products, such as compare prices, alcohol levels, etc.

The data with the product line comes from Systembolaget's own published open data (an XML file published on their website). It's the same data which is used by the Systembolaget app, and the Systembolaget website.

In this first encounter with the API, we'll just look at a small program (written by us) which uses the API to get all the products as Java objects of type se.itu.systemet.domain.Product so that you get exposed to Java source code written by someone else. The reason for this is that it is actually a lot more common to work with existing code than to write software from scratch!

We will ask you some questions about the code (like what you think it does and how you think it does it), and also to change the code slightly and add some small additions.

You don't have to worry if you only understand part of the code. That is to be expected this early in the course. We think, however, that exposing you to stuff which is understandable (but not something you could write yourself) will prepare you for future lectures when we dig deeper into Java programming.

We wish you good luck and hope that you will have fun with the API.

Why did we choose the Systembolaget for creating an API? Well, this hand-in was first produced for "Systemvetetare" (IT systems program student), so we gathered that you should know a few things of Systemet. However, drink carefully! Remember that there are a lot of alcohol-free products in the product line. Perhaps a future hand-in will ask you to filter out only products with 0% alcohol?

This hand in uses a lot of scripts. These scripts are here to help you compile several files, run a program and check your environment among other things. Doing this manually would be way more cumbersome. So download the scripts (see below) and make them excutable and have a little faith in us.

Files to hand in

Write your answers in a file called answers-weekly02.txt and make sure the file contains your name. You will make changes to the source code file Weekly02.java (which you will download, see below). You should hand in exactly one file for this weekly assignment:

answer_weekly_02.zip - which is a zip archive containing answers-weekly-02.txt and Weekly02.java with your changes (see below).

To create this zip file you need the zip command line program. The syntax will be:

$ zip answer_weekly_02.zip answers-weekly02.txt Weekly02.java

You can check that the resulting zipfile contains the two files:

$ unzip -l answer_weekly_02.zip

The above lists the contents inside the zip file, and requires that you have installed the unzip command.

You can download and run the script sanity.sh and run it to see if you have all commands which are required for this weekly hand-in.

Task

This week's hand-in is mostly about:

  • Reading code you get from us
  • Understanding (partly at least) what the code does
  • Making the code compile and run
  • Making small changes to the code, making it compile and run

Software required to complete this hand-in

You will need to have the following commands (programs) installed in your bash environment (runnable from the terminal command line):

  • tidy - A program to make source code look prettier (used to make the XML file readable by humans by indentation)
  • wget - A program for downloading files from the web
  • java - The virtual machine and environment to run Java programs
  • javac - The compiler for Java source code files
  • zip and unzip - Commands to create zip files and extract files from zip files

These software should have been installed if you've followed the instructions on the page Setting up your environment. If you miss any of the programs, follow the instructions for installing additional software again.

Hint: Download sanity.sh and run it to check if you have all of the above commands installed:

$ ./sanity.sh

tidy

The tidy command is used by our scripts to make the XML file readable. It is not essential for completing this hand-in but you'll see a lot of error messages if you don't have it installed.

wget

You will be using wget, curl, a browser or some other tool throughout the course for download files from the web. We really recommend installing it. If you fail installing it, please contact the supervisors and the teachers after using a search engine to find help on how to install it. You can always download files using a browser, and right-click (Mac: command-click) on a link and choose save file... but this will be much slower for you.

java and javac

If you haven't installed and verified that java and javac works by now, you must make an effort to do so, or you will fall behind in this course. These commands are the most important in the whole course!

zip and unzip

You will hand in a zip file (yes, a zip file, not a rar, p7zip or anything else, a ZIP file created with the zip command).

To zip two files:

$ zip answer_weekly_02.zip Weekly02.java answers-weekly02.txt

The above command will create a so called zip archive with the name answer_weekly_02.zip and the two files inside it.

Download the files necessary

Create a new directory in your home directory (e.g. mkdir weekly02 ). Change your current directory to this one using cd weekly02 .

Download the following files:

  1. Script to check if you have everything installed (zip, tidy, unzip, wget, java, javac) sanity.sh
  2. The Java program source code to work with Weekly02.java
  3. Script to fetch our Systembolaget API (a file with Java stuff we have written, which your program will use) get_latest_jar.sh
  4. Script to fetch the Systembolaget product line XML file get_latest_file.sh
  5. Script to compile the program build_weekly02.sh
  6. Script to run the program run_weekly02.sh

You may use wget, curl or your browser to download these files. The scripts, however, are using wget and tidy, so those need to be installed and working in your terminal/environment.

If you want to read the documentation of our Systembolaget API, feel free to go here.


Note If you get "permission denied" when trying to execute a script or one of your commands/programs what's wrong? Don't forget to make the scripts executable. Check the FAQ: "permission denied".

Task 1

Preparation

Compile and run the program Weekly02, to see what the effect of running the program is (what the program does!):

$ ./build_weekly02.sh
$ ./run_weekly02.sh

Now run the above again! Does the output look any different this time?

Look at the source code of Weekly02.java using your editor. Read the comments and try to understand how the program works.

Question 1.a

There is one loop in the program, which prints out all products' price and name.

What part of the code is the loop? Copy and paste the loop into your answer file and mark is as Answer 1.a.

Question 1.b

What is the Java type of the price of a Product? Write the answer and mark it as Answer 1.b.

Question 1.c

What statement or statements in the code do you think causes the printing of information? Answer 1.c.

Task 2

Now it's time to to make some changes around here!

Question 2.a

Add a statement to the code, so that the product information is printed twice for each product. Yes, we know this is stupid, but just do it. Re-compile the program, using the scripts ./build_weekly02.sh and ./run_weekly02.sh and make sure your changes compiles. Since Weekly02.java is included in the zip file you will hand in, we will check that your code is correct and compiles!

Question 2.b

Create a variable before the loop in the source code and initialize it to 0. This variable should be called totalPrice and contain the sum of all Systembolagets products (as if we'd purchase one of each, which we don't recommend in real life).

Inside the loop, you shall add the current Product's price to the current value of your variable totalPrice, so that it adds up all the prices of the products.

It is up to you to figure out what type to choose for totalPrice. Hint: What is the type of one product's price?

After the loop, print the value of the total price so that it looks something like this:

Total price: A gazillion (where "gazillion" is the actual total price).

Question 2.c

Create another variable before the loop in the source code for counting how many products there are, by incrementing it by one inside the loop. Choose a good name and type for this variable. After the loop, print the value of the variable and see if it sounds reasonable. It should be around 18 000 products.

Hint: you may have to initialize the variable to 0 before its first use.

Question 2.d

You can actually check the number of products in a much easier way! Check the documentation of the Java API for java.util.List. There is a so called "method" in List called size() .

Add a statement just after the printing of the counter variable which is using this "method" instead.

Hint: To call the method, you can use the following syntax:

int someVariable = allProducts.size();

In the example above, it is assumed that the type of the variable allProducts is some type of List (which it is in the source code you are working on!)

Expand using link to the right to see a hint.

Expand using link to the right to see a hint.

Expand using link to the right to see a hint.

Expand using link to the right to see a hint.

Expand using link to the right to see a hint.

Hand in the file (ask your teachers for instructions on how to submit your answers).

Food for thought

The keen student always want to learn more and reflect upon exercises and lectures. For those who wish, here are a few starting points for thinking about this hand-in. This section is totally voluntary and only for those who want to think ahead and slightly outside the bare minimum of the scope of this course!

We will never require that you spend time on this section. Think of it as a teaser for those who want to dig deeper. The reset of you can stop reading here! ;-)

The scripts

The command line for compiling this small program is rather complicated for someone who has just started to learn Java and programming. So we created a script for you which solves this for you. It is much simpler to run the script:

$ ./build_weekly02.sh

...than it is to write the whole command line.

Look inside the script and guess what it does (how it works).

$ cat build_weekly02.sh
#!/bin/bash

PATHSEP=":"
if [[ $OS == "Windows_NT" ]] || [[ $OSTYPE == "cygwin" ]]
then
    PATHSEP=";"
fi

echo -e "\n=========="
echo -e "${0}: Compiling program..."
if [ ! -f "systemet.jar" ]
then
    echo -e "${0}: No jar file found. Fetching...\n"
    ./get_latest_jar.sh
    echo -e "\n\n${0}: Done fetching jar file. Saved as systemet.jar ."
    sleep 1
fi
javac -cp "systemet.jar${PATHSEP}." Weekly02.java
echo -e "==========\n"

Can you guess if there are any variables used in the script (build_weekly02.sh, see above), ? Can you find any bash if-statements? Is the script calling any other scripts?

The line near the end starting with javac -cp is the line which performs the actual compilation.

A reason for the script looking a little more complex than it really is, is that it should work both on Windows (in Cygwin) and on UNIX-like systems like GNU/Linux and Mac OS. There is a slight difference in syntax for javac when the -cp option (or "flag") is involved. To separate a list of directories in Windows, a semicolon is used. On all other platforms (known to us) a plain colon is used. The script tries to discover if you run it on Windows or some other platform and then uses the correct path separator (semicolon or colon).

You can also investigate the script run_weekly02.sh for running the program and try to guess what it does.

Bash syntax and bash scripts are outside of the scope of this course, but we encourage you to learn at least the basics if you find the time to do so. We have a few books on this wiki for those who wants to learn more, and there are plenty of resources online for learning bash.

What about the sanity.sh script? It checks if you have the required programs/commands installed in your command line environment. Can you understand how it works? It uses a loop. Does it look like any kind of loop you've seen in Java?

The output of the price

When you output the variable from a real number (floating point type) in Java, you can get a lot of unnecessary decimals (zeros) at the end. Can you understand how we made the output limited to only two decimals precision?

Experiment with the technique we were using and see if you can output some decimal value with three, four, five etc digits precision.

If you want to learn more about formatting output, here's a tutorial on Oracle.