Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CSc 127A

Version 1.0
October 7, 2010
Assignment #6: FindWord and ThreeLargest
Due: Wednesday, October 13th by 9:00 pm.
Put a block comment at the beginning of the Java file that contains:
/*
Your first and last name
Your section leader’s name
Your section #, day, and time
Your partner’s name & section # (skip this line if working solo)
Assignment 6 - Problem 1, CSc 127A, Spring 2010
Short description of what your program does
*/
Include your partner’s name if you work with a partner on either or both programs. See the
course syllabus for details on working with a partner. The partner’s name and section number are
required to avoid an Academic Integrity violation.
Grading Notes:
Programs should compile without errors or warnings; those that do not compile will not receive a
passing grade.
For full credit, programs should produce the output shown below.

Program 1: FindWord
Name your program: FindWord.java.
The program will ask the user for a search word.
The program will then ask the user for words. The user will enter stop when done entering
words. The word stop should be recognized regardless of the case of the letters in the word.
The program will then print four totals:
• The number of times the search word appears exactly in the list of words.
• The number of times the search word appears but the case does not match.
• The number of words that have exactly the same first character as the search word.
• The number of words that have the same first character but the case does not match.
Example:
Enter search word: computer
Enter a word, type 'stop' to halt: break
Enter a word, type 'stop' to halt: computer
Enter a word, type 'stop' to halt: java
Enter a word, type 'stop' to halt: CompUTER
Enter a word, type 'stop' to halt: case
Enter a word, type 'stop' to halt: CARROT
Enter a word, type 'stop' to halt: while
Enter a word, type 'stop' to halt: if
Enter a word, type 'stop' to halt: STop


1 of 4
CSc 127A
Version 1.0
October 7, 2010

computer appears 1 time


computer appears 1 time when case does not match
2 words start with the same letter as computer
2 words start with the same letter as computer but the opposite case

Program 2: ThreeLargest
Name your program: ThreeLargest.java.
This program will ask the user for non-negative integers (integers that are >= 0). When the user
enters -1, the program will stop asking for integers. The program will then print the three largest
integers, and a count of the non-negative integers.
An example:
Enter an integer >= 0, use -1 to stop: 45
Enter an integer >= 0, use -1 to stop: 53
Enter an integer >= 0, use -1 to stop: 27
Enter an integer >= 0, use -1 to stop: 32
Enter an integer >= 0, use -1 to stop: 98
Enter an integer >= 0, use -1 to stop: -1

5 non-negative numbers were entered.


The largest number is 98
The second largest number is 53
The third largest number is 45

Programming hints:
Start by writing a loop that reads the numbers and prints the total.
Then, add the code to find the largest and print the largest.
Then, work through the special cases (see what follows).
Special cases:
If the user enters a negative number, other than -1, do not count the number and do not consider
it as a candidate for one of the largest numbers. Example:
Enter an integer >= 0, use -1 to stop: 42
Enter an integer >= 0, use -1 to stop: 57
Enter an integer >= 0, use -1 to stop: -18
Enter an integer >= 0, use -1 to stop: 62
Enter an integer >= 0, use -1 to stop: -3928
Enter an integer >= 0, use -1 to stop: 50
Enter an integer >= 0, use -1 to stop: -1

4 non-negative numbers were entered.


The largest number is 62
The second largest number is 57
The third largest number is 50


2 of 4
CSc 127A
Version 1.0
October 7, 2010
If the user enters less than 3 numbers, do not print all three large values.
Example with two non-negative integers entered:
Enter an integer >= 0, use -1 to stop: 23
Enter an integer >= 0, use -1 to stop: 20
Enter an integer >= 0, use -1 to stop: -1

2 non-negative numbers were entered.


The largest number is 23
The second largest number is 20

Example with one non-negative integer entered:


Enter an integer >= 0, use -1 to stop: 67
Enter an integer >= 0, use -1 to stop: -1

1 non-negative number was entered.

Example with no non-negative integers entered:


Enter an integer >= 0, use -1 to stop: -1

No non-negative numbers were entered.

The second largest number must be less than the largest number, and the third largest number
must be less than the second largest number.
Example with duplicate instances of the largest and second largest number:
Enter an integer >= 0, use -1 to stop: 18
Enter an integer >= 0, use -1 to stop: 20
Enter an integer >= 0, use -1 to stop: 18
Enter an integer >= 0, use -1 to stop: 20
Enter an integer >= 0, use -1 to stop: 18
Enter an integer >= 0, use -1 to stop: -1

5 non-negative numbers were entered.


The largest number is 20
The second largest number is 18

Another example:
Enter an integer >= 0, use -1 to stop: 42
Enter an integer >= 0, use -1 to stop: 63
Enter an integer >= 0, use -1 to stop: 63
Enter an integer >= 0, use -1 to stop: 63
Enter an integer >= 0, use -1 to stop: -633
Enter an integer >= 0, use -1 to stop: 42
Enter an integer >= 0, use -1 to stop: 50
Enter an integer >= 0, use -1 to stop: 50
Enter an integer >= 0, use -1 to stop: -1

7 non-negative numbers were entered.


The largest number is 63


3 of 4
CSc 127A
Version 1.0
October 7, 2010
The second largest number is 50
The third largest number is 42

Turnin:
We are accepting programs via D2L only.
• Name your programs FindWord.java and ThreeLargest.java.
• Log on to D2L, and select CSc 127A.
• Select “Dropbox”
• You will find Program6 and Program6-Late. Only one of these will be active. Click on the
active one — will be Program6 if you are turning in the work on time.
• You should now be at the page: “Submit Files - Program6”. Click on the “Add a File”
button. A pop-up window will appear. Click on the “Choose File” button. Use the file
browser that will appear to select your FindWord.java or ThreeLargest.java file.
Click on the “Upload” button in the lower-right corner of the pop-up window.
• You are now back at “Submit Files - Program6”. Click on “Upload” in the lower-right of
this window. You should now be at the “File Upload Results” page, and should see the
message File Submission Successful.


4 of 4

You might also like