Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.
-
Upload
todd-chapman -
Category
Documents
-
view
213 -
download
0
Transcript of Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.
Introduction to Computer Programming
CS 126
Lecture 4
Zeke Maier
Plan for Today
• Questions
• Administrivia
• Variables & Methods
• Assignment
Questions
• Campaign email lists
• Blackberry
• Openness
• Issues:– Supports Net Neutrality– Modern communications
Infrastructure– Invest in Research
Administrivia
• http://students.cec.wustl.edu/~ejm3/– Added example code and example questions– Excused absence policy– CEC accounts– Lab assignment– Readings
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;
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”;
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;
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
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
Method Invokation
• Program execution always begins with the first statement in the main method– Additional methods can then be called
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);
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));
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
Method Example Code
• http://students.cec.wustl.edu/~ejm3/CS126/web/code.html#areaMethod
Assignment
• Lab 1 assigned today!
• Readings– Friday
• AD Chapter 3• KG Notes