Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case.

Similar presentations


Presentation on theme: "CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case."— Presentation transcript:

1 CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case analysis –iterative refinement n Use of comments as higher-level statements n This Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases –Reading: n Lewis & Loftus, Chapter 4 and Section 5.1 n Savitch, Chapter 4

2 CS 100Lecture 62 Computation n Program n Processor follows instructions of program –Allocate variables –Input and Output –Store a value into a variable (assignment) –Load a value from a variable –Evaluate expressions –Sequential Execution, Conditional Execution, and Iteration Variables Program Processor store load input output instructions to be followed

3 CS 100Lecture 63 Computational Power n If that’s all there is, where do computers get their power from? –Hardware speed n A 500 megahertz processor is doing a half a billion things a second! –Linguistic abstraction n Programs are organized into hierarchies of abstractions so that lengthy computations can be described succinctly n abstraction –A named compound thing that can be manipulated as a unit –Examples: A group of declarations that go together A group of declarations that go together A group of statements that go together A group of statements that go together n The subject of this lecture is how to structure programs, but the fundamental computational steps remain the same.

4 CS 100Lecture 64 Aggregation n Suppose a bank account is represented by these state variables, e.g., // Bank account. int balance;// current balance int deposits;// deposits to date int withdrawals;// withdrawals to date n Shortcomings: –Understandability: The relationship of the variables to one another is only implicit, not explicit. –Scalability: If we want to represent two bank accounts, we have to write twice as much: // Bank account 1. int balance1;// current balance int deposits1;// deposits to date int withdrawals1;// withdrawals to date // Bank account 2. int balance2;// current balance int deposits2;// deposits to date int withdrawals2;// withdrawals to date

5 CS 100Lecture 65 Goals of Aggregation n Understandability: a way to group variables into a new abstraction that makes their relationship to one another explicit –Such an abstraction is called a class –Example: the class Account n Scalability: a way to create multiple instances of the abstraction –Each instance of the abstraction is called an object –Example: account1 and account2

6 CS 100Lecture 66 Class Definitions n Template class class-name {declarations} n Example: class Account { int balance; // current balance int deposits; // deposits to date int withdrawals; // withdrawals to date } Declarations of a class define fields of the class that go together to make one thing Declarations of a class define fields of the class that go together to make one thing Each class is a type. Whereas int, double, and boolean are called primitive types, classes are not primitive types Each class is a type. Whereas int, double, and boolean are called primitive types, classes are not primitive types

7 CS 100Lecture 67 Variables and Values Revisited n variable A named place that can hold a value of a particular type n Values of primitive type are held directly in variables n Values of non-primitive type are references to objects shown graphically by arrows 0 An int variable value name count balance deposits withdrawals 0 0 0 An Account variable account1 balance deposits withdrawals 0 0 0 Another Account variable account2

8 CS 100Lecture 68 Declarations Revisited n Declarations have the form: type name ; type name ; Example: Example: in t count; Account account1; Account account1; Account account2; Account account2; n The initial value of a variable depends on its type –int variables: 0 –non-primitive-type variables: null n Value null signifies that no object is referenced. It can be stored in a variable of any class type 0 An int variable count An Account variable account1 Another Account variable account2 null

9 CS 100Lecture 69 Declarations with Initialization Expressions Declarations can have an initialization expression: Declarations can have an initialization expression: type name = expression ; type name = expression ; Example: int count = 0; int count = 0; Account account1 = new Account(); Account account1 = new Account(); Account account2 = new Account(); Account account2 = new Account(); An expression of the form An expression of the form new class-name() new class-name() computes a reference to a newly allocated object of the given class.

10 CS 100Lecture 610 Manipulating Fields of an Object n If –r is an expression whose value refers to an object o of class c, –f is a field of o n then –r.f is a variable of object o n Example: // Deposit d into account1. account1.balance = account1.balance + d; account1.deposits = account1.deposits + d;

11 CS 100Lecture 611 References are Values Suppose you have declared a to be an Account variable, i.e., you have the declaration Suppose you have declared a to be an Account variable, i.e., you have the declaration Account a; Account a; Then you can assign any reference to an Account object into variable a. Then you can assign any reference to an Account object into variable a. Example: Example: /* If k is 1, deposit d into account1, otherwise deposit d into account2. */ /* If k is 1, deposit d into account1, otherwise deposit d into account2. */ if ( k == 1 ) a = account1; if ( k == 1 ) a = account1; else a = account2; else a = account2; // Deposit d to Account a. // Deposit d to Account a. a.balance = a.balance + d; a.balance = a.balance + d; a.deposits = a.deposits + d; a.deposits = a.deposits + d; n Two or more variables that refer to the same object are called aliases.

12 CS 100Lecture 612 References are Values, cont. balance deposits withdrawals 0 0 0 An Account variable account1 balance deposits withdrawals 0 0 0 Another Account variable account2 An Account variable a null 2 An int variable k

13 CS 100Lecture 613 Methods n Classes have methods: class class-name { declarations declarations methods methods} A method is a named parameterized group of statements. A method is a named parameterized group of statements. return-type method-name( parameter-list ) return-type method-name( parameter-list ) { statement-list statement-list } Return-type void signifies no return value. Return-type void signifies no return value.

14 CS 100Lecture 614 Example Method Definitions class Account { int balance; // current balance int balance; // current balance int deposits; // deposits to date int deposits; // deposits to date int withdrawals; // withdrawals to date int withdrawals; // withdrawals to date // deposit d to account // deposit d to account void deposit(int d) void deposit(int d) { balance = balance + d; deposits = deposits + d; } // withdraw w from account // withdraw w from account void withdraw(int w) void withdraw(int w) { balance = balance - w; withdrawals = withdrawals + w; }......}

15 CS 100Lecture 615 Method Use n If –r is an expression whose value refers to an object o of class c –m is a method of o n then r.m ( expression-list ) r.m ( expression-list ) invokes method m on object o n When method m is executed, field names signify the corresponding variables of object o n Example Invocation: account1.deposit(200); balance deposits withdrawals deposit withdraw 0 0 0 An Account variable account1

16 CS 100Lecture 616 Example Method Use // Withdraw 100 from account2. account2.withdraw( 100 ); account2.withdraw( 100 ); /* If k is 1, deposit 200 into account1, otherwise deposit 200 into account2. */ if ( k == 1 ) a = account1; if ( k == 1 ) a = account1; else a = account2; else a = account2; // Deposit 200 to Account a. // Deposit 200 to Account a. a.deposit( 200 ); a.deposit( 200 ); // Let n be the next integer. n = in.readInt(); n = in.readInt(); // Output 300. System.out.println(300); System.out.println(300);


Download ppt "CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case."

Similar presentations


Ads by Google