Results 1 to 3 of 3
  1. #1
    SamJava_the_Hut is offline Senior Member
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    100
    Rep Power
    0

    Default How to detect when 12 months have passed by

    I have another simple programming assignment. I need to calculate accumulated interest, but I also need to keep track of how many times a 12-month-period passes by.

    Here's the code so far:
    Java Code:
    package com.sam;
    
    import java.util.Scanner;
    
    public class MainMethodPlayground {
    	
    	private static final String correctMain = "The main method of the MainMethodPlayground class is being used.\n"; 
    	
    	public static void main(String[] args) {
    		System.out.println(correctMain);
    		
    		Scanner intInput = new Scanner(System.in);
    		Scanner dblInput = new Scanner(System.in);
    		/*
    		13. Develop a program that accepts an initial amount of money (called the principal), a simple annual 
    		interest rate, and a number of months will compute the balance at the end of that time. Assume that no
    		additional deposits or withdrawals are made and that a month is 1/12 of a year. Total interest is the
    		product of the principal, the annual interest rate expressed as a decimal, and the number of years.
    		*/
    		
    		System.out.println("Enter an initial amount of money:");
    		double principal = dblInput.nextDouble();
    		System.out.println("Enter an annual interest rate:");
    		double interestRate = dblInput.nextDouble();
    		System.out.println("Enter number of months to accumulate interest:");
    		int numMonths = intInput.nextInt();
    		double years = 0;
    		double sumOfInterest = 0;
    		for(int i = 0; i < numMonths; i++) {
    			sumOfInterest += (interestRate*principal);
    			if(i == 12) {
    				System.out.println("Interest has accumulated for another full year.");
    			}
    		}
    		System.out.println("After " + years + " years, we have accumulated $" + sumOfInterest);		
    	}
    }
    Before I can test it, I need to figure out how to dynamically keep track of how many times 12 months have gone by in the if statement of my for loop on line 31. The problem with the way it currently is is that I might reach 24 months, 36 months, and so on.

    1/12 = 0.0833333333333333 doesn't look very clean, and it wouldn't look proper to print out something like:
    "After 1.0833333333333333 years the interest is..."//13 months
    "After 1..1666666666666666‬ years, the interest is..."//14 months

    Isn't there a better way involving some kind of trick with modulus, where you see if the quotient of a fraction has a remainder of 0, which we could use to detect when i has incremented a number divisible by 12 or something? I can't remember how that kind of trick works and I'm not sure how to explain it well enough to figure out what to google search for. Any ideas?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to detect when 12 months have passed by

    some kind of trick with modulus,
    Try writing a test program with a loop that prints out the modulus value to see how it works:
    Java Code:
       System.out.println("aNumber="+ aNumber + " % anotherNumber= " + anotherNumber + " = "+ (aNumber % anotherNumber));
    Can you post an example of some sample input and the desired output?
    Last edited by Norm; 08-09-2019 at 02:47 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SamJava_the_Hut is offline Senior Member
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    100
    Rep Power
    0

    Default Re: How to detect when 12 months have passed by

    I figured it out:

    Java Code:
    System.out.println("Enter an initial amount of money:");
    		double principal = dblInput.nextDouble();
    		System.out.println("Enter an annual interest rate:");
    		double interestRate = dblInput.nextDouble();
    		System.out.println("Enter number of months to accumulate interest:");
    		int numMonths = intInput.nextInt();
    		int i = 0;
    		int years = 0;
    		double sumOfInterest = 0;
    		for(i = 0; i < numMonths; i++) {
    			sumOfInterest += (interestRate*principal);
    			if(i % 12 == 0) {
    				years++;
    			}
    		}
    		if(numMonths % 12 == 0) {//if n years and 0 months have gone by
    			System.out.println("After " + years + " years, we have accumulated $" + sumOfInterest + " of interest.");
    		}
    		else if((numMonths > 12) && (numMonths % 12 != 0)) {//if n years and n months have gone by
    			System.out.println("After " + years + " years and " + (numMonths % 12) + " months, we have accumulated $" 
    								+ sumOfInterest + " of interest.");			
    		}
    		else if(numMonths < 12){//if less than a year has gone by
    			System.out.println("After " + numMonths + " months, we have accumulated $" + sumOfInterest);
    		}

Similar Threads

  1. Replies: 2
    Last Post: 12-29-2010, 06:30 AM
  2. entities are passed by value or passed by reference
    By syntrax in forum New To Java
    Replies: 1
    Last Post: 12-17-2009, 07:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •