Could you please complete the missing part in JavaBeanTester class. JellyBean Cl
ID: 3910736 • Letter: C
Question
Could you please complete the missing part in JavaBeanTester class.
JellyBean Class:
public class JellyBean
{
//These are the 3 instance variables of a JellyBean object:
String flavor;
String color;
boolean eatMe;
/**
* The purpose of this constructor is to move the parameters passed to the constructor
* into the instance variables of the JellyBean object The 2 parameters are:
* @param aFlavor
* @param aColor
*/
public JellyBean(String aFlavor, String aColor)
{
//Initialize each of the instance variables of the JellyBean object with the parameters passed to the constructor
//Then, set eatMe attribute to false
this.flavor = flavor;
this.color= color;
this.eatMe= false;
}
public String getFlavor()
{
return flavor;
}
public void setFlavor(String aFlavor)
{
flavor = aFlavor;
}
//Define the getters for color and eatMe:
public String getColor()
{
return color;
}
public void setColor(String aColor)
{
color = aColor;
}
public boolean isEatMe(){
return eatMe;
}
public void setEatMe(boolean eatMe){
this.eatMe = eatMe;
}
/**
*
* @return a String representation of all the attributes in the JellyBean class
*/
public String toString()
{
//return all the attributes of the JellyBean object, all concatenated together
return flavor+","+color+"and"+eatMe;
}
}
JellyBeanTester Class:
public class JellyBeanTester
{
// These are the 3 global variables that will each hold a JellyBean object. These variables
// can be accessed by any method in the tester class because they are global.
static JellyBean jb1;
static JellyBean jb2;
static JellyBean jb3;
static Scanner keyboard = new Scanner(System.in);
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// call the createJellyBeanBag
createJellyBeanBag();
//call the processJellyBeanBag and store the return value in a variable
processJellyBeanBag();
//Print out the total number of orange JellyBeans
System.out.println("The number of orange colored jelly beans: ");
//Print out all the jellybean
//Print out each of the 3 JellyBean objects:
System.out.println("Jelly bean1: " + jb1);
System.out.println("Jelly bean2: " + jb2);
System.out.println("Jelly bean3: " + jb3);
}
/**
* The createJellyBeanBag method will ask the user for input, and will
* use that input to create 3 JellyBean objects.
*/
public static void createJellyBeanBag()
{
String userInputJBColor;
String userInputJBFlavor;
//Ask the user to enter the color of the first jelly bean object, and save their answer in userInputJBColor:
System.out.print("Enter the Color of the First Jelly Bean :");
userInputJBColor = keyboard.next();
//Ask the user to enter the flavor of the first jelly bean object, and save their answer in userInputJBFlavor:
System.out.print("Enter the Flavor of the First Jelly Bean :");
userInputJBFlavor=keyboard.next();
//Instantiate a JellyBean object using the userInputJBFlavor and userInputJBColor:
jb1 = new JellyBean(userInputJBFlavor, userInputJBColor);
//*** Repeat the above steps for JellyBean #2
System.out.print("Enter the Color of the second Jelly Bean :");
userInputJBColor = keyboard.next();
System.out.print("Enter the Flavor of the second Jelly Bean :");
userInputJBFlavor=keyboard.next();
jb2 = new JellyBean(userInputJBFlavor, userInputJBColor);
//*** Repeat the above steps for JellyBean #3
System.out.print("Enter the Color of the third Jelly Bean :");
userInputJBColor = keyboard.next();
System.out.print("Enter the Flavor of the third Jelly Bean :");
userInputJBFlavor=keyboard.next();
jb3 = new JellyBean(userInputJBFlavor, userInputJBColor);
}
/**
* The processJellyBeanBag() method will check how many JellyBean objects have the color of orange,
* and will change each of the orange JellyBeans' eatMe attribute to true. The processJellyBeanBag() method will also
* count how many JellyBeans are orange in color, and will display that count at the end of the method.
* It will also display the content of each of the 3 JellyBean objects.
*/
public static void processJellyBeanBag()
{
int orangeJellyBeanCounter = 0;
// Check if jb1.getColor().equalsIgnoreCase("orange"))
// if it does, then do the following 2 actions:
// add 1 to the orangeJellyBeanCounter.
// use the eatMe attribute's setter method to change its value to true.
//Repeat the check for the jb2 object:
//Repeat the check for the jb3 object:
}
}
Explanation / Answer
public static void processJellyBeanBag()
{
int orangeJellyBeanCounter = 0;
// Check if jb1.getColor().equalsIgnoreCase("orange"))
// if it does, then do the following 2 actions:
// add 1 to the orangeJellyBeanCounter.
// use the eatMe attribute's setter method to change its value to true.
if (jb1.getColor().equalsIgnoreCase("orange")){
orangeJellyBeanCounter++;
jb1.setEatMe(true)
}
//Repeat the check for the jb2 object:
if (jb2.getColor().equalsIgnoreCase("orange")){
orangeJellyBeanCounter++;
jb2.setEatMe(true)
}
//Repeat the check for the jb3 object:
if (jb3.getColor().equalsIgnoreCase("orange")){
orangeJellyBeanCounter++;
jb3.setEatMe(true)
}
System.out.println("Number of orange jelly beans:" + orangeJellyBeanCounter);
System.out.println(jb1);
System.out.println(jb2);
System.out.println(jb3);
}
}