160 likes | 173 Views
CS100J Lecture 6. Previous Lecture Programming Concepts Programming by stepwise refinement a pattern sequential refinement case analysis iterative refinement Use of comments as higher-level statements This Lecture Computation and computational power Abstraction
E N D
CS100J Lecture 6 • Previous Lecture • Programming Concepts • Programming by stepwise refinement • a pattern • sequential refinement • case analysis • iterative refinement • Use of comments as higher-level statements • This Lecture • Computation and computational power • Abstraction • Classes, Objects, and Methods • References and aliases • Reading: • Lewis & Loftus, Chapter 4 and Section 5.1 • Savitch, Chapter 4 Lecture 6
Computation • Program • 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 Program instructions to be followed Processor output input load store Variables Lecture 6
Computational Power • If that’s all there is, where do computers get their power from? • Hardware speed • A 500 megahertz processor is doing a half a billion things a second! • Linguistic abstraction • Programs are organized into hierarchies of abstractions so that lengthy computations can be described succinctly • abstraction • A named compound thing that can be manipulated as a unit • Examples: • A group of declarations that go together • A group of statements that go together • The subject of this lecture is how to structureprograms, but the fundamental computational steps remain the same. Lecture 6
Aggregation • 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 • 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 Lecture 6
Goals of Aggregation • 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 • Scalability: a way to create multiple instances of the abstraction • Each instance of the abstraction is called an object • Example:account1 and account2 Lecture 6
Class Definitions • Template classclass-name { declarations } • 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 • Each class is a type. Whereas int, double, andboolean are called primitive types, classes are not primitive types Lecture 6
value name Variables and Values Revisited • variable A named place that can hold a value of a particular type • Values of primitive type are held directly in variables • Values of non-primitive type are references to objects shown graphically by arrows Anintvariable 0 count AnAccountvariable account1 balance deposits withdrawals 0 0 0 AnotherAccountvariable account2 balance deposits withdrawals 0 0 0 Lecture 6
Declarations Revisited • Declarations have the form: typename ; Example: int count; Account account1; Account account2; • The initial value of a variable depends on its type • int variables: 0 • non-primitive-type variables: null • Value null signifies that no object is referenced. It can be stored in a variable of any class type Anintvariable 0 count null AnAccountvariable account1 null AnotherAccountvariable account2 Lecture 6
Declarations with Initialization Expressions • Declarations can have an initialization expression: typename = expression ; Example: int count = 0; Account account1 = new Account(); Account account2 = new Account(); • An expression of the form newclass-name() computes a reference to a newly allocated object of the given class. Lecture 6
Manipulating Fields of an Object • Let f be a field of class c • Let r refer to object o of class c • Then r.f is a variable of object o • Example: // Deposit d into account1. account1.balance = account1.balance + d; account1.deposits = account1.deposits + d; Lecture 6
References are Values • Suppose you have declared a to be an Account variable, i.e., you have the declaration Account a; • Then you can assign any reference to an Account object into variable a. • Example: /* If k is 1, deposit d into account1, otherwise deposit d into account2. */ if ( k == 1 ) a = account1; else a = account2; // Deposit d to Account a. a.balance = a.balance + d; a.deposits = a.deposits + d; • Two or more variables that refer to the same object are called aliases. Lecture 6
References are Values, cont. 2 Anintvariable k null AnAccountvariable a AnAccountvariable account1 balance deposits withdrawals 0 0 0 AnotherAccountvariable account2 balance deposits withdrawals 0 0 0 Lecture 6
Methods • Classes have methods: classclass-name { declarations methods } • A method is a named parameterized group of statements. return-typemethod-name( parameter-list ) { statement-list } • Return-typevoid signifies no return value. Lecture 6
Example Method Definitions class Account { int balance; // current balance int deposits; // deposits to date int withdrawals; // withdrawals to date // deposit d to account void deposit(int d) { balance = balance + d; deposits = deposits + d; } // withdraw w from account void withdraw(int w) { balance = balance - w; withdrawals = withdrawals + w; } . . . } Lecture 6
Method Use • Let m be a method of class c • Let r refer to object o of class c • Then expression r.m ( expression-list ) or statement r.m ( expression-list ); invokes method m on object o • When method m is executed, field names signify the corresponding variables of object o • Example Invocation: account1.deposit(200); account1 AnAccountvariable balance deposits withdrawals deposit withdraw 0 0 0 Lecture 6
Example Method Use // Withdraw 100 from account2. account2.withdraw( 100 ); /* If k is 1, deposit 200 into account1, otherwise deposit 200 into account2. */ if ( k == 1 ) a = account1; else a = account2; // Deposit 200 to Account a. a.deposit( 200 ); // Let n be the next integer. n = in.readInt(); // Output 300. System.out.println(300); Lecture 6