Download COSC 117 Lab 6 – Spring 2017 Page 1 of 2 Programs for this lab

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
Transcript
COSC 117 Lab 6 – Spring 2017
Programs for this lab should be stored in your Eclipse repository. Create a new Java project in
your workspace called lab6. Remember to use a location on your P: drive or a USB drive (not
the C: drive). When your programs are running correctly, turn in a printout of the Java code.
But wait… there’s more.!
Starting with this lab you also need to send an email containing your .java files for each
program (as separate file attachments) to the instructor at [email protected] with the
subject line “COSC 117 Lab 6”. The .java files should be located in the src directory for your
Eclipse project. Since this lab consists of three programs, Ithere should be three attachments.
Problem 1
Write a program called BetterSurvey that tallies the results for a beverage survey like the Survey
program in the previous lab. For this version, the program should not assume that the user
always enters a valid choice. Instead, display an error message when the user enters an incorrect
number and prompt them to enter a choice again. This version should also display a counter with
each person so that the survey taker knows how many people have been surveyed. Your output
should look similar to the example below.
Example 1:
1.Coffee
2.Milk
3.Red Bull
Please enter person #1's choice:
1.Coffee
2.Milk
3.Red Bull
Please enter person #2's choice:
7 is not a valid choice. Please
1.Coffee
2.Milk
3.Red Bull
Please enter person #2's choice:
1.Coffee
2.Milk
3.Red Bull
Please enter person #3's choice:
4.Water
2
4.Water
7
try again.
4.Water
2
4.Water
-1
Survey Results
==============
Coffee
0
Milk
2
Red Bull
0
Water
0
Problem 2
Write a new program named RomanNumeralsII that prompts the user to enter an integer between
1 and 10 (inclusive). The program should then display the Roman numeral equivalent for that
number (see http://en.wikipedia.org/wiki/Roman_numerals). The difference between this
program and the RomanNumeral program in lab 4 is that this program will validate that the
entered number and force the user to enter a number in the specified range. It should keep
prompting the user to enter integers until they enter one in the correct range. Since the user has to
Page 1 of 2
COSC 117 Lab 6 – Spring 2017
enter a number in the correct range to continue, the if-else in the main program no longer needs
to handle numbers that are not in the correct range.
Example 1:
Enter an integer from 1 to 10 (inclusive): 4
The Roman numeral for 4 is IV.
Example 2:
Enter an integer from 1 to 10 (inclusive): -3
-3 is not a valid choice.
Enter an integer from 1 to 10 (inclusive): 2
The Roman numeral for 2 is II.
Example 3:
Enter an integer from 1 to
347 is not a valid choice.
Enter an integer from 1 to
-9 is not a valid choice.
Enter an integer from 1 to
0 is not a valid choice.
Enter an integer from 1 to
The Roman numeral for 9 is
10 (inclusive): 347
10 (inclusive): -9
10 (inclusive): 0
10 (inclusive): 9
IX.
Problem 3
In an earlier lab you were asked to write a program that calculated the factorial of an integer (see
http://en.wikipedia.org/wiki/Factorial). For this problem you are going to expand on the earlier
Factorial program. Write a program called FactMore that repeatedly prompts the user for an
integer number (n) and displays the factorial of that number (n!). Your program should contain
two methods: a main method which uses a while loop to do all necessary input and output, and a
separate method called factorial that accepts one integer parameter and returns the factorial of
that integer. The factorial method should not perform any input from or output to the console.
Example 1:
Enter a non-negative integer (-1 to quit): 4
4! = 24
Enter a non-negative integer (-1 to quit): 4
0! = 1
Enter a non-negative integer (-1 to quit): -1
Goodbye!
Page 2 of 2