Question

In: Computer Science

java Programming Problem 2: Pez Candy You may like the Pez candies and the wonderful dispensers...

java Programming Problem 2: Pez Candy

You may like the Pez candies and the wonderful dispensers that can be customized in so many ways. In each plastic container of Pez candy, the candies are of different colors, and they are stored in random order. Assume that you like the red candies only. Take a Pez dispenser, take out all candies one by one, eat the red ones, and put the others in a spare container you have at hand. Return the remaining candies in the initial container.

Your Tasks:

  1. Create a class PezCandy in which you implement an array-based stack
  2. Create a class PezCandyDriver in which you solve the problem as described above
  3. Create an algorithm showing how you take out the candies, eat the red ones, and put them back in the container, in the original order. Place the algorithm, as a comment in the top of your PezCandyDriver file
  4. Use an initial Pez dispenser with 12 candies, in which the colors of the candies are randomly chosen from: red, yellow, green, pink, blue.

    Hints

  5. Run the program and capture a snapshot showing the initial content of the Pez container, and the content after you ate all red candies.
  6. If you implement all the required methods properly, the driver program should generate outputs similar to the following:

Solutions

Expert Solution

Save the below file as "PezCandy.java"

import java.util.*;

public class PezCandy {

    private String[] list; // array of string to contains candy

    private int size; // size of candies

    private int index; // current index

  

    // constructor

    public PezCandy(int s) {

      this.size = s;

      this.index = 0;

      this.list = new String[this.size];

    }

  

    // add candy in stack

    public void add(String str) {

      if (this.index < this.size) {

        this.list[this.index++] = str;

      }

    }

  

    // size of stack

    public int getSize() {

      return this.index;

    }

  

    // pop the last candy from stack

    public String pop() {

      if (this.index > 0) {

        String ret = this.list[this.index - 1];

        this.index--;

        return ret;

      }

      return "";

    }

  

    // if stack is empty or not

    public boolean isEmpty() {

      return this.index == 0;

    }

  

    // to display candy stack

    public String toString() {

      String ret = "Candy stack:\n";

      for (int i = this.index - 1; i >= 0; i--) {

        ret += "\t" + this.list[i] + "\n";

      }

      return ret;

    }

  }

Save the below file as "PezCandyDriver.java"

import java.util.*;

public class PezCandyDriver {

  public static void main(String[] args) {

    Random rand = new Random();

    // pez candy with size 12

    PezCandy pc = new PezCandy(12);

    for (int i = 0; i < 12; i++) {

      int r = rand.nextInt(5);

      String val = "";

      // random candy add in stack

      if (r == 0) {

        val = "red";

      }

      if (r == 1) {

        val = "yellow";

      }

      if (r == 2) {

        val = "green";

      }

      if (r == 3) {

        val = "pink";

      }

      if (r == 4) {

        val = "blue";

      }

      pc.add(val);

    }

    // original stack

    System.out.println("The original contenet of the Pez container");

    System.out.print(pc);

    PezCandy pc1 = new PezCandy(12);

    // eat red candies

    while (pc.isEmpty() == false) {

      String candy = pc.pop();

      if (candy.equals("red")) {

        System.out.println("I am just eating red candy!!!");

      } else {

        pc1.add(candy);

      }

    }

    System.out.println("The spare dispenser");

    System.out.print(pc1);

    while (pc1.isEmpty() == false) {

      String candy = pc1.pop();

      pc.add(candy);

    }

    // display the container

    System.out.println("The content of Pez container after eating red candies");

    System.out.print(pc);

  }

}

Make sure to save both files in the same folder

//SAMPLE OUTPUT

PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT SECTION


Related Solutions

PEZ Candy Inc. produces the popular small candy that is dispensed in collectible flip-top dispensers. PEZ...
PEZ Candy Inc. produces the popular small candy that is dispensed in collectible flip-top dispensers. PEZ candy was invented as a breath mint in Vienna, Austria, in 1927. The name PEZ is derived from “pfefferminz” which is the word for peppermint in German. In the United States, PEZ candies are produced in a factory in Connecticut since 1973. The PEZ candies are made from about 95% sugar, which makes the PEZ product cost particularly sensitive to changes in the cost...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
The Mars candy company claims that the percentage of blue M&Ms [2] candies is 24%. A...
The Mars candy company claims that the percentage of blue M&Ms [2] candies is 24%. A sample of 200 M&Ms candies was selected and 54 candies were blue. a) State the null hypothesis and the alternative hypothesis. b) Test the claim using a significance level of 0.05. c) State your conclusion. Should the Mars company take corrective action?
Java programming problem: For simplicity, you can consider DNA sequences as strings in the alphabet of...
Java programming problem: For simplicity, you can consider DNA sequences as strings in the alphabet of {A, C, G, T}. Implement a special hash table to store all short DNA segments of certain length (e.g., 20) found in a long DNA sequence. Every DNA segment is stored as a (key, value) pair, where the key is the sequence of the segment, and the value is the position of the segment in the DNA. For example given a DNA sequence of...
Problem 13-22 Net Present Value Analysis [LO13-2] The Sweetwater Candy Company would like to buy a...
Problem 13-22 Net Present Value Analysis [LO13-2] The Sweetwater Candy Company would like to buy a new machine that would automatically “dip” chocolates. The dipping operation currently is done largely by hand. The machine the company is considering costs $190,000. The manufacturer estimates that the machine would be usable for five years but would require the replacement of several key parts at the end of the third year. These parts would cost $11,100, including installation. After five years, the machine...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a)...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a) No fraction allowed Max Weight W= 9 item 1: profit $20, weight 2, prof/weight=$10 item 2: profit $30, weight 5, prof/weight=$6 item 3: profit $35, weight 7, prof/weight=$5 item 4: profit $12, weight 3, prof/weight=$4 item 5: profit $3, weight 1, prof/weight=$3
This problem is about the java programming and contain two parts First part: a word that...
This problem is about the java programming and contain two parts First part: a word that reads the same forward and backward is called a palindrome, e.g., "mom", "dad", "racecar", "madam", and "Radar" (case-insensitive). Write a program called TestPalindrome, that asks the user for a word and prints whether the word is a palindrome or not. Hint: for a string called inStr, you can use inStr. toLowerCase() which returns a new string which is all in lower case letters. Use...
Java Programming THE PROBLEM: At the beginning of each online meeting, the instructor randomly greets a...
Java Programming THE PROBLEM: At the beginning of each online meeting, the instructor randomly greets a student whose camera is on. That student greets, also at random, another student whose camera is on. This chain continues until there is only one student with the camera on. That student says hello to the instructor, concluding the hello chain. A lot of things can go wrong during the hello chain. For example a student's audio may be muffled, muted, or noisy. We...
The following code is included for the java programming problem: public class Bunny {        private...
The following code is included for the java programming problem: public class Bunny {        private int bunnyNum;        public Bunny(int i) {               bunnyNum = i;        }        public void hop() {               System.out.println("Bunny " + bunnyNum + " hops");        } } Create an ArrayList <????> with Bunny as the generic type. Use an index for-loop to build (use .add(….) ) the Bunny ArrayList. From the output below, you need to have 5. Use an index for-loop...
java Programming Problem 1: Reverse Characters Create an application ReverseCharacters in which you use an array-based...
java Programming Problem 1: Reverse Characters Create an application ReverseCharacters in which you use an array-based stack to read a sentence from the keyboard, and reverse the order of the letters in each word. Print the initial sentence, as well as the result of the reversal operation. Your Tasks: Using the information given in your textbook, create a class named ArrayBasedStack, that will help you solve the problem given above by providing the stack operations you need for this application....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT