MELJUN CORTES's - Java List of Source Code Application (THIRD HAND-OUTS)
MELJUN CORTES's - Java Numerical Data Types & Expressions
-
Upload
meljun-cortes-mbampa -
Category
Documents
-
view
219 -
download
0
Transcript of MELJUN CORTES's - Java Numerical Data Types & Expressions
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
1/64
MELJUN CORTES MBA MPA BSCS ACSMELJUN CORTES MBA MPA BSCS ACS
Chapter 3
Numerical Data
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
2/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Chapter 3 Objectives
After you have read and studied thischapter, you should be able to
Select proper types for numerical data.
Write arithmetic expressions in Java.
Evaluate arithmetic expressions, using theprecedence rules.
Describe how the memory allocationworks for objects and primitive datavalues.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
3/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Chapter 3 Objectives, cont.
After you have read and studied thischapter, you should be able to
Write mathematical expressions, using
methods in the Math class.
Use the GregorianCalendarclass in
manipulating date information such as year,
month, and day.
Use the DecimalFormat class to format
numerical data.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
4/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Chapter 3 Objectives, cont.
After you have read and studied thischapter, you should be able to Convert input string values to numerical
data.
Apply the incremental developmenttechnique in writing programs.
(Optional) Describe how integers and real
numbers are represented in memory.
Input data by using System.in and outputdata using System.out.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
5/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.1 Variables
To compute the sum and the differenceofx and y in a program, we must first
declare what kind of data will be
assigned to them.
After we assign values to them, we can
compute their sum and difference.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
6/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.1 Variables
When a declaration, such asint x,y;
is made, memory locations to store datavalues forx and y are allocated.
These memory locations are calledvariables, and x and y are the names
we associate with the memorylocations.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
7/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.1 Variables
A variable has three properties: A memory location to store the value.
The type of data stored in the memory
location.
The name used to refer to the memorylocation.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
8/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.1 Variables
The syntax for declaring variables is ;
where is a sequence of
identifiers separated by commas.
Every variable we use in a program
must be declared.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
9/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.1 Variables
There are six numerical data types inJava: byte
short
int
long
float
double
Data types byte, short, int, and longare for integers.
Float and double are for real numbers.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
10/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.1 Variables
At the time a variable is declared, it canalso be initialized.
int count = 10, height = 34;
We assign a value to a variable by
using an assignment statement.
Do not confuse mathematical equalityand assignment. The following is not
valid Java code:
4 + 5 = x;
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
11/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.1 Variables
The only difference between a variablefor numbers and a variable for objects
is the contents in the memory
locations.
For numbers, a variable contains thenumerical value itself.
For objects, a variable contains an
address where the object is stored.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
12/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Fig. 3.1
A diagram showing how two memorylocations (variables) are declared, and
values are assigned to them.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
13/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Fig. 3.2
A difference between object declarationand numerical data declaration.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
14/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.1 Variables
We use the new command to create anobject.
Objects are called reference data types,
because the contents are addressesthat refer to memory locations where
the objects are actually stored.
Numerical data are calledprimitive data
types.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
15/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Fig. 3.3
An effect of assigning the content of onevariable to another.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
16/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.2 Arithmetic Expressions
Arithmetic operators in Java
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
17/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.2 Arithmetic Expressions
An illustration of a subexpression in theexpression x + 3 * y
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
18/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.2 Arithmetic Expressions
Precedence rules for arithmeticoperators and parentheses
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
19/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.2 Arithmetic Expressions
Rules for arithmetic promotion
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
20/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.3 Constants
If we want a variable to remain fixed, weuse a constant.
A constant is declared in a manner similar
to a variable, but with the additionalreserved word final.
final double PI = 3.14159;final int MONTHS_IN_YEAR = 12;
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
21/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.3 Constants
If a literal constant contains a decimalpoint, it is of type double by default.
To designate a literal constant of type
float, append a letter f or F to thenumber:
2 * PI * 345.79F
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
22/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.3 Constants
Numbers in scientific notation, such asNumber x 10exponent
are expressed in Java using the syntax E
12.40e+209
29.009E-102
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
23/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.4 Getting Numerical Input Values
Wrapper classes are used to performnecessary type conversions, such as
converting a String object to a
numerical value.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
24/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.4 Getting Numerical Input Values
/*
Chapter 3 Sample Program: Compute AreaandCircumference
File: Ch3Circle.java
*/
import javax.swing.*;
import java.text.*;
class Ch3Circle {
public static void main( String [] args ) {
finaldouble PI = 3.14159;
String radiusStr;
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
25/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.4 Getting Numerical Input Values
doubleradius,area, circumference;
radiusStr =JOptionPane.showInputDialog(null, "Enterradius:");
radius = Double.parseDouble(radiusStr);
//computeareaand circumferencearea = PI * radius * radius;
circumference = 2.0 * PI * radius;
JOptionPane.showMessageDialog(null, "GivenRadius: " + radius + "\n" + "Area: "+ area + "\n" + "Circumference: " +circumference);
}
}
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
26/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.4 Getting Numerical Input Values
An illustration of how the compilerinterprets an overloaded operator.
int x = 1;
int y = 2;String output = test + x + y;
String output = x + y + test;
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
27/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Fig. 3.4
The dialog that appears when the inputvalue 2.35 was entered into the
Ch3Circle program.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
28/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Fig. 3.5
The result of formatting the outputvalues by using a DecimalFormat
object.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
29/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.5 Standard Output
The showMessageDialog method isintended for displaying short one-line
messages, not for a general-purpose
output mechanism.
Using System.out, we can output
multiple lines of text to the standard
output window.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
30/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Fig. 3.6
The standard output window fordisplaying multiple lines of text.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
31/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.5 Standard Output
We use the print method to output avalue.
The print method will continue printing
from the end of the currently displayedoutput.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
32/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Fig. 3.7
Result of executingSystem.out.print(Hello, Dr.
Caffeine.).
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
33/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Fig. 3.8
Result of sending five print messagesto System.out of Figure 3.7.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
34/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.5 Standard Output
The print method will do the necessarytype conversion if we pass numerical
data.
We can print an argument and skip tothe next line so that subsequent output
will start on the next line by using
println instead ofprint.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
35/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Fig. 3.9
The result of mixing print with fourprintln messages to System.out.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
36/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.6 Standard Input
The technique of using System.in toinput data is called standard input.
We can only input a single byte using
System.in directly.
To input primitive data values, we usethe Scanner class (from Java 2 SDK
1.5).
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
37/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Here's a sample code to input oneinteger and one double:
Scanner scanner;
scanner = Scanner.create(System.in);
int num = scanner.nextInt();
double val = scanner.nextDouble();
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
38/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Method Example
nextByte( ) byte b = scanner.nextByte( );
nextDouble( ) double d = scanner.nextDouble( );nextFloat( ) float f = scanner.nextFloat( );
nextInt( ) int i = scanner.nextInt( );
nextLong( ) long l = scanner.nextLong( );
nextShort( ) short s = scanner.nextShort( );
next() String str = scanner.next();
Common Methods:
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
39/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.7 The Math Class
The Math class in thejava.langpackage contains class methods for
commonly used mathematical
functions.
Table 3.6 contains a partial list of class
methods available in the Math class.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
40/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.8 The GregorianCalendarClass
The GregorianCalendarclass is usefulin manipulating calendar information
such as year, month, and day.
Note that the first month of the year,January, is represented by 0.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
41/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Fig. 3.12
Result of running the Ch3TestCalendarprogram at November 11, 2002, 6:13
p.m.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
42/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
Program flow: Get three input values:
loanAmount, interestRate, and
loanPeriod.
Compute the monthly and totalpayments.
Output the results.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
43/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Fig. 3.13
The object diagram for the programLoanCalculator.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
44/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
Steps of implementation:1. Start with code to accept three input
values.
2. Add code to output the results.
3. Add code to compute the monthlyand total payments.
4. Update or modify code and tie up
any loose ends.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
45/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
Step 1 Development: Input Three DataValues
Call the showInputDialog method to
accept three input values: loan amount,
annual interest rate,
loan period.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
46/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
/*
Chapter 3 Sample Development: Loan Calculator(Step 1)
File: Step1/Ch3LoanCalculator.java
Step 1: Input Data Values
*/
import javax.swing.*;
class Ch3LoanCalculator {
public static void main (String[] args) {
double loanAmount,annualInterestRate;
int loanPeriod;
String inputStr;
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
47/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
//get input values
inputStr = JOptionPane.showInputDialog(null,"Loan Amount (Dollars+Cents):");
loanAmount = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(null,
"Annual Interest Rate (e.g.,9.5):");
annualInterestRate = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(null,
"Loan Period - # of years:");
loanPeriod = Integer.parseInt(inputStr);
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
48/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
//echo print the input values
System.out.println("Loan Amount: $" +loanAmount);
System.out.println("Annual Interest Rate: " +annualInterestRate + "%");
System.out.println("Loan Period (years): " +loanPeriod);
}
}
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
49/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
Step 2 Development: Output Values
We must determine an appropriate
format to display the computed results,
so that the results will becomprehensible to the user.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
50/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
Fig. 3.14
Two display formats: One with inputvalues displayed, and the other with
only the computed values displayed.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
51/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
/*
Chapte
r3 S
ample De
velopment: Lo
an C
alcula
tor(Step2)
File: Step2/Ch3LoanCalculator.java
Step2: Display the Result
*/
import javax.swing.*;
class Ch3LoanCalculator {
public static void main (String[] args) {
double loanAmount,
annu
alInte
restR
ate;
double monthlyPayment,
totalPayment;
int loanPeriod;
String inputStr;
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
52/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
//get input values
inputStr = JOptionPane.showInputDialog(null,"Loan Amount (Dollars+Cents):");
loanAmount = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(null,
"Annual
Interest R
ate (e.g.,9.5):");
annualInterestRate = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(null,"Loan Period - # of years:");
loanPeriod = Integer.parseInt(inputStr);
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
53/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
//compute the monthlyand totalpayments
monthlyPayment = 132.15;totalPayment = 15858.10;
//display theresult
System.out.println("Loan Amount: $" +loanAmount);
System.out.
print
l
n("Annual
Interes
t Ra
te: " +annualInterestRate + "%");
System.out.println("Loan Period (years): " +loanPeriod);
System.out.println("\n");//skip two lines
System.out.println("Monthlypayment is $ " +monthlyPayment);
System.out.println(" TOTAL payment is $ " +totalPayment);
}
}
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
54/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
Step 3 Development: Compute LoanAmount
Complete the program by implementing
the formula derived in the design
phase.
We must convert the annual interest
rate (input value) to a monthly interest
rate (per the formula), and the loanperiod to the number of monthly
payments.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
55/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
/*
Chapte
r3 S
ample De
velopment: Lo
an C
alcula
tor
(Step 3)
File: Step3/Ch3LoanCalculator.java
Step 3: Compute the monthlyand totalpayments
*/
import javax.swing.*;
class Ch3LoanCalculator {
public static void main (String[] args) {
final int MONTHS_IN_YEAR = 12;
double loanAmount,annualInterestRate,monthlyPayment, totalPayment;
double monthlyInterestRate;
int loanPeriod,numberOfPayments;
String inputStr;
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
56/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
//get input values
inputStr = JOptionPane.showInputDialog(null,"Loan Amount(Dollars+Cents):");
loanAmount = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(null,"Annual Interest Rate (e.g.,9.5):");
annualInterestRate=Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(null,"Loan Period - # of years:");
loanPeriod = Integer.parseInt(inputStr);
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
57/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
//compute the monthlyand totalpayments
monthlyInterestRate = annualInterestRate/MONTHS_IN_YEAR / 100;
numberOfPayments = loanPeriod * MONTHS_IN_YEAR;
monthlyPayment =
(loanAmount * monthlyInterestRate) /
(1 - Math.pow(1/(1 + monthlyInterestRate),numberOfPayments ) );
totalPayment = monthlyPayment * numberOfPayments;
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
58/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
//display theresult
System.out.println("Loan Amount: $" +loanAmount);
System.out.println("Annual Interest Rate: " +annualInterestRate + "%");
System.out.println("Loan Period (years): " +loanPeriod);
System.out.println("\n");//skip two lines
System.out.println("Monthlypayment is $ "
+ monthlyPayment);
System.out.println(" TOTAL payment is $ "
+ totalPayment);
}
}
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
59/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
Step 4 Development: Finishing Up
Finalize the program by making
necessary modifications or additions.
We will add a program description and
format the monthly and total payments
to two decimal places.
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
60/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
/*
Chapter 3 Sample Development: Loan Calculator (Step 4)
File: Step4/Ch3LoanCalculator.java
Step 4: Finalize theprogram
*/
import javax.swing.*;
import java.text.*;
class Ch3LoanCalculator {
public static void main (String[] args) {
final int MONTHS_IN_YEAR = 12;
double loanAmount,annualInterestRate;
d
ouble
monthlyPayment,tot
al
Payment;
double monthlyInterestRate;
int loanPeriod, numberOfPayments;
String inputStr;
DecimalFormatdf = new Decim
alFormat("0.00");
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
61/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
//describe theprogram
System.out.println("Thisprogram computes themonthlyand total");
System.out.println("payments fora given loanamount,annual ");
System.out.println("interest rate,andloan
period.");
System.out.println("Loan amount in dollarsandcents,e.g., 12345.50");
System.out.println("Annual interest rate inper
centa
ge,e.g.
,12.75")
;
System.out.println("Loan period in number ofyears,e.g., 15");
System.out.println("\n");//skip two lines
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
62/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
//get input values
inputStr = JOptionPane.showInputDialog(null,"Loan Amount(Dollars+Cents):");
loanAmount = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(null,
"Annual
Interest R
a
te (e.g.,9.5):");
annualInterestRate = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(null,
"Loan Period - # of years:");
loanPeriod = Integer.parseInt(inputStr);
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
63/64
MELJUN CORTES,MBA,MPA,BSCS,ACSMELJUN CORTES,MBA,MPA,BSCS,ACS
3.9 Sample Development: Loan Calculator
//compute the monthlyand totalpayments
monthlyInterestRate = annualInterestRate/MONTHS_IN_YEAR / 100;
numberOfPayments = loanPeriod * MONTHS_IN_YEAR;
monthlyPayment =
(loanAmount * monthlyInterestRate) /
(1 - Math.pow(1/(1 + monthlyInterestRate),numberOfPayments ) );
totalPayment = monthlyPayment * numberOfPayments;
-
8/9/2019 MELJUN CORTES's - Java Numerical Data Types & Expressions
64/64