1 / 10

CS 112 Introduction to Programming

CS 112 Introduction to Programming. Method Parameters Logical/Boolean expressions; Cumulative Loops Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu. A "Parameter Mystery" problem. public class ParameterMystery {

quiana
Télécharger la présentation

CS 112 Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 112 Introduction to Programming Method Parameters Logical/Boolean expressions;Cumulative Loops Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

  2. A "Parameter Mystery" problem public class ParameterMystery { public static void main(String[] args) { int x = 9; int y = 2; int z = 5; mystery(z, y, x); mystery(y, x, z+y); } public static void mystery(int x, int z, int y) { System.out.println(z + " and " + (y - x)); } } What is the output?

  3. Practice: Scanner Fun • Please try out ScannerFun.java ScannerFun.java

  4. Exercise: Basic Boolean Expressions • Gentle || active • All • !active && healthy • Cuddles • !(clean && healthy) • Vamp, Yoda • !clean || !healthy • Vamp, Yoda • !(clean || gentle) • Vamp • !clean &&!gentle • Vamp

  5. Exercise: “Marriage Penalty” in 2014 • http://www.cbiz.com/page.asp?pid=11059 • See Tax2014.java http://www.newsoxy.com/business/marriage-penalty-return-103064.html

  6. Example: BMI • See BMI.java

  7. Cumulative Scanning DataAnalyzer.java • A common type of loop is cumulative scan • E.g., counting those satisfy a condition, summing up numbers Initialize state variable Loop over each element e if (element satisfies condition) update state variable State Variable • int sum = 0; • for (int y = 2000; y <= 2100; y++) { • if ( isLeapYear( y ) ) { • sum ++; } • } // end of for Update State Variable

  8. Cumulative Scan: Practice • Exercise: Design a data analyzer • first asks user for a number N, and then reads in a sequence of N grades and compute min, max, and average.

  9. Cumulative Max • Issue: what is the initial value of currentMax • Approach one: • currentMax = first number; • Approach two: • currentMax = minimum of range; (e.g., Interger.MIN_VALUE) Loop over each number n if (n > currentMax) // check condition currentMax = n; // update state

  10. TODO: String Methods • Review of String methods and regular expressions (see Wednesday slides on what was covered).

More Related