1 cs 177 week 15 recitation slides review. announcements final exam on sat. may 8th phy 112 from...

Post on 21-Dec-2015

213 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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. Your opinion matters!!!

Project 6 due … Just kidding

2

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

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

Primitive Data Types:

5

Type Kind of values Sample Literals

int Integers -50

900031

double Floating-pointNumbers

3.14-0.6

6.02e23

boolean Boolean values true false

char Single characters ‘A’‘Z’‘&’

String Sequences ofcharaters

“Hello World”“Goodbye”

From example exam:

6

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(<Boolean Expression>)

<Statement1>

else

<Statement2>7

switch(data) { case val1: <Statement1> (break;) case val2: <Statement2> (break;) default: <Statement3> }

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

Example:

9

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

Example with arrays and loops

11

12

What if i started with 0?

Input Output

java ProgramOut > output.txt

java ProgramIn < output.txt

OR

java ProgramOut | java ProgramIn

13

StdDraw

14

Method Use

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)

Method Use

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

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

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

Method Use

static double[] read(String file) Read a WAV file into an array of doubles

static void save(String file, double[] input)

Save an array of doubles (samples) into a WAV file

static void play(String file) Play a WAV file

static void play(double[] input) Play an array of doubles (samples)

Classes and Objects

public class Name {

private type member1;

private type member2; //member variables

public Name(…) { //constructor

….

}

//other methods ….

}19

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

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

23

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

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(n2) time The “best” possible is O(n log n) time

25

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

28

ANY QUESTIONS

29

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

top related