For this assignment, you are allowed to work in pairs if you wish. You must sign up for a group in Blackboard even if you are in a group of one.
These exercises will allow you to write some programs structured using simple functions and procedures.
##Objectives
- Practice with programming fundamentals
- Variables - Declaration and Assignment
- Primitive types
- Arithmetic Expressions
- Simple keyboard input and text display output
- Branching - if-elseif-else syntax
- Loops - simple while loops, nested while loops
- Methods - simple functions
- Works towards the following Course Goals:
- Competency with using basic coding features of a high-level imperative programming language
- Competency with writing computer programs to implement given simple algorithms
- Familiarity with designing simple text-oriented user interfaces
- To start, fork this repository.
- Clone the repository to your computer.
You will be writing a simple Java program that counts the words in a String. A "skeleton" of this code is provided in the file you cloned. You will see that there are three methods declared in this file with no code provided. You must fill in the appropriate code. Pay close attention to what each method should be doing based on the information given in the comments before the method.
This is a sample transcript of what your program should do. Text in bold is expected input from the user rather than output from the program:
Enter a string: the quick brown fox jumped Your string has 5 words in it.
If you provide an empty string, your program should report an error until the user enters a non-empty string:
Enter a string: ERROR - string must not be empty. Enter a string: A man, a plan, a canal; Panama Your string has 7 words in it.
NOTE: As described in the comments in the skeleton file, you may assume that words will have exactly one space between them.
Next, commit your changes. Use the commit message "Finish exercise 1."
For this exercise, you should extend the code that you wrote in Exercise 1 with a new method. This new method should use the following method header:
private static String getFirstWord(String input)
It should take a String as input and return the first word of that String, using the definition of a word given in Exercise 1. If the String
is empty, it should return the empty string.
Then modify your main program so that after it reports how many words are in your String
it also reports what the first word of your input String
is.
This is a sample transcript of what your program should do.
Enter a string: The quick brown fox jumped Your string has 5 words in it. The first word is: The
If there is only one word in your String
, make sure that your program identifies it correctly:
Enter a string: One Your string has 1 words in it. The first word is: One
If the user enters an empty String
, the code should report an error as it did in Exercise 1:
Enter a string: ERROR - string must not be empty. Enter a string: Four score and seven years ago Your string has 6 words in it. The first word is: Four
Next, commit your changes. Use the commit message "Finish exercise 2."
Write a new method getWord()
that uses this method header:
private static String getWord(String input, int n)
This method should act just like getFirstWord()
did in Exercise 2 above, except that instead of returning the first word, it returns the nth word in the string, with the first word as word 0, the second word as word 1, etc. For example the following code:
String myString = getWord("the quick brown fox jumped", 3);
should result in the variable myString taking on the value of "fox". If the requested index is out of bounds your method should return an empty String
"".
Modfiy your main loop to use this new method and allow the user to pick an arbitrary word to extract from your input String
.
Finally:
- Commit your changes. Use the commit message "Finish exercise 3."
- Push/sync the changes up to GitHub.
- Create a pull request on the original repository to turn in the assignment.
- Submit, in Blackboard, your GitHub username.