Methods basics

1 / 107

# Methods basics - PowerPoint PPT Presentation

Methods basics. Before this week Solve the problem de l’heure by developing a static method main( ) Did use other libraries Class methods Non-void and void methods Methods with and without parameters. Things to learn and understand. Mechanics of method invocation Activation records

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Methods basics' - mira

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Methods basics
• Before this week
• Solve the problem de l’heure by developing a static method main()
• Did use other libraries
• Class methods
• Non-void and void methods
• Methods with and without parameters
Things to learn and understand
• Mechanics of method invocation
• Activation records
• Flow of control
• Return statement
• Parameter passing
• Scope
Methods
• What are they
• A named piece of code that can take parameters and produce a value
• What are they good for
• Support modular problem solving
• Re-use
• Correctness

double ratio = Math.cos( 10 );

boolean b = MagicEightBall.shouldI( ”buy it” );

Carbon dating
• Archeologists can date a biological artifact by studying its relative concentration of carbon-14 to carbon-12
• Concentration is an indicator of agebecause radioactive carbon-14 decaysto carbon-12 in a very predictable manner
• Carbon dating formula
• Age = ln( concentration ) * –8260
Sample run

Let’s write a method that does the carbon dating and a program that uses the method

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

• A block is a list of statements nested within braces
• A method body is a block
• A block can be placed anywhere a statement would be legal
Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

What is necessary for this to compile and run?

Dater.java

public class Analyze {

public static void main(String[] args) {

String s = Dater.carbonDate();

String t = Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Does this compile?

Dater.java

public class Analyze {

public static void main(String[] args) {

String s = Dater.carbonDate();

String t = Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Does this compile?

No! There is nothing that carbonDating() is supplying for s and t to use.

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Does this compile?

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Does this compile?

No! Without static then carbonDate() is a message method that must used with an object to take the message

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

public – other classes can use the method

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

static – library method – performs a service does not send a message to an object

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

void – does not bring back a value; i.e., it does not share anything with the invoker

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Like all programs begins with the running – invocation – of method main()

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Programmers that say the method being invoked has the flow of control – its statements are the ones being currently executed

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Every method invocation begins with the creation in the computer memory of an activation record that stores the values of the variables belonging to the method

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

main() uses carbonDate() to create the display

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Because carbonDate() is a class method, invocation starts with the name of its class

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

publicstatic void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

carbonDate() takes the flow of control and its gets a new activation record

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

publicstatic void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

publicstatic void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Ratio of C-14 to C-12? 0.05

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

publicstatic void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Ratio of C-14 to C-12? 0.05

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

publicstatic void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

publicstatic void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

A ratio of 0.05 indicates the object is approximately 24744 years old

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

publicstatic void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Completion of carbonDate() releases its activation record and main() retakes the flow of control

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

publicstatic void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

carbonDate() again starts up with a new activation record and the flow of control

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

publicstatic void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

publicstatic void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Ratio of C-14 to C-12? 0.10

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

publicstatic void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Ratio of C-14 to C-12? 0.10

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

publicstatic void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

publicstatic void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

A ratio of 0.10 indicates the object is approximately 19019 years old

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Completion of carbonDate() releases its activation record and main() retakes the flow of control

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

main() completes and returns control back to the system

Dater.java

public class Analyze {

public static void main(String[] args) {

Dater.carbonDate();

Dater.carbonDate();

}

}

public class Dater {

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int) (Math.log(concentration)

* -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n");

}

}

main() completes and returns control back to the system

Dater.java

publicstatic void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int)(Math.log(concentration) * -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n")

}

Modified Dater.java

public class Analyze {

// main(): program starting point

public static void main(String[] args) {

Dater.carbonDate();

System.out.println( concentration );

}

public static void carbonDate() {

...

double concentration = stdin.nextDouble();

}

Does this compile and run? Why?

Modified Dater.java

public class Analyze {

// main(): program starting point

public static void main(String[] args) {

Dater.carbonDate();

System.out.println( concentration );

}

public static void carbonDate() {

...

double concentration = stdin.nextDouble();

}

Does this compile and run? Why?

No. Local variables of other methods are not accessible

Dater.java

public static void carbonDate() {

Scanner stdin = new Scanner(System.in);

System.out.print("Ratio of C-14 to C-12? ");

double concentration = stdin.nextDouble();

int ageInYears = (int)(Math.log(concentration) * -8260);

System.out.println("A ratio of " + concentration

+ " indicates the object is approximately "

+ ageInYears + " years old\n")

}

• What’s defined in braces stays in braces
• Method main() cannot use carbonDate()’s stdin, concentration, or ageInYears
• When main() has the flow of control, carbonDate()’s local variables do not exist
Bringing back something
• Consider again

String t = s.substring(5, 7);

double ratio = Math.cos(10);

What is happening here?

What is happening here?

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

What type of value does promptAndReadInt() need to return?

StandardInput.java

public class StandardInput {

// promptAndReadInt(): returns next input as an int

// get the number

Scanner stdin = new Scanner(System.in);

System.out.print("Enter an integer value: ");

int result = stdin.nextInt();

// return the number

return result;

}

}

promptAndReadInt() method in class StandardInput has a return type ofint – a promptAndReadInt() invocation produces anintas its return value

StandardInput.java

public class StandardInput {

// promptAndReadInt(): returns next input as an int

// get the number

Scanner stdin = new Scanner(System.in);

System.out.print("Enter an integer value: ");

int result = stdin.nextInt();

// return the number

return result;

}

}

A method supplies its return value with areturn statement

The return tells Java to return the flow of control back where it came from and what value to share back there

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

public class StandardInput {

Scanner stdin = new Scanner(System.in);

System.out.print("Enter an integer value: ");

int result = stdin.nextInt();

return result;

}

}

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

public class StandardInput {

Scanner stdin = new Scanner(System.in);

System.out.print("Enter an integer value: ");

int result = stdin.nextInt();

return result;

}

}

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

public class StandardInput {

Scanner stdin = new Scanner(System.in);

System.out.print("Enter an integer value: ");

int result = stdin.nextInt();

return result;

}

}

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

public class StandardInput {

Scanner stdin = new Scanner(System.in);

System.out.print("Enter an integer value: ");

int result = stdin.nextInt();

return result;

}

}

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

public class StandardInput {

Scanner stdin = new Scanner(System.in);

System.out.print("Enter an integer value: ");

int result = stdin.nextInt();

return result;

}

}

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

11 * 11 = 121

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

11 * 11 = 121

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

public class StandardInput {

Scanner stdin = new Scanner(System.in);

System.out.print("Enter an integer value: ");

int result = stdin.nextInt();

return result;

}

}

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

public class StandardInput {

Scanner stdin = new Scanner(System.in);

System.out.print("Enter an integer value: ");

int result = stdin.nextInt();

return result;

}

}

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

public class StandardInput {

Scanner stdin = new Scanner(System.in);

System.out.print("Enter an integer value: ");

int result = stdin.nextInt();

return result;

}

}

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

public class StandardInput {

Scanner stdin = new Scanner(System.in);

System.out.print("Enter an integer value: ");

int result = stdin.nextInt();

return result;

}

}

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

public class StandardInput {

Scanner stdin = new Scanner(System.in);

System.out.print("Enter an integer value: ");

int result = stdin.nextInt();

return result;

}

}

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

public class StandardInput {

Scanner stdin = new Scanner(System.in);

System.out.print("Enter an integer value: ");

int result = stdin.nextInt();

return result;

}

}

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

11 * 11 = 121

9 * 9 = 81

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

11 * 11 = 121

9 * 9 = 81

ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

System.out.println(n1 + "*" + n1 + "=" + (n1 * n1));

System.out.println();

System.out.println(n2 + "*" + n2 + "=" + (n2 * n2));

System.out.println();

}

}

11 * 11 = 121

9 * 9 = 81

WhatIsUp.java

public class WhatIsUp {

public static void main(String[] args) {

int n = WhatIsUp.hereIsATwo ();

}

public static int hereIsATwo() {

return 2;

}

}

Does this compile and run? Why?

What is the program output?

WhatIsUp.java

public class WhatIsUp {

public static void main(String[] args) {

int n = WhatIsUp.hereIsATwo();

}

public static int hereIsATwo() {

return 2;

}

}

Does this compile and run? Why?

What is the program output?

NO OUTPUT!!!!

RETURN DOES NOT MEAN OUTPUT!!!!

IT TAKES A PRINT() OR PRINTLN() FOR OUTPUT!!!!

Passing primitive values
• What’s going on

String t =s.substring(5, 7);

double ratio =Math.cos(10);

How are 5, 7, and 10 being used?

Passing primitive values
• What’s going on

String t =s.substring(5, 7);

double ratio =Math.cos(10);

How are 5, 7, and 10 being used?

They are arguments / actual parameters in some method invocations

How much money

Compound interest

Occurs when an account earns intereston both principal and previously awardedinterest

Formula for yearly compound interest total

Finance.java

// Purpose: provide a class method for computing interest

public class Finance {

// interest(): computes amount paid per year

public static double interest(double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal * (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

}

Finance.java

public static double interest(double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal * (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

Finance.java

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

Finance.java

Formal parameters are initialized using the values of the actual parameters from an invocation

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

Finance.java

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

Different invocations can supply different values for the formal parameters – the flexibility allows the method to tailor its actions

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Finance.java

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Finance.java

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Finance.java

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

return amount;

double interest1 = Finance.interest(

5000, 0.045, 1 );

double interest4 = Finance.interest(

5000, 0.045, 4 );

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest1 = Finance.interest(

5000, 0.045, 1 );

double interest4 = Finance.interest(

5000, 0.045, 4 );

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest4 = Finance.interest(

5000, 0.045, 4 );

double interest365 = Finance.interest(

5000, 0.045, 365 );

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest1 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest4 = Finance.interest(

5000, 0.045, 4 );

double interest365 = Finance.interest(

5000, 0.045, 365 );

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

return amount;

double interest4 = Finance.interest(

5000, 0.045, 4 );

double interest365 = Finance.interest(

5000, 0.045, 365 );

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest4 = Finance.interest(

5000, 0.045, 4 );

double interest365 = Finance.interest(

5000, 0.045, 365 );

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest365 = Finance.interest(

5000, 0.045, 365 );

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest1 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest365 = Finance.interest(

5000, 0.045, 365 );

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

return amount;

double interest365 = Finance.interest(

5000, 0.045, 365 );

}

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest365 = Finance.interest(

5000, 0.045, 365 );

}

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Parameter passing

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest365 = Finance.interest(

5000, 0.045, 365 );

}

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Two questions
• When does the following method produce as output?
• What is the return value for the method?

public static int f( int a ) {

a = a * 2;

return 0;

}

Finance.java

public static double interest(

double principal, double rate, int n) {

// compute interest using standard formula

double amount = principal

* (Math.pow(1 + rate / n, n) - 1);

// return the computed interest

return amount;

}

double interest1 = Finance.interest( 5000, 0.045, 1 );

double interest4 = Finance.interest( 5000, 0.045, 4 );

double interest365 = Finance.interest( 5000, 0.045, 365 );

Pass by value
• Java is a pass-by-value language
• This means that a COPY of the parameter’s value is passed into the method
• If a method changes that value, only the COPY is changed
• Once the method returns, the copy is forgotten
• Change is not visible outside the method
Two questions
• Suppose mystery() now has the definition

public static void mystery( int a ) {

a = 1;

}

• Consider

int n = 4;

mystery( n );

System.out.println( n );

int a = 4;

s1.mystery( n );

System.out.println( n );

• What does the first println() display?
• What does the second println() display?