TIG015 WEEKLY 3

From Juneday education
Jump to: navigation, search

About

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

As before, if you download scripts from the internet (as you do in most of the weeklies), you will need to change the permissions of the scripts in order to run them in Bash (or you'll get the infamous "Permission denied" error message).

A simple way to do this is explained in our FAQ.

Files to hand in

answer_weekly_03.zip containing TestingObjects.java and answer_weekly_03.txt (the latter should also contain your name!).

Tasks

Task 1. Investigating the class se.itu.systemet.domain.Product

Download the systemet.jar library file using the script get_latest_jar.sh which you can download from github: get_latest_jar.sh .

Note: if you get "permission denied" when executing the script get_latest_jar.sh, check out the FAQ entry permission denied.

Create a new directory where you do all work, e.g. mkdir hand-in-03; cd hand-in-03 . Move the systemet.jar to this directory. Unzip the file (extract all directories and files from the jar file):

$ unzip systemet.jar

Open the file (extracted from the jar file) se/itu/systemet/domain/Product.java in an editor.

In your browser, navigate to the API documentation for the same class. We'll give you the link to the main page of the API here. You are not allowed to use the link in the frame labelled "All classes" which here is considering a shortcut and cheating. Use a link from one of the other frames to find the Product class.

Look at the path to the Product.java file. How do you think you can find the documentation for Product by looking at the path?

Also, look at the very first line of code in the Product.java file. What is the relation between this statement and the path to the file itself?

In your answers_weekly_03.txt file, describe the relation between the path to the Product.java file and the way to find the documentation page for Product and also the first statement in the source code file. You may answer by describing how to navigate from the start page of the documentation web page to the documentation for the Product class. Then write some words about how this relates to the path to the source code file and what similarities exist between all this and the first statement in the source code.

Some notes on the jar file

Note: All jar files don't contain both the class files and the source code files. We decided to include the source code in our jar file systemet.jar for educational purposes. It is more common to only include the class files but it is up to the developers to also include the source code files for convenience or reference.

Task 2. Another view of the Product class

Product class diagram

Take a look at this "class diagram" for the Product class. Still with the source file in front of you (in your favorite editor or so), write in answer_weekly_03.txt your explanation of the following styles and symbols in the class diagram (your best guess is enough here!):

  • The equivalent keyword in Java for the plus sign and minus sign in front of names
  • The equivalent keyword in Java for names etc which are underlined in the diagram

Hint: Look at all names in the diagram beginning with a minus sign. Look at the same names in the source code and see what keyword they have in common. Do the same for all names prefixed with a plus sign in the diagram. Look at the names with underline in the diagram and look at the same names in the source code file.

Note: You don't have to understand what the Java keywords actually means. Finding if the plus and minus signs and the underline have a correspondence in Java i enough.

Task 3. Re-visiting the online documentation

Look at the documentation page for the Product class again. Now, find the "method" called alcohol() in the documentation page. Click on it in the list of methods to see a more detailed description.

There is a header called Returns: and under it some text explaining what this method "returns" (gives the caller back).

Now, look at the source code and locate the definition of the alcohol() method. There are some comments above (before) the Java code for the actual method. Can you see the relation between these comments and the web page with the documentation online?

This style of documentation is called "javadoc". Google for Java doc and see if you can read about it. Write in your answer_weekly_03.txt file what an example command line could be to generate HTML documentation from Java source code. Do you have the command needed installed on your computer? Where is it installed? Hint: which <some-command> is a useful tool for finding out where some command is installed.

Task 4. Create some custom project objects

Task 4. part 1

Start by creating a new directory for this part of the hand-in, called e.g. weekly-03-src and change directory to this new one. Download the necessary files:

Expand using link to the right to see download hints, following the instructions at Download Files.

With wget:

wget --no-check-certificate https://raw.githubusercontent.com/progund/tig015-weekly/master/weekly03/TestingObjects.java 
wget --no-check-certificate https://raw.githubusercontent.com/progund/tig015-weekly/master/weekly03/get_latest_file.sh 
wget --no-check-certificate https://raw.githubusercontent.com/progund/tig015-weekly/master/weekly03/get_latest_jar.sh 
wget --no-check-certificate https://raw.githubusercontent.com/progund/tig015-weekly/master/weekly03/build_testing_objects.sh
wget --no-check-certificate https://raw.githubusercontent.com/progund/tig015-weekly/master/weekly03/run_testing_objects.sh

With curl:

curl -k https://raw.githubusercontent.com/progund/tig015-weekly/master/weekly03/TestingObjects.java -o TestingObjects.java
curl -k https://raw.githubusercontent.com/progund/tig015-weekly/master/weekly03/get_latest_file.sh -o get_latest_file.sh
curl -k https://raw.githubusercontent.com/progund/tig015-weekly/master/weekly03/get_latest_jar.sh -o get_latest_jar.sh
curl -k https://raw.githubusercontent.com/progund/tig015-weekly/master/weekly03/build_testing_objects.sh -o build_testing_objects.sh
curl -k https://raw.githubusercontent.com/progund/tig015-weekly/master/weekly03/run_testing_objects.sh -o run_testing_objects.sh

Note:On some MacOS versions there seems to be a problem with downloading files via scripts. If you experience odd errors, try downloading via your browser the following files:

  • systemet.jar - and store in your weekly 03 folder
  • sortiment.xml - and store it in a folder called resources which, in turn, should be located in tyour weekly 03 folder.

In the file TestingObjects.java (download here: github), in the main method, create two product references and initialize them to some new se.itu.systemet.domain.Product objects with the values of your liking.

You can call the references aProduct and anotherProduct. Remember, to initialize reference values to object references, you can use the constructor of the class you want to instantiate. Look at the class diagram above or the online documentation online to see how many arguments you should use with new Product(...), and what types they are and in what order the arguments must come.

Next, add two println statements for printing the objects to standard out (i.e. your terminal) by passing the references to System.out.println().

To compile and run the TestingObjects.java program, use:

$ ./build_testing_objects.sh && ./run_testing_objects.sh

Investigate the output and compare it with the documentation for toString() and also the source code for the toString() method in the se/itu/systemet/domain/Product.java Java source code file. Write in the answer_weekly_03.txt file if you think that this method seems to be invoked when you send a Product reference to println and motivate your answer (why you think so or why you don't think so).

Next (after the println statements) create a third product reference and initialize it to a new Product using exactly the same arguments as the variable aProduct used. Name the variable aSimilarProduct .

Now, let's see if aProduct thinks that it is equal to aSimilarProduct. You can check this using an if statement which in its test calls aProduct.equals(aSimilarProduct) and in the block of the if statement, use System.out.println to print that the products are equal, and in the else branch, print that they are not equal.

Expand using link to the right to see a hint.

If you don't remember the syntax for the IF statement, revisit the Control flow chapter and do all the exercises again! This is something basic that you really must know if you aim to pass the exam and any future Java courses.

Here's an example in pseudo code:

if (some predicate test) {
  // What to to if test is evaluated to true
} else {
  // what to do if test is evaluated to false
}

Next, add a new test (after the first) checking if anotherProduct is equal to aProduct and use println to verify that they are not equal (according to anotherProduct).

Task 4. part 2 - Questions for answer_weekly_03.txt

Task 3 Q1: Do you think Systembolaget has created all their 18 000+ products for e.g. their Android app, using the new operator for some Java class, with hand-typed (hard coded) values just like you did when you typed in the creation of the three products above?

Task 3 Q2: If not, where do you think all the data of the products for e.g. the android app or the web page for Systembolaget comes from?

Task 3 Q4: The products you created yourself with the data you chose yourself, do they exist in the real world?

Task 5. Revisiting the XML file

We, the teachers, wrote the se.itu.systemet.domain.Product class (and the classes needed to read data from the XML file from Systembolaget's API page).

Use the less command, or some other command to look at a portion of the XML file. Can you see where the data for one product starts and ends? Paste one complete XML fragment for exactly one product into your answer_weekly_03.txt file.

Now, count how many fields of data exist for a product in the XML file. There's a lot of information included for each product! But our Product class only contains four fields: name, price, alcohol and volume.

Our API used in the Java course as educational material does not include all the data for a Product. This is very common when you design and write a Java class. If the system you are writing doesn't need all the existing data for a class of objects, you simply don't include it. Imagine if you was to create an address book application and decided to have a class Contact representing one of your contacts. You could start by thinking about what data could describe a contact. If you come up with tons of data, you obviously must ask yourself: "Is this relevant and necessary for my contact application?". Perhaps you first end up with this list of data describing a contact:

  • phone number
  • home number
  • work number
  • home address
  • email address
  • name
  • age
  • nationality
  • eye color
  • hair color
  • sex
  • IQ
  • education
  • favorite food
  • birthday
  • birth date
  • number of kids
  • marital status
  • ...

Now, even if all contacts represented by the contact book application surely has all of those properties in the real world, are they really necessary for making the contact book application work and be useful?

Write in your answer_weekly_03.txt file what properties from the list above you would actually keep in a class Contact for use in a contact book application, for e.g. a phone app. (There is no correct answer for this, but you should motivate your selection of properties).

The point of this exercise is to make you think about the way one can design a class. Just because the data about "country of origin" or "ecological or not" in the data source for our Product:s in our API, we don't need to include all of them. We chose more or less arbitrarily what fields to include in order to make the Product class small enough for using in education but interesting enough for use in exercises.

Task 6. Filter products on alcohol level

This task might be the most challenging. You should use one loop to print only products with alcohol level over 40.0% alcohol, and one loop to print only products with 0.00% alcohol (see how politically correct we are!).

The task will train you to use object references to call methods and conditionally (using e.g. an if statement) act upon the return value from the method calls.

You should put your code after the code you wrote above inside the main method. Start by getting a list of all the products and store the list in a reference variable called products.

You can look in the code for previous week's hand-in Weekly 02 to see how to do this. Or you can cheat and look at the hint below.

Expand using link to the right to see a hint.

You will need a ProductLine object and ask it for all products. Below is the code for that.

ProductLine catalog = ProductLineFactory.getProductLine();
List<Product> products = catalog.getAllProducts();

Look in the online API and find out where the classes ProductLine and ProductLineFactory are located. See if you can match that with the import statements in the TestingObjects.java file. Since we are using classes defined somewhere else (than the same directory as our TestProduct class, we need to import those classes. The classes were written by the teachers for the systemebolaget API and are included in the jar file.

But the list of products is imported too, from java.util.List and typed as a list of Product (-references). The java.util.List is included in the standard Java API, but we still need to import it in order to use List in our program.

Now, create the first loop to iterate over all the products and find the ones whose alcohol level is greater than 40.0%. You may use either the for-each-loop construct or a for loop using a loop variable i to access each product from the list.

If you need to get a hint for this, you can see the hint section below, but you should really also revisit the chapter on control flow and check out the for loop section and do the exercises again (if you need to).

Expand using link to the right to see a hint.

Here are some examples looping through the products list in different ways:

// As before:
ProductLine catalog = ProductLineFactory.getProductLine();
List<Product> products = catalog.getAllProducts();
// Old style for loop:
for (int i = 0; i < products.size(); i++) {
  Product product = products.get(i);
  // if statement to check alcohol etc
}

// For-each-loop:
for (Product product : products) {
  // Use the "product" variable for the if statement etc
}

// Iterator style - note java.util.Iterator must be imported
Iterator<Product> productIterator = products.iterator();
while ( productIterator.hasNext() ) {
  Product product = productIterator.next();
  // if statement using product to check alcohol etc
}

Finally, add a similar loop to print only the products with alcohol of 0%

Task 7. Self-assesments

Add the following to your answer_weekly_03.txt file (fill in the blanks):

I spent ___ hours solving the weekly 3 hand-in.

I found the following tasks hardest:

___ becuase ____

___ because ____

(etc)

Other comments I have are _______

Some notes on the weeklies:

It is important that you first complete all the exercises for each chapter before starting with the weekly hand-in! The exercises are there to manifest the theories taught in videos and lectures. After completing all ordinary exercises, it is much easier to complete the Weekly hand-in! You shouldn't let the weekly assignment steal time from doing the ordinary exercises. It is better to only do the exercises and be a little late with the weeklies, than to complete all weeklies and do no exercises.

The weeklies serve as a tool to check your own progress with the exercises - they check that you learnt from the exercises (to some degree). The weeklies also serve as a tool for the teachers to see the level of commitment and understanding of the students.

This is the first time we try out having weekly assignments, so it is important for us to get your feedback on how you perceive them.

The weekly hand-ins are also a way to expose you to some tasks which covers some stuff which comes only later in the course. We think it is good for you to try to read and understand code someone else has written just as children often learn how to read before they learn how to write (and spell and use correct grammar). Our goal is to show you that you can use programming to do stuff, which you didn't even know how to do yet! Later in the course, the techniques used in the hand-ins will be explained and practiced in more detail. It is our hope that you will recognize and remember stuff from previous weeklies, and that it will help your learning process.

Don't hesitate to use the above questions (fill-in-the-blanks) to give us your thoughts on the weeklies.

Creating the zip file to hand in

Create answer_weekly_03.zip containing the following files: TestingObjects.java - the source code where you wrote the code answer_weekly_03.txt - the text file with your name(!) and your answers to the questions

Voluntary extra challenge

Let the user running the program decide the alcohol limit

Use args to let the user decide the minimum alcohol level!!!

To pass an argument to your program, just put it after the run script:

./build_testing_objects.sh && ./run_testing_objects.sh 75

The above gives your Java program the argument of 75 .

If you don't remember how to use arguments to a Java program, please see Objects - creating - Exercises Arguments challenge exercise.

In this hand-in, however, we've helped you and made the run_testing_objects.sh script so that it accepts arguments (like "75" as shown above) and the script passes those arguments on to the java command line which actually runs your program.

Why aren't your own products included in the printing of all products?

Print all products in the list (products) with alcohol greater than 78 (using the same technique as above).

How many products did you see?

Now, Declare and initialize a reference to a really strong Product called strongProduct with an alcohol level of, say, 90%.

Next, print again all products in the list (products) with alcohol greater than 78.

How many products did you see?

Your newly created Product was not in the list. That's not so weird, because the list came from the actual XML file from Systembolaget and your strongProduct was just a bogus Product living its own life outside of the list.

Let's add the strongProduct to the list! You can use products.add(strongProduct); for doing this.

Next, print again all products in the list (products) with alcohol greater than 78.

How many products did you see?

You do not need to include this voluntary task to your hand-in (since it is voluntary).