Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Test37.java import java.util.*; public class Test37 { public static final String KEYBOARD = q2we4r5ty7u8i9op-[=zxdcfvgbnjmk,.;/' ; // keyboard layout public static void main(String[] args) { double[]

image text in transcribedimage text in transcribedimage text in transcribed

Test37.java

import java.util.*; public class Test37 { public static final String KEYBOARD = "q2we4r5ty7u8i9op-[=zxdcfvgbnjmk,.;/' "; // keyboard layout public static void main(String[] args) { double[] frequencies = {110.25, 116.25, 123.25, 130.25, 138.25, 146.25, 155.25, 164.25, 174.25, 184.25, 195.25, 207.25, 220.25, 233.25, 246.25, 261.25, 277.25, 293.25, 311.25, 329.25, 349.25, 369.25, 391.25, 415.25, 440.25, 466.25, 493.25, 523.25, 554.25, 587.25, 622.25, 659.25, 698.25, 739.25, 783.25, 830.25, 880.25}; Guitar g = new Guitar37(); compare(frequencies, GuitarString.nums); for (int i = 0; i nums1) { Set nums2 = new TreeSet(); for (double n : nums) { nums2.add((int) n); } if (!nums1.equals(nums2)) { System.out.println("Wrong frequencies for Guitar37 construction."); System.out.println("should be approximately: " + nums2); System.out.println("yours are approximately: " + nums1); System.exit(1); } } }

GuitarString.java

import java.util.*; public class GuitarString { static Set nums = new TreeSet(); // observed frequency values double value; double freq; public GuitarString(double frequency) { freq = frequency; nums.add((int) frequency); } public void pluck() { value = (int) freq + 0.25; } public void tic() { if (value

All I need is a correct and working Guitar37.java that fulfills all of the requirements. Thanks.

Part 2: Guitar 37 Class In the second part of the assignment, you are going to build on the GuitarString class to write a class that keeps track of a musical instrument with multiple strings. There could he many possible guitar objects with different kinds of strings. As a result, we introduce an interface known as Guitar that each guitar object implements. The Guitar interface is defined as follows: public interface Guitar i public void ylayNote(int yitch), public boluan anstring(char key; public void pluck Ichar key); public double sample(); puble voic tic(); public int time(): 1 The interface allows a client to specify what to play in one of two ways. A client can specify exactly which note to play by calling the playNote method passing it a pitch. Pitch is specified as an integer where the value o represents concert-A and all other notes are specified relative to concert-A using what is known as a chromatic scale. Not every value of pitch can be played by any given guitar. If it can't be played, it is ignored. A client can also specify a character that indicates which note to play by calling the pluck method. Different guitar objects will have different mappings from characters to notes. The interface includes a method called hasString that is paired with pluck that lets a client verify that a particular character has a corresponding string for this guitar. The pluck method has a precondition that the key is legal for this guitar. The Guitar interface also has methods for getting the current sound sample (the sum of all samples from the strings of the guitar), to advance the time forward one "tic," and an optional method for determining the current time (the number of times tic has been called). If the time method is not implemented, it returns -1. You are being provided with a sample class called GuitarLite that implements the Guitar interface. Once you have verified that your GuitarString class passes the testing program, you can play the Guitar Lite instrument. It has only two strings: a and c. Keep in mind that Guitar Lite does not have a main method. There is a separate class called Guitarllero that has main (the initial version constructs a GuitarLite object). In this second part of the assignment, your task is to make a variation of GuitarLite known as Guitar 37. It will model a guitar with 37 different strings. Because it has so many strings, we will want to keep track of them in a data structure. Your Guiter:37 objects should each keep track of an array of 37 GuitarString objects. The Guitar37 class has a total of 37 notes on the chromatic scale from 110Hz to 880Hz. We will use the In this second part of the assignment, your task is to make a variation of Guitar Lite known as Guitar37. It will model a guitar with 37 different strings. Because it has so many strings, we will want to keep track of them in a data structure. Your Guiter37 objects should each keep track of an array of 37 GuitarString objects. The Guitar37 class has a total of 37 notes on the chromatic scale from 110Hz to 880Hz. We will use the following string to map keys typed by the user to positions in your array of strings. The i-th character of this string should correspond to the i-th character of your array: "q2we4r5ty7u819cp- =zxdcfvgbnjmk,.;/'" This use of keyboard characters imitates a piano keyboard, making playing songs a little easier for people used to a piano keyboard. The white keys are on the qwerty and zxcv rows and the black keys on the 12345 and asdf rows of the keyboard, as in the drawing below. 2 4 5 7 8 9 = TI 9 wertyuiop df 8 zxcvbnm,. You are being provided a skeleton version of the Guitar37 class that includes this string defined as a constant called KEYBOARD. The i-th character of the string corresponds to a frequency of 440 x 26-24)/12, so that the character "q" is 110Hz, "1" is 220Hz, v" is 440Hz, and "" (space) is 880Hz. As noted above, a pitch of 0 is supposed to correspond to concert-A, which will be at index 24 for the Guitar37 object (corresponding to the character "v"). Thus, you can convert from a pitch value to an index in your string by adding 24 to the pitch value. The table below shows some examples of this conversion. Key Pitch "q" 24 In working on this second part of the assignment, you are generalizing the code that you will find in GuitarLite. Because that instrument has just two strings, it uses two separate fields. Your instrument has 37 strings, so it uses an array of strings. Each of the operations defined in the interface needs to be generalized from using two specific strings to using an array of strings. For example, the sample method returns the sum of the current samples. GuitarLite does this by adding together two numbers. Your version will have to use a loop to find the sum of all 37 samples. The GuitarLite class is not well documented and does not handle illegal keys. Your Guitar37 class should include complete comments. The pluck method should throw an IllegalArgumentException if the key is not one of the 37 keys it is designed to play (as noted above, this differs from the playNote method that simply ignores notes it cant play). Recall that strings have an indexOf method that you might find helpful. As noted in the description of the interface, the method called time is optional. It is not implemented in the GuitarLite class, but you should implement it in the Guitar37 class. In order to run the program, you will have to have the files StdAudio.java and StdDraw.java in the same folder as your other class files. Remember that the main method runs indefinitely. As demonstrated in lecture, you can select a quit option from the Guitar Hero window that pops up or you can use the End command in jGRASP. As mentioned earlier, the ring buffer in your GuitarString class should be implemented as a queue. You are allowed to use any of the methods defined in the Queue interface. In particular, you are allowed to use the peek method that allows you to examine the value at the front of the queue without removing it. The GuitarString class would be inefficient if you didn't have the ability to peek at the front of the queue. To generate random real numbers, you should construct a Random object and call its nextDouble method. It returns a random real value n such that 0 <>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+8. Will the merger or acquisition affect specific competitors?

Answered: 1 week ago

Question

3. Define the attributions we use to explain behavior

Answered: 1 week ago