Create two Java files a driver called Crayolajava and a fi

Create two Java files:

• a “driver” called Crayola.java

• and a file called CrayonBox.java

The idea is that the CrayonBox uses an array to store a collection of Strings corresponding to the different colors that you have at your disposal in your crayon box.

Specifications Driver

The “driver” called Crayola.java that has a single method (main) It’s purpose is two-fold.

It will print the scenario:

Your mother took you to ‘‘Crayola Experience’’
Because you behaved yourself, she will let you get a box of crayons

and display the options to the user:

What option will you choose? 1. Buy a prepackaged box. 2. Build your own box.

It will handle the user input
• The selection to the prompt, and

• if the use chooses option #2, the driver will prompt the user to enter the number of crayons they have in their box, as well as ask for the “colors”.

CrayonBox

The CrayonBox.java class has a single instance variable crayons which stores an array of String objects. Your class should have the following four methods:

1. A default constructor, that is initialized with the following colors:
red, yellow, green, blue, brown, black, orange, purple

2. A parameterized constructor with a single integer parameter (numCrayons) numCrayons will be the length of the crayons array

3. An overloaded toString method that says:
These are the crayon colors you have in your box

Followed by the colors


4. A setColor method that takes two parameters.

(a) an Integer type corresponding to the index into the crayons array

(b) a String type corresponding to the value (“color”) that we will place at the specified index.

Notes/Hints

Below are some general hints/tips for completing this project.

1. If the user selects option #1, create a CrayonBox object using the default constructor and print.

2. If the user selects option #2, first ask for the number of crayons and then use the parameterized constructor to create an array of the correct size. Do not forget to initialize a value for each indexed variable. Then, use a loop structure to prompt the user for a color, and use the setColor method to do the assignment.

Example Output 1

Your mother took you to ‘ ‘ Crayola Experience ’ ’
Because you behaved yourself , she will let you get a box of crayons

What option will you choose?

1. Buy a prepackaged box
2. Build your own box

1
These are the crayon colors you have in your box

red

yellow

green

blue

brown

black

orange

purple

Example Output 2

Your mother took you to ‘ ‘ Crayola Experience ’ ’
Because you behaved yourself , she will let you get a box of crayons

What option will you choose?

1. Buy a prepackaged box
2. Build your own box

2
How many crayons are in your box
5
What color is Crayon 1: Blue
What color is Crayon 2: Navy Blue
What color is Crayon 3: Midnight Blue
What color is Crayon 4: Celestial Blue
What color is Crayon 5: Ultramarine Blue
These are the crayon colors you have in your box

Blue

Navy Blue

Midnight Blue

Celestial Blue

Ultramarine Blue

Solution

//CrayonBox.java
public class CrayonBox {
  
   //instance variabeles
   //declare an arary of type string
   private String[] crayons ;
   //integer variable
   private int numCrayons;
  
   //default constructor
   public CrayonBox() {
       numCrayons=8;      
       crayons=new String[numCrayons];
       crayons[0]=\"red\";
       crayons[1]=\"yellow\";
       crayons[2]=\"green\";
       crayons[3]=\"blue\";
       crayons[4]=\"brown\";
       crayons[5]=\"black\";
       crayons[6]=\"orange\";
       crayons[7]=\"purple\";      
   }
   //parameterized constructor
   public CrayonBox(int numCrayons) {
       this.numCrayons=numCrayons;      
       crayons=new String[numCrayons];          
   }
  
   public void setColor(int index,String color){      
       crayons[index]=color;
   }
  
  
   //override toString method that returns
   //the crayon colors
   @Override
   public String toString() {
       String crayonsDesc=\" These are the crayon colors you have in your box\";
      
       for (String color : crayons) {
           crayonsDesc+=color+\"\ \";
       }
      
       return crayonsDesc;
      
   }

}


--------------------------------------------------------------------------------


//Crayola.java
import java.util.Scanner;
public class Crayola {
  
   public static void main(String[] args) {
      
       //Create an instance of Scanner
       Scanner scanner=new Scanner(System.in);
       System.out.println(\"Your mother took you to \\\"Crayola Experience\\\"\");
       System.out.println(\"Because you behaved yourself , she will let you get a box of crayons\");

       //Declare CrayonBox variable
       CrayonBox crayonBox;
      
       System.out.println(\"1. Buy a prepackaged box\");
       System.out.println(\"2. Build your own box\");
       System.out.println(\"What option will you choose?\");
       //prompt for user choice
       int choice=Integer.parseInt(scanner.nextLine());
      
      
      
       if(choice==1)
       {
           //Create an default constructor object
           crayonBox=new CrayonBox();
           System.out.println(crayonBox);
       }
       else if(choice==2)
       {
           System.out.println(\"How many crayons are in your box\");
           int numCrayons=Integer.parseInt(scanner.nextLine());
           //Create an instance of parameterized object
           crayonBox=new CrayonBox(numCrayons);
          
           for (int i = 0; i < numCrayons; i++)
           {
               System.out.printf(\"What color is Crayon \"+(i+1)+\" :\ \");
               String color=scanner.nextLine();
               crayonBox.setColor(i, color);              
           }
          
           System.out.println(crayonBox);
       }
       else
           System.out.println(\"Invalid Choice\");
      
      
   }

}

--------------------------------------------------------------------------------

Sample output:


Run1:

Your mother took you to \"Crayola Experience\"
Because you behaved yourself , she will let you get a box of crayons
1. Buy a prepackaged box
2. Build your own box
What option will you choose?
1
These are the crayon colors you have in your boxred
yellow
green
blue
brown
black
orange
purple


Run2:
Your mother took you to \"Crayola Experience\"
Because you behaved yourself , she will let you get a box of crayons
1. Buy a prepackaged box
2. Build your own box
What option will you choose?
2
How many crayons are in your box
5
What color is Crayon 1 :
Blue
What color is Crayon 2 :
Navy Blue
What color is Crayon 3 :
Midnight Blue
What color is Crayon 4 :
Celestial Blue
What color is Crayon 5 :
Ultramarine Blue
These are the crayon colors you have in your boxBlue
Navy Blue
Midnight Blue
Celestial Blue
Ultramarine Blue

Create two Java files: • a “driver” called Crayola.java • and a file called CrayonBox.java The idea is that the CrayonBox uses an array to store a collection of
Create two Java files: • a “driver” called Crayola.java • and a file called CrayonBox.java The idea is that the CrayonBox uses an array to store a collection of
Create two Java files: • a “driver” called Crayola.java • and a file called CrayonBox.java The idea is that the CrayonBox uses an array to store a collection of
Create two Java files: • a “driver” called Crayola.java • and a file called CrayonBox.java The idea is that the CrayonBox uses an array to store a collection of
Create two Java files: • a “driver” called Crayola.java • and a file called CrayonBox.java The idea is that the CrayonBox uses an array to store a collection of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site