Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.

Similar presentations


Presentation on theme: "1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes."— Presentation transcript:

1 1 CS 177 Week 15 Recitation Slides Review

2 Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.  Your opinion matters!!! Project 6 due …  Just kidding 2

3 How to Study Know every detail of previous exams you have taken.  If there is something you DON’T understand, look it up, Google it and ask questions! Review the slides. Recitation slides are a summary of each week’s lecture.  Good to review both; they will compliment one another. Understand your labs and projects as though you had to re-program them from scratch.  Re-program key parts and ** learn how to trace a program… ** Use the book to fill in any question areas. DO NOT ONLY RELY ON THE EXAMPLE FINAL!!!!! 3

4 Remember the Good Ole’ Days Compile and run a program called Program.java:  javac Program.java  java Program Difference between System.out.println() and System.out.print() ?  println() adds a new line at the end of the printed statement Java is case sensitive:  VarName is different from varName. 4

5 Primitive Data Types: 5 TypeKind of valuesSample Literals int Integers -5 0 900031 double Floating-point Numbers 3.14 -0.6 6.02e23 boolean Boolean values true false char Single characters ‘A’ ‘Z’ ‘&’ String Sequences of charaters “Hello World” “Goodbye”

6 From example exam: 6

7 Conditionals if statements use a boolean expression and execute a set of statements if the expression is true.  There may be a corresponding else block that handles cases where the expression is false Switch statements allow for the use of choices of several values, but must only be equality relationships for integers and characters. if( ) else 7 switch(data) { case val1: (break;) case val2: (break;) default: }

8 Loops For loops – Count controlled While loops – Event controlled Do/While – Event controlled loops that need to execute AT LEAST once Conditions in loops are ways to enter the loop but also the condition on which to leave the loop – The STOP condition (Keep going until…) 8

9 Example: 9

10 Arrays Multiple elements stored and accessed together via the same variable name, with indices 0 – (length of array-1) All elements must be the same type To declare an array called example of 100 integers: int example = new int[100]; To create an array with initial elements, you can use: int example = {1, 2, 3, 4, 5}; 10

11 Example with arrays and loops 11

12 12 What if i started with 0?

13 Input Output java ProgramOut > output.txt java ProgramIn < output.txt OR java ProgramOut | java ProgramIn 13

14 StdDraw 14 MethodUse void line(double x0, double y0, double x1, double y1) Draw a line from (x0,y0) to (x1,y1) void point(double x, double y) Draw a point at (x,y) MethodUse void circle(double x, double y, double r) Draw a circle centered at (x,y) with radius r void filledCircle(double x, double y, double r) Draw a filled circle centered at (x,y) with radius r void square(double x, double y, double r) Draw a square centered at (x,y) with edges 2r void filledSquare(double x, double y, double r) Draw a filled square centered at (x,y) with edges 2r void setPenColor(Color c) Start drawing with color c

15 Methods public static type methodName(type name1, … type nameN) or private return type optional public int calculateSum(int[] array) { int sum = 0; for(int i = 0; i < array.length; i++) sum += array[i]; return sum; } 15

16 16

17 Audio Digital audio is made by sampling the heights of the sound wave many times per second CD quality audio samples at 44,100 Hz The frequency of the wave determines how high or low the wave sounds The height of the peaks from the troughs (amplitude) determines the loudness (volume) StdAudio lets us load WAV files as an array of samples in the range [-1.0,1,0] and save such arrays as WAV files 17

18 18 MethodUse static double[] read(String file) Read a WAV file into an array of double s static void save(String file, double[] input) Save an array of double s (samples) into a WAV file static void play(String file) Play a WAV file static void play(double[] input) Play an array of double s (samples)

19 Classes and Objects public class Name { private type member1; private type member2; //member variables … public Name(…) { //constructor …. } //other methods …. } 19

20 Objects and Classes cont. Objects behave differently from primitive data types Reference variables are used to keep track of objects When a reference is first declared, its value is null If you try to use the methods or members of a null reference, your program will crash Multiple references can point at a single object Changing the object that they point to will change it for all references 20

21 Objects contain  Members  Methods Members are the data inside of objects, and they are usually private Methods are ways of accessing and modifying that data, and they are usually public 21

22 22

23 23

24 Running Time and Performance Not every solution that “works” is the best solution to use.  Sometimes, even if a program works, it can be too slow.  Usually depends on how many loops and how many nested loops there are. 24

25 Searching and Sorting Searching  Can be done in O(n) time by just scanning through the array  Can be done in O(log n) time if the array is sorted by playing a high-low game called binary search Cutting the search space in half every time It’s an algorithm that works well even with very large n Sorting  Relatively simple methods take O(n 2 ) time  The “best” possible is O(n log n) time 25

26 Sorting Many possible ways of sorting.  The more efficient, the more difficult to program… Bubble Sort Insertion Sort Merge Sort Bucket Sort (Many others…)  SHOULD BECOME FAMILIAR WITH ALGORITHMS FROM SLIDES!!!!! 26

27 27

28 28

29 ANY QUESTIONS 29

30 QUIZ Using what you know and the remaining time, write down a question and answer that shows something you have learned this semester. Don’t forget your name in the process… (Please do not get over zealous where you may not be able to finish on time.) GOOD LUCK ON THE FINAL  (AND DON’T FORGET EVALUATIONS!!!) 30


Download ppt "1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes."

Similar presentations


Ads by Google