Docsity

Introduction to Computing and Programming Lab 5 | CSCI 1301, Lab Reports of Computer Science

Toggle sidebar
bg1
CSCI 1301 – Lab #5
Exercise
The goal of this exercise is to familiarize you with String variables and the use of some of
the methods of the class String.
Write a java program that contains a class called PlayingWithStrings with a main
method that performs the following actions:
a. Write a declaration of a String variable called bestSwimmer and initialize it
“MICHAEL F. WHELPS”.
b. Write an assignment statement to replace W in the string bestSwimmer with a capital
P using the appropriate String method.
c. Write a declaration for a String variable called firstName and initialize it to the
substring containing the first 7 characters of the String bestSwimmer (use a string
method to obtain the substring).
d. Write a declaration for a String variable called lastName and use the appropriate
String method to initialize lastName to the string containing the last 6 characters of
the String bestSwimmer.
e. Write a declaration of a String variable called middleInitial and initialize it to the
substring of characters in bestSwimmer with indexes between 8 and 9 using the
appropriate String method.
f. Write several assignments statements to convert the characters in the string variables
firstName and lastName to lowercase.
g. Use the appropriate String method to replace the first letter of the firstName and
lastName by its corresponding capital letter.
h. Write a declaration of a String variable called reversedName and initialize it to the
concatenation of lastName, firstName and middleInitial so the name is in the following
format: Last Name, First Name MiddleInitial.
i. Display the following short biography inserting the variables reversedName, firstName,
middleInitial and lastName in the appropriate locations:
reversedName –,
June 30, 1985,
Baltimore, Maryland.
firstName middleInitial lastName is an American swimmer. lastName has won 14 career
Olympic gold medals, the most by any Olympian. lastName went a perfect 8-for-8 in
Beijing, breaking Mark Spitz's single-Games record for gold medals. As of September
2008, firstName lastName holds seven world records in swimming.
After you have completed this exercise, demonstrate it to your lab instructor.
pf2

Partial preview of the text

Download Introduction to Computing and Programming Lab 5 | CSCI 1301 and more Lab Reports Computer Science in PDF only on Docsity!

CSCI 1301 – Lab

Exercise

The goal of this exercise is to familiarize you with String variables and the use of some of the methods of the class String.

Write a java program that contains a class called PlayingWithStrings with a main method that performs the following actions:

a. Write a declaration of a String variable called bestSwimmer and initialize it “MICHAEL F. WHELPS”.

b. Write an assignment statement to replace W in the string bestSwimmer with a capital P using the appropriate String method.

c. Write a declaration for a String variable called firstName and initialize it to the substring containing the first 7 characters of the String bestSwimmer (use a string method to obtain the substring).

d. Write a declaration for a String variable called lastName and use the appropriate String method to initialize lastName to the string containing the last 6 characters of the String bestSwimmer.

e. Write a declaration of a String variable called middleInitial and initialize it to the substring of characters in bestSwimmer with indexes between 8 and 9 using the appropriate String method.

f. Write several assignments statements to convert the characters in the string variables firstName and lastName to lowercase.

g. Use the appropriate String method to replace the first letter of the firstName and lastName by its corresponding capital letter.

h. Write a declaration of a String variable called reversedName and initialize it to the concatenation of lastName, firstName and middleInitial so the name is in the following format: Last Name, First Name MiddleInitial.

i. Display the following short biography inserting the variables reversedName, firstName, middleInitial and lastName in the appropriate locations:

reversedName –, June 30, 1985, Baltimore, Maryland.

firstName middleInitial lastName is an American swimmer. lastName has won 14 career Olympic gold medals, the most by any Olympian. lastName went a perfect 8-for-8 in Beijing, breaking Mark Spitz's single-Games record for gold medals. As of September 2008, firstName lastName holds seven world records in swimming.

After you have completed this exercise, demonstrate it to your lab instructor.

WebCT Submission

After you have completed both exercises in this lab, upload the file PlayingWithStrings.java in WebCT and submit it to receive credit.