Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a JAVA program called PlaySingleNotes to play a sequence of notes represented by note names in a textfile. Write the program so that it:

Write a JAVA program called PlaySingleNotes to play a sequence of notes represented by note names in a textfile.

Write the program so that it:

Declares and creates a symbol table using the algs31.BinarySearchST class;

Reads in a file notes_frequencies.txt where each line is a pair of strings separated by whitespace. The first string is the name of a musical note and the second a double value that is its sound frequency as found on a piano. For example, the note A4 is paired with the frequency 440.0 and the note C4 with the frequency 261.626. As each line is read, an entry is made in the symbol table where the note name is the key and the frequency is the value.

Reads in a note file, where each line contains a duration in seconds (floating point) and a note name. The values are separated by whitespace. A sample file is single_notes.txt.

Both the notes and frequencies file and the single notes file should be placed into the Eclipse data directory and, as in the previous program, read in using StdIn and the fromFile method.

To process a text file where each line contains a fixed set of data fields:

Use the method readLine in the StdIn class, which returns a string;

Split the string into an array of strings using the instance method split in the String class;

Convert the numeric strings into numeric values using the method parseDouble in the Double class.

To play each chord, place into your program and call this method:

 public static void playNote(double duration, double frequency) { final int sliceCount = (int) (StdAudio.SAMPLE_RATE * duration); final double[] slices = new double[sliceCount+1]; for (int i = 0; i <= sliceCount; i++) { slices[i] += Math.sin(2 * Math.PI * i * frequency / StdAudio.SAMPLE_RATE); } StdAudio.play(slices); } 

You will also have to import stdlib.StdAudio.

notes_frequencies.txt:

A0 27.5 A#0 29.1353 B0 30.8677 C1 32.7032 C#1 34.6479 D1 36.7081 D#1 38.8909 E1 41.2035 F1 43.6536 F#1 46.2493 G1 48.9995 G#1 51.913 A1 55 A#1 58.2705 B1 61.7354 C2 65.4064 C#2 69.2957 D2 73.4162 D#2 77.7817 E2 82.4069 F2 87.3071 F#2 92.4986 G2 97.9989 G#2 103.826 A2 110 A#2 116.541 B2 123.471 C3 130.813 C#3 138.591 D3 146.832 D#3 155.563 E3 164.814 F3 174.614 F#3 184.997 G3 195.998 G#3 207.652 A3 220 A#3 233.082 B3 246.942 C4 261.626 C#4 277.183 D4 293.665 D#4 311.127 E4 329.628 F4 349.228 F#4 369.994 G4 391.995 G#4 415.305 A4 440 A#4 466.164 B4 493.883 C5 523.251 C#5 554.365 D5 587.33 D#5 622.254 E5 659.255 F5 698.456 F#5 739.989 G5 783.991 G#5 830.609 A5 880 A#5 932.328 B5 987.767 C6 1046.5 C#6 1108.73 D6 1174.66 D#6 1244.51 E6 1318.51 F6 1396.91 F#6 1479.98 G6 1567.98 G#6 1661.22 A6 1760 A#6 1864.66 B6 1975.53 C7 2093 C#7 2217.46 D7 2349.32 D#7 2489.02 E7 2637.02 F7 2793.83 F#7 2959.96 G7 3135.96 G#7 3322.44 A7 3520 A#7 3729.31 B7 3951.07 C8 4186.01

single_notes.txt

0.5 C1 0.5 C2 0.5 C3 0.5 C4 0.5 C5 0.5 C6 0.5 C7 0.5 C8 0.25 E4 0.25 D4 0.25 C4 0.25 D4 0.25 E4 0.25 E4 0.25 E4

algs31.BinarySearchST class

package algs31;

import stdlib.*;

import algs13.Queue;

/* ***********************************************************************

* Compilation: javac BinarySearchST.java

* Execution: java BinarySearchST

* Dependencies: StdIn.java StdOut.java

* Data files: http://algs4.cs.princeton.edu/31elementary/tinyST.txt

*

* Symbol table implementation with binary search in an ordered array.

*

* % more tinyST.txt

* S E A R C H E X A M P L E

*

* % java BinarySearchST < tinyST.txt

* A 8

* C 4

* E 12

* H 5

* L 11

* M 9

* P 10

* R 3

* S 0

* X 7

*

*************************************************************************/

public class BinarySearchST, V> {

private static final int INIT_CAPACITY = 2;

private K[] keys;

private V[] vals;

private int N = 0;

// create an empty symbol table with default initial capacity

public BinarySearchST() { this(INIT_CAPACITY); }

// create an empty symbol table with given initial capacity

@SuppressWarnings("unchecked")

public BinarySearchST(int capacity) {

keys = (K[]) new Comparable[capacity];

vals = (V[]) new Object[capacity];

}

// resize the underlying arrays

@SuppressWarnings("unchecked")

private void resize(int capacity) {

if (capacity <= N) throw new IllegalArgumentException ();

K[] tempk = (K[]) new Comparable[capacity];

V[] tempv = (V[]) new Object[capacity];

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

tempk[i] = keys[i];

tempv[i] = vals[i];

}

vals = tempv;

keys = tempk;

}

// is the key in the table?

public boolean contains(K key) { return get(key) != null; }

// number of key-value pairs in the table

public int size() { return N; }

// is the symbol table empty?

public boolean isEmpty() { return size() == 0; }

// return the value associated with the given key, or null if no such key

public V get(K key) {

if (isEmpty()) return null;

int i = rank(key);

if (i < N && keys[i].compareTo(key) == 0) return vals[i];

return null;

}

// return the number of keys in the table that are smaller than given key

public int rank(K key) {

int lo = 0, hi = N-1;

while (lo <= hi) {

int m = lo + (hi - lo) / 2;

int cmp = key.compareTo(keys[m]);

if (cmp < 0) hi = m - 1;

else if (cmp > 0) lo = m + 1;

else return m;

}

return lo;

}

// Search for key. Update value if found; grow table if new.

public void put(K key, V val) {

if (val == null) { delete(key); return; }

int i = rank(key);

// key is already in table

if (i < N && keys[i].compareTo(key) == 0) {

vals[i] = val;

return;

}

// insert new key-value pair

if (N == keys.length) resize(2*keys.length);

for (int j = N; j > i; j--) {

keys[j] = keys[j-1];

vals[j] = vals[j-1];

}

keys[i] = key;

vals[i] = val;

N++;

//assert check();

}

// Remove the key-value pair if present

public void delete(K key) {

if (isEmpty()) return;

// compute rank

int i = rank(key);

// key not in table

if (i == N || keys[i].compareTo(key) != 0) {

return;

}

for (int j = i; j < N-1; j++) {

keys[j] = keys[j+1];

vals[j] = vals[j+1];

}

N--;

keys[N] = null; // to avoid loitering

vals[N] = null;

// resize if 1/4 full

if (N > 0 && N == keys.length/4) resize(keys.length/2);

//assert check();

}

// delete the minimum key and its associated value

public void deleteMin() {

if (isEmpty()) throw new Error("Symbol table underflow error");

delete(min());

}

// delete the maximum key and its associated value

public void deleteMax() {

if (isEmpty()) throw new Error("Symbol table underflow error");

delete(max());

}

/* ***************************************************************************

* Ordered symbol table methods

*****************************************************************************/

public K min() {

if (isEmpty()) return null;

return keys[0];

}

public K max() {

if (isEmpty()) return null;

return keys[N-1];

}

public K select(int k) {

if (k < 0 || k >= N) return null;

return keys[k];

}

public K floor(K key) {

int i = rank(key);

if (i < N && key.compareTo(keys[i]) == 0) return keys[i];

if (i == 0) return null;

else return keys[i-1];

}

public K ceiling(K key) {

int i = rank(key);

if (i == N) return null;

else return keys[i];

}

public int size(K lo, K hi) {

if (lo.compareTo(hi) > 0) return 0;

if (contains(hi)) return rank(hi) - rank(lo) + 1;

else return rank(hi) - rank(lo);

}

public Iterable keys() {

return keys(min(), max());

}

public Iterable keys(K lo, K hi) {

Queue queue = new Queue<>();

if (lo == null && hi == null) return queue;

if (lo == null) throw new Error("lo is null in keys()");

if (hi == null) throw new Error("hi is null in keys()");

if (lo.compareTo(hi) > 0) return queue;

for (int i = rank(lo); i < rank(hi); i++)

queue.enqueue(keys[i]);

if (contains(hi)) queue.enqueue(keys[rank(hi)]);

return queue;

}

/* ***************************************************************************

* Check internal invariants

*****************************************************************************/

private boolean check() {

return isSorted() && rankCheck();

}

// are the items in the array in ascending order?

private boolean isSorted() {

for (int i = 1; i < size(); i++)

if (keys[i].compareTo(keys[i-1]) < 0) return false;

return true;

}

// check that rank(select(i)) = i

private boolean rankCheck() {

for (int i = 0; i < size(); i++)

if (i != rank(select(i))) return false;

for (int i = 0; i < size(); i++)

if (keys[i].compareTo(select(rank(keys[i]))) != 0) return false;

return true;

}

/* ***************************************************************************

* Test client

*****************************************************************************/

public static void main(String[] args) {

StdIn.fromFile("data/tiny.txt");

BinarySearchST st = new BinarySearchST<>();

for (int i = 0; !StdIn.isEmpty(); i++) {

String key = StdIn.readString();

st.put(key, i);

}

for (String s : st.keys())

StdOut.println(s + " " + st.get(s));

}

}

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_2

Step: 3

blur-text-image_3

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

Database Systems For Advanced Applications 27th International Conference Dasfaa 2022 Virtual Event April 11 14 2022 Proceedings Part 2 Lncs 13246

Authors: Arnab Bhattacharya ,Janice Lee Mong Li ,Divyakant Agrawal ,P. Krishna Reddy ,Mukesh Mohania ,Anirban Mondal ,Vikram Goyal ,Rage Uday Kiran

1st Edition

3031001257, 978-3031001253

More Books

Students also viewed these Databases questions