CPE 102
Spring 2014
Lab 2
Objectives
- More practice
developing and using a simple Java class.
- More practice
developing and running a complete Java program.
- To become
familiar with Java Standard Library documentation.
- To become
familiar with the Java String and Scanner classes.
- More practice
compiling and running a Java program on unix from the command-line.
Resources
- Your text.
- Your peers.
- Your instructor.
- The Java Standard
Library - Book-mark this web page, you will be
using the Java Standard Library documentation regularly this quarter.
Find String in the alphabetic ordered "All Classes"
frame and pay special attention to the discussion of the "+" operator for string concatenation
- especially useful for this lab.
Ground
Rules
None
Orientation
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.
Specification
- 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.)
- Be sure all
instance variables are declared as private.
- 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!
- 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.
- 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.
- 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
System.out.println(greeting);
}
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).
- 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