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

Pre-lab

This week's lab work is intended to aquaint you with the use of methods and values.

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. Why does the following version of refundBalance not give the same results as what we saw in class?

  public int refundBalance()
  {
     balance = 0;
     return balance;
}

P2. What happens if you try to compile the TicketMachine class with the following version of refundBalance?

  public int refundBalance()   
{
return balance; balance = 0; }

P3. We have seen a few arithmetic operators in Java. Find a table with the Java operators and their priorities. Note down where you found this. What does the ternary operator in x?a:b do?

P4. What do you expect the the following expressions to evaluate to? Do not use a compiler or the codepad, only your head!

Expression Value Type
99 + 3    
"cat" + "fish"    
"cat" + 9    
9 + 3 + "cat"    
"cat" + 3 + 9 + "fish"    
"catfish".substring(3,4)    
"catfish".substring(3,8)    


 

Post-Lab, AKA  What To Turn In

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

Lab assignments are due the night 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

CodePad

  1. Open BlueJ and find the CodePad. Use it to test your predictions for P4 and record where you were correct and where you made mistakes.

Making a Book

  1. Download the BookExercise project from Moodle. Add two accessor methods to the class — getAuthor and getTitle — that return the author and title fields as their respective results. Test your class by creating some instances and calling the methods.
  2. Add two methods, printAuthor and printTitle, to the Book class. These should print the author and title fields, respectively, to the terminal window.
  3. Add a further field, pages, to the Book class to store the number of pages. This should be of type int, and its initial value should be passed to the single constructor, along with the author and title strings. Include an appropriate getPages accessor method for this field.
  4. Add a method, printDetails, to the Book class. This should print details of the author, title, and pages to the terminal window. It is your choice how the details are formatted. You might want to include some explanatory text.
  5. Add a further field, refNumber, to the Book class. This field can store a reference number for a library, for example. It should be of type String and initialized to the zero-length string in the constructor. Define a mutator for it with the following signature:
    public void setRefNumber (String ref)
    The body of this method should assign the value of the parameter to the refNumber field. Add the corresponding accessor getRefNumber.
  6. Modify your printDetails method to include printing the reference number. However, the method should print the reference number only if it has been set. Hint: use a conditional! Note that Strings have a length method.
  7. Modify your setRefNumber mutator so that it sets the refNumber field only if the parameter is a string of at least three characters. If it is less than three, then print an error message and leave the field unchanged.
  8. (For the bored) Create a new project, heater-exercise, within BlueJ. Edit the details in the project description — the text note you see in the diagram. Create a class Heater, that contains a single integer field, temperature. Define a constructor that takes no parameters. The temperature field should be set to the value 15 in the constructor. Define the mutators warmer and cooler, whose effect is to increase or decrease the value of temperature by 2 degrees respectively. Define an accessor method to return the value of temperature.
  9. (For the bored) Modify your Heater class to define three new integer fields: min, max and increment. The values of min and max should be set by parameters pssed to the constructor. The value of increment should be set to 2 in the constructor. Modify the definitions of warmer and cooler so that the use the value of increment rather than an explicit value. Check that everything works as before. Now modify the warmer method so that it will not allow the teperature to be set to a value greater than max. Similarly, modify cooler so that it will not allow temperature to be set to a value less than min. Check that the class works properly. Now add a method, setIncrement, that takes a single integer parameter and uses it to set the value of increment. Add a check to prevent a negative number being used here!

 

Writeup

Submit your writeup as your post-lab to the Moodle area by 22.00 the night before your next lab.


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