CS1101X: Programming Methodology Recitation 2 Classes.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Advertisements

Reading Input from the Console eval function. variable = input("Enter a value: ") The value entered is a string. You can use the function eval to evaluate.
Discussion 3. Questions? Term test? Lectures? Labs?
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Sample Development Loan Calculator.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Odds and Ends  Formatted Output  Random numbers.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes Part 1 Animated.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: *Sample Development Loan Calculator.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter Chapter 4 Defining Your Own Classes Part 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 3b Standard Input and Output Sample Development.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Sample Development Revisiting the Loan.
Chapter Four Defining Your Own Classes continued.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Computer Programming Lab(5).
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Fundamental concepts in Java. Lesson plan Variable declaration, assign statement & practice Design document & practice.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 4 Defining Your Own Classes Part 1 Animated Version.
BCS 2143 Object Oriented Programming Defining Your Own Classes (Part 1)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3 Numerical Data.
CS1101X: Programming Methodology Recitation 3 Control Structures.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data.
Discussion 5. Lab 3 Not really using rectangle class Return type of getPoint is String instead of Point You are not able to retrieve the point if you.
Static Class Methods CSIS 3701: Advanced Object Oriented Programming.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Writing JavaScript Functions. Goals By the end of this unit, you should understand … How to breakdown applications into individual, re-usable modules.
Discussion 4. Labs public MyPoint(double xInit, double yInit ) { MyPoint p = new MyPoint(0, 0); } ClassProblem.java recursive java.lang.StackOverflowError.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
CS1101X: Programming Methodology Recitation 4 Design Issues and Problem Solving.
Programming Fundamentals I (COSC- 1336), Lecture 10 (prepared after Chapter 10 of Liang’s 2011 textbook) Stefan Andrei 12/18/20151 COSC-1336, Lecture 10.
CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
COMPSCI 172 – Introduction to Objected- Oriented programming in Java Class hour: 12:05-12:55 pm MWF (section 1). 1:10-2:00 pm MWF (section 2) McGraw Room.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Building java programs, chapter 3 Parameters, Methods and Objects.
CSCI 1100/1202 January 23, Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Classes - Intermediate
Lab 04-2 Objectives:  Understand what a file is.  Learn how to use the File class  Learn how to use the Scanner class to read from files  Learn about.
Intro to OOP with Java, C. Thomas Wu Numerical Data
Value-Returning Functions
Interactive Standard Input/output
Data types, Expressions and assignment, Input from User
Chapter 4 Defining Instantiable Classes
Defining Your Own Classes
Chapter 3 Numerical Data
Chapter 6 Methods.
JOptionPane class.
Methods/Functions.
Presentation transcript:

CS1101X: Programming Methodology Recitation 2 Classes

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.

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

CS1101X Recitation #24 Required Classes LoanCalculator Loan JOptionPane PrintStream inputcomputationoutput

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.

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

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

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( ) { }

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

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 }

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 }

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

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.

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;

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

CS1101X Recitation #216 Step 2: Code (3/5) //Returns the annual interest rate. public double getRate( ) { return monthlyInterestRate * * 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; }

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

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

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

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.

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

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

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

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.

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

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

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

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

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

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

CS1101X Recitation #231 End of Recitation #2