Java only uses Default Constructor won't calculate by entered parameters -
i've looked on code few times , i'm not sure affecting , forcing utilize default constructor. illustration if seek set in 2000 amount invested still default 1000.
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() { homecoming moneyinvested; } public double getyearsinvested() { homecoming 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 @ end of year " + inityears + " " + investreturn; joptionpane.showmessagedialog(null, returnvalue); inityears++; } homecoming returnvalue; } } 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 amount invest (9999).", "investment amount", joptionpane.question_message); if (amountamount == null || amountamount.length() == 0) { joptionpane .showmessagedialog( null, "nothing entered - must come in number amount invested.", "investment amount error", joptionpane.error_message); homecoming false; } (int = 0; < amountamount.length(); i++) { if (!character.isdigit(amountamount.charat(i))) { joptionpane.showmessagedialog(null, "you must come in number amount invested.", "investment amount error", joptionpane.error_message); homecoming false; } } double dblamount = double.parsedouble(amountamount); homecoming true; } public static boolean inputyears() { string yearamount = ""; yearamount = joptionpane.showinputdialog(null, "enter number of years invest.", "investment years", joptionpane.question_message); if (yearamount == null || yearamount.length() == 0) { joptionpane .showmessagedialog( null, "nothing entered - must come in number years invest.", "investment years error", joptionpane.error_message); homecoming false; } (int = 0; < yearamount.length(); i++) { if (!character.isdigit(yearamount.charat(i))) { joptionpane.showmessagedialog(null, "you must come in number of years invest.", "investment years error", joptionpane.error_message); homecoming false; } } double dblyear = double.parsedouble(yearamount); homecoming true; } }
nothing "forcing utilize default constructor". you're ever calling default constructor
investment otherclass = new investment()
to utilize 2-argument constructor, pass in arguments
new investment(2000.0d, 5.0d)
java parameters default-constructor
No comments:
Post a Comment