|
|
|
|
CS4984 OBJECT ORIENTED SOFTWARE DESIGN AND CONSTRUCTION WITH JAVA HOMEWORK #1 GIVEN: February 1, 2001 DUE: February 15, 2001
PROBLEM #1: Design a Java program entitled NameCount.java that takes a first name and a last name as command line arguments in that order. In response, the program should print the following to the screen: "My name is FIRSTNAME LASTNAME." "My name contains NUMCHARS characters." Of course, FIRSTNAME is the first name that you entered on the command line, LASTNAME represents the last name that you entered on the command line. NUMCHARS represents the number of characters in the first name and the last name added together. For example, if you type: "java NameCount Cynthia North" The following would be printed to the screen by your program: My name is Cynthia North. My name contains 12 characters. Make sure that if the user enters a number of command arguments that is something other than two that you print an error message showing the user how many arguments he entered and how many are legal. For example, if you type the following on the command line: java NameCount Andy One response to this could be: ERROR: You typed 1 argument. All commands should be of the form java NameCount FIRSTNAME LASTNAME. HINTS: You should examine the Echo.java program that was posted to the class listserver for an example of how to write a program with command line arguments.
PROBLEM #2: Design a WordCounter class. When invoked on the command line, ("java WordCounter") the following options shall appear on the screen: 0 Quit 1 Add sentence 2 Get letter count 3 Get word start count 4 Clear all entries so far. The action taken for each option is : Quit- Exit the java program. Add sentence- Prompt the user to type a sentence. Any text that is typed will be added to a large string of characters that have been previously typed. End the text entry by pressing carriage return. Get letter count- Prompt the user to enter a character. When the user enters any character and then line return, the count of that particular character entered so far should be printed to the screen. Get word start count- Prompt the user to enter a character. When the user enters any character and then line return, the count of the number of words entered so far that begin with that character should be printed to the screen. Clear all entries so far- All sentences stored by the program will be cleared. After each choice is made and serviced by the program, redisplay the five options. Make sure that your program is forgiving. If a number other than 0-4 is entered when the main menu appears, have the program print an error message prompting the user to try again. HINTS: You will need to make heavy use of the String class and its methods. You also need to examine the example of console input in Java that I showed you in the lecture on Chapter 2. Make sure that your code is well commented. I will judge your code by reading it, and also by copying your .java file(s) into TextPad, compiling it, and then running it. Copyright © 2000-2001 First Principles. All rights
reserved. |