My class is currently working on Looping; Characters, Strings, and the StringBuffer Assignment and I'm having a bit of an issue with my code as it will only use the default constructor. On top of that instead of posting results in one message dialog box it posts it to multiple.
The following is the assignment as it is written:
Create a class named Investment. This class should have private variables for the amount invested and the years invested. The class should have a public symbolic constant for the interest rate. Use 12% (.12) for this constant. Create the following public methods in the Investment class. A default constructor. This constructor will set the investment amount to 1000 and the years to one. Make each of these default values a symbolic constant. The constants should be public. An overloaded constructor that takes an argument for the amount and years. Edit each value to insure it is greater than zero. Use the default value if an argument value is zero or less. For example, use 1000 for the amount if the amount argument is zero. A get and set method for each private variable. Edit each value in its respective set method to insure it is greater than zero. Use the default value if an argument value is zero or less. For example, use 1000 for amount if the amount argument is zero. A method to calculate the annual return on the investment. You may calculate the annual balance in two ways. The first requires you to repeatedly multiply the amount invested by 1.12 (one plus the interest rate). Assume for example the investment amount is 2000 and the number of years is two. The calculation for the annual balance would be: 1st year amount = (1.12) * 2000 2nd year amount = (1.12) * 1st year amount The second method requires the use of the Math.pow method. Use the Math.pow method to raise a number to a power. This method returns a double and has two arguments - both doubles. The first number is the amount to be "raised" which is 1.12 (one plus the interest rate). The second number is the power to which it should be raised which is the number of years invested. Using the same amount and years as above, the calculation for the annual balance when investing $2000 for 2 years would be: year 1 amount = Math.pow(1.12, 1) * 2000 year 2 amount = Math.pow(1.12, 2) * 2000 You must use a loop to calculate the yearly amounts. You can't use multiple selection statements grouped together to attempt to cover all possible combinations of years and and amounts. This method must return a String that includes the output for each year. For example, the String created using the same amount and years as above would contain the following. The amount at end of year 1 is $2240 <new line character> The amount at end of year 2 is $2508 Create a class named MakeInvestment. This class must create and use an object of the Investment class. Create a separate method in the MakeInvestment class to get the amount invested from the user. You must use an input dialog box like the one in Figure 1 below to get the input. The amount invested must be a number and must be greater than zero. To check for a number, inspect each character entered in the dialog to make sure it is a digit. Do not use exception processing or regular expressions to determine if the string is a valid number. (We will do this in a later assignment!) By checking for digits only, you will not allow a decimal point or a negative sign. Display an error dialog box if the data entered is not a number (Figure 2). After displaying either message dialog box, display the input dialog again to get a valid investment amount. Continue to display the input dialog and the appropriate error dialog until an acceptable value is entered. Create a separate method in the MakeInvestment class to get the number of years to invest from the user. You must use an input dialog box like the one in Figure 3 below to get the input. The number of years to invest must be a number and must be greater than zero. To check for a number, inspect each character entered in the dialog to make sure it is a digit. Do not use exception processing or regular expressions to determine if the string is a valid number. (We will do this in a later assignment!) By checking for digits only, you will not allow a decimal point or a negative sign. Display an error dialog box if the data entered is not a number (Figure 4). After displaying either message dialog box display the input dialog to get a valid number of years again. Continue to display the input dialog and the appropriate error dialog until an acceptable value is entered. Display the output indicating the balance at the end of each investment year. Use a message dialog to display this output. Using the same amount, years, and rate as above, the output when investing $2000 for 2 years at 12 % would be as displayed in Figure 5 below. The output when investing $5000 for 5 years at 12 % would be as displayed in Figure 6 below. Remember, the output displayed should be the String returned from the method in the Investment class that calculates the annual balances. You do not need to program a response when the user clicks the Cancel button or clicks the X in the upper right corner of the dialog in Figures 1 and 4. I will not test these features when I grade your application. The MakeInvestment class cannot have any "class wide" variables. This means all the variables in the MakeInvestment class must be defined within a method.
So my current code for the Investment class is:
import javax.swing.JOptionPane; public class Investment { private double moneyInvested; private double yearsInvested; public final static double AMOUNT_DEFAULT = 1000; public final static double YEARS_DEFAULT = 5; public final static double RATE = 0.12; public Investment() { moneyInvested = AMOUNT_DEFAULT; yearsInvested = YEARS_DEFAULT; } public Investment(double AMOUNT_DEFAULT, double YEARS_DEFAULT) { if (moneyInvested <= 0) { moneyInvested = AMOUNT_DEFAULT; } if (yearsInvested <= 0) { yearsInvested = YEARS_DEFAULT; } } public double getMoneyInvested(){return moneyInvested;} public double getYearsInvested(){return yearsInvested;} public void setMoneyInvested(double inputMoney) { moneyInvested = inputMoney; if (inputMoney <= 0) { inputMoney = 1000; } } public void setYearsInvested(double inputYears) { yearsInvested = inputYears; if (inputYears <= 0) { inputYears = 1; } } public static String returnValue(double inputYears, double inputMoney) { double returnInvestment; int initYears = 1; String returnValue = ""; while(initYears <= inputYears) { returnInvestment = Math.pow(1.12, initYears) * inputMoney; int investReturn = (int)returnInvestment; returnValue = "The amount at end of year " + initYears + " is " + investReturn; JOptionPane.showMessageDialog(null,returnValue); initYears++; } return returnValue; } }
and my code for the MakeInvestment class is:
import javax.swing.JOptionPane; public class MakeInvestment { public static void main(String[] args) { Investment otherClass = new Investment(); double yA = otherClass.YEARS_DEFAULT; double mA = otherClass.AMOUNT_DEFAULT; while (inputAmount() == false) { inputAmount(); } while (inputYears() == false) { inputYears(); } otherClass.returnValue(yA, mA); } public static boolean inputAmount() { String amountAmount = ""; amountAmount = JOptionPane.showInputDialog( null, "Enter the amount to invest (9999)." , "Investment Amount" , JOptionPane.QUESTION_MESSAGE); if (amountAmount == null || amountAmount.length() == 0) { JOptionPane.showMessageDialog(null, "Nothing entered - You must enter a number for amount invested.", "Investment Amount Error", JOptionPane.ERROR_MESSAGE); return false; } for (int i = 0; i < amountAmount.length(); i++) { if (!Character.isDigit(amountAmount.charAt(i))) { JOptionPane.showMessageDialog(null, "You must enter a number for amount invested.", "Investment Amount Error", JOptionPane.ERROR_MESSAGE); return false; } } double dblAmount = Double.parseDouble(amountAmount); return true; } public static boolean inputYears() { String yearAmount = ""; yearAmount = JOptionPane.showInputDialog( null, "Enter the number of years to invest." , "Investment Years" , JOptionPane.QUESTION_MESSAGE); if (yearAmount == null || yearAmount.length() == 0) { JOptionPane.showMessageDialog(null, "Nothing entered - You must enter a number for years to invest.", "Investment Years Error", JOptionPane.ERROR_MESSAGE); return false; } for (int i = 0; i < yearAmount.length(); i++) { if (!Character.isDigit(yearAmount.charAt(i))) { JOptionPane.showMessageDialog(null, "You must enter a number of years to invest.", "Investment Years Error", JOptionPane.ERROR_MESSAGE); return false; } } double dblYear = Double.parseDouble(yearAmount); return true; } }
I've looked over the code a few times and I'm not sure what is affecting this and forcing it to only use the default constructor. For example if I try to put in 2000 for the amount invested it will still default to 1000.