Methods basics
Download
1 / 107

Methods basics - PowerPoint PPT Presentation


  • 128 Views
  • Uploaded on

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

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

PowerPoint Slideshow about ' Methods basics' - mira


An Image/Link below is provided (as is) to download presentation

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
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
Things to learn and understand

  • Mechanics of method invocation

  • Activation records

  • Flow of control

  • Return statement

  • Parameter passing

  • Scope


Methods
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
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
Sample run

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


Dater java
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 java1
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 java2
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 java3
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 java4
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 java5
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 java6
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 java7
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 java8
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 java9
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 java10
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 java11
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 java12
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 java13
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 java14
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 java15
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 java16
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 java17
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 java18
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 java19
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 java20
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 java21
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 java22
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 java23
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 java24
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 java25
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 java26
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 java27
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 java28
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 java29
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 java30
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 java31
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 java32
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 java33
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 java34
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 java35
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 java36
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
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 java1
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 java37
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
Bringing back something

  • Consider again

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

    double ratio = Math.cos(10);

  • How about

    int n1 = StandardInput.promptAndReadInt();

    int n2 = StandardInput.promptAndReadInt();

What is happening here?

What is happening here?


Returndemo java
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

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



Standardinput java
StandardInput.java

public class StandardInput {

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

public static intpromptAndReadInt() {

// 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 java1
StandardInput.java

public class StandardInput {

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

public static int promptAndReadInt() {

// 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 java2
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}


Returndemo java3
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}


Returndemo java4
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

public class StandardInput {

public static int promptAndReadInt() {

Scanner stdin = new Scanner(System.in);

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

int result = stdin.nextInt();

return result;

}

}


Returndemo java5
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

public class StandardInput {

public static int promptAndReadInt() {

Scanner stdin = new Scanner(System.in);

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

int result = stdin.nextInt();

return result;

}

}


Returndemo java6
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

public class StandardInput {

public static int promptAndReadInt() {

Scanner stdin = new Scanner(System.in);

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

int result = stdin.nextInt();

return result;

}

}


Returndemo java7
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

public class StandardInput {

public static int promptAndReadInt() {

Scanner stdin = new Scanner(System.in);

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

int result = stdin.nextInt();

return result;

}

}


Returndemo java8
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

public class StandardInput {

public static int promptAndReadInt() {

Scanner stdin = new Scanner(System.in);

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

int result = stdin.nextInt();

return result;

}

}


Returndemo java9
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

11 * 11 = 121


Returndemo java10
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

11 * 11 = 121


Returndemo java11
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

public class StandardInput {

public static int promptAndReadInt() {

Scanner stdin = new Scanner(System.in);

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

int result = stdin.nextInt();

return result;

}

}


Returndemo java12
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

public class StandardInput {

public static int promptAndReadInt() {

Scanner stdin = new Scanner(System.in);

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

int result = stdin.nextInt();

return result;

}

}


Returndemo java13
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

public class StandardInput {

public static int promptAndReadInt() {

Scanner stdin = new Scanner(System.in);

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

int result = stdin.nextInt();

return result;

}

}


Returndemo java14
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

public class StandardInput {

public static int promptAndReadInt() {

Scanner stdin = new Scanner(System.in);

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

int result = stdin.nextInt();

return result;

}

}


Returndemo java15
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

public class StandardInput {

public static int promptAndReadInt() {

Scanner stdin = new Scanner(System.in);

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

int result = stdin.nextInt();

return result;

}

}


Returndemo java16
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

public class StandardInput {

public static int promptAndReadInt() {

Scanner stdin = new Scanner(System.in);

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

int result = stdin.nextInt();

return result;

}

}


Returndemo java17
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

11 * 11 = 121

9 * 9 = 81


Returndemo java18
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

11 * 11 = 121

9 * 9 = 81


Returndemo java19
ReturnDemo.java

public class ReturnDemo {

// main(): program starting point

public static void main(String[] args) {

int n1 = StandardInput.promptAndReadInt();

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

System.out.println();

int n2 = StandardInput.promptAndReadInt();

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

System.out.println();

}

}

11 * 11 = 121

9 * 9 = 81


Whatisup java
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 java1
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
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 values1
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 money1
How much money

Compound interest

Occurs when an account earns intereston both principal and previously awardedinterest

Formula for yearly compound interest total


Finance java
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 java1
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 java2
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 java3
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 java4
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 java5
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 java6
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 java7
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
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 passing1
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 passing2
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 passing3
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 passing4
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 passing5
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 passing6
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 passing7
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 passing8
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 passing9
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 passing10
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 passing11
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 passing12
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 passing13
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 passing14
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 passing15
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 passing16
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 passing17
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 passing18
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
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 java8
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
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 questions1
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?


ad