FHTW Berlin

FHTW Berlin
Fachbereich 4
Internationale Medieninformatik
PROG1: Programmierung I
Wintersemester 03/04


Laboratory 6: Classes



This week's exercise is a recitation, we will be going through some of the end-of-chapter exercises on classes. The sub-numbering is all screwed up in these chapters, have patience.

A Meeter/Greeter is someone who has been appointed to stand at the door of an establishment like a church or a store and make people feel that they are welcome. Why anyone in their right mind would feel welcome just because someone with a fake smile is shaking their hands is beyond me, but maybe that will help you do the exercise 1. Work in groups of two!

  1. Consider the following definition:

    public class MeeterGreeter
    {

    private String greeterName;

    public MeeterGreeter( String name )
    {
    this.greeterName = name;
    }

    public void sayHello()
    {
    Console.println( "Hello, I'm " + this.greeterName );
    }

    public void sayHello( String toWhom )
    {
    Console.println( "Hello, " + toWhom
    + ", I'm " + this.greeterName );
    }

    public String getNameWithIntroduction ( String toWhom )
    {
    // ****
    this.sayHello( toWhom );
    return this.greeterName;
    }

    }

    Now assume that the following definition is executed:

    MeeterGreeter pat = new MeeterGreeter( "Pat" ),
    terry = new MeeterGreeter( "Terry" );

    1. What is printed by pat.sayHello()? What is returned? Which method is invoked?
    2. What is printed by new MeeterGreeter( "Chris" ).sayHello( "Terry" )? What is returned? Which method is invoked?
    3. What is printed by terry.sayHello( "Pat" )? What is returned? Which method is invoked?
    4. Assume that the expression pat.getNameWithIntroduction( "Chris" ) is being evaluated. What would the value be of each of the following expressions if they were to appear on the ****'d line:
      1. toWhom
      2. this.greeterName
      3. name
      4. this.sayHello()
      5. new MeeterGreeter( "Pat" )
      6. this.getNameWithIntroduction( toWhom );

  2. Now consider the following modification of the MeeterGreeter code. Assume that we add the field definition

    private static String greeting = "Hello";

    We will want to make several other modifications to the MeeterGreeter code.
    1. Write a changeGreeting method that allows a user to change the greeting string.
    2. What arguments should this take?
    3. What should it return?
    4. What should its body say?
    5. To which object should this method belong?
    6. Write an expression that invokes the changeGreeting method that you have written.
    7. Next, modify the sayHello methods to replace the fixed string "Hello" with the a reference to the greeting field. Whose greeting field is it?

  3. If you have time, define a class whose instances each have one method, rememberAndReturnPrevious, that takes a String and returns the String it was previously given. Supply the first return value through the instance creation expression. Give an example of your code in use.


The exercise for next week is an experiment, note that you will need to prepare some finger exercises!


This course is an adaption for the Fachhochschule für Technik und Wirtschaft , Berlin by Prof. Dr. Debora Weber-Wulff of a part of Prof. Dr. Lynn Andrea Stein's Rethinking CS101 project produced while she was at the MIT AI Lab belonging to the Department of Electrical Engineering and Computer Science (EECS) at the Massachusetts Institute of Technology. She is now with Franklin W. Olin College of Engineering. The copyright for all materials belongs to Lynn Andrea Stein, this adaptation is used by permission. All rights reserved.
A textbook is in preparation by Morgan Kaufmann Publishers


Questions or comments: <weberwu@fhtw-berlin.de>