Key words: Java Programming Assignment Help, Java Homework Help, Java Questions
CSC 110: Fundamentals of Programming I
Assignment #7: Classes and OOP
Part (a): Problems from the Textbook
Complete chapter 8 self--check problems 1 to 3, 5 to 8, 10, 11, 13, 14, 16, 17, 19 to 21, 24 and 25. Compare your answers to those available at:
http://www.buildingjavaprograms.com/self-check-solutions-4ed.html
Part (b): Transposing Music
Problem description
Your task in this assignment is to write TransposeSong.java and as part of completing this program you will write and use other classes:
- Note.java
- Voice.java
The basic problem is we have files containing notes (i.e., pitches and durations) in a text--like format. The overall task of your assignment is to transpose the notes in song files. Here are the contents of one file named 01_example.sng:
tempo 120
voices 1
title O Canada
instrument soprano
notes 4
g4 2
b_4 4.
b_4 8
e_4 2.
You can play this song by using the provided JAR file. Note that this is the first time we have used JAR files in the course; it is one way that a large number of classes can be combined together into one archive. (And do not worry—you will not be asked to create a JAR file for this assignment!) Assuming a7.jar and 01_xample.sng are in the same directory, type the following:
$ java -jar a7.jar 01_example.sng
If your computer is properly configured for Java audio you will hear the first four notes of our national anthem. (Most computers are already correctly configured and so you probably do not need to do anything special for this assignment. Ignore any error messages printed by the a7.jar program. If you are in ECS 258 you will need to plug your earphones or earbuds into the workstation in order to hear the sound.)
Notes consist of a pitch and duration. Pitches consist of a note letter (or tone) plus an octave number, and there may also be a single accidental marking, where “^” means “sharp” and “_” means “flat”. (You may have seen these symbols in your musical education as ? and ?.) For example, the first note “g4 2” means G in the fourth octave, also known as G above middle C, and the note’s length or duration is “2” (a half note). The second note “b_4 4.” is B flat above middle C and with a duration of “4.” (a dotted--quarter note).
Transposing music means raising (i.e., transpose up) or lowering (i.e, transpose down) the pitches of notes in a consistent way. (Transposition does not change the duration of notes.) Consider the diagram below showing part of piano’s keyboard.
Musicians refer to middle C as c4 (i.e., the piano key with the gray dot). If we were to transpose c4 up by three pitches (i.e., three semitones), then the new pitch will be e_4 (or “E--flat above middle C”), or the key with the white dot. This new pitch also has two possible names (i.e., it could also be named d^4 or “D--sharp above middle C”). A note will have at most one accidental (i.e., notes such as c^^4 are not possible).
Transposing down is similar. If we do this to c4 by transposing down three pitches, the new pitch will be a3 (or “A natural below middle C”)—see the black--dot key.
Your task is to write TransposeSong.java. It will take three arguments:
- The nameof some song file.
- The name of the filein which to store the transposed song (and make sure this is different than the filename in 1).
- The amount of the transpositionas a positive or negative integer (or zero).
For example, to transpose the example down by three pitches and to store the result in example_down3.sng, we would enter the following at the command prompt:
$ java TransposeSong 01_example.sng example_down3.sng -3
Note that the program does not play the song but simply performs the transposition.
(You can play the result stored in the new file using the JAR file in the manner described on page 2.) Here is one possible example_down3.sng:
tempo 120
voices 1
title O Canada
instrument soprano
notes 4
e4 |
2 |
g4 |
4. |
g4 |
8 |
c4 |
2. |
One last detail: Some of the note entries in a song are not pitches at all but are instead rests (or silences) having a duration. When transposing a song, you need only reproduce the same rest (i.e., a note of “r 4” before transposition begins is just “r 4” after transposition is ends).
What you are to write
The class Note.java will be used to store information about a single note’s pitch and duration. You are welcome to choose instance variables that make most sense to you (i.e., you may or may not choose to separate the pitch into a tone and an octave.)
Methods in this class will also be used to transpose the note.
- There must exist private attributes to store information about the note.
- The constructor Note(String pitch, String duration): This will assign the Note’s instance variables with correct values given the pitchand duration provided to the constructor.
- private void transposeUp() andprivate void transposeDown(): These methods will transpose the note up one pitch/semitone and down one pitch/semitone. (See the Appendix for some ideas on how to determine the higher or lower pitch given some existing pitch.)
- public void transpose(int semitones): This will calltransposeUp() or transposeDown() as many times as is needed. For example, in order to transpose up by three semitones, the method will end up calling transposeUp() three times. A negative value for semitones means “transpose down”, and a positive value means “transpose up”.
- public String toString(): Create a string representing the note. The stringwould represent the note as it would appear in a song file.
The class Voice.java will be used to store information about a sequence of notes. Notice on page 2 that a voice always begins with an instrument and the number of notes in the voice part. These can easily be read using a scanner that is passed to the Voice constructor. The number of notes should be used to create an array of Note instances and to control the for--loop needed to read notes from the scanner.
- There must exist a private array of Notescalled notes and a private String called instrument.
- The constructor Voice(Scanner input): Using the scanner parameter this constructor will read the instrument, number of notes, and notes themselves; and the method will store these in instance variables. This constructor will only ever be called when the scanner’s cursor is just before the string “instrument” in some song file. You will need to call next() or nextInt() on the scanner as appropriate. This method must need to create an array of Note with the correct size given the number of notes in the voice.
- public String toString(): Create a string representing the instrument, numberof notes, and note values themselves. The result will be a string concatenation probably involving, amongst other things, the use of the newline character