Change Contents of the Bubble
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

FinalExam Review Sp2005: From Increase to Reverse

Answers, Questions, Comments on Answers, etc.
(Back to Final Exam Review Sp2005)


public void reverse(){
    SoundSample [] samples = this.getSamples();
    SoundSample current = null;
    
    for (int i=0; i < samples.length; i++) {
      current = samples[i];
      current.setValue(samples.length()-1, (int));
    }
  }

I am not sure if this was right. I was trying to model it after the reverse method in lecture slides, but it varies from the increase method given in the review.

Did you want to reverse in-place, or create a new sound that is the reverse and return that? If the latter, I'd create a returned sound (Sound returnedSound = new Sound(this.getLength());), put the reversed sound in there, then return returnedSound. (reverse() would then return Sound and not void.) The code that you have won't work – try it. It's hard to reverse in-place (same sound) because you end up copying over yourself. Mark Guzdial

  public Sound reverse()
  {
    Sound target = new Sound(getLength());
    int sampleValue;
    
    for (int srcIndex=0,trgIndex=getLength()-1;
         srcIndex < getLength();
         srcIndex++,trgIndex--)
    {
      sampleValue = this.getSampleValueAt(srcIndex);
      target.setSampleValueAt(trgIndex,sampleValue);
    };
    return target;
  }


According to the lecture slides, the code above is correct, but I am having trouble seeing how you would derive that from the code given on the review, any suggestions?
If I give you code, it'll be to make the syntax evident – how a for loop works, how you set a sample, how you get a sample. The logic of the method is up to you. Mark Guzdial

I was thinking the same thing, To me there appears to be little if no relation between the set of codes. Infact, the only help the code provides is the proper construction for a "for loop"
Yup – I don't expect you to memorize syntax, but I do expect you to be able to generate the algorithm. Mark Guzdial

I agree with the others- this code looks nothing like the actual method. How are we supposed to generate this with no good example? Plus, if we are supposed to just generate the algorithm from memory which code do you suggest that we memorize for the exam because there has been a LOT of it...

I don't expect you to memorize ANY code! I do expect that you can think through a few things, with a code example to give you a sense of syntax. REMOVEDscale and reverse are two examples I'm giving you here in the review of the level of things that I think you can generate. I don't expect you to generate a simulation from scratch! But if you asked to find the last() in a linked list, or add up the elements in an array, or find an element in an array or linked list – I expect that, if I gave you an example for syntax, you could do those things. Mark Guzdial



Link to this Page