22222 The following are requirements of ALL programs that yo
2.2222 The following are requirements of ALL programs that you write from this point on!
You must write instructions to the screen to tell the user exactly what you want him/her to do. Your instructions must be very clear and descriptive, and should not contain any spelling or grammar errors.
You MUST choose variable names that describe the contents of the variable. Unless I specifically tell you to do so, single character variable names will not be acceptable unless you are using them to hold temporary data.
In addition to putting your name at the top of the program, you should also add a comment, 1-2 lines long, to briefly describe what the program does.
You MUST follow proper indention techniques in your code as discussed in class, and seen in the examples in your book. If you aren\'t sure you have the indention correct, you can use the correct indention option from the source menu in Eclipse.
Your source code must be NEAT and easy to read. Declare all of your variables at the beginning of the program. Add blank lines between sections of code to space out your program more and make the sections easier to see.
You MUST add comments all throughout your code to explain what your code is trying to do.
Pay close attention to the directions!! Make absolutely certain that you are doing exactly what the assignment asks you to do. If you don\'t understand the problem, make sure to ask your instructor or an assistant.
This is the last time you will be reminded of these requirements-- you must remember to start doing them in all of your programs from now on!
part 2
Create a new Java class inside your project folder.
The name of the class should be: ColorFinder
Note that this means you should have the following line at the top of your program:
public class ColorFinder
In color theory, the primary colors of an additive color system are red, green, and blue. An example of a computer device which uses the additive color system would be your computer monitor, because it adds red, green, and blue light together to make all of the colors you see on your screen.
The primary colors of a subtractive color system are magenta, yellow, and cyan. An example of a computer device which uses the subtractive color system would be your printer, because it mixes magenta, yellow, and cyan colored inks together to make all of the colors you see on paper.
Write a program that gets a string from the keyboard and tests whether the string contains one of our primary colors. Your program should be able to find the primary colors, no matter what letters in the color are capitalized. For example, your program should be able to finds words like grEeN.
Begin by asking the user to type in a sentence.
Once you have the sentence, you should proceed to test it to see if it contains one of the primary colors.
If one of the additive colors is found in the sentence, print the message \"Additive primary color found.\" to the screen.
If one of the subtractive colors is found in the sentence, print the message \"Subtractive primary color found.\" to the screen.
If a sentence contains both additive and subtractive colors, you should print both messages \"Additive primary color found.\" and \"Subtractive primary color found.\" to the screen.
If none of the colors are found in the sentence, print the message \"No primary colors found.\" to the screen.
The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user\'s values are shown below in italics:
Enter a sentence:
Roses are red, violets are blue.
Additive primary color found.
Here is another example run of the program:
Enter a sentence:
Aqua is a color that is similar to Cyan.
Subtractive primary color found.
Here is another example run of the program:
Enter a sentence:
Have you been to Yellowstone Park?
Subtractive primary color found.
Here is another example run of the program:
Enter a sentence:
Yellow is next to green in the rainbow.
Additive primary color found.
Subtractive primary color found.
Here is another example run of the program:
Enter a sentence:
Black is technically not a color.
No primary colors found.
Hints:
Refer to the JAVA documentation website to find the String methods that will help you accomplish your goals.
In order to deal with the possible ways that the user may mix upper and lowercase letters, you should begin by converting the inputted string to all lowercase (or all uppercase) letters. There is a String method to do this for you.
When trying to figure out if there is NOT a primary color in the sentence, do not attempt to re-test for all of the words again; this is too inefficient. Instead, use a boolean variable (also called a flag variable) to help you record when you have found a color in the sentence.
Solution
import java.util.Scanner;
/**
* @author
*
*/
public class ColorFinder {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
// read string from console
System.out.print(\"Enter a sentence:\");
String sentence = scanner.nextLine();
// change to all to lowercase
sentence = sentence.toLowerCase();
String[] sentenceArray = sentence.split(\" \");
// System.out.println(sentenceArray.length);
boolean addFlag = true, subFlag = true;
for (int i = 0; i < sentenceArray.length; i++) {
// checking for additive colors
if (addFlag) {
if (sentenceArray[i].contains(\"red\")) {
System.out.println(\"Additive primary color found.\");
addFlag = false;
continue;
} else if (sentenceArray[i].contains(\"green\")) {
System.out.println(\"Additive primary color found.\");
addFlag = false;
continue;
} else if (sentenceArray[i].contains(\"blue\")) {
System.out.println(\"Additive primary color found.\");
addFlag = false;
continue;
}
}
// checking for subtractive colors
if (subFlag) {
if (sentenceArray[i].contains(\"magenta\")) {
System.out.println(\"Subtractive primary color found.\");
subFlag = false;
continue;
} else if (sentenceArray[i].contains(\"yellow\")) {
System.out.println(\"Subtractive primary color found.\");
subFlag = false;
continue;
} else if (sentenceArray[i].contains(\"cyan\")) {
System.out.println(\"Subtractive primary color found.\");
subFlag = false;
continue;
}
}
}
if (addFlag && subFlag)
System.out.println(\"No primary colors found.\");
} catch (Exception e) {
// TODO: handle exception
}
}
}
OUTPUT:
Enter a sentence:Roses are red, violets are blue.
Additive primary color found.
Enter a sentence:Aqua is a color that is similar to Cyan.
Subtractive primary color found.
Enter a sentence:Have you been to Yellowstone Park?
Subtractive primary color found.
Enter a sentence:Yellow is next to green in the rainbow.
Subtractive primary color found.
Additive primary color found.
Enter a sentence:Black is technically not a color.
No primary colors found.



