CPE 102
Winter
2008
Lab 1
Errata:
None – any updates or corrections will appear here.
Objectives
- To be able to develop and use a
simple Java class.
- To be able to develop and run a
complete Java program.
- To become familiar with the
Integrated Development Environment (IDE) of your choice.
- To be able to compile and run a
Java program in your IDE.
- To be able to compile and run a
Java program on hornet from the command-line.
- To be able to turn in your
source code electronically on hornet.
-
To become familiar with JUnit
tests.
-
To be able to compile and run
JUnit tests in your IDE.
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.
- Handin Reference
-
JCreator and JUnit Integration reference
-
JGrasp and JUnit Integration reference
Ground
Rules
None
Orientation
You will be developing a Java program comprised of three files
(one source file, one test file, and one test driver) that will be able to greet a person by name.
The source file
will contain a simple Java class to support greeting a person by name.
This class will be a server-class, i.e., it provides services to another piece
of code. The test file will check the class's expected interface and
validate its correctness. The test driver will execute the test file and report
to the user about successes and failures.
Specification
- You must
implement the classes and methods exactly as described.
- Be sure all
instance variables are declared as private.
- Include the following comment block at
the beginning of every source file:
/**
* Provide a brief description of what the
source file is/does
*
* @author Your Name Here
* @version Lab X (replace X
with the actual lab number)
* @version CPE102-X (replace the
X with your section number)
* @version Winter 2008
*/
- Write a class 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.
- 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 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. See the Resources section above for help with
concatenation of strings. Compile your code and fix any errors before
continuing.
- To test your Greeter class's
validity, we will use the JUnit testing framework. First, make sure that you
have JUnit installed and integrated with JGrasp or
JCreator, or your favorite IDE. Download and
compile my test file and
test driver. Make sure you understand what is going on in the testGreet()
method in the GreeterTest class.
Note that the test driver, AllTests.java, contains a main method. The method signature must appear as shown to result 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). The parameter
type is an array of String objects that represent the command-line arguments to
the program, if any.
- Execute the program defined in
the AllTests class. You should see output reporting to you that one test ran
and that it completed successfully. The output should look something like this:
.
Time: 0.015
OK (1 test)
The . represents one test that was run.
Now, in the testGreet() method, change the value assigned to
name
to your own name, and modify the call to
assertEquals to match. How do you
expect this to affect the test results? Recompile GreeterTest.java and re-run
the application. Did the output match your expectations?
Now, in the testGreet() method, change the final line to:
assertEquals("Goodbye [insert name here]", greeting);
where [insert name here] is replaced with
name's value.
How do you expect this to affect the test results? Recompile GreeterTest.java
and re-run the application. Did the output match your expectations?
-
Now that you have tested your Greeter class and you are confident that it works correctly,
let's use it in an interactive program. In a new file, write a class
called Driver. Do you know what name the file must have? If
not, read item 4 above more carefully before continuing. This class
should have the following method in it:
/**
* Don’t forget the required comment block
specified above!
*/
import java.util.Scanner;
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 must appear as shown to result 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). The parameter
type is an array of String objects that represent the command-line arguments to
the program, if any.
- Here is what a sample run of
your complete program should look like. Notice that the arrows
represent shell prompts and may or may not appear, depending on what
IDE/environment you are running your program in. Also note that the
bold John Doe is user input not program output.
> What is your name? John Doe
> Hello John Doe
Suggestions
None