-1
import java.io.*;
import java.util.*;

public class DonaldsonDuaneMidtermActivity3A {
    public static void main (String[] args) {
        Scanner keyboard = new Scanner(System.in);
        float annualRate = 0.0F;
        float quarterlyRate = 0.0F;
        double principal = 0.0;
        double interest = 0.0;
        double finalAmount = 0.0;
        byte quarter = 0;
        int year = 0;
        annualRate = 0.05F;                                                             
        System.out.print("Enter the year: ");
        year = Integer.parseInt(keyboard.nextLine());                                   
        System.out.print("Enter the initial principal: ");
        principal = Double.parseDouble(keyboard.nextLine());                            
        System.out.printf("%s%.2f%n", "Principal = ", principal);                       
        System.out.printf("%s%.2f%c%n", "Interest Rate = ", annualRate * 100, '%');
        System.out.printf("%6s%8s%16s%30s%n", "Year", "Quarter", "Interest Earned", >"Amount at end of quarter");
        quarterlyRate = annualRate / 4;                                                 
        quarter = 1;
        interest = principal * quarterlyRate;                                           
        finalAmount = principal + interest;
        System.out.printf("%6s%8d%16.2f%30.2f%n", year, quarter, interest, finalAmount);
        principal = finalAmount;
        quarter = 2;                                                                    
        interest = principal * quarterlyRate;                                           
        finalAmount = principal + interest;
        System.out.printf("%6s%8d%16.2f%30.2f%n", year, quarter, interest, finalAmount);

        principal = finalAmount;
        quarter = 3;                                                                    
        interest = principal * quarterlyRate;                                           
        finalAmount = principal + interest;                                             
        System.out.printf("%6s%8d%16.2f%30.2f%n", year, quarter, interest, finalAmount);

        principal = finalAmount;                                                        
        quarter = 4;                                                                    
        interest = principal * quarterlyRate;                                           
        finalAmount = principal + interest;                                             
        System.out.printf("%6s%8d%16.2f%30.2f%n", year, quarter, interest, finalAmount);

        System.exit(0);`enter code here`                                                                
    }
}

First post, please be kind, I have searched for similar programs but did not see any like this one. I will assume that I need to put more in the for loop than just my 4 different quarters to iterate through them. I also need to output something to the screen which will be exactly the same as the program without the for loop. I have played with it some and cant get everything to print.out as required. Thanks for helping an old man try something new. I am also using JCreator so I get everything in the output window at the lower area of the software.

quarterlyRate = annualRate / 4;

    for (quarter = 1; quarter <= 4; quarter = quarter + 1) {

    interest = principal * quarterlyRate;

    finalAmount = principal + interest;                                             // comment out if uncomment next two lines
    System.out.printf("%6s%8d%16.2f%30.2f%n", year, quarter, interest, finalAmount);// comment out if uncomment next two lines
    //principal += interest;                                                          // add principal to interest and assign back to principal 

    //System.out.printf("%6s%8d%16.2f%30.2f%n", year, quarter, interest, principal);  // change finalAmount to principal but it keeps the same output

    principal = finalAmount;
    }                                                                               // end of for loop

    System.exit(0);
  • // Enter the year: 2005 // Enter the initial principal: 10000.00 // Principal: = 10000.00 // Annual Interest Rate: 5.00% // Year Quarter Interest Earned Amount at end of quarter // 2005 1 125.00 10125.00 // 2005 2 126.56 10251.56 // 2005 3 128.14 10379.70 // 2005 4 129.75 10509.45 // DELIMITeach line via the//, you should understand the columns of info, let me know if //you dont please. When i copy pasted my code the formatting was a little off on a couple of lines, thanks for helping an old dog learn new tricks. Humpty Dumpty over 50. – Humpty Dumpty Oct 10 '14 at 14:36
  • You've got code, you've got output, but you don't exactly have a question here. What are you asking? – Makoto Oct 10 '14 at 14:37
  • What is the purpose of the for loop? Please describe us the ultimate goal in using one. For example, is it supposed to generate quarter reports for several years? Ask for additional inputs? That'll help us determine what you're actually looking for. – Compass Oct 10 '14 at 14:38
  • I need to iterate through the four (4) quarters ONLY and give the same output, but I need to do it with a for loop now, thanks for looking into this. – Humpty Dumpty Oct 10 '14 at 14:39
  • have a look here if you need help with loops: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html – PeterK Oct 10 '14 at 14:42
  • There are 5 lines of code from principal thru the printf line, I am thinking that they all need to go into a for loop and get interated 4 times??? and since this iteration will do the job of assigning a number 1 thru 4 into each quarter, I think I should not assign the number explicitly??? I have looked into documentation and many snippits online but nothing like what I have is available, thanks for the link. – Humpty Dumpty Oct 10 '14 at 14:43
  • why not post the code which you are trying to do with for loop? even it may not correct, but someone here may guide your more. – Surasin Tancharoen Oct 10 '14 at 14:49
  • I will minimize my request to iterate through the 1 thru 4 for the quarters only for the moment, sorry folks, I am on the other side of the world and its way past bed time, I will catch up with any posts tomorrow, thanks to all and a good night. – Humpty Dumpty Oct 10 '14 at 14:55
  • Welcome to stackoverflow. Unfortunately your question is still not specific enough. The way to make progress with programming is to break down your problem into smaller problems. Think about how you'd approach your problem step by step at a high conceptual level (top-down). Then write code for the smaller parts (bottom-up). When you get stuck, ask a _specific_ question about that part, leaving the rest out of the question (and also out of the example, so it stands by itself). – alexis Oct 11 '14 at 12:29
  • For those looking for a better question, its been cleared up in the new code and a new question has come up in the comments, thanks to all for your concern and I will try to be more specific in the future, I am just a beginner for now, as you already know. – Humpty Dumpty Oct 11 '14 at 13:21

1 Answers1

1

If I follow your question, then you should use a for loop to apply the interest to the principal and then print your message. That should look something like

for (int quarter = 1; quarter <= 4; quarter++) {
  interest = principal * quarterlyRate;                                           
  principal += interest;
  System.out.printf("%6s%8d%16.2f%30.2f%n", year, quarter, interest, principal);
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thank you, I will give it a try and let you know, I did make the for statement as you did but I did not think to put 3 of 5 lines inside the for loop. I tried with only the quarter line or all 5 lines of repeated original code but I was not getting the output I needed. Need to go to work, will try tonight again. – Humpty Dumpty Oct 10 '14 at 22:34
  • I got it working with my original code but by using Elliott's code with the principal += interest was elegant. I had to get another line of code from the next set just below the printf. I will try to post the changes, last time it took me forever to post the code and make it accept. – Humpty Dumpty Oct 11 '14 at 11:55
  • I have it running and would like to post the code, having same troubles I had the first time, lack of experience on this site, working on it now. – Humpty Dumpty Oct 11 '14 at 12:04
  • OK, thanks to all that helped, I liked your coding style better but I wanted to stick to the original as much as possible, I needed more code than anticipated. Can anyone explain why the quarter needed to be changed to an int instead of leaving it a byte as in the original code, I have JCreator regular, not the PRO so I cannot debug with the software, once again thanks to everybody, sorry my OP was not explicit enough but 30 minutes and I had it going with all your help. – Humpty Dumpty Oct 11 '14 at 12:09
  • "%6s%8d%16.2f%30.2f%n" I see in the printf statement that the quarter section is %8d, I do not know the designator for a byte, is there one, should this one work as is or do I need to cast the quarter in the for loop to an int OR just declare it in the for loop statement as suggested by Elliot. Of course this is assuming the printf statement is the fault. Thanks again for clearing this up, I will do some online research now. – Humpty Dumpty Oct 11 '14 at 12:35
  • I have my answer for the byte vs integer issue I was unaware of as to why my for loop would not count. Originally the quarter was declared as byte which I believe only uses a small amount of memory for storage of numbers. While trying to add one to the count the computer wants to add a ONE which is an integer that requires 32 bits of memory and Java doesnt automatically upgrade the byte to an int so the quarter counter needs to be type cast to an int for the math to take place. If a counter of (byte)(quarter + 1) will be used then it will need to be coded within parens and cast first as shown – Humpty Dumpty Oct 14 '14 at 09:28