130 likes | 226 Views
CSE 1341 - Honors Principles of Computer Science I. Mark Fontenot mfonten@engr.smu.edu. Note Set 6. Note Set 6 Overview. Methods – In Depth Return values and the call stack Argument promotion/casting Random Number Generation Scope. Understanding Calls - Stack Data Structure.
E N D
CSE 1341 - HonorsPrinciples of Computer Science I Mark Fontenot mfonten@engr.smu.edu Note Set 6
Note Set 6 Overview • Methods – In Depth • Return values and the call stack • Argument promotion/casting • Random Number Generation • Scope
Understanding Calls - Stack Data Structure • Understanding stacks is useful for understanding return • Stacks are LIFO • Last In – First Out • Last value put in (pushed) is first value removed (pop) • When method A calls method B, the system (jvm) must know where to return to in A when B is finished • Activation Record – information on local variables in a method
Argument Promotion and Casting Expects a double, but can be sent an int. Int would be cast to double System.out.println(sqrt(4)); • Arguments can have different data types from parameters under certain conditions • Header for square root • public static double sqrt(double a)
Valid promotions May get compile time errors if you try to implicitly “demote” a type double to int Use explicit casting in this case square( (int) doubleValuedVariable );
Random Number Generation Random randGenerator = new Random() intrandomValue = randGenerator.nextInt(); double doubleRandom = randomGenerator.nextDouble(); • used in simulation, games, cryptography • Use Random Class that is in java.util • can generate random boolean, byte, float, double, int, long • Produce numbers using mathematical function with VERY large period
Random Ints • Produces values from -2,147,483,648 to 2,147,483,647 • Limit between 0 and n • int x = randGenerator(24) • Will produce pseudo sequence of numbers on the range [0, 23]. • How would you produce numbers between m and n, m > n and m >= 0?
Random Generator • Because it is a function, will produce the same sequence of numbers unless you tell it where to start every time • Random randGenerator = new Random() • Random randGenerator = new Random( 25 ); • Random randGenerator = new Random(???) • Need something that changes every time the program is run. Ideas???
Scope of Variables • declaration introduces a entity with a name that can be referred to in code. • Scope – the portion of the program that can refer to the declared entity by name • Scope Rules • scope of parameter declaration is the body of the method in which the declaration appears • scope of local variable is from the point of declaration to the end of the block • scope of variable that appears in the initialization section of a for loop header if for the body of the for statement and other parts of for header • scope of a method or field of a class is the entire body of the class
Shadowing public class Test { private int x; public void someMethod() { int x = 5; System.out.println(x); } } Will access Local variable x instead of instance variable x Local variable in an instance method with same name as an instance variable shadows the instance variable.
Method Overloading public class X { public void myMethod (int x, int y) { } public intmyMethod (float z, float y){ } } public class TestX { public static void main (String [] args) { X myVar = new X(); myVar.myMethod(1, 2); myVar.myMethod (3.0, 5.0); } } • Overloaded Methods – Methods in the same class that have the same name but different sets of parameters • determined by the number, types and order of parameters • Differences in return type are irrelevant when determining if 2 methods are overloaded
Overloading public class Tester { public intmyMethod(int) { } public void myMethod(int) { } } Still ambiguous to the compiler • Compiler distinguishes methods by their signature • combination of the method’s name and the number, types and order of its parameters. • Remember – Return type of method DOES NOT MATTER • Overloaded methods do not need to have the same number of parameters