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.

72 Chapter 2 Elementary <strong>Programming</strong><br />

monthlyPayment<br />

<strong>to</strong>talPayment<br />

preparing output<br />

30 // Calculate payment<br />

31 double monthlyPayment = loanAmount * monthlyInterestRate / (1<br />

32 – 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));<br />

33 double <strong>to</strong>talPayment = monthlyPayment * numberOfYears * 12;<br />

34<br />

35 // Format <strong>to</strong> keep two digits after the decimal point<br />

36 monthlyPayment = (int)(monthlyPayment * 100) / 100.0;<br />

37 <strong>to</strong>talPayment = (int)(<strong>to</strong>talPayment * 100) / 100.0;<br />

38<br />

39 // Display results<br />

40 String output = "The monthly payment is $" + monthlyPayment +<br />

41 "\nThe <strong>to</strong>tal payment is $" + <strong>to</strong>talPayment;<br />

42 JOptionPane.showMessageDialog(null, output );<br />

43 }<br />

44 }<br />

The showInputDialog method in lines 6–7 displays an input dialog. Enter the interest<br />

rate as a double value and click OK <strong>to</strong> accept the input. The value is returned as a string that<br />

is assigned <strong>to</strong> the String variable annualInterestRateString. The<br />

Double.parseDouble (annualInterestRateString) (line 11) is used <strong>to</strong> convert the<br />

string in<strong>to</strong> a double value.<br />

JOptionPane or Scanner?<br />

✓Point✓ Check<br />

Pedagogical Note<br />

For obtaining input you can use either JOptionPane or Scanner—whichever is<br />

more convenient. For consistency and simplicity, the examples in this book use<br />

Scanner for getting input. You can easily revise the examples using JOptionPane<br />

for getting input.<br />

2.34 Why do you have <strong>to</strong> import JOptionPane but not the Math class?<br />

2.35 How do you prompt the user <strong>to</strong> enter an input using a dialog box?<br />

2.36 How do you convert a string <strong>to</strong> an integer? How do you convert a string <strong>to</strong> a<br />

double?<br />

KEY TERMS<br />

algorithm 34<br />

assignment opera<strong>to</strong>r (=) 42<br />

assignment statement 42<br />

byte type 45<br />

casting 56<br />

char type 62<br />

constant 43<br />

data type 35<br />

declare variables 35<br />

decrement opera<strong>to</strong>r (– –) 54<br />

double type 45<br />

encoding 62<br />

escape character 63<br />

expression 42<br />

final keyword 43<br />

float type 45<br />

floating-point number 35<br />

identifier 40<br />

increment opera<strong>to</strong>r (++) 54<br />

incremental development<br />

and testing 37<br />

int type 45<br />

IPO 39<br />

literal 48<br />

long type 45<br />

narrowing (of types) 56<br />

operands 46<br />

opera<strong>to</strong>r 46<br />

overflow 45<br />

postdecrement 54<br />

postincrement 54<br />

predecrement 54<br />

preincrement 54<br />

primitive data type 35<br />

pseudocode 34<br />

requirements specification 58

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

Saved successfully!

Ooh no, something went wrong!