19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

178 Chapter 5 Methods<br />

problem<br />

why methods?<br />

define sum method<br />

main method<br />

invoke sum<br />

method<br />

Key<br />

Point<br />

Key<br />

Point<br />

5.1 <strong>Introduction</strong><br />

Methods can be used <strong>to</strong> define reusable code and organize and simplify code.<br />

Suppose that you need <strong>to</strong> find the sum of integers from 1 <strong>to</strong> 10, from 20 <strong>to</strong> 37, and from 35<br />

<strong>to</strong> 49, respectively. You may write the code as follows:<br />

int sum = 0;<br />

for (int i = 1; i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!