IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).



Similar documents
AP Computer Science Static Methods, Strings, User Input

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Sample CSE8A midterm Multiple Choice (circle one)

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Geometric Series and Annuities

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

LOOPS CHAPTER CHAPTER GOALS

CS 106 Introduction to Computer Science I

JAVA ARRAY EXAMPLE PDF

Introduction to Java. CS 3: Computer Programming in Java

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Programming Fundamentals I CS 110, Central Washington University. November 2015

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Part I. Multiple Choice Questions (2 points each):

Lab 5: Bank Account. Defining objects & classes

Introduction to Java

CS170 Lab 11 Abstract Data Types & Objects

J a v a Quiz (Unit 3, Test 0 Practice)

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Using Two-Dimensional Arrays

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Comp 248 Introduction to Programming

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Chapter 2 Introduction to Java programming

Basics of Java Programming Input and the Scanner class

MAT 2170: Laboratory 3

AP Computer Science Java Mr. Clausen Program 9A, 9B

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Chapter 3. Input and output. 3.1 The System class

Using Files as Input/Output in Java 5.0 Applications

Chapter 2 Elementary Programming

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Discounted Cash Flow Valuation

Arrays in Java. Working with Arrays

Introduction to Programming

Pseudo code Tutorial and Exercises Teacher s Version

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Chapter 7. Process Analysis and Diagramming

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Topic 11 Scanner object, conditional execution

CSE 308. Coding Conventions. Reference

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Example of a Java program

Lecture 1 Introduction to Java

More on Objects and Classes

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

Arrays. Introduction. Chapter 7

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Programmierpraktikum

Iteration CHAPTER 6. Topic Summary

The following program is aiming to extract from a simple text file an analysis of the content such as:

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Building Java Programs

Some Scanner Class Methods

Building Java Programs

Annuities and Sinking Funds

Solving Compound Interest Problems

Problem Solving. Software Engineering. Behavior. OCD in a Nutshell. Behavior. Objects Operations Algorithm. Object-Centered Design

Chapter 3 Mathematics of Finance

This explanations are for absolute beginners. Skilled programmers should (and probably will) use more effective approach.

Interactive Applications (CLI) and Math

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

JavaScript: Control Statements I

CMSC 202H. ArrayList, Multidimensional Arrays

Problem Solving Basics and Computer Programming

Chapter 4: Managing Your Money Lecture notes Math 1030 Section C

D06 PROGRAMMING with JAVA

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

In this Chapter you ll learn:

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

Building Java Programs

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Explain the relationship between a class and an object. Which is general and which is specific?

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

Building Java Programs

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Financial Management Spring 2012

Computer Programming I

D06 PROGRAMMING with JAVA

Install Java Development Kit (JDK) 1.8

CompSci-61B, Data Structures Final Exam

MAT116 Project 2 Chapters 8 & 9

Moving from CS 61A Scheme to CS 61B Java

1.3 Conditionals and Loops

Data Structures Lecture 1

Chapter 1 Java Program Design and Development

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Python for Rookies. Example Examination Paper

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Object-Oriented Programming in Java

Java 進 階 程 式 設 計 03/14~03/21

Transcription:

IRA EXAMPLES This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account). Definite Counting Loop Example IRA After x Years This first example illustrates how to combine the suggestions covered in the topic Help for Beginners. Specifically, we re going to follow the technique How to Invent an Algorithm (version 2) but as we do so, keep an eye out for where we can use the definite counting loop idiom. Pretend you ve won $40,000 in the state lottery. You invest it in an IRA that pays a fixed 5% APR (annual percentage rate) compounded monthly. You want to know what its value will be in 20 years. Write a Java application that finds out. How to Invent an Algorithm (version 2) Step What? How? 1 Understand the problem Use pencil and paper to solve the problem by hand. Use all the problem solving techniques available. Write a list of any questions &or ambiguities you encounter and seek answers. To calculate the amount of interest for one month, multiply the $40,000 by the monthly interest rate, which is 1 12 the annual rate. 5% means 5 out of 100. Therefore, the computation of the first month s interest is: $40,000 5/100/12 = $166.67 Compounded monthly means that you add the month s interest onto the IRA balance so that during the following month you earn interest on the total balance that includes the first month s interest. Thus, the second month s interest is: $40,166.67 5/100/12 = $167.36 IRA Examples Page 1

Continuing, I calculate the interest and balance for several years. Month IRA Balance IRA Balance Multiplied By Interest (before) (after) 1 $40,000.00 5/100/12 $166.67 $40.166.67 2 40.166.67 5/100/12 167.36 40,334.03 3 40,334.03 5/100/12 168.06 40,502.09 4 40,502.09 5/100/12 168.76 40,670.84 At this point, I see that I can get the final answer by doing this computation for 20 12 months (or 20 years). I stopped the pencil and paper computation because if I did it for all 20 years there d be no need to write a Java program. On the other hand, what good is a program that works only for such specific data? For example, what if you can t get 5% APR? So, let s continue algorithm development by opening up the problem. Step What? How? 2 Determine the program s requirements Identify the program s output. Work backwards from the output to determine what input values are necessary to produce it. Determine what computations transform the input into the output. input the starting value of the IRA, the annual percentage rate and the number of years to carry on the calculation calculate the value of the IRA each year for the given number of years at the given APR compounded monthly output the final value of the IRA IRA Examples Page 2

Step What? How? 3 Write down a first-draft algorithm Write in English the steps you employed when solving the problem by hand. Don't worry about language or details; just get your ideas on paper in outline form. Returning to my pencil and paper work in step 1, I now write the steps that I employed: input starting value 40,000, apr of 5% and number of years 20 During month 1, 40,000 5/100/12 = 166.67 new IRA value is 40,000 + 166.67 = 40.166.67 During month 2, 40.166.67 5/100/12 = 167.36 new IRA value is 40.166.67 + 167.36 = 40,334.03 ETC. Step What? How? 4 Refine your algorithm Identify loops. o In your draft outline, look for (1) verbs that imply repetition (e.g. repeat) and (2) repetitive sentences. o Isolate these sentences and wrap them within an appropriate looping statement. IRA Examples Page 3

To identify the loop, I see that steps 2 and 3 in my paper and pencil work are repeated for each month. Step 2 uses the current value of the IRA to calculate the interest and step 3 uses the current value of the IRA and the interest to compute the IRA value to be used in the next loop cycle: input starting value 40,000, apr of 5% and number of years 20 LOOP During month 1, 40,000 5/100/12 = 166.67 used in next cycle IRA's value amount of month's interest new IRA value is 40,000 + 166.67 = 40.166.67 During month 2, 40.166.67 5/100/12 = 167.36 new IRA value is 40.166.67 + 167.36 = 40,334.03 Step What? How? 4 Refine your algorithm Introduce program variables suggested by the nouns in your outline. Noun Value of IRA annual percentage rate number of years to calculate month counter amount of month s interest Variable iravalue apr years month interest IRA Examples Page 4

Step What? How? 4 Refine your algorithm Identify loops. o To determine the loop s truth value, look for variables that change inside the loop and ask what they should be when the loop is to quit. o Make appropriate initializations before the loop. Rewrite the algorithm using pseudo-code. The loop that I identified in the paper and pencil work above is a definite counting loop using month as the loop control variable. Here s the first draft of the pseudo-code. input iravalue, apr, years month = 0; while ( )... month++; print iravalue The loop must quit after the last month of the last year. input iravalue, apr, years month = 0; while ( month < years * 12 )... month++; print iravalue IRA Examples Page 5

Take the repeated statements out of the pencil and paper work shown on page 5, alter the nouns to variable names and use some programming notation to express the calculations and we get this final pseudo-code algorithm: input iravalue, apr, years month = 0; while ( month < years * 12 ) interest = iravalue*apr/100/12; iravalue += interest; month++; print iravalue Algorithm The coded Java application is shown on the next page. IRA Examples Page 6

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 import java.text.decimalformat; import java.util.scanner; import static javax.swing.joptionpane.*; public class IRA1 public static void main( String [] args ) // declare data double iravalue; // current value of the IRA double apr; // annual percentage rate int years; // number of years to calculate // input these values String prompt, input, output; prompt = "Enter initial IRA deposit, APR " + "and number of years to calculate"; input = showinputdialog( prompt ); Scanner in = new Scanner( input ); iravalue = in.nextdouble( ); apr = in.nextdouble( ); years = in.nextint( ); // iterate over the number of years // compounding is monthly int month = 0; while ( month < years*12 ) double interest = iravalue * apr/100./12.; iravalue += interest; month++; // count the passing month // print results DecimalFormat df = new DecimalFormat( "$#,###.00" ); output = "IRA value after " + years + " years is " + df.format( iravalue ); showmessagedialog( null, output ); IRA Examples Page 7

Indefinite Counting Loop Example How Many Years for IRA Suggestion #2 in Help for Beginners advises you, when faced with a programming problem, to look for solutions to similar problems. This example illustrates this technique by solving a variation of the previous example. Having invested $40,000 in an IRA, you want to know how many years it will take to grow it into $1 million. Again, assume 5% APR compounded monthly. This program requires the same calculations as the previous example. The difference is that an indefinite loop is called for that quits when the IRA s value reaches $1 million. We take and edit the algorithm for the previous example: input iravalue, apr, years month = 0; Quit when IRA value reaches $1 million while ( month < years * 12 ) interest = iravalue*apr/100/12; iravalue += interest; month++; print iravalue Calculate number of years as month/12 with extra months being month % 12 Algorithm IRA Examples Page 8

Rewritten into pseudo-code: input iravalue, apr month = 0; while ( iravalue < 1 million ) interest = iravalue*apr/100/12; iravalue += interest; month++; years = month / 12; print years, month % 12 Algorithm The algorithm coded into Java is shown on the next page. IRA Examples Page 9

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 import java.text.decimalformat; import java.util.scanner; import static javax.swing.joptionpane.*; public class IRA2 public static void main( String [] args ) // declare data double iravalue; // current value of the IRA double apr; // annual percentage rate int years; // number of years // input these values String prompt, input, output; prompt = "Enter initial IRA deposit and APR "; input = showinputdialog( prompt ); Scanner in = new Scanner( input ); iravalue = in.nextdouble( ); apr = in.nextdouble( ); // iterate until IRA value reaches $1 million // compounding is monthly int month = 0; while ( iravalue < 1E6 ) double interest = iravalue * apr/100./12.; iravalue += interest; month++; // count the passing month // print results years = month / 12; int extra = month % 12; DecimalFormat df = new DecimalFormat( "$#,###.00" ); output = "IRA value will reach " + df.format( iravalue ) + " after " + years + " and " + extra + "months"; showmessagedialog( null, output ); IRA Examples Page 10

Programming Exercises 1. Write a Java application that estimates and prints the mathematical constant e (equal to 2.71828). You can estimate its value using the formula: 1 1 1 1 e = 1 + + + +... + +... 1! 2! 3! k! Quit looping when the term 1 becomes less than 10 k! -4. 2. Write a Java application that inputs the number of ants in your ant farm and calculates the number of months it will take for the ant population to double if it increases by 2% each month. 3. Strontium-90 decays at a rate of 2.4% per year. Write a Java application to calculate and print its half-life (the number of years it takes to degrade into half its amount). For your output print: Strontium-90 half-life is between x and x+1 years For example, if its half-life is 8.25 years then print 8 for x and 9 for x+1. 4. Flatland has a population of 165,000, 58% of which are Squares and the rest Circles. The number of Squares increases by 1.5% per year; the number of Circles by 2%. Circle leaders want to know how many years it will take Circles to` outnumber Squares so they can win all the elections. Write a Java application that gives them the answer. 5. Pretend that Grandma lives in a nursing home that charges $5,000 per month. She receives $2,000 per month in Social Security payments. The remaining charges must come out of her savings, which amounts to $100,000 invested at 5% APR compounded monthly. When Grandma's savings are reduced to $1,000, Medicaid will take over her nursing home payments. How many years and months will that be? Write a Java application to find out. 6. Suppose you open an IRA with $5,000. The IRA pays a fixed 5% APR compounded monthly. Every year you add $5,000 to it. You want to know how long it will take to grow to $1 million. Write a Java application that finds out and prints the number of years and months. IRA Examples Page 11