CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG 1
Notice Assignments Reading Assignment: Chapter 3: Introduction to Parameters and Objects The Class 9 Exercise for today is posted here. The homework 3 and posted it here They are due this coming Sunday, at 11:59 PM. Mid- term Exam Date: the class on October 27th It covers the knowledge from Class 1 to Class 14 (October 20) To prepare the mid- term exam: class notes & quizzes & homework & exercises The exam will not be like the quizzes. You will be expected to read and write short segments of code A Mid- term review will be in the class on October 25 th hold by TA 2
Review Scope The scope of a an identifier is the part of the program where it can be used Scope applies to Variables Constant Methods The scope of a static method is the entire class in which it appears This means that any method can be called by any other method in the class 3
Scope A code block is section of code that is surrounded... by a matched pair of curly braces, { } We can use the idea of a code block to describe the scope of a variable The scope of a variable starts with the line where it is declared... and continues to the end of the code block in which it is declared We can use the same variable name inside two different methods... because each method has a different scope A variable declared inside a method is called local variables 4
The Scope of for Loop Variable The general scope rule for variables does not work for loop variables The scope of a loop variable is The part of the code between the for loop curly braces And the part of the code between the parentheses that starts the loop 5
Pseudocode When you write more complex algorithms, it s hard to write the entire algorithm immediately One way to deal with this is to use pseudocode When we write pseudocode, we write steps to solve a problem in simple English Then we break down these steps into simpler steps We keep doing this until we have an English description of the steps that can then be translated into Java code 6
Reusing Existing Code The Java code of DrawDownwardTriangle is like this When we run this code we get 7
Reusing Existing Code Sometimes the easiest way to write a program... is to modify some existing code Let's say you want to write a program to draw the following We might be able to use DrawDownwardTriangle.java instead If you look carefully, you will see that the 1st line of DrawDownwardTriangle... is the same as the 5th line of the figure we want... And so on 8
Reusing Existing Code So all we have to do to get the figure we want is change the direction of counting in the outer for loop of DrawDownwardTriangle In DrawDownwardTriangle.java the outer loop counted from 1 to 5 For this code, we want the outer loop to count from 5 to 1 Here is how we do it 9
Class Constants For the code in DrawCone.java for ( int i = 1; i <= (11 - (2 * line_number)); i++) Someone reading the code for the first time might ask "Why 11?" Programmers call values like this magic numbers They make the program work but it is not clear how Magic numbers make for bad code Whenever we can, we should replace such numbers with an expression... and those expressions should use constants A constant is like a variable except its value cannot be changed 10
Class Constants When creating a constant, you must use the keyword final... using the following format final DATA_TYPE CONSTANT_NAME = VALUE; like this final inthours_in_a_day = 24; By convention, constant names are spelled using ALL_CAPITALS The best place to put a constant is inside the class but outside all the methods. Such constants are called class constants. That way they are available to all methods in the class. The format is like this: public static final DATA_TYPE CONSTANT_NAME = VALUE; 11
New Materials Outline Parameters The Mechanics of Parameters Limitation of Parameters Multiple Parameters Overloading Methods 12
Parameters Let's look at the DrawCone2 class again When writing a program, we break a big problem down into smaller tasks If we turn each of the inter for loop into methods, the program would be cleaner 13
Parameters Java allows us to do that using parameters We use parameters to pass a value to a method which the method can use to do its work A parameter is a special local variable of a method that receives its value from the method call When you write the method header of a method that has parameters... you must specify the number and type of parameters the method uses The parameter declaration appears in the method header... after the name of the method and inside the parentheses 14
Parameters We need to rewrite the method header for writespaces as follows public static void writespaces (intnumber) Then we need to modify the method so it uses this parameter When we call this new method, we must put a value inside the parentheses of a method call The value can be any valid expression That means an argument can be a Literal Variable Constant Calculation using operators 15
Parameters Let's rewrite the code DrawCone3: Argument is the value given to the formal parameters inside the parentheses of the method call A formal parameter is the local variable declared in the method header The scope of the parameters is the method in which it is defined We can use the same variable name in both methods A parameter gets its value from the argument in the method call 16
New Materials Outline Parameters The Mechanics of Parameters Limitation of Parameters Multiple Parameters Overloading Methods 17
The Mechanics of Parameters initial Call writespaces After writespaces finishes The parameter only lives while the method runs 18
The Mechanics of Parameters When Java executes a method call, it Evaluates the expressions inside the parentheses of the method call Creates a new variable of the type specified in the method header Sets the value of this variable to the value given by the method call 19
Common Programming Error: Confusing Arguments and Formal Parameters Students sometimes make a mistake like this writespaces(int spaces1); // this doesn't work the argument of a method call should be like this: writespaces(spaces1); And a parameter is declaration inside a method header public static void writespaces(int number) The data type must be specified when the method is declared... not when a method is called 20
New Materials Outline Parameters The Mechanics of Parameters Limitation of Parameters Multiple Parameters Overloading Methods 21
Limitations of Parameters Parameters are used to get values into a method But they cannot be used to get values out of the method When you call a method, the machine sets aside memory... for each of the parameters declared in the method header... and initializes them with the values received from the method call The parameters are variables that are local to the method itself They are in a different scope from the method call Parameters only exist when the method is running... and can't be used to get information... back to the section of code that called them 22
Limitations of Parameters Consider the following example 23
Limitations of Parameters The program begins by declaring and initializing the integer variable x Then it calls doublenumber... using the value of x to initialize the parameter number... which is local to the method doublenumber The machine then executes the statements in the method It prints the original value of number, 17 Then it doubles the value of number Finally, it prints the new value of number, 34 24
Limitations of Parameters Here is the situation before the method finishes And after the method call is finished, we have The next statement in the main method prints the value of x, 17 Then a new variable, number, is declared in the main method And initialized to 42 Now the code calls doublenumber again... using the version of number in the main method as the argument 25
Limitations of Parameters Before the first statement of the method is executed we have This is an unusual situation We have two different variables named number But Java doesn't care They have different scopes and each is invisible to the other After doublenumberprints the original value, 42 It doubles value in doublenumber to 84 But this does not affect the value of number in the main method 26
Limitations of Parameters After the method call ends, we once again have So the change in the doublenumber version of number... did not affect the value of number in the main method Here is the output of the program 27
New Materials Outline Parameters The Mechanics of Parameters Limitation of Parameters Multiple Parameters Overloading Methods 28
Multiple Parameters Java allows you to define any number of parameters in a method header Here is the format for a method header public static void METHOD_NAME ( DATA_TYPE PARAMETER_NAME,... ) { STATEMENT;... } 29
Multiple Parameters Let's look at the DrawCone3 example once more In it we define two methods The only difference between them is them is the character they print We can replace these two methods with single method... if we supply the character as a second parameter 30
Multiple Parameters The new method will look like this This new parameter is of a different type, char... so we have to specify that in the message header Using this new method, we can rewrite our code as follows 31
New Materials Outline Parameters The Mechanics of Parameters Limitation of Parameters Multiple Parameters Overloading Methods 32
Overloading Methods Java allows us to have more than one method with the same name.. as long as the number and types of the parameters are different The combination of the method name and the data types of all parameters is called the method signature Allowing Java to have methods with the same name... but different signatures is called method overloading For example: 33
Class 9 Quiz 1. What is the scope of a parameter? the method in which it is defined 2. Where does a parameter get its value? from the argument in the method call 3. Where in a method header do you declare a parameter? inside the parentheses after the method name 4. Does a parameter keep its value after the method has finished? no. the parameter only lives while the method runs 5. Can a parameter be used to give a value back to the statement that calls the method? no 34
Class 9 Quiz 6. Write the method header for a method named printaverage that takes a single integer parameter called num public static void printaverage(intnum) 7. Write a statement that calls printaverage with the argument 10. printaverage(10); 8. Write the method header for a method named printmultiply that takes two integer parameters, a and b.? public static void printmultiply(int a, int b); 9. Write a statement that calls printmultiply with the arguments 4 and 5. printmultiply(4, 5); 10. What is a method signature? the name of the method together with the name and type of each of the parameters 35
Weekly Quiz1 1. What must the name of the source code file for the Java class CountToTenbe? CountToTen.java 2. If you want to run a Java program from the command line, what must the source code file contain inside the class definition? a main method 3. What is the first line of a method called? method header 4. What is a method? a group of statements that performs a specific task 36
Weekly Quiz1 5. Write the Java statement that prints "Hello world!"? System.out.println("Hello world!"); 6. In the previous statement, what does Java call "Hello world!"? a string literal 7. Write the escape sequence for the Tab character? \t 8. Can you use a Java keyword as an identifier? no 37
Weekly Quiz1 9. Is 1st_try a valid Java identifier? no 10. Write a one line comment that contains the text "This is a one line comment" // This is a one line comment 38