Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data.

Similar presentations


Presentation on theme: "CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data."— Presentation transcript:

1 CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data

2 CS1101X Recitation #12 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

3 CS1101X Recitation #13 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

4 CS1101X Recitation #14 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

5 CS1101X Recitation #15 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 #2 Input the full name at once We choose Style #2 because it is easier and quicker for the user to enter the information

6 CS1101X Recitation #16 Step 1: Code /* Chapter 2 Sample Program: Displays the Monogram File: 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); }

7 CS1101X Recitation #17 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

8 CS1101X Recitation #18 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)

9 CS1101X Recitation #19 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”

10 CS1101X Recitation #110 Step 2: Code (1/2) /* Chapter 2 Sample Program: Displays the Monogram File: 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 name name = JOptionPane.showInputDialog(null, "Enter your full name (first, middle, last):" );

11 CS1101X Recitation #111 Step 2: Code (2/2) //Extract first, middle, and last names first = 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 monogram monogram = first.substring(0, 1) + middle.substring(0, 1) + last.substring(0,1); //Output the result JOptionPane.showMessageDialog(null, "Your monogram is " + monogram); }

12 CS1101X Recitation #112 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.

13 CS1101X Recitation #113 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

14 CS1101X Recitation #114 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.

15 CS1101X Recitation #115 Overall Plan Tasks:  Get three input values: loanAmount, interestRate, and loanPeriod.  Compute the monthly and total payments.  Output the results.

16 CS1101X Recitation #116 Required Classes

17 CS1101X Recitation #117 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.

18 CS1101X Recitation #118 Step 1 Design Call the showInputDialog method to accept three input values:  loan amount,  annual interest rate,  loan period. Data types are InputFormatData Type loan amountdollars and centsdouble annual interest ratein percent (e.g.,12.5) double loan periodin yearsint

19 CS1101X Recitation #119 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 ask you to view the source files using your Java IDE.

20 CS1101X Recitation #120 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); }

21 CS1101X Recitation #121 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

22 CS1101X Recitation #122 Step 2 Design We will consider the display format for out. Two possibilities are (among many others)

23 CS1101X Recitation #123 Step 2 Code (1/3) Directory: Chapter3/Step2 Source File: Ch3LoanCalculator.java Directory: Chapter3/Step2 Source File: Ch3LoanCalculator.java

24 CS1101X Recitation #124 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);

25 CS1101X Recitation #125 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); }

26 CS1101X Recitation #126 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

27 CS1101X Recitation #127 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.

28 CS1101X Recitation #128 Step 3 Code (1/3) Directory: Chapter3/Step3 Source File: Ch3LoanCalculator.java Directory: Chapter3/Step3 Source File: Ch3LoanCalculator.java

29 CS1101X Recitation #129 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);

30 CS1101X Recitation #130 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); }

31 CS1101X Recitation #131 Step 3 Test We run the program numerous times with different types of input values and check the results.

32 CS1101X Recitation #132 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

33 CS1101X Recitation #133 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

34 CS1101X Recitation #134 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;

35 CS1101X Recitation #135 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)); }

36 CS1101X Recitation #136 End of Recitation #1


Download ppt "CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data."

Similar presentations


Ads by Google