
























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
An in-depth look into the mechanisms of passing information between methods in java. It covers topics such as method declaration and usage, method call stack and activation records, argument promotion and casting, and the java api. The document also includes case studies on random number generation and a game of chance, as well as discussions on static methods, constants, and scopes.
Typology: Study notes
1 / 32
This page cannot be seen from the preview
Don't miss anything!

























In this lecture you will learn:
Static method
common Math methods available in the Java API.
To understand the mechanisms for passing
information between methods.
How the method call/return mechanism is supported
by the method call stack and activation records.
How packages group related classes.
Example: random number generatorExample: random number generator
Scoping
Method overloading
3 import java.util.Scanner; 4 55 public class MaximumFinderpublic class MaximumFinder 6 { 7 // obtain three floating-point values and locate the maximum value 8 public void determineMaximum() 9 { 10 // create Scanner for input from command window 11 Scanner input = new Scanner( System.in ); 12 13 // obtain user input 14 System.out.print( 15 "Enter three floating-point values separated by spaces: " ); 16 double number1 = input nextDouble(); // read first double
16 double number1 = input.nextDouble(); // read first double 17 double number2 = input.nextDouble(); // read second double 18 double number3 = input.nextDouble(); // read third double 19 20 // determine the maximum value 21 double result = maximum( number1, number2, number3 ); 22 23 // display maximum value 24 System.out.println( "Maximum is: " + result ); 25 } // end method determineMaximum 26
Declare the maximum method
Compare y and maximumValue
Compare z and maximumValue
Return the maximum value
1 // Fig. 6.4: MaximumFinderTest.java 22 //// Application to test classApplication to test class MaximumFinderMaximumFinder. 3 4 public class MaximumFinderTest 5 { 6 // application starting point 7 public static void main( String args[] ) 8 { 9 MaximumFinder maximumFinder = new MaximumFinder(); 10 maximumFinder.determineMaximum(); 11 } // end main 12 } // end class MaximumFinderTest
Create a MaximumFinder object
Call the determineMaximum method
12 } // end class MaximumFinderTest
Enter three floating-point values separated by spaces: 9.35 2.74 5. Maximum is: 9.
Enter three floating-point values separated by spaces: 5.8 12.45 8. Maximum is: 12.
Enter three floating-point values separated by spaces: 6.46 4.12 10. Maximum is: 10.
double None float double long float or double int long , float or double char int , long , float or double short int , long , float or double (but not char ) byte short , int , long , float or double (but not char ) boolean None ( boolean values are not considered to be numbers in Java)
java.net (^) The Java Networking Package contains classes and interfaces that enable programs to
communicate via computer networks like the Internet. (You will learn more about this in Chapter 24, Networking.) java.text (^) The Java Text Package contains classes and interfaces that enable programs to manipulate
numbers, dates, characters and strings. The package provides internationalization capabilities that enable a program to be customized to a specific locale (e.g., a program may display strings in different languages, based on the user’s country). java.util (^) The Java Utilities Package contains utility classes and interfaces that enable such actions as date
and time manipulations, random-number processing (class Random ), the storing and processing of large amounts of data and the breaking of strings into smaller pieces called tokens (class StringTokenizer ). (You will learn more about the features of this package in Chapter 19, Collections.)) javax.swing (^) The Java Swing GUI Components Package contains classes and interfaces for Java’s Swing
GUI components that provide support for portable GUIs. (You will learn more about this package in Chapter 11, GUI Components: Part 1 and Chapter 22, GUI Components: Part 2.) javax.swing.event (^) The Java Swing Event Package contains classes and interfaces that enable event handling (e.g.,
responding to button clicks) for GUI components in package javax.swing. (You will learn more about this package in Chapter 11, GUI Components: Part 1 and Chapter 22, GUI Components: Part 2.)
1 // Fig. 6.7: RandomIntegers.java 2 // Shifted and scaled random integers. 3 import java.util.Random; // program uses class Random
5 public class RandomIntegers 6 { 7 public static void main( String args[] ) 8 { 9 Random randomNumbers = new Random(); // random number generator 10 int face; // stores each random integer generated 11 12 // loop 20 times 13 for ( int counter = 1; counter <= 20; counter++ ) 14 { 15 // pick random integer from 1 to 6 16 face = 1 + randomNumbers.nextInt( 6 );
18 System.out.printf( "%d ", face ); // display generated value 19 20 // if counter is divisible by 5, start a new line of output 21 if ( counter % 5 == 0 ) 22 System.out.println(); 23 } // end for 24 } // end main 25 } // end class RandomIntegers
18 int face; // stores most recently rolled value 19 20 // summarize results of 6000 rolls of a die 21 for ( int roll = 1; roll <= 6000; roll++ ) 22 { 23 face = 1 + randomNumbers.nextInt( 6 ); // number from 1 to 6 24 25 // determine roll value 1-6 and increment appropriate counter 26 switch ( face ) 27 { 28 case 1: 29 ++frequency1; // increment the 1s counter 30 break;
Iterate 6000 times
Generate a random die roll
30 break; 31 case 2: 32 ++frequency2; // increment the 2s counter 33 break; 34 case 3: 35 ++frequency3; // increment the 3s counter 36 break; 37 case 4: 38 ++frequency4; // increment the 4s counter 39 break; 40 case 5: 41 ++frequency5; // increment the 5s counter 42 break; 43 6
switch based on the die roll
43 case 6: 44 ++frequency6; // increment the 6s counter 45 break; // optional at end of switch 46 } // end switch 47 } // end for 48
49 System.out.println( "Face\tFrequency" ); // output headers 5050 SystemSystem.out.printf( "1\t%d\n2\t%d\n3\t%d\n4\t%d\n5\t%d\n6\t%d\n", out printf( "1\t%d\n2\t%d\n3\t%d\n4\t%d\n5\t%d\n6\t%d\n" 51 frequency1, frequency2, frequency3, frequency4, 52 frequency5, frequency6 ); 53 } // end main 54 } // end class RollDie
Face Frequency 1 982 2 1001 3 1015 4 1005 5 1009 6 988
Display die roll frequencies
Face Frequency 1 1029 2 994 3 1017 4 1007 5 972 6 981