home—info—lects—labs—exams—hws
tutor/PIs—Object120 + its docs—java.lang docs—java.util docs
hw02
Strings, functions, booleans
shortened greetings
Part (a), due Sep.17 (Fri) by class
On WebCT > assessments, complete the questions for hw02a.
(Note how WebCT shows that you have one attempt and 30min to complete
the questions after you start.
There are 15 questions, similar in style to the hw02a-practice
(which you can repeat as many times as you like, not for a grade, with
no time limit).
You will not submit any hardcopy.
If you notice a typo or mistake in a question, please let me know.
Reading: § 5.1, and §5.2 up until (but not including)
"Conditional Operator".
Part (b), due Sep.20 (Mon) by class
Turn in a hard copy, and attach your Emcee.java
to WebCT > assessments > hw02b.
-
Make a class class Emcee extends Object120,
(importing Object120 as discussed in the
start of lab01b—Strings).
-
Optional/encouraged:
Add an opening javadoc comment which includes at least
one sentence and your name as an @author.
See examples of code from lab/lecture
(e.g. Pizzeria.java.)
In BlueJ, you can choose Documentation in the upper-right
to view the documentation and see if it's correct.
You can
-
Make the function shorten, which takes in somebody's
first last and first name (in that order), and returns
a shortened form of their name: their initials.
For example,
shorten( "Rogers", "Fred" ) should return "F.R.".
However, if the first and last initial are the same
then return that letter followed and apostrophe and ‘s’:
shorten( "Brummel", "Beau" ) should return "B's".
Follow the usual steps:
- (4pts) Write a signature for this function and a stub implementation.
- Optional: Write Javadoc for the function,
including @param and @return tags
appropriately. (See programs in lab/lecture notes for examples.)
- (4pts) Write test cases.
We will always assume that names contain at least one character.
(So you don't need any tests for the empty string,
however you should include one test for names with one letter,
e.g. Malcom X.)
(7pts) Then, complete the function body
(removing the stub body from step (i)).
heads-up:
When you check if two initials are equal:
use == if comparing chars,
but use boolean Object120.equals(String,String)
if comparing Strings.
(I suspect using chars
is a more natural fit for this problem,
but there are also reasonable approaches using Strings.)
-
Make a second Emcee function, greetFriend,
which takes in last and first name, and returns
a greeting for that friend.
The greeting must include their shortened name,
converted to lower case1.
For example, Fred Rogers might be greeted by
“Yo, f.r. -- whazzup?”.
(If you want to have greetFriend
answer with some pattern besides “yo ... whazzup”
that's fine.
Follow the same steps (a)-(d) as before (15pts).
When writing the function body, do not repeat any previous code
from shorten; instead call that function if you
want to use a shortened name.
1See
String Object120.toLowerCase(String). ↩
home—info—lects—labs—exams—hws
tutor/PIs—Object120 + its docs—java.lang docs—java.util docs