Repetition and Counting Colors

19 Jan 2023 - 14:03 | Version 1 |

Learning Objective(s)

  1. Learn about repetition
  2. Learn to use the while statement
  3. Learn to reject some bad input
  4. Learn to use the switch statement
  5. Optional: Learn to use the ++ operator

Overview of the Program

For this program you are to get count the number of each type of color inputted by the user and output the percentage for each color

Details

  1. Your colors are to be red, blue, green, and yellow.
  2. You must use constants for the colors and the end of data
  3. Have the user input 0 to indicate being done inputting
  4. Use a switch statement to decide which variable to increment. You may use an if statement if you wish but you will lose a few points.
  5. Program should be called ColorCounter

Example program dialogue:

These are not meant to be sufficient testing. Nor must you follow the exact wording. They are just to demonstrate what an acceptable program might do for certain input
What color (1=blue, 2=red, 3=green, 4=yellow, 0=end)
1
Next color?
2
Next color?
5
That was not a valid choice. Again?
1
Next color?
2
Next color?
0
The % of blue was 0.5
The % of red was 0.5
The % of green was 0.0
The % of yellow was 0.0

Submission

  1. create an empty zip folder
  2. place your java file into it
  3. create a pdf document that contains your Software Development Report for this program and place the pdf file into the zip folder
  4. submit the zip folder on canvas

Hints

  1. Program design
  2. Hint for getting valid input
  3. My code looks perfect but I am just getting 0's. I can't find the problem
  4. Help to debug a program that compiles but does not generate the right output

Optional improvements

Optional improvements should ONLY be worked on once the program is completing working. In addition, the work is to be done on your own with little if any help for lab assistants or the instructor.
  1. Allow the user to input the colors as a string and the code not be case sensitive. Do not use strings in the switch statement
  2. Have it properly (and nicely) handle the user entering 0 for the first color.
  3. Extremely advanced Have it work with 25 different colors but only have one special variable to contain all the counters for individual colors.
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback
This website is using cookies. More info. That's Fine