Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Start with the following array of strings: String[] colors = { \"Red\", \"White\

ID: 3578690 • Letter: S

Question

Start with the following array of strings:

String[] colors = { "Red", "White", "Blue", "Black" };

Use the scanner to read from console input and ask the user; “Stack or Queue?”

Put the colors into either a stack or queue (standard queue, not a priority queue), pushing or queuing them, based on the user response.

Ask the user for an additional color and either push it on the stack or queue it in the queue.

Write the colors to a text file in the program’s local directory, popping or dequeuing each word as you get it to insert it in the text file.

Explanation / Answer

Hi, Please find my program.

Please let me know in case of any isseu.

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

import java.util.LinkedList;

import java.util.Queue;

import java.util.Scanner;

import java.util.Stack;

public class Color {

   public static void main(String[] args) throws IOException {

       String[] colors = { "Red", "White", "Blue", "Black" };

       Scanner sc = new Scanner(System.in);

       System.out.print("1. Stack ot 2. Queue ? ");

       int ch = sc.nextInt();

       String fileName;

       if(ch == 1){

           Stack<String> stack = new Stack<String>();

           for(String s : colors)

               stack.push(s);

           while(true){

               System.out.print("Do you want to push additional color(y/n) ? ");

               char c = sc.next().charAt(0);

               if(c != 'y')

                   break;

               System.out.print("Enter name of color ? ");

               String color = sc.next();

               stack.push(color);

           }

           System.out.print("Enter input file name: ");

           fileName = sc.next();

           FileWriter fr = new FileWriter(fileName);

           BufferedWriter bw = new BufferedWriter(fr);

           while(!stack.isEmpty()){

               bw.write(stack.pop());

               bw.newLine();

           }

           bw.close();

           fr.close();

       }

       else{

           Queue<String> queue = new LinkedList<String>();

           for(String s : colors)

               queue.add(s);

           while(true){

               System.out.print("Do you want to push additional color(y/n) ? ");

               char c = sc.next().charAt(0);

               if(c != 'y')

                   break;

               System.out.print("Enter name of color ? ");

               String color = sc.next();

               queue.add(color);

           }

           System.out.print("Enter input file name: ");

           fileName = sc.next();

           FileWriter fr = new FileWriter(fileName);

           BufferedWriter bw = new BufferedWriter(fr);

           while(!queue.isEmpty()){

               bw.write(queue.poll());

               bw.newLine();

           }

           bw.close();

           fr.close();

       }

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote