Announcements
E N D
Presentation Transcript
Announcements • Homework P1 due on Thursday • Homework P2 handed out Lecture 6
Today’s Topics • Review of Friday • Review of topics covered so far • Scope of variables • Introduction to while Lecture 6
Review of Friday • Inheritance: subclass vs. superclass • The method toString() • Overriding methods • super() • public vs. private Lecture 6
Confusion on Strings String s = new String("Millett"); String t = new String("Millett"); if (s == t) System.out.println("s and t are equal!"); String r = "Millett"; String q = "Millett"; if (r == q) System.out.println("r and q are equal!"); if (s == r) System.out.println("s and r are equal!"); Lecture 6
Today: • Definitions of basic entities that make up a Java program • Syntax of these entities • Memorize these • Be able to come up with definitions and examples on a test Lecture 6
Class • A “model” or blueprint for the objects (or instances) of the class; a class defines the fields (or variables) and methods of each object of the class. • Analogy: a blueprint for a house is a design for a house, many houses (objects) can be built from the same blueprint, but they may differ in color of rooms, wallpaper, etc. • Java syntax: publicclass <class name> {declaration of fields and methods } • Example: publicclass C {int x; String s; public C(String sp); {x= 0; s= sp;} public void addToX(int y) {x= x+y;} } Lecture 6
Variable • A named box that can contain the value of some type or class. For a type like int, the value is an integer. For a class, it is the name of (or reference to) an instance of the class • Declaration: a definition of the name of the variable and the type or class of value it can containt • Syntax: <class or type name> <identifier>; • Examples:// a variable x that can contain an integerint x; // variable s that can contain the name of an object of class StringString s; // a variable that can contain a boolean value (true or false)boolean b; Lecture 6
Method • A parameterized sequence of statements, whose execution performs some task. In Java, there are three kinds of methods: • procedures, • functions, • constructors. • The next few slides describe them Lecture 6
Notes on methods • A method should be accompanied by a comment that says what the method does. This is the specification of the method. The comment has to be precise and clear. A potential user of the method should be able to look only at the comment and the list of parameters to know how to use it; they should not have to look at the list of instructions. • Example: When you want to bake a cake, you look at the title of a recipe, a short description, and the list of ingredients to determine whether you want to use that recipe --not the list of instructions to bake it. Lecture 6
Procedure • A method that performs some task and doesn’t return a value • Java syntax: // comment that explains what the procedure doespublic void <method name> (<parameter(s)>) { sequence of statements to execute } • Example: // Raise the salary by n dollars if the salary is <$20000 publicvoid raiseSal(double n); {if (salary < 20000) salary= salary + n; } • Example procedure call: raiseSal(20 * y); Lecture 6
Function • A method that performs some task and returns a value. Instead of the keyword void, the type or class of the return value is used. Statement return <value> is used to terminate execution of a function call and return <value>. • Syntax: // Comment that explains what the function does. It should include // something like “Yield …” to describe what the function returns. public<type> <method name> (<parameters>) {Sequence of statements to execute} • Example: // Yield the maximum of x and y publicint max (int x, int y); {if (x>= y) return x; return y; } • Example function call (within some statement) z = 1 + max(x, y); Lecture 6
Constructor • A method that initializes the fields of an instance of a class when the instance is created. • Syntax: // Comment that explains what the constructor does. public <class name> (<parameters>) {Sequence of statements to execute} • Example (within class Employee): // Constructor: an Employee with name n,salary 0, and year hired d public Employee Employee (String n, int d); {name= n; salary= 0; yearHired= d; } • Example use (when creating a new instance):d = new Employee(“Millett”, 1999); Lecture 6
Execution of Assignment • Execution of an assignment statement stores a value in a variable • Syntax: <variable name> = <expression>; • Examples:b = 2 + c; s = “Millett” + “ ” + yearHired; Lecture 6
Block • A block is used to unify a sequence of statements into a single statement. • Syntax: { sequence of statements } • Example: Here is a sequence of statements:a = 10;if (a < c) then a = c; • Here is a single statement which is a block:{ a = 10; if (a < c) then a = c; } Lecture 6
Conditional Statement • Execution of a conditional statement allows a choice of execution • Syntax: if (<boolean expression>) <statement> or if (<boolean expression>) <statement1> else <statement2> Lecture 6
Subclass • A subclass B (say) is a class that extends another class A (say). This means that an instance of B has all the fields and methods that an instance of A has, in addition to the ones declared in B. • Syntax: public class <class name> extends <class name> { declarations of fields and methods } • Example: public class VIP extends Employee {privatedouble bonus; // Constructor: an VIP with name n, salary s, bonus b, and year hired d public VIP (String n, double s, double b, int d) {super(n,d); salary= s; bonus= b;} } • Note the use of super to call a constructor of the superclass Employee Lecture 6
Access modifiers • Suppose d is an instance of Employee, where class Employee is declared as: publicclass Employee {<access modifier> int x; … } • If the <access modifier> is: • public, then field d.x can be referenced anywhere that d can be referenced. • private, then field d.x can be referenced anywhere within class Employee that d can be referenced • protected, then field d.x can be referenced anywhere within the package in which Employee is declared (packages are discussed later). Lecture 6
A field of Class1 Scope of Variables publicclass Class1 {publicint x; publicint y; // Constructor: … public Class1 (int z) {z= z; y= 2*z;} // Set y to the maximum of p and -p publicvoid sety(int p) { int x; x= p; if (p < -p) x= -p; y= x;}} parameters A local variable of method sety Lecture 6
More on Scope • The scope of a variable is the set of places in which it can be referenced. A variable can be declared only once within a method. We call this a local variable. • The scope of a local variable of a method is the sequence of statements following it • Example:// Set y to the maximum of p and -p publicvoid sety(int p) {int x; x= p; if (p > -p) x= -p; y= p; } Scope of x Lecture 6
Scope of x Another example // ... public sety(int p) {if (y= p); {int x; x= p; if (p > -p) x= -p; } y= p; y= -y; } x cannot be referenced here Lecture 6
Scope of p Scope of parameters • The scope of a parameter of a method is the method body. A parameter may not be redeclared within the body. // ... public test(int p) {if (y= p); {int x; x= p; if (p > -p) x= -p; } y= p; } Lecture 6
Scope of fields • The scope of a field of a class consists of:(1) the bodies of all methods declared in the class and(2) all declarations of fields that follow the declaration of the field. publicclass Text {int x= 5; int y= x+15; publicvoid test(int p) {if (x= p); {int x= 35; x= p; if (p > -p) x= -p;} x= p; } Lecture 6
The while loop -- briefly • Computer programs are very good at repetition (and don’t get bored) • Sometimes it is necessary to repeat a statement or block many times • One way to do this is with a while loop • A while statement controls how many times another statement/block is executed Lecture 6
true Evaluate B false Execute S While syntax while ( condition ) while(B) statement; S; • Similar to if, but at the end of the statement, the condition is checked again Lecture 6
Example // use while to print first 10 powers of 2 final static int LIMIT = 10; int count = 1, power_of_two = 1; while (count <= LIMIT) { power_of_two = power_of_two * 2; System.out.println(power_of_two); count = count + 1; } Lecture 6