Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

15
Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier

Transcript of Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

Page 1: Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

Introduction to Computer Programming

CS 126

Lecture 4

Zeke Maier

Page 2: Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

Plan for Today

• Questions

• Administrivia

• Variables & Methods

• Assignment

Page 3: Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

Questions

• Campaign email lists

• Blackberry

• Openness

• Issues:– Supports Net Neutrality– Modern communications

Infrastructure– Invest in Research

Page 4: Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

Administrivia

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

Page 5: Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

Data Typesint double boolean String char

+ - * / %

int candies = 10;int people = 3;(int/double) result = candies/people;

int candies = 10;double people = 3;(int/double) result = candies/people;

Page 6: Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

VariablesHow do I declare a variable?

<Data Type> <name>; int count;

Declare a variable holding only true/false values?

boolean <name>; boolean guess;

Is this a valid variable assignment?String class == “CSE 126!”

String className = “CSE 126”;

Page 7: Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

Data Type Example Code• http://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 operationsdouble pi = 3.14159;

int pie = (int) pi;

Page 8: Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

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

MethodInput Values

Returned Value

Page 9: Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

Method Structure

<dataType> <name>(<dataType> <name>,…) {//procedure bodyreturn <expression>;

}

double circleArea(double radius) {return radius * radius * 3.14159;

}

Let’s write a method to calculate the area of a circle

Page 10: Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

Method Invokation

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

Page 11: Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

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);

Page 12: Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

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));

Page 13: Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

Procedural Abstraction

• Advantages:

1. Hides details2. Allows for building of a framework, but ignore

details3. Gives us reusable building blocks4. Let’s us name code segments logically5. Let’s us replace implementations easily6. Allows us to reduce code size

Page 14: Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

Method Example Code

• http://students.cec.wustl.edu/~ejm3/CS126/web/code.html#areaMethod

Page 15: Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.

Assignment

• Lab 1 assigned today!

• Readings– Friday

• AD Chapter 3• KG Notes