Create a program that simulates a pack of eight Crayons (an array of objects). E
ID: 3832590 • Letter: C
Question
Create a program that simulates a pack of eight Crayons (an array of objects). Each crayon should have a color (red, yellow, green, blue, brown, black, orange and purple), length in millimeters and sharpness (a number between 1 and 5). The user wants to be able to randomly select a crayon and then be presented with the color, length and sharpness (no one likes a dull crayon). Let the user randomly select three Crayons. The same color Crayon cannot be select more than once. (Crayon.java) (13 pts) The user should also be able to view all of the Crayons (color, length and sharpness) in a menu as an alternative to the random selection. Extra-Credit: The user should be able to exit the program at any time by typing ‘end’(IN JAVA
Explanation / Answer
PROGRAM CODE:
package util;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class CrayonChooser {
public static void main(String[] args) {
ArrayList<Crayon> packOfCrayons = new ArrayList<>();
packOfCrayons.add(new Crayon("Blue", 10, 3));
packOfCrayons.add(new Crayon("Yellow", 10, 3));
packOfCrayons.add(new Crayon("Red", 10, 3));
packOfCrayons.add(new Crayon("Black", 10, 3));
packOfCrayons.add(new Crayon("Green", 10, 3));
packOfCrayons.add(new Crayon("Orange", 10, 3));
packOfCrayons.add(new Crayon("Pink", 10, 3));
packOfCrayons.add(new Crayon("Brown", 10, 3));
Scanner keyboard = new Scanner(System.in);
Random random = new Random();
while(true)
{
String choice;
System.out.println(" Show - Show a crayon View - view all crayons End - End the program Enter your choice");
choice = keyboard.next();
if(choice.equalsIgnoreCase("end"))
System.exit(0);
else if(choice.equalsIgnoreCase("show"))
{
if(packOfCrayons.size()>0)
{
int rand = random.nextInt(packOfCrayons.size());
System.out.println(packOfCrayons.get(rand));
packOfCrayons.remove(rand);
}
}
else if(choice.equalsIgnoreCase("view"))
{
for(int i=0; i<packOfCrayons.size(); i++)
System.out.println(packOfCrayons.get(i));
}
}
}
}
OUTPUT:
Show - Show a crayon
View - view all crayons
End - End the program
Enter your choice
show
Crayon: Red of 10.0 millimeters and sharpness of 3
Show - Show a crayon
View - view all crayons
End - End the program
Enter your choice
view
Crayon: Blue of 10.0 millimeters and sharpness of 3
Crayon: Yellow of 10.0 millimeters and sharpness of 3
Crayon: Black of 10.0 millimeters and sharpness of 3
Crayon: Green of 10.0 millimeters and sharpness of 3
Crayon: Orange of 10.0 millimeters and sharpness of 3
Crayon: Pink of 10.0 millimeters and sharpness of 3
Crayon: Brown of 10.0 millimeters and sharpness of 3
Show - Show a crayon
View - view all crayons
End - End the program
Enter your choice
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.