Write a Guitar Hero program that uses a queue to simulate th
Solution
I had attached the code for Queue.java also but have removed it now. Writing a Queue is a basic task and so i leave it to the student. Please do revert if you need help with writing the Queue class.
RingBuffer.java
 public class RingBuffer {
   
    private Queue<Double> ringBuffer;
   
    public RingBuffer(int capacity) {
        ringBuffer = new Queue<>(capacity);
    }
   
    private RingBuffer(Queue other) {
        this.ringBuffer=other;
    }
   
    int size() {
        return this.ringBuffer.size();
    }
   
    public boolean isEmpty() {
        return this.ringBuffer.isEmpty();
    }
   
    public boolean isFull() {
        return this.ringBuffer.isFull();
    }
   
    public void enqueue(double x) {
        this.ringBuffer.enqueue(x);
    }
   
    public double dequeue() {
        return this.ringBuffer.dequeue();
    }
   
    public double peek() {
        return this.ringBuffer.peek();
    }
   
    public RingBuffer replace(RingBuffer other) {
        return new RingBuffer(ringBuffer.replace(other.ringBuffer));
       
    }
}
GuitarString.java
import java.util.Iterator;
 import java.util.stream.DoubleStream;
 import java.util.concurrent.ThreadLocalRandom;
public class GuitarString {
   
    private RingBuffer ringBuffer;
    private static int samplingRate = 44100;
    private static double decayFactor = 0.994;
   
    public GuitarString(double frequency) {
       
        int N = (int) Math.round(samplingRate/frequency);
        ringBuffer = new RingBuffer(N);
        for(int i=0;i<N;i++) {
            ringBuffer.enqueue(0);
        }
       
    }
   
    public GuitarString(double[] init) {
        int N=init.length;
        ringBuffer = new RingBuffer(N);
        for(int i=0;i<N;i++) {
            ringBuffer.enqueue(init[i]);
        }
       
    }
   
    public void pluck() {
        int N=ringBuffer.size();
        DoubleStream randVals=generateRandomValues(-0.5, 0.5, N);
        RingBuffer other = new RingBuffer(N);
       
        Iterator<Double> itr=randVals.iterator();
        for(int i=0;i<N;i++) {
            if(itr.hasNext()) {
                other.enqueue((double) itr.next());
            }
        }
       
        this.ringBuffer=this.ringBuffer.replace(other);
       
    }
   
    public void tic() {
       
        double first=this.ringBuffer.dequeue();
        double avg=((this.ringBuffer.peek() + first)/2)*decayFactor;
        this.ringBuffer.enqueue(avg);
    }
     // not implemented
    private double sample() {
        return this.ringBuffer.peek();
    }
     // not implemented
    private int time() {
       
        return 1;
    }
 // We are usin Streams in Java 8 here
    private DoubleStream generateRandomValues(double rangeMin, double rangeMax, int N) {
       
        DoubleStream randValues=ThreadLocalRandom.current().doubles((long) N,rangeMin,rangeMax);
        return randValues;
    }
}
Driver program:
import java.awt.KeyEventDispatcher;
 import java.awt.KeyboardFocusManager;
 import java.awt.event.KeyEvent;
 import java.util.ArrayList;
 import java.util.Scanner;
public class GuitarHeroLite {
   
    private static boolean keyPressed = false;
    private static String keys = \"q2we4r5ty7u8i9op-[=zxdcfvgbnjmk,.;/\' \";
    private static ArrayList keyList = new ArrayList<>();
    private static int sampleSum=0;
   
    public static boolean isKeyPressed() {
 synchronized (GuitarHeroLite.class) {
 return keyPressed;
 }  
 }
   
 private void addToList() {
   
    for(int i=0;i<keys.length();i++) {
        keyList.add((keys.charAt(i)+\"\").trim());
    }  
 }
   
 private double getFrequency(int notePlaceValue) {
 return 440 * Math.pow(1.05956, notePlaceValue-24);
 }
   
    public static void main(String[] args) {
       
        GuitarHeroLite ghl = new GuitarHeroLite();
        GuitarString[] guitarStrings = new GuitarString[37];
        ghl.addToList();
       
        for(int i=0; i < 37;i++) {
            double frequency = ghl.getFrequency(i);
    guitarStrings[i] = new GuitarString(frequency);
    }
   
       
        // For the sake of convenience, we get all the notes in one line as console input.
        // In reality, we need to use the KeyEent class to detect keyboard input.
        // Checkout the KeyEventDemo.java project in Oracle docs
        System.out.println(\"Enter the notes for your concert in one line: \");
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        for(int i=0;i<input.length();i++) {
            if(keyList.contains(input.charAt(i)+\"\")) {
        int index=keys.indexOf(input.charAt(i));
        guitarStrings[index].pluck();
       
        for(int j=0;j<37;j++) {
            sampleSum+=guitarStrings[j].sample();
        }
       
        // Play this sample
        // have to write code to feed this into an audio device
        for(int k=0;k<37;i++) {
            guitarStrings[k].tic();
        }
       
    }
        }
 }
       
       
    }




