cse 8a lecture 15
DESCRIPTION
CSE 8A Lecture 15. Reading for next class : 10.1-10.4 Today’s (and Tuesday’s) goals: Recover form the midterm (try not to worry too much—it was hard!) Practice tracing code Practice writing code from scratch Applying the same algorithm to a picture and a sound Modifying data “in place” - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/1.jpg)
CSE 8A Lecture 15• Reading for next class: 10.1-10.4
• Today’s (and Tuesday’s) goals:– Recover form the midterm (try not to worry too much—it was hard!)– Practice tracing code– Practice writing code from scratch– Applying the same algorithm to a picture and a sound– Modifying data “in place”
• PSA 7 (sounds) due next Monday (11/19)
![Page 2: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/2.jpg)
Become a super hero!
Sneha Jayaprakash and Sarah Haroon
![Page 3: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/3.jpg)
Sandra Hui and Sandeep Gill
![Page 4: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/4.jpg)
Meditation
Sachi Pitkin and Tiffany Truong
![Page 5: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/5.jpg)
Dylan Mozlowski and Luis Castillo
![Page 6: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/6.jpg)
Political commentary
Michael Chin + Chu Jang
![Page 7: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/7.jpg)
Pop Culture
Michelle Wu and Kirk Wong"It's a-me, Mario! And my brother, Luigi!" :D
![Page 8: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/8.jpg)
Jason Tan and Zeyu Chen
![Page 9: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/9.jpg)
???
Daniel Chang and Kevin Nguyen
![Page 10: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/10.jpg)
SO MANY MORE… GO CHECK IT OUT!
![Page 11: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/11.jpg)
Options to raisePitch• Create new Sound
– V1) Of exact length needed for higher pitched sound– V2) Of same length as original with “silence” at end
![Page 12: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/12.jpg)
Complete the raisePitch method
public Sound raisePitch(){ SoundSample[] original = this.getSamples(); Sound highP = new Sound( original.length / 2 ); SoundSample[] higher = highP.getSamples(); int newPlace = 0; for (int origI = 0; origI < original.length; origI+=2) { higher[newPlace].setValue( original[origI].getValue() ); newPlace++; } return highP;}
![Page 13: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/13.jpg)
Complete V2: Create new sound of same length with 0 at end
public Sound raiseP() { Sound highP = new Sound(this); SoundSample[] original = this.getSamples(); SoundSample[] higher = highP.getSamples(); int newPlace = 0;
![Page 14: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/14.jpg)
How would the code belowchange the SoundSample array?
1) Solo: (60 sec)2) Discuss: (2 min)3) Group: (20 sec)
// In the sound classpublic void mystery() { SoundSample[] original = this.getSamples(); for ( int index = 0; index < original.length; index++ ) { original[index].setValue( original[index/2].getValue() ); }}
100 150 200 300 140 10 -40 -100 -250 -150
100 100 150 150 200 200 300 300 140 140
100 100 100 100 100 100 100 100 100 100
100 200 140 -40 -250 10 -40 -100 -250 -150
A
B
C
DIt causesan error
![Page 15: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/15.jpg)
Lowering the Pitch of the Sound1) Discuss: (2 min)
// In the sound classpublic void lowerPitch() { SoundSample[] original = this.getSamples(); for ( int index = 0; index < original.length; index++ ) { original[index].setValue( original[index/2].getValue() ); }}
100 150 200 300 140 10 -40 -100 -250 -150
Problem: We are overwriting the values we need to use before we have used them!Possible solutions?
100 100 150 150 200 200 300 300 140 140
before
after
![Page 16: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/16.jpg)
Start at the end!1) Think solo (2 min)2) Discuss: (2 min)
// In the sound classpublic void lowerPitch() { SoundSample[] original = this.getSamples();
for ( ___________________________________________________ ) { original[index].setValue( original[index/2].getValue() ); }}
100 150 200 300 140 10 -40 -100 -250 -150
100 100 150 150 200 200 300 300 140 140
before
after
![Page 17: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/17.jpg)
Another name for this method…// In the sound classpublic void stretchInPlace() { SoundSample[] original = this.getSamples();
for ( int index = original.length-1; index >= 0; index-- ) { original[index].setValue( original[index/2].getValue() ); }}
100 150 200 300 140 10 -40 -100 -250 -150
100 100 150 150 200 200 300 300 140 140
before
after
![Page 18: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/18.jpg)
…that could apply to Pictures too!
// In the Picture classpublic void stretchInPlace() {
for ( int x = ) { for ( int y = ) { Pixel source = this.getPixel( ); Pixel target = this.getPixel( );
target.setColor( source.getColor() ); } }}
Complete the code below to stretch the calling object Picture both horizontallyand vertically
// In the sound classpublic void stretchInPlace() { SoundSample[] original = this.getSamples(); for ( int index = original.length-1; index >= 0; index-- ) { original[index].setValue( original[index/2].getValue() ); } }
![Page 19: CSE 8A Lecture 15](https://reader035.vdocuments.net/reader035/viewer/2022062315/56816361550346895dd42f65/html5/thumbnails/19.jpg)
TODO
• Reading for next class: 10.1-10.4• Finish PSA7 (Bring headphones to lab!)