Lab Assignment #2: Objects and Strings

Due by 11:59PM on Wednesday, Feb 3.


Reading Assignment

Read section 2.6 (again) and sections 3.1-3.2 of the text Computing Concepts with Java Essentials. This covers material on the String and Time classes.

Please do the readings from the book and also read this entire assignment before lab this week!


Getting Started

Note that computer failures will not be accepted as an excuse for late assignments unless they are widespread and long lasting. We recommend that you start your programs early to avoid the risk of unexpected computer failures. Do not wait until the last minute to mail in your programs! Late submissions will not be accepted.


Comments

As usual, make sure that all your programs are header by a comment block of the following form:

/*
Your Name
Your ID Number
Your Username
Which Assignment
Your Laboratory Section Number
*/

Fully document your code with meaningful comments. Use a consistent and readable indentation scheme. You will be graded on both the correctness of your programs and the style in which they are written.


In-Lab Work

To receive credit for this in-lab assignment, you must complete the following two exercises and submit them during your lab session. You will mail your submission as usual to a201@cs.indiana.edu. Your AI will provide you with the correct subject during lab.

This is not a test! If you have any trouble at all with these programs, your AI will be happy to help you!

  1. Write a program that prompts the user for a 4 letter word and then prints the word backwards.

    Here's a sample run.

    Enter a four letter word: frog

    The word frog is gorf backwards.

    Do not worry about what happens if the user enters a word which is longer or shorter than four characters.

    You will need to make use of the substring method in the String class. This function is described on page 68 of the text.

    Save your program as Backwards.java.

  2. Write a program that prompts the user for three integer values: a year, a month, and a day. Construct a Time object representing the date given by the user and then print the date one week later.

    Here's a sample run.

    Please enter the year: 1988
    Please enter the month: 2
    Please enter the day: 28

    One week after 2/28/1988 is 3/6/1988.

    Here's another example:

    Please enter the year: 1987
    Please enter the month: 12
    Please enter the day: 30

    One week after 12/30/1997 is 1/6/1998.

    Consult the table on page 91 of the text for a handy list of the methods available in the Time class. All of the get methods return an int type value. The addseconds method does not return a value; it actually updates the object itself by changing its internal state. We call such a method a mutator.

    Save your program as Week.java.


Lab Assignment

This is your actual lab assignment for the week. You will write and debug three programs. Your AI may discuss strategies for solving these problems, but there will not be time in lab for you to complete the programs.

Please take care to use the exact filenames specified in the problems below.

  1. Exercise P2.13, Page 81. Refer to the table on the bottom of page 73 for a synopsis of the important String methods. Save your program in a file named Comma.java.

  2. Exercise P2.22, Page 84. Save your program in a file named Months.java.

  3. Exercise P3.17, Page 129. Display a nice welcome message to the user with instructions detailing what he is supposed to do. To give the user time to read the instructions, pause by prompting him to press Enter when he is ready to take the typing test. After he has pressed Enter, determine the current time and then read the sentence that he types. Again determine the current time and figure out the number of seconds that have elapsed since the test started. For example,

    Welcome to the Typing Test!
    Please type the following sentence exactly as it appears below.

    The quick brown fox jumps over the lazy dog.

    Press Enter when you are ready to begin... <user presses Enter here>

    Theq uick brown fix jumcs over the laay dog.

    Your time: 5 seconds

    Good-bye.

    Notice that you do not need to count typos. It doesn't really matter what the user types at all.

    Save your program in a file named Typing.java.


Submitting Your Lab Assignment

For this lab, you must submit the two in-lab exercise during your normally scheduled lab session. By Wednesday, February 3 at midninght you should submit these three files:

Do not submit .prj or .class files. Only the three files named above should be submitted.

In order to submit files, click on the following link to send mail to:

a201@cs.indiana.edu

with the following Subject:

lab2

and then Attach your program files to the e-mail message. You may attach all three files in the same message, but you don't have to. You can submit your programs in separate e-mails if you wish. Do not type anything in the body of the message! You must use the filenames specified in this assignment and you must attach the files to the mail message.


Last modified: Thu Feb 4 21:57:43 EST 1999 by asengupt@cs.indiana.edu