CS 201 - Computer Science I - Spring 2004
Lab 2 - Strings
Loyola College >
Department of Computer Science >
CS 201 >
Labs >
Lab 2
Due
Electronic version: Monday, September 27 at 11:59pm.
Labs submitted one day late will be assessed a
20% penalty. Labs will not be accepted more than one day late.
Print out: Wednesday, September 29 beginning of class
Write-up: Wednesday, September 29 beginning of class
Objectives
- to create and manipulate String objects
- to use classes and methods described in the online Java documentation
Reading
Koffman and Wolz, sections 2.3-2.5
Introduction
In section 2.3, Koffman and Wolz introduce the following five reasons for
calling methods:
- to change the state of an object;
- to calculate a result;
- to retrieve data that is stored in an object;
- to get data from the user; and
- to display the result of an operation.
In section 2.4, Koffman and Wolz introduce the String class
and some of the methods that can be used with it. Many more String
methods are available; their descriptions can be found in the online
Java API documentation.
For each method, the documentation lists the information you need to know in
order to use the method. This information includes the following.
- The type of the value returned by the method; if the method is
executed for its effect this will be void.
- Whether the method is a class method or an instance method. Class
methods are listed with static before the return type.
- The name of the method.
- The types of the method arguments. Names are also given for the
arguments but from the point of view of someone using the method these names
serve only as a reminder of what the arguments are for.
- A brief description of what the method does. Clicking on the method
name will reveal more detailed information.
The documentation also lists a plethora of other classes. For each
class, the documentation includes the name of
the package it is in (this can be used with an import
statement at the beginning of a file to allow you to use classes in
that package), a description of the class,
a list of the constructors that are available for that
class, and a list of the methods that are available for that class.
Problem Description
In this lab you will learn about one of the techniques mass-marketing companies
use to discover your e-mail address, called e-mail harvesting. An e-mail harvester is a program that seaches the Web for files containing e-mail addresses. For this assignment you will design a program that searches a user-provided string to see if it contains an e-mail address. (Cohoon, J. and J. Davidson, Java Program Design, McGraw Hill Companis, Inc.: New York, 2004.)
Assignment
Write a program that prompts a user for the for the text of interest. The program then displays the first e-mail address contained in that text. For our purposes an e-mail address is composed of an "@" and the two maximal-length strings that preceed and follow the "@" with no embedded whitespace. The program should output an "@" if no e-mail address is found. Your program should
do the following:
- Using only one dialog box, prompt the user to enter a line of text. To accomplish this:
- Include the line import javax.swing.*; after the class header
- For first line of main, write: String input = JOptionPane.showInputDialog("Enter your sentence");
- The previous line stores the user's input in a String object.
- Locate the first "@" in the input and store its location.
- Determine where the address begins by using the location of the "@" and the method lastIndexOf and save the location.
- Determine where the address ends by using the location of the "@" and the method indexOf and save that location.
- Store the e-mail address by extracting the substring from the message.
- Display the address in the terminal window
Hint: Before you begin programming, you may want to sketch out a possible input and label the parts of the drawing that you are interested in so that you can visualize the specific information that will be stored during the intermediate steps of this program.
Once the program described above works, make your program more robust by verifying that the program works correctly by testing the following cases:
- Finds an address when it is embedded in the middle of the input.
- Finds an address when it is at the beginning of the input.
- Finds an address when it is at the end of the input.
- Finds the first of multiple addresses.
- Finds the address when it is the only input.
- Outputs "@" when there is no address.
Hint: It is acceptable to modify the user's input as long as the modification does not effect the ability of the program to function correctly.
Individual Write-up
In this write-up you will describe each line of code in main. The body of this paper should explain the reason that you choose to call each method. Use a working example, beginning with a user input, and note the value of each variable once the line of code is executed using your example input. Also, provide a sketch of the run-time stack once the code is executed. Be sure to properly sketch primitive variables and reference variables (i.e. objects). The write-up should also include an introduction that describes the program and a conclusion, which includes any comments or concerns you have pertaining to the lab.
This write up will be graded for quality of writing and use of technical terms.
Grading
- Attendance in lab (5 pts)
- Code compiles and runs (10 pts)
- Code produces the expected output (10 pts)
- Followed naming conventions (10 pts)
- Program is formatted correctly (5 pts)
- Program includes sufficient comments (10 pts)
- Write-up (50 pts)
Submission
Submit the source code (.java file) to the submission page. Turn in a print out of your code and write-ups in class.