Introduction to Computer Programming CS 126 Lecture 6 Zeke Maier
Plan for Today Questions Administrivia Quiz Instance Variables Conditionals Assignment
Questions Boolean backed by 1/0? 3 == true (syntax error) Windows 7… Recovery.gov Microsoft job cuts
Administrivia http://students.cec.wustl.edu/~ejm3/ Lab assignment due Wednesday
Method Example Code http://students.cec.wustl.edu/~ejm3/CS126/web/code.html#instance Pay attention to the coding style!
Variables Def: A symbol which can hold a value of a particular type <dataType> <name>; int count; Variable Instance Variable Defined within a method Defined within a class Can only be accessed within its method Can be accessed anywhere in the class Used as a temporary holder of intermediate values Used to hold values describing the class A symbol which can hold a value of a particular type
Instance/Data Variables How do we create an instance variable? <dataType> <name>; int count; Which variable is the instance variable? class Student { String name; public static void main(String args[]) { } void printName() { String fullName = “Mr./Ms. ” + name; System.out.println(“Name: ” + name); AKA attributes
Class Layout A Class is a blueprint for an object Class consists of: It models something Class consists of: Instance/Data Variables Describe Methods Perform Actions 1 class to model a user, many users Usually, a class represents a person, place, or thing - it is an abstraction of a concept within a computer program. Fundamentally, it encapsulates the state and behavior of that which it conceptually represents. It encapsulates state through data placeholders called member variables; it encapsulates behavior through reusable code called methods. Instance variables: noun Methods: verbs Let’s sketch out a car class
Instance Example Code http://students.cec.wustl.edu/~ejm3/CS126/web/code.html#instance
Conditional Statements Allows us to change the behavior of the program based on certain conditions Example: absolute value If (condition) { //Statements to execute if the condition is true } How would we write a method to calculate the abs value for us Now let’s shorten it: double abs(double x) { if (x >= 0) return x; return -x; } How about even vs. odd method else { //Statements to execute if the condition is false }
Assignment Lab 1 due Wednesday in Lab Readings Wednesday AD Chapter 4: 4.1 - 4.7 KG Notes