Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1101X: Programming Methodology Recitation 2 Classes.

Similar presentations


Presentation on theme: "CS1101X: Programming Methodology Recitation 2 Classes."— Presentation transcript:

1 CS1101X: Programming Methodology Recitation 2 Classes

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

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

4 CS1101X Recitation #24 Required Classes LoanCalculator Loan JOptionPane PrintStream inputcomputationoutput

5 CS1101X Recitation #25 Development Steps We will develop this program in five steps: 1.Start with the main class LoanCalculator. Define a temporary placeholder Loan class. 2.Implement the input routine to accept three input values. 3.Implement the output routine to display the results. 4.Implement the computation routine to compute the monthly and total payments. 5.Finalize the program.

6 CS1101X Recitation #26 Step 1: Design The methods of the LoanCalculator class MethodVisibilityPurpose startpublicStarts the loan calcution. Calls other methods computePaymentprivateGive three parameters, compute the monthly and total payments describeProgramprivateDisplays a short description of a program displayOutputprivateDisplays the output getInputprivateGets three input values

7 CS1101X Recitation #27 Step 1: Code (1/5) Directory: Chapter4/Step1 Source Files: LoanCalculator.java Loan.java Directory: Chapter4/Step1 Source Files: LoanCalculator.java Loan.java

8 CS1101X Recitation #28 Step 1: Code (2/5) // Loan Class (Step 1) /* Chapter 4 Sample Development: Loan Calculation (Step 1) File: Step1/Loan.java This class handles the loan computation. */ class Loan { //---------------------------------- // Data Members //---------------------------------- // Default contructor. public Loan( ) { }

9 CS1101X Recitation #29 Step 1: Code (3/5) /* Chapter 4 Sample Development: Loan Calculation (Step 1) File: Step1/LoanCalculator.java This class controls the input, computation, and output of loan */ class LoanCalculator { // Data Members // This object does the actual loan computation private Loan loan; //---------------------------------- // Main Method //---------------------------------- public static void main(String[] arg) { LoanCalculator calculator = new LoanCalculator(); calculator.start(); }

10 CS1101X Recitation #210 Step 1: Code (4/5) //---------------------------------- // Constructors //---------------------------------- public LoanCalculator() { loan = new Loan(); } // Public Methods: void start ( ) // Top-level method that calls other private methods public void start() { describeProgram(); //tell what the program does getInput(); //get three input values computePayment(); //compute the monthly payment and total displayOutput(); //diaply the results }

11 CS1101X Recitation #211 Step 1: Code (5/5) // Private Methods: // Computes the monthly and total loan payments. private void computePayment() { System.out.println("inside computePayment"); //TEMP } // Provides a brief explaination of the program to the user. private void describeProgram() { System.out.println("inside describeProgram"); //TEMP } //Display the input values and monthly and total payments. private void displayOutput() { System.out.println("inside displayOutput"); //TEMP } // Gets three input values--loan amount, interest rate, and // loan period--using an InputBox object private void getInput() { System.out.println("inside getInput"); //TEMP }

12 CS1101X Recitation #212 Step 1: Test In the testing phase, we run the program multiple times and verify that we get the following output inside describeProgram inside getInput inside computePayment inside displayOutput

13 CS1101X Recitation #213 Step 2: Design Design the input routines  LoanCalculator will handle the user interaction of prompting and getting three input values  LoanCalculator calls the setAmount, setRate and setPeriod of a Loan object.

14 CS1101X Recitation #214 Step 2: Code (1/5) // Loan Class (Step 2) class Loan { // Data Members // Constant for the number of months in a year private final int MONTHS_IN_YEAR = 12; // The amount of the loan private double loanAmount; // The monthly interest rate private double monthlyInterestRate; // The number of monthly payments private int numberOfPayments;

15 CS1101X Recitation #215 Step 2: Code (2/5) //Creates a new Loan object with passed values. public Loan(double amount, double rate, int period) { setAmount(amount); setRate (rate ); setPeriod(period); } //Returns the loan amount. public double getAmount( ) { return loanAmount; } //Returns the loan period in the number of years. public int getPeriod( ) { return numberOfPayments / MONTHS_IN_YEAR; }

16 CS1101X Recitation #216 Step 2: Code (3/5) //Returns the annual interest rate. public double getRate( ) { return monthlyInterestRate * 100.0 * MONTHS_IN_YEAR; } //Sets the loan amount of this loan. public void setAmount(double amount) { loanAmount = amount; } //Sets the interest rate of this loan. public void setRate(double annualRate) { monthlyInterestRate=annualRate/100.0/MONTHS_IN_YEAR; } //Sets the loan period of this loan. public void setPeriod(int periodInYears) { numberOfPayments = periodInYears * MONTHS_IN_YEAR; }

17 CS1101X Recitation #217 Step 2: Code (4/5) // File: Step2/LoanCalculator.java // Gets three input values--loan amount, interest rate, // and loan period private void getInput() { double loanAmount, annualInterestRate; int loanPeriod; String inputStr; 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);

18 CS1101X Recitation #218 Step 2: Code (5/5) //create a new loan with the input values loan = new Loan(loanAmount, annualInterestRate, loanPeriod); //TEMP System.out.println("Loan Amount: $" + loan.getAmount()); System.out.println("Annual Interest Rate:" + loan.getRate() + "%"); System.out.println("Loan Period (years):" + loan.getPeriod()); }

19 CS1101X Recitation #219 Step 2: Test System.out.println("Loan Amount: $" + loan.getAmount()); System.out.println("Annual Interest Rate:" + loan.getRate() + "%"); System.out.println("Loan Period (years):" + loan.getPeriod()); We run the program numerous times with different input values Check the correctness of input values by echo printing

20 CS1101X Recitation #220 Step 3 Design We will implement the displayOutput method. We will reuse the same design we adopted in Chapter 3 sample development.

21 CS1101X Recitation #221 Step 3: Code (1/2) // Loan Class (Step 3) class Loan { //Returns the monthly payment public double getMonthlyPayment( ) { return 132.15; //TEMP } //Returns the total payment public double getTotalPayment( ) { return 15858.10; //TEMP };

22 CS1101X Recitation #222 Step 3: Code (2/2) // File: Step3/LoanCalculator.java // Gets three input values--loan amount, interest rate, // and loan period // Displays the input values and monthly and total payments. private void displayOutput() { System.out.println("Loan Amount: $" + loan.getAmount()); System.out.println("Annual Interest Rate:" + loan.getRate() + "%"); System.out.println("Loan Period (years): " + loan.getPeriod()); System.out.println("Monthly payment is $ " + loan.getMonthlyPayment()); System.out.println(" TOTAL payment is $ " + loan.getTotalPayment()); }

23 CS1101X Recitation #223 Step 3 Test We run the program numerous times with different input values and check the output display format. Adjust the formatting as appropriate

24 CS1101X Recitation #224 Step 4 Design Two methods getMonthlyPayment and getTotalPayment are defined for the Loan class We will implement them so that they work independent of each other. It is considered a poor design if the clients must call getMonthlyPayment before calling getTotalPayment.

25 CS1101X Recitation #225 Step 4: Code (1/2) // Loan Class (Step 4) class Loan { //Returns the monthly payment public double getMonthlyPayment( ) { double monthlyPayment; monthlyPayment = (loanAmount * monthlyInterestRate) / (1 - Math.pow(1/(1 + monthlyInterestRate), numberOfPayments ) ); return monthlyPayment; }

26 CS1101X Recitation #226 Step 4: Code (2/2) //Returns the total payment public double getTotalPayment( ) { double totalPayment; totalPayment = getMonthlyPayment( ) * numberOfPayments; return totalPayment; }

27 CS1101X Recitation #227 Step 4 Test We run the program numerous times with different types of input values and check the results.

28 CS1101X Recitation #228 Step 5 Finalize We will implement the describeProgram method We will format the monthly and total payments to two decimal places using DecimalFormat. Directory: Chapter4/Step5 Source Files (final version): LoanCalculator.java Loan.java

29 CS1101X Recitation #229 Step 5: Code (1/2) // File: Step5/LoanCalculator.java // Gets three input values--loan amount, interest rate, // and loan period // Provides a brief explaination of the program to the user. private void describeProgram() { 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 (# of years)."); System.out.println("\n"); }

30 CS1101X Recitation #230 Step 5: Code (2/2) //Displays the input values and monthly and total payments. private void displayOutput() { DecimalFormat df = new DecimalFormat("0.00"); System.out.println("Loan Amount: $" + loan.getAmount()); System.out.println("Annual Interest Rate:" + loan.getRate() + "%"); System.out.println("Loan Period (years): " + loan.getPeriod()); System.out.println("Monthly payment is $ " + df.format(loan.getMonthlyPayment())); System.out.println(" TOTAL payment is $ " + df.format(loan.getTotalPayment())); }

31 CS1101X Recitation #231 End of Recitation #2


Download ppt "CS1101X: Programming Methodology Recitation 2 Classes."

Similar presentations


Ads by Google