Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.

Similar presentations


Presentation on theme: "CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the."— Presentation transcript:

1 CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the Java syntax for them and examples of them. Memorize these definitions. You should know them backwards and forwards by now, for they form the backbone of whatever we do from now on. On a test, you should be able to write such definitions and examples. What you write must be precise and clear.

2 CS100A, Fall 1998 Key Concepts 2 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: public class {declaration of fields and methods } Example: public class C {int x; String s; public C(String sp); {x= 0; s= sp;} public void addToX(int y) {x= x+y;} }

3 CS100A, Fall 1998 Key Concepts 3 Variable: A named box that can contain a 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 of a variable: a definition of the name of the variable and the type or class of value it can contain. Java syntax: ; Examples of variable declarations: A variable x that can contain an integer: int x; A variable s that can contain the name of an object of class String: String s; A variable c that can contain a boolean value (true or false): boolean b;

4 CS100A, Fall 1998 Key Concepts 4 Method: A parameterized sequence of statements, whose execution performs some task. In Java, there are three kinds of methods: –procedures, –functions, –constructors. The next three slides describe them. 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 instruc- tions. 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.

5 CS100A, Fall 1998 Key Concepts 5 A procedure is a method that performs some task (and doesn’t return a value) Java syntax: // Comment that explains what the method does public void ( ) {Sequence of statements to execute} Example: // Raise the salary by n dollars if the salary is <$20000 public void raiseSal(double n); {if (salary < 20000) salary= salary + n; } Example procedure call: raiseSal(20*y);

6 CS100A, Fall 1998 Key Concepts 6 A function is 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 is used to terminate execution of a function call and return. Java syntax: // Comment that explains what the function does. It should include something like “Yield …” to describe what the function returns. public void ( ) {Sequence of statements to execute} Example: // Yield the maximum of x and y public int max (int x, int y); {if (x>= y) return x; return y; } Example of a function call of max (within some statement): z= 1 + max(x,y);

7 CS100A, Fall 1998 Key Concepts 7 A constructor is a method that initializes the fields of an instance of a class when the instance is created. Java syntax: // Comment that explains what the constructor does. public ( ) {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 of the use of a constructor (when creating a new instance of an Employee) d= new Employee(“Gries”, 1983);

8 CS100A, Fall 1998 Key Concepts 8 Execution of an assignment statement stores a value in a variable. Java syntax: = ; Examples: b= 2+c; s= “Cardie” + “ ” + yearHired”;

9 CS100A, Fall 1998 Key Concepts 9 A block is used to unify a sequence of statement into a single statement. Java 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; }

10 CS100A, Fall 1998 Key Concepts 10 Execution of a conditional statement allows a choice of execution. Java syntax: if ( ) or if ( ) else The first form is executed as follows: if is true, then execute The second form is executed as follows: if is true, then execute ; if the.

11 CS100A, Fall 1998 Key Concepts 11 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. Java syntax: public class extends {declarations of fields and methods} Example: public class VIP extends Employee {private double 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 super class Employee.

12 CS100A, Fall 1998 Key Concepts 12 Access modifiers: Suppose d is an instance of Employee, where class Employee is declared as: public class Employee { int x; … } If the 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).

13 CS100A, Fall 1998 Key Concepts 13 More about variables and their scope public class Class1 {public int x; public int y; // Constructor: … public Class1 (int z) {z= z; y= 2*z;} // Set y to the maximum of p and -p public void sety(int p) { int x; x= p; if (p > -p) x= -p; y= p; } A field of Class1 A local variable method sety parameters

14 CS100A, Fall 1998 Key Concepts 14 The scope of a name is the set of places in which it can be referenced. 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 public void sety(int p) {int x; x= p; if (p > -p) x= -p; y= p; } Scope of x

15 CS100A, Fall 1998 Key Concepts 15 The scope of a name is the set of places in which it can be referenced. A variable can be declared only once within a method. Such a variable is sometimes called a local variable (of the method) The scope of a local variable of a method is the sequence of statements following it. Example: //... public test(int p) {y= p; int x; x= p; if (p > -p) x= -p; y= p; } Scope of x

16 CS100A, Fall 1998 Key Concepts 16 The scope of a name is the set of places in which it can be referenced. The scope of a local variable of a method is the sequence of statements following it. Example: //... public sety(int p) {if (y= p); {int x; x= p; if (p > -p) x= -p; } y= p; y= -y; } Scope of x x cannot be referenced here

17 CS100A, Fall 1998 Key Concepts 17 The scope of a parameter of a method is the method body. A parameter may not be redeclared within the body. Example: //... public test(int p) {if (y= p); {int x; x= p; if (p > -p) x= -p; } y= p; } Scope of p

18 CS100A, Fall 1998 Key Concepts 18 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. Example: public class Text x {int x= 5; int y= x+15; public void test(int p) {if (x= p); {int x= 35; x x= p; if (p > -p) x= -p; } x= p; } 35 20


Download ppt "CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the."

Similar presentations


Ads by Google