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

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

• 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

• Mechanics of method invocation

• Activation records

• Flow of control

• Return statement

• Parameter passing

• Scope

• 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” );

• 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

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

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 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

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?

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?

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.

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?

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

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

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

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

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()

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

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

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 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 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

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

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

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");

}

}

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

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

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");

}

}

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

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");

}

}

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

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

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 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");

}

}

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

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

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");

}

}

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

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

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 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

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

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")

}

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?

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

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

• Consider again

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

double ratio = Math.cos(10);

int n1 = StandardInput.promptAndReadInt();

int n2 = StandardInput.promptAndReadInt();

What is happening here?

What is happening here?

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?

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

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

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 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;

}

}

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;

}

}

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;

}

}

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;

}

}

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;

}

}

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

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

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;

}

}

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;

}

}

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;

}

}

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;

}

}

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;

}

}

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;

}

}

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

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

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

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?

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!!!!

• What’s going on

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

double ratio =Math.cos(10);

How are 5, 7, and 10 being used?

• 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

Compound interest

Occurs when an account earns intereston both principal and previously awardedinterest

Formula for yearly compound interest total

// 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;

}

}

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;

}

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;

}

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;

}

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

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 );

• 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;

}

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 );

• 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

• 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?