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