CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data

36
CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data

description

CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data. Problem Statement. Problem statement: Write a program that asks for the user’s first, middle, and last names and replies with their initials. Example: input: Andrew Lloyd Weber output: ALW. Overall Plan. - PowerPoint PPT Presentation

Transcript of CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data

CS1101X: Programming Methodology

Recitation 1 Java Basics

Numerical Data

CS1101X Recitation #1 2

Problem Statement

Problem statement:

Write a program that asks for the user’s first, middle, and last names and replies with their initials.

Example:

input: Andrew Lloyd Weberoutput: ALW

CS1101X Recitation #1 3

Overall Plan

Identify the major tasks the program has to perform.

We need to know what to develop before we develop!

Tasks: Get the user’s first, middle, and last names Extract the initials and create the monogram Output the monogram

CS1101X Recitation #1 4

Development Steps

We will develop this program in two steps:

1. Start with the program template and add code to get input

2. Add code to compute and display the monogram

CS1101X Recitation #1 5

Step 1: Design

The program specification states “get the user’s name” but doesn’t say how.

We will consider “how” in the Step 1 design We will use JOptionPane for input Input Style Choice #1

Input first, middle, and last names separately

Input Style Choice #2Input the full name at once

We choose Style #2 because it is easier and quicker for the user to enter the information

CS1101X Recitation #1 6

Step 1: Code

/*Chapter 2 Sample Program: Displays the MonogramFile: Step1/Ch2Monogram.java

*/import javax.swing.*;

class Ch2Monogram {public static void main (String[ ] args) {

String name;

name = JOptionPane.showInputDialog(null, "Enter your full name (first, middle, last):");

JOptionPane.showMessageDialog(null, name);}

}

CS1101X Recitation #1 7

Step 1: Test

In the testing phase, we run the program and verify that we can enter the name the name we enter is displayed correctly

CS1101X Recitation #1 8

Step 2: Design (1/2)

Our programming skills are limited, so we will make the following assumptions: input string contains first, middle, and last

names first, middle, and last names are separated

by single blank spaces Examples

John Quincy Adams (okay)

John Kennedy (not okay)

Harrison, William Henry (not okay)

CS1101X Recitation #1 9

Step 2: Design (2/2)

Given the valid input, we can compute the monogram by breaking the input name into first, middle, and last extracting the first character from them concatenating three first characters

“Aaron Ben Cosner”

“Aaron” “Ben Cosner”

“Ben” “Cosner”

“ABC”

CS1101X Recitation #1 10

Step 2: Code (1/2)

/*Chapter 2 Sample Program: Displays the MonogramFile: Step 2/Ch2MonogramStep2.java

*/import javax.swing.*;

class Ch2Monogram {

public static void main (String[ ] args) {String name, first, middle, last,

space, monogram;

space = " ";

//Input the full namename = JOptionPane.showInputDialog(null, "Enter your full name (first, middle,

last):" );

CS1101X Recitation #1 11

Step 2: Code (2/2)

//Extract first, middle, and last namesfirst = name.substring(0, name.indexOf(space));name = name.substring(name.indexOf(space)+1,

name.length());

middle = name.substring(0, name.indexOf(space));last = name.substring(name.indexOf(space)+1,

name.length());

//Compute the monogrammonogram = first.substring(0, 1) +

middle.substring(0, 1) + last.substring(0,1);

//Output the resultJOptionPane.showMessageDialog(null,

"Your monogram is " + monogram);}

}

CS1101X Recitation #1 12

Step 2: Test

In the testing phase, we run the program and verify that, for all valid input values, correct monograms are displayed.

We run the program numerous times. Seeing one correct answer is not enough. We have to try out many different types of (valid) input values.

CS1101X Recitation #1 13

Program Review

The work of a programmer is not done yet. Once the working program is developed, we

perform a critical review and see if there are any missing features or possible improvements

One suggestion Improve the initial prompt so the user knows the

valid input format requires single spaces between the first, middle, and last names

CS1101X Recitation #1 14

Problem Statement

Problem statement:

Write a loan calculator program that computes both monthly and total payments for a given loan amount, annual interest rate, and loan period.

CS1101X Recitation #1 15

Overall Plan

Tasks: Get three input values: loanAmount, interestRate,

and loanPeriod. Compute the monthly and total payments. Output the results.

CS1101X Recitation #1 16

Required Classes

CS1101X Recitation #1 17

Development Steps

We will develop this program in four steps:

1. Start with code to accept three input values.

2. Add code to output the results.

3. Add code to compute the monthly and total payments.

4. Update or modify code and tie up any loose ends.

CS1101X Recitation #1 18

Step 1 Design

Call the showInputDialog method to accept three input values: loan amount, annual interest rate, loan period.

Data types areInput Format Data Type

loan amount dollars and cents double

annual interest rate in percent (e.g.,12.5)

double

loan period in years int

CS1101X Recitation #1 19

Step 1 Code (1/2)

Directory: Chapter3/Step1

Source File: Ch3LoanCalculator.java

Directory: Chapter3/Step1

Source File: Ch3LoanCalculator.java

Program source file is too big to list here. From now on, we askyou to view the source files using your Java IDE.

CS1101X Recitation #1 20

Step 1 Code (2/2)import javax.swing.*;

class Ch3LoanCalculator {

public static void main (String[] args) { double loanAmount, annualInterestRate; int loanPeriod; String inputStr;

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

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

CS1101X Recitation #1 21

Step 1 Test

In the testing phase, we run the program multiple times and verify that we can enter three input values we see the entered values echo-printed correctly

on the standard output window

CS1101X Recitation #1 22

Step 2 Design

We will consider the display format for out. Two possibilities are (among many others)

CS1101X Recitation #1 23

Step 2 Code (1/3)

Directory: Chapter3/Step2

Source File: Ch3LoanCalculator.java

Directory: Chapter3/Step2

Source File: Ch3LoanCalculator.java

CS1101X Recitation #1 24

Step 2 Code (2/3)import javax.swing.*;

class Ch3LoanCalculator {

public static void main (String[] args) { double loanAmount, annualInterestRate; double monthlyPayment, totalPayment; int loanPeriod; String inputStr;

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

CS1101X Recitation #1 25

Step 2 Code (3/3)

//compute the monthly and total payments monthlyPayment = 132.15; totalPayment = 15858.10;

//display the result 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("Monthly payment is $ " + monthlyPayment); System.out.println(" TOTAL payment is $ " + totalPayment); }

}

CS1101X Recitation #1 26

Step 2 Test

We run the program numerous times with different types of input values and check the output display format.

Adjust the formatting as appropriate

CS1101X Recitation #1 27

Step 3 Design

The formula to compute the geometric progression is the one we can use to compute the monthly payment.

The formula requires the loan period in months and interest rate as monthly interest rate.

So we must convert the annual interest rate (input value) to a monthly interest rate (per the formula), and the loan period to the number of monthly payments.

CS1101X Recitation #1 28

Step 3 Code (1/3)

Directory: Chapter3/Step3

Source File: Ch3LoanCalculator.java

Directory: Chapter3/Step3

Source File: Ch3LoanCalculator.java

CS1101X Recitation #1 29

Step 3 Code (2/3)

import javax.swing.*;

class Ch3LoanCalculator {

public static void main (String[] args) { final int MONTHS_IN_YEAR = 12; double loanAmount, annualInterestRate; double monthlyPayment, totalPayment, monthlyInterestRate; int loanPeriod, numberOfPayments; String inputStr;

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

CS1101X Recitation #1 30

Step 3 Code (3/3)

//compute the monthly and total payments monthlyInterestRate = annualInterestRate / MONTHS_IN_YEAR / 100; numberOfPayments = loanPeriod * MONTHS_IN_YEAR;

monthlyPayment = (loanAmount * monthlyInterestRate) / (1 - Math.pow(1/(1 + monthlyInterestRate), numberOfPayments ) );

totalPayment = monthlyPayment * numberOfPayments;

//display the result 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("Monthly payment is $ " + monthlyPayment); System.out.println(" TOTAL payment is $ " + totalPayment); }

}

CS1101X Recitation #1 31

Step 3 Test

We run the program numerous times with different types of input values and check the results.

CS1101X Recitation #1 32

Step 4 Finalize

We will add a program description We will format the monthly and total payments

to two decimal places using DecimalFormat.

Directory: Chapter3/Step4

Source File: Ch3LoanCalculator.java

CS1101X Recitation #1 33

Step 4 Code (1/3)import javax.swing.*;import java.text.*;

class Ch3LoanCalculator {

public static void main (String[] args) { final int MONTHS_IN_YEAR = 12; double loanAmount, annualInterestRate, monthlyPayment, totalPayment, monthlyInterestRate; int loanPeriod, numberOfPayments; String inputStr;

DecimalFormat df = new DecimalFormat("0.00");

//describe the program System.out.println("This program computes the monthly and total"); System.out.println("payments for a given loan amount, annual "); System.out.println("interest rate, and loan period."); System.out.println("Loan amount in dollars and cents, e.g., 12345.50"); System.out.println("Annual interest rate in percentage, e.g., 12.75"); System.out.println("Loan period in number of years, e.g., 15"); System.out.println("\n"); //skip two lines

CS1101X Recitation #1 34

Step 4 Code (2/3) //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);

//compute the monthly and total payments monthlyInterestRate = annualInterestRate / MONTHS_IN_YEAR / 100; numberOfPayments = loanPeriod * MONTHS_IN_YEAR;

monthlyPayment = (loanAmount * monthlyInterestRate) / (1 - Math.pow(1/(1 + monthlyInterestRate), numberOfPayments ) );

totalPayment = monthlyPayment * numberOfPayments;

CS1101X Recitation #1 35

Step 4 Code (3/3)

//display the result 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("Monthly payment is $ " + df.format(monthlyPayment)); System.out.println(" TOTAL payment is $ " + df.format(totalPayment)); }

}

CS1101X Recitation #1 36

End of Recitation #1