CS 112 Introduction to Programming Digital Audio; User-Defined Data Types (struct) Yang (Richard) Yang Computer Science Department Yale University 208A.

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: Ch. 8 #1-9 exercises:
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8: Classes Lecture 8-1: Intro to Classes and Objects reading:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: #1-9 exercises: #1-4.
Object Oriented Software Development
3.3 Designing Data Types Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · September.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Based on slides at buildingjavaprograms.com Objects and Classes, take 2 Managing Complexity with Programmer-Defined Types  Classes are programmer-defined.
Introduction to Object-Oriented Programming
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Topic 27 classes and objects, state and behavior Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: #1-9 exercises: #1-4.
CS 112 Introduction to Programming Arrays; Loop Patterns (break) Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
2.1 Functions Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · 9/10/08 9/10/08.
CS 112 Introduction to Programming User-Defined Data Types Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
Salman Marvasti Sharif University of Technology Winter 2015.
Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects; Fields and Methods reading: 8.1 – 8.2.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
OOP Basics Classes & Methods (c) IDMS/SQL News
Topic 27 classes and objects, state and behavior Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
CS 112 Introduction to Programming Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
CS 112 Introduction to Programming User-Defined Data Types: OOP Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs Chapter 8
Classes and Objects, State and Behavior
Building Java Programs
Topic 27 classes and objects, state and behavior
A programming problem Given a file of cities' (x, y) coordinates, which begins with the number of cities: Write.
References and objects
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
A programming problem Given a file of cities' (x, y) coordinates, which begins with the number of cities: Write.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
Building Java Programs Chapter 8
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CS 112 Introduction to Programming Digital Audio; User-Defined Data Types (struct) Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:

2 Admin  PS6 questions?  Planning of remaining of the semester

3 Recap: Arrays  Array reference semantics  Examples: Many useful methods defined in the Arrays class ( html) change the state of input arrays, e.g., int[] a = {1, 4, 2, 5, 3}; System.out.println( Arrays.toString(a) ); // [1, 4, 2, 5, 3] Arrays.sort(a); // change the ordering of the elements in the array System.out.println( Arrays.toString(a) ); // [1, 2, 3, 4, 5]  We will cover implementations of sorting alg later in course  2d arrays m Irregular 2d arrays can have advantage in settings such as sparse graphs for computing PageRank m We will cover distributed MapReduce later in the course  Arrays are used in many settings, e.g., 1d array in sound, 2d in image, …

Roadmap: Foundational Programming Concepts 4 objects methods and classes graphics, sound, and image I/O arrays conditionals and loops mathtext I/O assignment statementsprimitive data types any program you might want to write

5 Outline  Admin and recap  Digital audio

Sound  Sound is the perception of vibrations in our eardrums.

Digital Audio Audio CD -Computer represents audio as a sequence of samples -When these samples are played in succession, it approximates the continuous sound

Use Digital Audio 8 library developed from book:  Java has a complex digital audio system  We use the StdAudio library for playing and saving sound Typical use: generate a sequence of samples, each for 1/44100 sec.

Example: Generate Musical Tone  Play Concert A (440hz) for 1.5 seconds using StdAudio. 9 final double hz = 440.0; final double seconds = 1.5; final int SAMPLE_RATE = 44100; int N = (int) (seconds * SAMPLE_RATE); double[] a = new double[N]; for (int i = 0; i < N; i++) { a[i] = Math.sin(2 * Math.PI * hz * i / SAMPLE_RATE); } StdAudio.play(a);

Example: PlayThatTune  Read in pitches and durations from standard input and sonify with StdAudio.

PlayThatTune public class PlayThatTune { public static void main(String[] args) { Scanner console = new Scanner( System.in ); while (console.hasNextInt()) { int pitch = console.nextInt(); double seconds = console.nextDouble(); double hz = * Math.pow(2, pitch / 12.0); int SAMPLE_RATE = 44100; int N = (int) (seconds * SAMPLE_RATE); double[] a = new double[N]; for (int i = 0; i < N; i++) { a[i] = Math.sin(2 * Math.PI * hz * i / SAMPLE_RATE); } StdAudio.play(a); } Same as before

12 Comments  PS7 will ask you to simulate a guitar using StdAudio  PS7 to be released on Monday

Roadmap: Foundational Programming Concepts 13 objects methods and classes graphics, sound, and image I/O arrays conditionals and loops mathtext I/O assignment statementsprimitive data types any program you might want to write

14 Programming Organizing Structure  Method and class are programming organization concepts m Method provides an abstraction to group a sequence of statements abstraction: we can use it without knowing its details m Class So far our usage is to group similar methods (such as a folder) together, e.g., the Math class pre-defined by Java A general, important function of class is to organize both data and behaviors

Road Ahead: Object Oriented Programming  Four steps m Struct data type (organize data together) m Encapsulation (organize data and code) m Class inheritance m Polymorphism 15

A Programming Problem  Given a file of cities' (x, y) coordinates, which begins with the number of cities:  Write a program to draw the cities and then drop a center point and turn all cities that are within a given radius red : Center site x? 100 Center site y? 100 Radius? 75

Solution We Can Do So Far Scanner input = new Scanner(new File("cities.txt")); int cityCount = input.nextInt(); int[] xCoords = new int[cityCount]; int[] yCoords = new int[cityCount]; Scanner console = new Scanner( System.in ); int xCenter = console.nextInt(); int yCenter = console.nextInt(); for (int i = 0; i < cityCount; i++) { xCoords[i] = input.nextInt(); yCoords[i] = input.nextInt(); if (distance(xCoords[i], yCoords[i], xCenter, yCenter ) < THRESHOLD) { // draw red } else … }  Parallel arrays: 2 arrays with related data at same indexes.  Conceptually, we should have just a single array of points.

A Conceptual Design Scanner input = new Scanner(new File("cities.txt")); int cityCount = input.nextInt(); Point[] points = new Point[cityCount]; Point pointCenter = readPointFromUser(); for (int i = 0; i < cityCount; i++) { points[i] = readPoint(input); if ( distance(points[i], pointCenter) < THRESHOLD) { // draw red } }...  By introducing the concept of Point, we abstract away the details of the coordinate system m Even if later the program changes to another coordinate system, this part of code does not need to change.

Summary: Complex Data Type Motivation Scanner input = new Scanner(new File("cities.txt")); int cityCount = input.nextInt(); int[] xCoords = new int[cityCount]; int[] yCoords = new int[cityCount]; for (int i = 0; i < cityCount; i++) { xCoords[i] = input.nextInt(); yCoords[i] = input.nextInt(); if (distance(xCoords[i], yCoords[i], xCenter, yCenter ) < THRESHOLD) { // draw red } 2 parallel arrays Scanner input = new Scanner(new File("cities.txt")); int cityCount = input.nextInt(); Point[] points = new Point[cityCount]; for (int i = 0; i < cityCount; i++) { points[i] = readPoint(input); if ( distance(points[i], pointCenter) < THRESHOLD) { // draw red } points point abstraction

Language Support: Defining Point  Need a language structure to specify that each point is defined by two data components: m The x coordinate m The y coordinate public class Point { int x; int y; }  Save this code into a file named Point.java.  The above code creates a new user-defined type (class) named Point.  The Point class definition serves as a template for creating Point objects.

A Class and its client  Point.java is not, by itself, a runnable program. m A class can be used by multiple client programs. PointMain1.java (client program) public class PointMain1 { main(String args) { Point p1 = new Point(); p1.x = 50; p1.y = 27; Point p2 = new Point(); p2.x = 98; p2.y = 53; } Point.java (class of objects) public class Point { int x; int y; } x 50 y 27 x 98 y 53

User-Defined Type Detail: Fields public class Point { int x; int y; }  x or y is called a field m A non-static, class scope variable defined inside a class (new type) m A field is also called an instance variable, or an attribute m A field is part of the descriptive characteristics (state) of an object m Each object has its own copy of each field  More example: public class BankAccount { String acctName; int acctNumber; double balance; }

Another Client of Point: PointMain2 public class PointMain2 { public static void main(String[] args) { // create two Point objects Point p1 = new Point(); p1.x = 50; p1.y = 27; Point p2 = new Point(); p2.x = 98; p2.y = 53; System.out.println(p1.x + ", " + p1.y); // 50, 27 System.out.println(p2.x + ", " + p2.y); // 98, 53 // move p2 p2.x += 2; p2.y ++; System.out.println(p2.x + ", " + p2.y); // 100, 55 } }

Summary of Important Concepts: Class, Object, Variable, Field public class Point { int x; int y; }  The Point class definition serves as a template for creating Point objects. r A field (also called instance variable, attribute) is a class- scope, non-static variable describing characteristics of each object. Each object has its own copy of each field r A Point variable stores a reference to a Point object

Summary: Class, Object, Variable, Field PointMain.java (client program) public class PointMain { main(String args) { Point p1 = new Point(); p1.x = 50; p1.y = 27; Point p2 = p1; Point p3 = new Point(); p3.x = 98; p3.y = 53;... } Point.java (class of objects) public class Point { int x; int y; } x 50 y 27 x 98 y 53 fieldsobjectsclassObject variables (references)

Historical Note  Before object-oriented programming, traditional languages (e.g. C ) already have a language structure called struct to allow user to define data type (combine multiple data fields) r Object-oriented languages go one step further to organize both data and methods together (we will start the coverage on Monday) 26

Object Oriented Programming Overview r OOP philosophy. Software is a simulation of the real world. m We know (approximately) how the real world works. m Design software to model the real world. r Procedural programming. [verb-oriented] m Tell the computer to do this. m Tell the computer to do that. r Objected oriented programming (OOP). [noun-oriented] m Programming paradigm based on data types. m Identify objects that are part of the problem domain or solution. m Identity: objects are distinguished from other objects (references). m State: objects know things (instance variables). m Behavior: objects do things (methods). 27

Object Oriented Programming 28

Alan Kay r Alan Kay. [Xerox PARC 1970s] m Invented Smalltalk programming language. m Conceived Dynabook portable computer. m Ideas led to: laptop, modern GUI, OOP. Alan Kay 2003 Turing Award “ The computer revolution hasn't started yet. ” “ The best way to predict the future is to invent it. ” “ If you don't fail at least 90 per cent of the time, you're not aiming high enough. ” — Alan Kay 29

Motivation: Drawing Points  Suppose our client program wants to draw Point objects: // draw each city Point p1 = new Point(); p1.x = 15; p1.y = 37; StdDraw.filledCircle(p1.x, p1.y, 3); StdDraw.textLeft(p1.x, p1.y, "(" + p1.x + ", " + p1.y + ")” ); r To draw other points, the same code must be repeated. m We can remove this redundancy using a method.

Eliminating Redundancy, v1 r We can eliminate the redundancy with a static method: // Draws the given point using StdDraw public static void draw(Point p) { StdDraw.filledCircle(p.x, p.y, 3); StdDraw.textLeft(p.x, p.y, "(" + p.x + ", " + p.y + ")” ); }  main would call the method as follows: draw(p1); Question: where do we define the method draw(Point p)?

Where do we Define draw(p): Attempt 1: in each client e.g., PointMain1

Where do we Define draw(p): Attempt 2: in Point as a Static Method public class Point { int x; int y; public static void draw(Point p) { StdDraw.filledCircle(p.x, p.y, 3); StdDraw.textLeft(p.x, p.y, "(" + p.x + ", " + p.y + ")” ); } Point p1 = new Point(); p1.x = 7; p1.y = 2; Point.draw(p1); Point p2 = new Point(); p2.x = 4; p2.y = 3; Point.draw(p2);

Consistent Data Access and Method Access 34 Point p = new Point(); p.x = 10; p.y = 5; Point.draw(p); Accessing point p’s x attribute Accessing point p’s draw behavior p.draw();

Instance Methods r instance method (or object method): Exists inside each object of a class and gives behavior to each object. public type name ( parameters ) { statements ; }  same syntax as static methods, but without static keyword Example: public void shout() { System.out.println("HELLO THERE!"); }

Instance Method example Instance method (or object method) can access all class scope variables public class Point { int x; int y; // Draws this Point object with the given pen. public void draw() { StdDraw.filledCircle(x, y, 3); StdDraw.textLeft(x, y, "(" + x + ", " + y + ")”); } }  The draw method no longer has a Point p parameter. m How will the method know which point’s x/y to draw? p1: Point x = 50 y = 27 p2: Point x = 98 y = 53

Invoking Instance Method r Format. (…) r The provides an implicit parameter 37

The Implicit Parameter  During the call p1.draw(); the object referred to by p1 is the implicit parameter.  During the call p2.draw(); the object referred to by p2 is the implicit parameter. p1.draw()draw(p1) p2.draw()draw(p2) r In an instance method, when we refer to a field, we are referring to the field of the implicit parameter. m We say that it executes in the context of a particular object.

Summary: Defining Related Method and Data in the Same Class: Instance Method public class Point { int x; int y; public static void draw(Point p) { StdDraw.filledCircle(p.x, p.y, 3); StdDraw.textLeft(p.x, p.y, "(" + p.x + ", " + p.y + ")”); } Point p1 = new Point(); p1.x = 7; p1.y = 2; p1.draw(); // Point.draw(p1); Point p2 = new Point(); p2.x = 4; p2.y = 3; p2.draw(); // Point.draw(p2); p1 provides the implicit parameter: The x and y in draw() are those of the object referenced by p1. p2 provides the implicit parameter: The x and y in draw() are those of the object referenced by p2.