HTW Berlin Medieninformatik HTW Berlin
Fachbereich 4
Internationaler Studiengang
Internationale Medieninformatik (Bachelor)
Info 1: Informatik I
Summer Term 2014

Laboratory 3 : A Better Notebook

 

Pre-lab

This week's lab work is intended help you practice using loops.

What to Bring to Lab

Please bring these exercises printed out or written out with you to lab. Please have your name on your page.

P1. Write a loop on paper that prints out all multiples of 5 between 10 and 95.

P2. Write a method called sumBetween that adds up all numbers between two numbers, a and b, that are passed into the method as parameters. Include a but not b in the sum.

P3. Assume you have a collection of students called imi1. The Student class has a method getFirstName and a method getSurname. Write a loop that prints out all students in the collection with the last name first, then a comma, then a blank, and then the first name, each on a line by itself.

P4. Write a method isPrime that uses a while loop to test if a number given in a parameter is prime or not. A number is prime when it is divisible with a remainder of 0 only by 1 and itself.

Post-Lab, AKA  What To Turn In

Your completed assignment, submitted in Moodle as a pdf, should include:

Lab assignments are due before your next lab at 22.00. They may, of course, be turned in earlier. You hand them in by preparing the report in PDF and submitting it to Moodle.


Assignment

A Better Notebook

  1. Using the CodePad, test that your prelab work in P1 and P2 is correct. What test cases did you have to create?
  2. Adapt the notebook project, which is similar to the music library we worked on in class, to list all notes. Create a notebook and add some notes, checking that this method works as intended.
  3. Modify your list method so that it prints a number in front of each note that corresponds to its index in the ArrayList.
  4. Implement removeNote. You may have to look at the API for java.util.ArrayList to find out how to do this.
  5. Modify removeNote to print out an error message if the note number entered was not valid.
  6. Try and implement the search method that looks for a particular note. You need to add code to print out either the note that was found or the string "Search term not found.". Be sure to test your method with terms that are in the list, and ones which are not.
  7. Within a single execution of the search method, the notes collection might be asked repeatedly how many notes it is currently storing. Does the value returned by size vary from one check to the next? Rewrite your search method so that the size is not checked unnecessarily, perhaps using a local variable. Check that your version gives the same results! What cases will you have to test?
  8. Create a collection primes and use your prelab 4 to fill it with the prime numbers between 1 and 1000. Oops, this won't work directly, because collections don't take ints. You can use something called autoboxing to solve this. Look up Integer in the API and use this. How many prime numbers are there between 1 and 1000?
  9. (For the bored) Implement a search in the notebook with '?' as a joker standing for any character, and '*' standing for any number of characters.
  10. (For the really bored) Implement full regular expression search for the notebook.

Writeup

Submit your writeup as your post-lab to the Moodle area.

 


Some rights reserved. CC-BY-NC-SA Prof. Dr. Debora Weber-Wulff - CC-BY-NC-SA
Questions or comments: <weberwu@htw-berlin.de>


The exercises are adapted from Objects First with Java, A Practical Introduction Using BlueJ. David Barnes & Michael Kölling, 2011