1 / 36

CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data

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.

Download Presentation

CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1101X: Programming MethodologyRecitation 1 Java Basics Numerical Data

  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 Weber output: ALW

  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

  4. Development Steps • We will develop this program in two steps: • Start with the program template and add code to get input • Add code to compute and display the monogram

  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 #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. 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. 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. 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. “Aaron Ben Cosner” “Ben Cosner” “Aaron” “Cosner” “Ben” “ABC” 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

  10. 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. 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. 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. 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. 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. Overall Plan • Tasks: • Get three input values: loanAmount, interestRate, and loanPeriod. • Compute the monthly and total payments. • Output the results.

  16. Required Classes

  17. Development Steps • We will develop this program in four steps: • Start with code to accept three input values. • Add code to output the results. • Add code to compute the monthly and total payments. • Update or modify code and tie up any loose ends.

  18. Step 1 Design • Call the showInputDialog method to accept three input values: • loan amount, • annual interest rate, • loan period. • Data types are

  19. Step 1 Code (1/2) • 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. 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. 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. Step 2 Design • We will consider the display format for out. • Two possibilities are (among many others)

  23. Step 2 Code (1/3) • Directory: Chapter3/Step2 • Source File: Ch3LoanCalculator.java

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

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

  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

  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.

  28. Step 3 Code (1/3) • Directory: Chapter3/Step3 • Source File: Ch3LoanCalculator.java

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

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

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

  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

  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

  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;

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

  36. End of Recitation #1

More Related