CPE 102

Spring 2014

Lab 2








Ground Rules






You will be developing a simple Java program comprised of two source files that, using the language of our text, can be considered to be a client and a server. The server is a simple class that supports greeting a person by name but performs no input or output (I/O) with the outside world.  The client's job is to perform the necessary I/O and to make use of the server to generate greetings.




  1. You must implement the classes and methods exactly as described. (You will not be turning in this lab, but you need to practice following the specifcation so that you get full credit on all the work that you do hand in.)


  1. Be sure all instance variables are declared as private.


  1. Write an empty class (no methods or data yet) called Greeter in a file named Greeter.java (in java class names and file names must match or your code will not compile).  At this point you should be able to successfully compile your code - do so before continuing. Get in the practice of compiling and running your code often!


  1. Implement a constructor in the Greeter class that accepts a String representing the name of a person to greet.  Notice that constructors are special methods that have no return type and must have the name of the class.  There can be more than one constructor per class - they will all have the same name but must have different parameters (this is called overloading).  Use the String parameter to initialize a private instance variable (also of type String) that will be used later by another method of the class.  This instance variable represents the state of the object.  Compile your code and fix any errors before continuing.


  1. Implement a method called greet that is public, has a return type of String, and has no parameters.  The method must return the following String greeting:


            "Hello name"


where name is replaced with the name that was passed to the constructor and used to initialize the object's instance variable.  Read the Java Standard Library documentation (link in the Resources section above) for help with the String class and how to concatenation of strings.  Compile your code and fix any errors before continuing.


  1. In a new file, write a class called Driver.  Do you know what name the file must be and why?  If not, read item 3 above more carefully before continuing.  This class should have the following method and import statement in it:


// Put this above the class at the top of the file.
import java.util.Scanner;


// Put this method inside the class.
public static void main(String[] args)


   // Declare and construct a Scanner object to read from the command-line

   Scanner scanner = new Scanner(System.in);


   // Prompt for a name

   System.out.print("What is your name? ");


   // Read the name using the Scanner

   String name = scanner.nextLine();


   // Construct a Greeter object

   Greeter greeter = new Greeter(name);


   // Get the greeting and save it to a String

   String greeting = greeter.greet();


   // Display the greeting to the command-line




The method signature for main must appear as shown to be in a valid Java program.  This is the entry-point the Java Virtual Machine requires to run your program.  Notice the use of the word static - this means the method can be called without an object of the class that contains the method existing (more later in the quarter about that). 


  1. Here is what a sample run of your complete program should look like.  Note that the bold Cody Coderson is user input not program output.


What is your name? Cody Coderson

Hello Cody Coderson

Lab courtesy of Julie Workman.