
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Adjeroh; Class: Introduction-Computer Science; Subject: Computer Science; University: West Virginia University; Term: Unknown 1989;
Typology: Study notes
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















Java Programming: From Problem Analysis to Program Design, Second Edition 1
Java Programming: From Problem Analysis to Program Design, Second Edition 2
Java Programming: From Problem Analysis to Program Design, Second Edition 3
Predefined Classes
Java Programming: From Problem Analysis to Program Design, Second Edition 4
Java Programming: From Problem Analysis to Program Design, Second Edition 7
class Character (Package: java.lang)
Java Programming: From Problem Analysis to Program Design, Second Edition 8
Syntax of Value-Returning Method
modifier(s) returnType methodName (formal parameter list) { statements }
Java Programming: From Problem Analysis to Program Design, Second Edition 9
Value-returning methods: Used in expressions. Calculate and return a value. Can save value for later calculation or print value. modifiers: public, private, protected, static, abstract, final. returnType: Type of the value that the method calculates and returns (using return statement). methodName: Java identifier; name of method.
Java Programming: From Problem Analysis to Program Design, Second Edition 10
dataType identifier, dataType identifier,...
methodName(actual parameter list)
Java Programming: From Problem Analysis to Program Design, Second Edition 13
public static double larger(double x, double y) { if (x >= y) return x; else return y; }
Java Programming: From Problem Analysis to Program Design, Second Edition 14
public static double larger(double x, double y) { if (x >= y) return x;
return y; }
Java Programming: From Problem Analysis to Program Design, Second Edition 15
Java Programming: From Problem Analysis to Program Design, Second Edition 16
public static boolean isPalindrome(String str) { int len = str.length(); int i, j; j = len - 1; for (i = 0; i <= (len - 1) / 2; i++) { if (str.charAt(i) != str.charAt(j)) return false; j--; } return true; }
Java Programming: From Problem Analysis to Program Design, Second Edition 19
Execution always begins with the first statement in the method main. User-defined methods execute only when called. Call to method transfers control from caller to called method. In the method call statement, specify only actual parameters, not data type or method type. Control goes back to caller when method exits.
Java Programming: From Problem Analysis to Program Design, Second Edition 20
Input: Set of 10 numbers Output: Largest of 10 numbers Solution: Get numbers one at a time. Method largest number: Returns the larger of 2 numbers. For loop: Calls method largest number on each number received and compares to current largest number.
Java Programming: From Problem Analysis to Program Design, Second Edition 21
static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num; double max; int count; System.out.println("Enter 10 numbers."); num = console.nextDouble(); max = num; for (count = 1; count < 10; count++) { num = console.nextDouble(); max = larger(max, num); } System.out.println("The largest number is "
Java Programming: From Problem Analysis to Program Design, Second Edition 22
Sample Run: Enter 10 numbers: 10.5 56.34 73.3 42 22 67 88.55 26 62 11 The largest number is 88.
Java Programming: From Problem Analysis to Program Design, Second Edition 25
Method definition: The definition of a void method with parameters has the following syntax: modifier(s) void methodName (formal parameter list) { statements } Formal parameter list: The formal parameter list has the following syntax: dataType variable, dataType variable, ...
Java Programming: From Problem Analysis to Program Design, Second Edition 26
Method call: The method call has the following syntax: methodName(actual parameter list);
Actual parameter list: The actual parameter list has the following syntax: expression or variable, expression or variable, ...
Java Programming: From Problem Analysis to Program Design, Second Edition 27
Value of actual parameter is directly stored. Cannot pass information outside the method. Provides only a one-way link between actual parameters and formal parameters.
Java Programming: From Problem Analysis to Program Design, Second Edition 28
Copies value of corresponding actual parameter. Value of actual parameter is address of the object where actual data is stored. Both formal and actual parameters refer to same object.
Java Programming: From Problem Analysis to Program Design, Second Edition 31
Scope of an Identifier within a Class Local identifier: An identifier that is declared within a method or block and that is visible only within that method or block. Java does not allow the nesting of methods. That is, you cannot include the definition of one method in the body of another method. Within a method or a block, an identifier must be declared before it can be used. Note that a block is a set of statements enclosed within braces. A method’s definition can contain several blocks. The body of a loop or an if statement also forms a block. Within a class, outside of every method definition (and block), an identifier can be declared anywhere.
Java Programming: From Problem Analysis to Program Design, Second Edition 32
Scope of an Identifier within a Class
Within a method, an identifier that is used to name a variable in the outer block of the method cannot be used to name any other variable in an inner block of the method. For example, in the following method definition, the second declaration of the variable x is illegal:
public static void illegalIdentifierDeclaration() { int x; //block { double x; //illegal declaration, //x is already declared ... } }
Java Programming: From Problem Analysis to Program Design, Second Edition 33
Scope rules of an identifier that is declared within a class and accessed within a method (block) of the class. An identifier, say X, that is declared within a method (block) is accessible: Only within the block from the point at which it is declared until the end of the block. By those blocks that are nested within that block. Suppose X is an identifier that is declared within a class and outside of every method’s definition (block). If X is declared without the reserved word static (such as a named constant or a method name), then it cannot be accessed in a static method. If X is declared with the reserved word static (such as a named constant or a method name), then it can be accessed within a method (block) provided the method (block) does not have any other identifier named X.
Java Programming: From Problem Analysis to Program Design, Second Edition 34
Example 7- public class ScopeRules { static final double rate = 10.50; static int z; static double t; public static void main(String[] args) { int num; double x, z; char ch; //... } public static void one(int x, char y) { //... }
Java Programming: From Problem Analysis to Program Design, Second Edition 37
Method overloading: More than one method can have the same name. Two methods are said to have different formal parameter lists : If both methods have a different number of formal parameters. If the number of formal parameters is the same in both methods, the data type of the formal parameters in the order you list must differ in at least one position.
Java Programming: From Problem Analysis to Program Design, Second Edition 38
public void methodOne(int x) public void methodTwo(int x, double y) public void methodThree(double y, int x) public int methodFour(char ch, int x, double y) public int methodFive(char ch, int x, String name)
These methods all have different formal parameter lists.
Java Programming: From Problem Analysis to Program Design, Second Edition 39
public void methodSix(int x, double y, char ch) public void methodSeven(int one, double u, char firstCh)
The methods methodSix and methodSeven both have three formal parameters, and the data type of the corresponding parameters is the same.
These methods all have the same formal parameter lists.
Java Programming: From Problem Analysis to Program Design, Second Edition 40
Method overloading: Creating several methods within a class with the same name.
The signature of a method consists of the method name and its formal parameter list. Two methods have different signatures if they have either different names or different formal parameter lists. (Note that the signature of a method does not include the return type of the method.)