• +91 9898989898
  • info@assignmentonlinehelp.com
Blog

Use Loops And String Methods To Encode Text Data

Required Reading: Arrays and References
 
You are expected to only use materials from this, or past, readings in this course to complete this assignment. This means that even if you know of another way to solve problems, you are not supposed to use these alternatives. 
 
Learning Goals
  • Learning about the reference data type
  • Creating arrays
  • Traversing arrays using loops
  • Creating methods that return array references

Problem Scenario
It turns out that cryptomancery was not a waste after all! You were able to decrypt the contents of the book, which contained directions to a location that appeared to be nearby. With no other options, you decide to follow the directions and see where they lead. 
 
After several hours of wandering, you arrive at the foothills of some desolate craggy mountains. The directions further lead you to a cave. There is a faint greenish glow coming from the interior of the cave. It is enough light that you feel safe entering and exploring, even though there might be spiders…
 
Phew, no spiders or other nasty things encountered as you reach the source of the glowing light. It is a small electronic device. You pick it up and backtrack to leave the cave. Outside you are able to examine the device further. It is an ancient device called an MP3 player. Amazingly, it still works too! Luckily, one of your hobbies is collecting ancient tech and you brought along your lucky pair of corded headphones, which you plug into the device to see what is playing. 
 
Whatever is playing is not comprehensible. Remembering your audiophiliocrafting course, you think that you might be able to manipulate the sound files and make them understandable. This is your hardest challenge yet and you know that you will succeed!
Assignment Detailed Specifications:
 
In this assignment, you will be creating a program to help manipulate sound files. The sound files that you will manipulate are called wave files (.wav extension). In this case, the files are expected to be monaural, which means they are not in stereo.  
  
Once again, you are creating a program that has multiple Java files. In this case, two files are being provided: StdAudio.java and RunSoundManipulation.java. The StdAudio.java file requires no editing. The RunSoundManipulation.java file is there for you create your testing code and will not be turned in. 

Your task is to create the file, SoundManipulation.java, which will provide several methods that will manipulate monaural wave files. 
 
  • StdAudio.java – Provides methods to process sound files.
  • RunSoundManipulation.java – A file to run the methods in SoundManipulation.
  • SoundManipulation.java – The Java class that you will create and complete.
Three monaural wave files are also included. Two are eight seconds and one is five seconds.
 
The SoundManipulation Class (a.k.a., the main class) and Main Method
 
First, create a new file in Atom and name it SoundManipulation.java. Next write the basic code for your class, with the class name SoundManipulation. 
 
Add the required Javadoc comments for your class (short description, name, version/date). 
 
There is no main method required for this assignment. You can create one in this file for testing purposes. If you do create one, please put it at the end of the required methods. 
 
Required Methods
The method names must exactly match what is listed here. The parameter types needed must also match what is expected, and the methods must return the expected type of values. (Tip: this probably means that another program will be testing your methods to see if they work correctly.)
 
You can create additional methods in your program. If you do, make sure that they serve a useful purpose and that they are fully commented. 
 
Note: There are examples provided of calling each method. The actual parameter values are shown, and the return value is also shown. 
 
Important Note: These methods should not modify the original array(s), which means that the array parameters are not directly modified and that the returned array is a new array created in the method. This is true for ALL methods being implemented in this assignment.

Method: changeVolume
changeVolume( [0.0, 0.5, 1.0], 0.0 ) --> [0.0, 0.0, 0.0] changeVolume( [0.0, 0.5, 1.0], 1.0 ) --> [0.0, 0.5, 1.0] changeVolume( [0.0, 0.5, 1.0], -1.0 ) --> [0.0, -0.5, -1.0] changeVolume( [0.1, 0.2, 0.3], 3.0 ) --> [0.3, 0.6, 0.9]
changeVolume( [], 4.0 ) --> []
changeVolume( null, 5.0 ) --> null
 
Changes the volume of a sound by a factor. If the factor is greater than 1, the volume is increased. If the factor is less than 1, the volume is decreased. The parameters to the method are an array containing the sound data and a factor to use for changing the volume. The resulting array is returned, or null if the array could not be created.
 
Method: increaseVolume
increaseVolume( [0.0, 0.5, 1.0] ) --> [0.0, 1.0, 2.0] increaseVolume( [0.1, 0.2, 0.3] ) --> [0.2, 0.4, 0.6]
increaseVolume( [] ) --> []
increaseVolume( null ) --> null
 
This method increases the volume of a sound by 100%. The parameter to the method is an array containing the sound data. The resulting array is returned. 
 
Method: decreaseVolume
decreaseVolume ( [0.0, 0.5, 1.0] ) --> [0.0, 0.25, 0.50] decreaseVolume ( [0.1, 0.2, 0.3] ) --> [0.05, 0.1, 0.15]
decreaseVolume ( [] ) --> []
decreaseVolume ( null ) --> null
 
This method decreases the volume of a sound by 50%. The parameter to the method is an array containing the sound data. The resulting array is returned, or null if the array could not be created.  
 
Method: appendSounds
appendSounds( [0.0, 0.5, 1.0], [0.3, 0.4] ) --> 
                                [0.0, 0.5, 1.0, 0.3, 0.4]
appendSounds( [0.1], [0.3, 0.4] ) --> [0.1, 0.3, 0.4] appendSounds( [0.1, 0.4, 0.8], [] ) --> [0.1, 0.4, 0.8] appendSounds( [], [0.4, 0.8] ) --> [0.4, 0.8]
appendSounds( [], [] ) --> [] appendSounds( null, null ) --> null appendSounds( null, [0.1] ) --> null appendSounds( [0.1], null ) --> null This method creates a new sound by appending a sound to another sound. The parameters to the method are two arrays containing sound data. The resulting array is returned, or null if the array could not be created.
 
This is the algorithm:
Notes: The indenting shows the code block structure. Most variables are not mentioned.
  • Ensure     that neither    array reference parameter  is    null
  • Create     an    array of    the  correct size to    hold both sounds    
  • Loop over the  first sound data
  • Add  data in first sound to    beginning  of    bigger     array
  • Loop over the  second     sound data
  • Add  data in first sound to    bigger     array where other loop ended
  • Return     the  bigger     array (this could be null)
 
Method: mergeSounds
mergeSounds( [0.0, 0.5, 1.0], [0.3, 0.4, 0.5] ) --> 
                                          [0.3, 0.9, 1.5]
mergeSounds( [0.1, 0.2], [0.3, 0.4] ) --> [0.4, 0.6]
mergeSounds( [0.1, 0.2], [0.4] ) --> null mergeSounds( [0.1, 0.4, 0.8], [] ) --> null mergeSounds( [], [0.4, 0.8] ) --> null mergeSounds( [], [] ) --> []
mergeSounds( null, null ) --> null
 
This method merges two sounds together, forming a new sound. Both sounds must be the same length for the sounds to be merged. The parameters to the method are two arrays containing sound data. The resulting array is returned, or null if the array could not be created.
 
Method: fragmentSound
fragmentSound( [0.0, 0.2, 0.3, 0.4, 0.5], 1, 4 ) --> 
                                          [0.2, 0.3, 0.4]
fragmentSound( [0.0, 0.2, 0.3, 0.4, 0.5], 0, 2 ) --> 
                                          [0.0, 0.2]
fragmentSound( [0.0, 0.2, 0.3, 0.4, 0.5], 0, 5 ) --> 
                                [0.0, 0.2, 0.3, 0.4, 0.5]
fragmentSound( [0.1, 0.2], 0, 1 ) --> [0.1] fragmentSound( [0.1, 0.2], 1, 2 ) --> [0.2]
fragmentSound( [0.0, 0.2, 0.3, 0.4, 0.5], -1, 4 ) --> null fragmentSound( [0.0, 0.2, 0.3, 0.4, 0.5], 1, 6 )  --> null fragmentSound( [0.0, 0.2, 0.3, 0.4, 0.5], 1, -4 ) --> null fragmentSound( [0.0, 0.2, 0.3, 0.4, 0.5], 5, 3 )  --> null fragmentSound( [0.0, 0.2, 0.3, 0.4, 0.5], 1, 1 )  --> null
fragmentSound( null, 0, 1 ) --> null
Produces a fragment from a sound based on a starting index (inclusive) and an ending index (exclusive). Both indices must be within the bounds of the sound data length (inclusive). The parameters to the method are an array containing sound data, the starting index, and the ending index. The resulting array is returned, or null if the array could not be created.
 
Here is the general algorithm:
  • Ensure     that the  array reference  parameter  is not  null
  • Ensure     that the  start and  end indices    are  both valid
  • Create     a new  array that is    the  size of    the fragment  
  • Loop from the fragment   index start until the  end  (exclusive)
  • Add  the  data from original   sound array to fragment   array
  • Return     the  sound fragment   array
Method: decreaseIncreaseVolume
decreaseIncreaseVolume( [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] ) --> 
                              [0.05, 0.1, 0.15, 0.8, 1.0, 1.2]
decreaseIncreaseVolume( [0.1, 0.2, 0.3, 0.4, 0.5] ) --> 
                           [0.05, 0.1, 0.6, 0.8, 1.0]
decreaseIncreaseVolume( [] ) --> []
decreaseIncreaseVolume( null ) --> null
 
This method decreases the volume for the first half of the sound data and increases the volume for the second half of the sound data. The parameter to the method is an array containing the original sound data. The resulting array is returned, or null if the array could not be created.
 
Method: halveSpeed
halveSpeed( [0.1, 0.3, 0.5] ) --> [0.1, 0.1, 0.3, 0.3, 0.5, 0.5]
halveSpeed( [0.4, 0.8] ) --> [0.4, 0.4, 0.8, 0.8]
halveSpeed( [] ) --> []
halveSpeed( null ) --> null
 
This method halves the speed of the original sound by doubling the sound frequency. The parameter to the method is an array containing the original sound data. The resulting array is returned, or null if the array could not be created.
 
Method: doubleSpeed
halveSpeed( [0.1, 0.3, 0.5, 0.7, 0.9, 1.1] ) --> [0.1, 0.5, 0.9]  halveSpeed( [0.1, 0.3, 0.5, 0.7, 0.9] ) --> [0.1, 0.5, 0.9]
halveSpeed( [0.1, 0.3, 0.5, 0.7] ) --> [0.1, 0.5] halveSpeed( [0.1, 0.3, 0.5] ) --> [0.1, 0.5] halveSpeed( [0.1, 0.3] ) --> [0.1] halveSpeed( [0.1] ) --> [0.1] halveSpeed( [] ) --> []
halveSpeed( null ) --> null
 
This method doubles the speed of the original sound by halving the sound frequency. The parameter to the method is an array containing the original sound data. The resulting array is returned, or null if the array could not be created.
 
Method: slowFastSpeed
 slowFastSpeed( [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] ) --> 
                 [0.1, 0.1, 0.2, 0.2, 0.3, 0.3, 0.4, 0.6]
slowFastSpeed( [0.1, 0.2, 0.3, 0.4, 0.5] ) --> 
                      [0.1, 0.1, 0.2, 0.2, 0.3, 0.5]
slowFastSpeed( [] ) --> []
slowFastSpeed( null ) --> null
 
This method slows the speed for the first half of the sound data and increases the speed for the second half of the sound data. The parameter to the method is an array containing the original sound data. The resulting array is returned, or null if the array could not be created.
 
Expected Program Output 
Your program submission will not produce any displayed results. 
The RunSoundManipulation class can be used to help you test. You will need to implement most of the needed testing. Code that runs one method from the SoundManipulation class is commented out. This will help you create other test scenarios. 
 
Testing Your Methods and Whole Program
It is a great idea to test each of your methods after you implement them. The examples of the parameter values and return values for the method calls can help you in your testing. Listening to the sound files is another good way to test these methods, as there should be noticeable changes in each case. Note, that the examples are not exhaustive of all possible testing situations. 
 
Commenting & Coding Standards
Your SoundManipulation class needs Javadoc comments, including a description of the class, the author, and the version. 
 
All of your methods require Javadoc comments, which include a description of the method, description for each parameter, and description of the returned value. If a method does not have any parameters or a returned value, then those portions are omitted. 
 
Additionally, make sure to include at least three meaningful inline comments explaining two different areas in your program. 
 
Your code also needs to adhere to the course coding standards and requirements. 

Expert’s Corner - What Works and What Does Not
Manipulating sound data is a fun way to learn how to work with arrays. The results can be heard instead of merely looking at values. That does not mean that looking at the values produced is meaningless, as they are very helpful to determining if the methods are working correctly. This is one reason why there are expected results listed for each method. 
 
The various sound manipulations are similar to other manipulations that you will perform on arrays in the future. Just because the context for this assignment is sound, it does not mean that the concepts are not generalizable and useful in other domains.
 
Tips
Try not to overthink what you need to do, and also remember that you might have already written a method that can help with part of another. The order that the methods are presented is a reasonable order of completing them. 
 
Drawing pictures of what is supposed to happen with the arrays and indices can be very helpful for understanding what is happening in these problems. This can also be a good way to debug.
 
No nested loops were used in the solution for this assignment. 
 
Some of the methods require keeping track of multiple index values into the arrays being manipulated. This is because some of the manipulations do not have the same ranges or do not start/stop at the same index values. 
 
SOLUTION:
 
// @author :
public class SoundManipulation {
 
    //this method change the volume of the sound array passed as argument by the factor
    public static double[] changeVolume(double[] soundArr, double factor)
    {
        if(soundArr == null) // checking if array is null
        {
            return null;
        }
        double []newArr = new double[soundArr.length];
        if(soundArr.length == 0) // checking if array is empty
        {
            return newArr;
        }
        for(int i =0;i         {
            if(soundArr[i] == 0)
            {
                newArr[i] = soundArr[i];
            }
            else
            {
                newArr[i] = soundArr[i] * factor; // increasing the sound by the factor
            }
        }
        return newArr;
    }
 
    // this method increase the volume of the soundArr by 100%
    public static double[] increaseVolume(double[] soundArr)
    {
        if(soundArr == null) // checking if array is null
        {
            return null;
        }
        double []newArr = new double[soundArr.length];
        if(soundArr.length == 0) // checking if array is empty
        {
            return newArr;
        }
        for(int i =0;i         {
            newArr[i] = soundArr[i] * 2; // increasing the values by 100 %
        }
        return newArr;
    }
 
    // this method decrease the volume by 50 %
    public static double[] decreaseVolume(double[] soundArr)
    {
        if(soundArr == null) // checking if array is null
        {
            return null;
        }
        double []newArr = new double[soundArr.length];
        if(soundArr.length == 0) // checking if array is empty
        {
            return newArr;
        }
        for(int i =0;i         {
            newArr[i] = soundArr[i] / 2; // decreasing the values by 50%
        }
        return newArr;
    }
 
    // this function appends two sounds array
    public static double[] appendSounds(double soundArr1[], double soundArr2[])
    {
        if(soundArr1 == null || soundArr2 == null) // if any of the array is null
        {
            return null;
        }
        int new_length = soundArr1.length + soundArr2.length;
        double newArr[] = new double[new_length];
        if(new_length == 0) // if both the array are empty
        {
            return newArr;
        }
        // creating new array
        int index = 0;
        // adding first array values to new array
        for(int i = 0;i         {
            newArr[index] = soundArr1[i];
            index++;
        }
        // adding second array values to new array
        for(int i = 0;i         {
            newArr[index] = soundArr2[i];
            index++;
        }
        return newArr;
    }
 
    // this function merge the two array into one
    public static double[]  mergeSounds(double soundArr1[], double soundArr2[])
    {
 
        if(soundArr1 == null || soundArr2 == null) // if any of the array is null
        {
            return null;
        }
       
        if(soundArr1.length != soundArr2.length) // checking if both the array have equal length
        {
            return null;
        }
 
        int new_length = soundArr1.length + soundArr2.length;
        double []newArr = new double[soundArr1.length];
        if(new_length == 0) // if both the array are empty
        {
            return newArr;
        }
 
        if(soundArr1.length ==0 || soundArr2.length == 0) // checking if any array have 0 length
        {
            return null;
        }
      
        for(int i = 0;i         {
            newArr[i] = soundArr1[i] + soundArr2[i];
        }
        return newArr;
    }
 
    // this function get new array from a particular array between a particular indexes
    public static double[] fragmentSound(double[] soundArr, int min , int max)
    {
        if(soundArr == null || min < 0  || max < 0 || min >= max || max > soundArr.length) // checking if array is null or any of the min or max in invalid
        {
            return null;
        }
        if(soundArr.length == 0) // checking if the array is empty
        {
            return soundArr;
        }
        int length = max - min;
        double []newArr = new double[length];
        int index = 0;
        for(int i = min ; i         {
            newArr[index] = soundArr[i]; // adding values to new array
            index++;
        }
        return newArr;
    }
 
    // This method decreases the volume for the first half of the sound data and increases the volume  for the second half of the sound data
    public static double[] decreaseIncreaseVolume(double[] soundArr)
    {
        if(soundArr == null) // checking if array is null
        {
            return null;
        }
        if(soundArr.length == 0) // checking if array is empty
        {
            return soundArr;
        }
        int length = soundArr.length; // getting length of the array
        int half = length/2;
        for(int i = 0;i         {
            soundArr[i] = soundArr[i] / 2;
        }
        for(int i = half; i         {
            soundArr[i] = soundArr[i] * 2;
        }
        return soundArr;
    } 
 
    //this method decreases the volume for the first half of the sound data and increases the volume  for the second half of the sound data
    public static double[] halveSpeed(double[] soundArr)
    {
        if(soundArr == null) // checking if the array is  null
        {
            return null;
        }
 
        if(soundArr.length == 0) // checking if the array is empty or not
        {
            return soundArr;
        }
        int newLength = 2 * soundArr.length; // getting length of the array
        double [] arr = new double[newLength];
        int counter = 0;
        for(int i =0;i         {
            arr[counter] = soundArr[i];
            counter++;
            arr[counter] = soundArr[i];
            counter++;
        }
        return arr;
    }
 
    //this method doubles the speed of the original sound by halving the sound frequency
    public static double[] doubleSpeed(double[] soundArr)
    {
        if(soundArr == null) //  checking if the array is null or not
        {
            return null;
        }
        if(soundArr.length <= 1) // checking if array is empty or contains only one value
        {
            return soundArr;
        }
        int length = soundArr.length / 2;  // getting the length of the new array
        if(soundArr.length %2 != 0)
        {
            length++;
        }
        double newArr[] = new double[length];
        int index = 0;
        for(int i =0;i         {
            newArr[index] = soundArr[i];
            index++;
        }
        return newArr;
    }
 
 
    //This method slows the speed for the first half of the sound data and increases the speed for the  second half of the sound data
    public static double[] slowFastSpeed(double[] soundArr)
    {
        if(soundArr == null) //  checking if the array is null or not
        {
            return null;
        }
        if(soundArr.length == 0) // checking if the array is empty or not
        {
            return soundArr;
        }
        int length = soundArr.length /2;
        int secondLength = soundArr.length - length;
        double []firstArray = new double[length]; // creating first array for have speed
        double []secondArray = new double[secondLength]; // creting second array
        for(int i =0;i         {
            firstArray[i] = soundArr[i];
        }
        int index = 0;
        for(int i = length;i         {
            secondArray[index] = soundArr[i];
            index++;
        }
        double halveSpeedArray[] = halveSpeed(firstArray); // getting halve speed array
        double doubleSpeedArray[] = doubleSpeed(secondArray); // getting double speed array
        double newArr[] = appendSounds(halveSpeedArray, doubleSpeedArray); // getting the final array
        return newArr;
    }

Related Posts