Lab 3: Strings

Copyright RIT 2003
$Id: writeup.xml,v 1.7 2007/09/12 13:03:35 vcss231 Exp vcss231 $


Goal

This lab will provide you with the opportunity to use the Java String class in order to solve several small problems. You will have a chance to use some of the ideas discussed in class and to learn more about working with objects and writing simple programs.


Team Setup

You are to work on this lab completely on your own.

Overview

Objectives

Ask Dr. Tiger

Pre - Lab Work

  1. Review your class notes for the preceding week.

  2. Complete the string exercises found by clicking here (http://www.cs.rit.edu/~vcss231/Exercises/_Strings/) .

In-Lab Activities

Each lab you will do is separated into a set of activities. Each activity has specific things you must hand in to get credit for it. Each activity is graded separately.

Activity #1 : Obtain the files required to complete the lab

In many of the labs you will be working with files that we have previously created. To simplify the process of obtaining these files they will be stored in a Java archive file (or jar file for short). In order to use these files, you will need to download the corresponding jar file and unpack it.

Downloading the archive is usually very easy. We will provide a link in the lab that will point to the appropriate archive. The archive that contains the files you will need to complete this lab can be found here (/~vcss231/pub/lab03/lab3.jar) . Place the file in the directory that you created to hold your CS1 laboratory work.

Unpacking a jar file is also pretty easy. The command jar can be used to create or unpack a Java archive file (click here (http://java.sun.com/developer/Books/javaprogramming/JAR/basics/) for additional information on the jar command). Go to a shell window and change the working directory to the directory that contains the jar file that you just downloaded. Now type the ls command to determine the name of the jar file (it should be called lab3.jar). To unpack the archive type the following command:

jar xf lab3.jar

Any jar file that you download for a lab will unpack into a directory named for that lab. After unpacking the lab3.jar file, type the ls command, and you should see that a directory named lab3 has been created. If you look in the lab directory you should find several files: shape.questions, StringThing.java, and TheSame.java.

Once you are convinced that you have successfully unpacked the contents of the jar file, delete the jar file by using the rm command.

Activity #2 : The ShapeWorld Application

The ShapeWorld Application

The ShapeWorld application is a simple program that is designed to help you to understand the difference between a class and an object. Start the ShapeWorld application by typing the following command in a terminal window:

~vcss231/pub/Labs/03/shapeworld

After the application has started a screen should appear on your workstation. The screen can be divided into three areas. At the bottom of the screen are two shape factories. The shape factories can be used to create shapes and to place them in the shape area in the center of the screen. The factories provided you with the ability to set the initial settings that will be used when shapes are created, and to adjust the speed of all the shapes that have been created by the factory. Press on the button labelled "Create a new Oval". You should see a new oval appear in the area in the center of the screen. Use the factories to create a variety of different shapes with different attributes.

The top of the screen contains the shape inspector. The shape inspector can be used to examine any of the shapes in the center of the screen. To activate the shape inspector just click on one of the shapes in the center of the screen (note you might want to slow the shapes down and maybe create one or two big ones to make them easier to select).

When a shape is being inspected, its representation in the box will be changed from a filled shape to a hollow one. The attributes of the shape will be displayed in the shape inspector. While a shape is being inspected you can change its height, width, color, and whether or not it is moving. Use the shape inspector to examine some of the shapes you have created.

Think for a minute about the shape factories and the shapes moving in the box at the center of the screen. Which of these entities would you consider a class and which would you consider an object? Remember an object is a specific instance of a class, and a class is a template from which objects can be instantiated. You should also recall that objects have state and behavior. For those entities that you considered to be objects, identify the state and behavior associated with the object.

Experiment with the ShapeWorld application until you can answer the questions stated above. When you are ready, answer the questions contained in the shape.questions file that you obtained in Activity 1.

How To Submit

Type the following command in to your shell to send your answers to us:

try grd-231 lab3-1 shape.questions

Activity #3 : The StringThing

This activity will help you to learn some of the methods provided by the Java string class. Take a few minutes to review the documentation for java.lang.String before proceeding.

For this activity you will be working with the file StringThing.java, the other files will be used in the other activities of this lab.

Start up emacs and load StringThing.java into the working buffer. This program consists of a number of operations applied to strings. Your job is to complete each task as indicated in the comments. Complete the tasks one at a time by figuring out the appropriate java.lang.String class method to call to complete the task. Although you will be working with three different strings in each task, the code you write should be nearly identical for each of the three strings. The only difference should be the names of the string variables that you use.

When you run the program, you will be prompted to enter three strings--be sure that you enter them in the format requested or your program will crash when it attempts to break the strings apart.

Remember to follow the style guidelines and you must also follow any input or output text formatting requirements. Like many places in industry, we have automated routine testing of the programs you submit via try. If your output does not match our requirements, then your program will fail many if not all of the automated tests. Since this is a learning situation, an individual will go over and grade your program by hand if this happens. You will get points taken off, although the points will be minor in comparison with industry. In industry if you don't pass the tests, then you would get a 0.

How To Submit

After you are convinced that your program works correctly, submit it using the following command:

try grd-231 lab3-2 StringThing.java

Activity #4 : String Equality

In this activity you are to write a program that will read as input two strings and display the word true if the strings are the same and false otherwise. In this activity, two strings are the same if they contain exactly the same characters in the same order (i.e. 'A' and 'a' are not the same characters).

To help get you started, you will find in the lab 3 directory a file named TheSame.java. All you need to do is add the code to determine if the two strings are the same and display the output to standard out. The only thing that should appear in the output is the word true or false. No other characters should be produced as output.

The format of the prompts displayed by your program must be identical to the formats shown below:

Enter a string: 

Enter a second string: 

The output displayed by your program should be:


true

or


false

The output from the execution of your program should be identical to the output shown below for identical input by the user:

Enter a string: Hello

Enter a second string: Hello

true

Note that Hello is input by the user both times.

How To Submit

After you are sure your program produces the correct answers, submit it using the following command:

try grd-231 lab3-3 TheSame.java

Post-Lab Activity : Generating UNIX Account Names

Your unix account name consists of the first letter of your first name, followed by the first letter of your middle name, followed by the first letter of your last name, followed by 4 digits. These digits are most probably random numbers for your real account, but let's say that they are the last 4 digits from your phone number for this exercise.

Write a Java program, named UnixAccount.java, that uses Scanner to read a string that contains a user's first name, middle name, last name, and complete phone number with area code. Each piece of information will be separated by a single space and all the information will be read in a single string. The user should be able to enter only the digits of their number (i.e. no dashes); however, your program should work correctly even if the dashes are entered. There will be no parentheses entered. Please do not use the actual phone number of anyone you know when you test this program, in order to protect your(their) privacy. The program will compute the user's unix account name based on the input and display the result to standard out (on your monitor). Remember that the characters in a unix account name are all lowercase; however, a user's name may contain capital letters. You may assume that only valid input will be given to your program, i.e. correct name format followed by a phone number (with or without the dashes).

The format of the prompts displayed by your program must be identical to the format shown below:

Enter First Name, Middle Name, Last Name, and Phone Number: 

Note that there is a single space after the ":" and a blank line after the prompt. There should be no commas in the input from the user.

The format of the output displayed by your program must be identical to the format shown below:

Unix Account Name: abc1234

where abc1234 is the account name that your program has computed from the information input by the user. Note that there is one space after the ":". This is the only output that should be produced by your program.

The output from the execution of your program should be identical to the output shown below when input from the user is the same.

Enter First Name, Middle Name, Last Name, and Phone Number: Alicia Barbara Carpenter 7891561234

Unix Account Name: abc1234

Note that input from the user was Alicia Barbara Carpenter 7891561234. Also note that the same result would have been generated if the input was Alicia Barbara Carpenter 789-156-1234

How To Submit

Once you are convinced that your program is working correctly, submit it using the following command:

try grd-231 lab3-4 UnixAccount.java


Grade Computation

Grade Breakdown: