Week 10 - Friday.  What did we talk about last time?  References and primitive types  Started review.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Week 8 - Monday.  What did we talk about last time?  StdAudio.
Week 7: Input and Output 1.  Now we are going to talk a little bit about output  You have a lot of experience with System.out.println() and System.out.print()
1 Various Methods of Populating Arrays Randomly generated integers.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Week 7 - Friday.  What did we talk about last time?  Array examples.
IT151: Introduction to Programming
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
CS 177 Recitation Week 8 – Methods. Questions? Announcements  Project 3 milestone due next Thursday 10/22 9pm  Turn in with: turnin –c cs177=xxxx –p.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
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.
CS 201 Functions Debzani Deb.
Week 8: Audio Processing 1.  Light and sound are both transmitted in waves 2.
Introduction to Methods
Games and Simulations O-O Programming in Java The Walker School
Hello AP Computer Science!. What are some of the things that you have used computers for?
Week 7 - Wednesday.  What did we talk about last time?  Introduction to arrays  Lab 6.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
CSE 131 Computer Science 1 Module 1: (basics of Java)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
CS177 RECITATION WEEK 7 Input and Output (Text & Graphical)
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input.
Introduction to Programming Writing Java Beginning Java Programs.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
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.
CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
Week 7 - Friday.  What did we talk about last time?  Array examples  Sound.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Week 8 - Friday.  What did we talk about last time?  Static methods.
Methods CSCI 1301 What is a method? A method is a collection of statements that performs a specific task. Pre-Defined methods: available in Java library.
Week 7 - Wednesday.  What did we talk about last time?  Introduction to arrays  Lab 6.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Arrays Chapter 7.
Week 7 - Friday CS 121.
Java Course Review.
Week 7 - Wednesday CS 121.
Week 8 - Friday CS 121.
Week 8 - Monday CS 121.
Week 15 – Wednesday CS 121.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
CS 177 Week 15 Recitation Slides
Starting Out with Java: From Control Structures through Objects
Programming Vocabulary.
Chapter 4 void Functions
ITM 352 Functions.
Presentation transcript:

Week 10 - Friday

 What did we talk about last time?  References and primitive types  Started review

 StdDraw is a library of Java code developed by Robert Sedgewick and Kevin Wayne  StdDraw allows you to draw output on the screen easily  You can draw points, lines, and polygons in various colors  You can clear and resize the drawing area and even save the results  StdDraw is not standard Java that everyone uses, but it’s a nice tool for graphics

 The simplest things you can draw with StdDraw are lines and points  The first thing you should be aware of is that the canvas is drawn like Quadrant I of a Cartesian plane (0,0) (0,1)(1,1) (1,0)

 The following methods can be used to draw lines and points 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)

 Here are some methods for drawing circles and squares and setting the color for doing so: 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

 Eventually you will be able to define your own colors  For now you are limited to 13 presets  For example, to make something magenta, you would use the value StdDraw.MAGENTA BLACKBLUECYANDARK_GRAYGRAY GREENLIGHT_GRAYMAGENTAORANGEPINK REDWHITEYELLOW

 Audio data on Windows machines is sometimes stored in a WAV file  A WAV file is much simpler than an MP3 because it has no compression  Even so, it contains two channels (for stereo) and can have many different sample rates and formats for recording sound  The StdAudio class lets you read and write a WAV file easily and always deal with a single array of sound, sampled at 44,100 Hz

 Everything you’d want to do with sound:  To do interesting things, you have to manipulate the array of samples  Make sure you added StdAudio.java to your project before trying to use it 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)

 Let’s load a file into an array:  If the song has these samples:  Perhaps samples will contain: String file = "song.wav"; double[] samples = StdAudio.read(file); String file = "song.wav"; double[] samples = StdAudio.read(file);

 Static methods allow you to break your program into individual pieces that can be called by each other repeatedly  Advantages:  More modular programming ▪ Break a program into separate tasks ▪ Each task could be assigned to a different programmer  Code reusability ▪ Use code over and over ▪ Even from other programs (like Math.sqrt() ) ▪ Less code (and error) duplication  Improved readability ▪ Each method can do a few, clear tasks ▪ Well named method are self-documenting

 A method takes in 0 or more parameters and returns 0 or 1 values  A method that doesn’t return a value is declared as a void method  Definition syntax: public static type name( type arg1, type arg2, … ) { //statements //braces are always required! }

 Proper syntax for calling a static method gives first the name of the class that the method is in, a dot, the name of the method, then the arguments  If the method is in the same class as the code calling it, you can leave off the Class. part  If it is a value returning method, you can store that value into a variable of the right type Class.name(arg1, arg2, arg3);

 No connection between the two different x ’s and y ’s xy public static int add(int x, int y){ xy int z = x + y; // return z; } xy public static int add(int x, int y){ xy int z = x + y; // return z; } int a = 10; int x = 3; 5a int y = add( 5, a ); //y contains 15 now int a = 10; int x = 3; 5a int y = add( 5, a ); //y contains 15 now

 When a method is called, the arguments passed into the method are copied into the parameters  The names for the values inside the method can be different from the names outside of the method  Methods cannot change the values of the arguments on the outside for primitive types  Methods can change the values inside of arrays and sometimes inside of object types

 Complete the method below so that it draws a quincunx: public static void quincunx() { }

 Complete the method below that takes a String value and returns the number of uppercase and lowercase letter E's in it public static int countEs(String word) { }

 Complete the method below that does the following:  Generates two random numbers, each between 1 and 6, representing dice  If they are both 1, print out "Snake eyes!"  Otherwise, print the sum of the two values  If both numbers are the same, also print, " the hard way" public static void craps() { }  Potential sample output if craps() is run five times: 3 8 the hard way Snake eyes! 11 6

 Exam 2 on Monday!

 Study for Exam 2  Keep working on Project 4