Hi everyone! I need a little help with while loops please. I tried to create a class within my CD class that repeats the input of the CD info and the output of the CDs value at maturity until the principle of 0 is entered. I think I did this okay under the CDdriver class?

Then I also need to create another method under the CD class called
public int monthsToValue(double target) that will return the months it will take for the CD value to reach the value target. This is where I'm mostly stuck. I don't really know what to change in my main method to make this work. Can someone help me?

 * Trying to come up with the Final Balance, utilizing the correct variables
 * and writing our first program. 
 * @author ( ) 
 * @version (Version 1 Sept 14, 2009)
// put imports here 
import static java.lang.Math.*;
import javax.swing.*;
import java.awt.*; 
public class CD
    private final double  Principal;
    private final double  monthsInvested; 
    //so I changed my variables to only principal and numberofMonths because those are the only variables needed to be implemented in the new class

                   public double getPrincipal()
                   {  return  this.Principal; } 
                   public double getmonthsInvested()
                   { return this.monthsInvested; }
      //Named my accessors so that they would refer back to the CD class and so they would return the two principals which would need to be had by the accessors
      //because they are private. 
     * Constructor for objects of class CD
    public CD( ) 
        //Declared the user prompt strings 
 String frameTitle = "CD Lab #5";
 String enterPrincipal = "What is your principal?";
 String entermonthsInvested = "What are your months invested?";
 String response = JOptionPane.showInputDialog(null, enterPrincipal, frameTitle, JOptionPane.QUESTION_MESSAGE); 
        Principal = Double.valueOf(response).doubleValue();
    response = JOptionPane.showInputDialog(null, entermonthsInvested, frameTitle,JOptionPane.QUESTION_MESSAGE);  
        monthsInvested = Double.valueOf(response).doubleValue();
        // initialise instance variables
        //I only need the principal and monthsInvested for this new CD, instead of myAIR, etc. 
        //I also implemented string prompts and their responses 

     * FinalBalance method -using the equation given to us to represent the FinalBalance 
    public class CDdriver {
        public static void main(String[] args)
            CD myCD = new CD (); 
                while (myCD.getPrincipal () > 0) {
                    String frameTitle = "Lab #5";
                    String enterprincipal = "Enter Principal";
                    String entermonthsInvested= "Enter Months Invested";
                    CD cd1= new CD (); 
                    String message1 = "The Final Balance of CD 1 Is" + String.format ( "%8.2f", cd1. FinalBalance()); 
                    String message2 = "The PercentageChange of CD1 Is" + String.format("%8.2f", (cd1.FinalBalance()-cd1.getPrincipal())/cd1.getPrincipal()); 
       // Here, we added a while statement so that while the principal is greater than zero, the output for each CD will loop 
        public double FinalBalance();
             double BasicInterestRate; 
             double AnnualRateAdjustment; 
             double monthlyRate;
             if ( Principal < 10000 ) {
                 BasicInterestRate  = .015; }
                 else {
                      if ( Principal >= 10000 && Principal < 1000000 ) {
                            BasicInterestRate = .026; }
                       else { BasicInterestRate = .034; }
                            if ( monthsInvested < 18 ) {
                                AnnualRateAdjustment = 0; }
                                else {
                                    if ( monthsInvested >= 18 && monthsInvested < 30 ) {
                                        AnnualRateAdjustment = .005; }
                                        else {AnnualRateAdjustment = 5/6/100.0; }
                     monthlyRate = (BasicInterestRate + AnnualRateAdjustment) /12;
                     if ( monthsInvested < 24 ) {
                         return Principal *  java.lang.Math.pow( 1 + monthlyRate, monthsInvested); 
                             else { return Principal * java.lang.Math.exp (monthlyRate * monthsInvested); }
  public int monthstoValue(double target); 
        while (FinalBalance() > 0) { 
            return monthsInvested}  
 public String toString() {
 String enterP = "This is" + Principal; 
 String enterMI = "This is" + monthsInvested;
 return enterP + enterMI; 
7 Years
Discussion Span
Last Post by BestJewSinceJC

Try using netbeans. There are WAY to many mistakes for me to even try to help right now. I counted 11 big mistakes. Let netbeans guide you through fixing all of the big mistakes.


I'd agree that using netbeans would be helpful, but which editor he uses is ultimately up to him. Seems strange to me to respond that you found errors but not to bother mentioning any though.

edit: sorry

Also, OP, I don't have any advice to really offer you right now, but you should not make variables that start with uppercase letters! It's convention. Principal should be principal. And
public double FinalBalance(); looks like you should remove the ; .. isn't that supposed to be a method?

Edited by BestJewSinceJC: n/a

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.