Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

Similar presentations


Presentation on theme: "Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier."— Presentation transcript:

1 Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier

2 Plan for Today Questions Administrivia Variables & Methods Assignment

3 Questions Campaign email lists Blackberry Openness Issues: –Supports Net Neutrality –Modern communications Infrastructure –Invest in Research

4 Administrivia http://students.cec.wustl.edu/~ejm3/ –Added example code and example questions –Excused absence policy –CEC accounts –Lab assignment –Readings

5 Data Types intdoublebooleanStringchar +-*/%+-*/% int candies = 10; int people = 3; (int/double) result = candies/people; int candies = 10; double people = 3; (int/double) result = candies/people;

6 Variables How do I declare a variable? ;int count; Declare a variable holding only true/false values? boolean ;boolean guess; Is this a valid variable assignment? String class == “CSE 126!” String className = “CSE 126”;

7 Data Type Example Code http://students.cec.wustl.edu/~ejm3/CS12 6/web/code.html#dataTypehttp://students.cec.wustl.edu/~ejm3/CS12 6/web/code.html#dataType Typecasting “Cast/Mold” a value of a type into a different type Takes precedence in order of operations double pi = 3.14159; int pie = (int) pi;

8 Methods Remember our area of a circle code? –What code are we continually rewriting? Procedure which: 1.Take in input values 2.Executes its instructions 3.Can calculate a returned value 4.Can then return the value Method Input Values Returned Value

9 Method Structure (,…) { //procedure body return ; } double circleArea(double radius) { return radius * radius * 3.14159; } Let’s write a method to calculate the area of a circle

10 Method Invokation Program execution always begins with the first statement in the main method –Additional methods can then be called

11 Math Methods Java provides a set of functions which perform most of the mathematical operations we will need… –Located in the built in Math class Math.PI;final double PI = 3.14159; Math.pow(3.0, 2.0);3.0 * 3.0; Math.sqrt(4.0);

12 Method Composition You can use one method expression as part of another expression –Similar to numerical composition final double PI = 3.14159; double radius = 3.0; double area = radius * radius * 3.14159; System.out.println(“Area of circle: ” + area); double radius = 3.0; System.out.println(“Area of circle: ” + (Math.pow(radius, 2.0) * Math.PI));

13 Procedural Abstraction Advantages: 1.Hides details 2.Allows for building of a framework, but ignore details 3.Gives us reusable building blocks 4.Let’s us name code segments logically 5.Let’s us replace implementations easily 6.Allows us to reduce code size

14 Method Example Code http://students.cec.wustl.edu/~ejm3/CS12 6/web/code.html#areaMethodhttp://students.cec.wustl.edu/~ejm3/CS12 6/web/code.html#areaMethod

15 Assignment Lab 1 assigned today! Readings –Friday AD Chapter 3 KG Notes


Download ppt "Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier."

Similar presentations


Ads by Google