# Method Help

• March 21st, 2013, 08:18 PM
07.350
Method Help
I'm creating three new methods in my code; getPrincipalAmount (acquire principal amount from the user and validate it) , getInterestRate (acquire interest rate from the user and validate it), getTerm (acquire term length from the user and validate it).

Basically the part I'm confused on is in the method how to ask the user and have them input it and then validate it. I currently don't have it set up to ask the user in the method, I'm unsure on how to do that part and just confused. Any help would be greatly appreciated, thanks!

Code java:

```import java.util.Scanner; public class InterestCalculator5 { public static void main(String[] args ) { Scanner input = new Scanner(System.in);   String strAnsYesNo; char ansYesNo = 0;     //Prompt user to enter loan amount System.out.print("Enter Loan Amount: "); double principle = input.nextInt();   getPrincipalAmount(principle);   //Prompt user to enter interest rate System.out.print("Enter Yearly Interest Rate (1 to 100 percent): "); double interest = input.nextDouble();   getInterestRate(interest);   //Prompt user to enter term System.out.print("Enter the Term (in months): "); double term = input.nextInt();   getTerm(term);     int type=0; //Prompt user to enter calculation type do{System.out.print("Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded):"); type = input.nextInt();     switch(type) { case 1: displayInterest(principle,interest,term,"Simple",round((calculateSimpleInterest(principle, interest, term)),5),round((calculateSimpleInterest(principle, interest, term)+principle),2)); break;   case 2: displayInterest(principle,interest,term,"Compound Monthly", round(calculateCompoundInterest(principle, interest,term ,12.0 ),5), round((calculateCompoundInterest(principle, interest,term ,12.0)+principle),2)); break;   case 3: displayInterest(principle,interest,term,"Compound Daily", round(calculateCompoundInterest(principle, interest, term, 365.0 ),5), round((calculateCompoundInterest(principle, interest, term, 365.0)+principle),2)); break;   default: System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +type); break;   } }while (ansYesNo =='y' || ansYesNo =='Y'); System.out.println("Thank you for using Interest Calculator!"); }     /** Round **/ public static double round(double numb1, double numb2) { double round = ((double) Math.round(numb1*(Math.pow(10, numb2)))/(Math.pow(10, numb2)));; return round; }   /** Calculate Simple **/ public static double calculateSimpleInterest(double numb1, double numb2, double numb3) { double calculateSimpleInterest = ((numb1)*(numb2/100.0)*(numb3/12.0)); return calculateSimpleInterest; }   /** Calculate Compounded Daily **/ public static double calculateCompoundInterest(double numb1, double numb2, double numb3, double numb4 ) { double calculateCompoundInterest = (numb1*Math.pow((1.0+((numb2/100.0)/numb4)),(numb4*(numb3/12.0))))-numb1; return calculateCompoundInterest; }   /** display interest **/ public static void displayInterest(double numb1, double numb2, double numb3, String word1, double numb4, double numb5 ) {     System.out.println("Principal Amount Interest Rate Term Calculation Type Calculated Interest Total Repayment"); System.out.println("---------------- ------------- ---- ---------------- ------------------- ---------------"); System.out.print(numb1+" "); System.out.print(+numb2+" "); System.out.print(+numb3+" "); System.out.print(word1+" "); System.out.print(+numb4+" "); System.out.println(+numb5); }   /** Get principal amount **/ public static double getPrincipalAmount(double numb1) { double getPrincipalAmount = 0; do{if (numb1 > 0) getPrincipalAmount = numb1; else{ System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb1); } }while (numb1 < 0); return getPrincipalAmount; }   /** Get interest rate **/ public static double getInterestRate(double numb1) { double getInterestRate = 0; do{if (numb1 >= 0 && numb1 <= 100) getInterestRate = numb1; else{ System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +numb1); } }while (numb1 <= 0 && numb1 >= 100); return getInterestRate; }   /** Get term **/ public static double getTerm(double numb1) { double getTerm = 0; do{if (numb1 > 0) getTerm = numb1; else{ System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb1); } }while (numb1 < 0); return getTerm; }   /** Get calculation type **/ public static double getCalculationType(double numb1) { double getCalculationType = 0; do{if (numb1 <1||numb1 >3) getCalculationType = numb1; else{ System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +numb1);; } }while (numb1 > 1||numb1 < 3); return getCalculationType; } }```
• March 21st, 2013, 08:22 PM
Norm
Re: Method Help
Quote:

I'm confused on is in the method how to ask the user and have them input it
The following statements from your posted code does that:
Code :

``` System.out.print("Enter Loan Amount: "); // ask the user to enter data double principle = input.nextInt(); // read the data that the user entered```
• March 21st, 2013, 08:26 PM
07.350
Re: Method Help
Quote:

Originally Posted by Norm
The following statements from your posted code does that:
Code :

``` System.out.print("Enter Loan Amount: "); // ask the user to enter data double principle = input.nextInt(); // read the data that the user entered```

Is there a way I can implement that in my method instead of having to ask that in the main?
• March 21st, 2013, 08:29 PM
Norm
Re: Method Help
The method should use statement just like I posted except with the names of variables that are in the method.
Don't use the names from the main() method.
• March 21st, 2013, 08:42 PM
07.350
Re: Method Help
When I do that
Code java:

``` /** Get principal amount **/ public static double getPrincipalAmount(double numb1) { System.out.print("Enter Loan Amount: "); double numb1 = input.nextdouble(); double getPrincipalAmount = 0; do{if (numb1 > 0) getPrincipalAmount = numb1; else{ System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb1); } }while (numb1 < 0); return getPrincipalAmount; }```

I get an error input cannot be resolved
• March 21st, 2013, 08:55 PM
Norm
Re: Method Help
Quote:

I get an error input cannot be resolved
When you get compiler errors, please copy the full text of the error messages and paste it here.

The compiler can't find a definition for the variable: input. One solution is to copy its definition from the main() method.
• March 21st, 2013, 09:03 PM
07.350
Re: Method Help
Sorry, when I'm in the main and call the for the method, how would I make the method go ahead and ask for the principal amount instead of putting in the principal amount get in the method how would I know what to put in the ( ), of the getPrincipalAmount() to have the method just run?

Duplicate local variable numb1
input cannot be resolved

at InterestCalculator5.getPrincipalAmount(InterestCal culator5.java:59)
at InterestCalculator5.main(InterestCalculator5.java: 15)
• March 21st, 2013, 09:05 PM
Norm
Re: Method Help
Copy the definition for the variable: input from the main() method to the method. This is where input is defined:
Code :

` Scanner input = new Scanner(System.in);`

I'm done for tonight.
• March 21st, 2013, 09:12 PM
07.350
Re: Method Help
• March 21st, 2013, 10:34 PM
Junky
Re: Method Help
Method Help

Duplicate post.
• March 22nd, 2013, 06:27 PM
07.350
Re: Method Help
I have the getPrincipalAmount, getInterestRate, getCalculationType, and getTerm methods correct. I'm working on the askYesNo method now.. Also when the user enters in the data for getPrincipalAmount, getInterestRate, and getTerm I'm confused on how to use that entered data in the formulas I made methods for. Would I put those formulas in the getCalculationType method in an If statement if numb1==1 then it performs the simple interest calc.,2, 3 etc..

Code java:

```import java.util.Scanner; public class InterestCalculator5 { public static void main(String[] args ) { Scanner input = new Scanner(System.in); do{   String strAnsYesNo; char ansYesNo = 0; getPrincipalAmount(1); getInterestRate(1); getTerm(1);     } while (askYesNo("yes"));   }     /** Round **/ public static double round(double numb1, double numb2) { double round = ((double) Math.round(numb1*(Math.pow(10, numb2)))/(Math.pow(10, numb2)));; return round; }   /** Calculate Simple **/ public static double calculateSimpleInterest(double numb1, double numb2, double numb3) { double calculateSimpleInterest = ((numb1)*(numb2/100.0)*(numb3/12.0)); return calculateSimpleInterest; }   /** Calculate Compounded Daily **/ public static double calculateCompoundInterest(double numb1, double numb2, double numb3, double numb4 ) { double calculateCompoundInterest = (numb1*Math.pow((1.0+((numb2/100.0)/numb4)),(numb4*(numb3/12.0))))-numb1; return calculateCompoundInterest; }   /** display interest **/ public static void displayInterest(double numb1, double numb2, double numb3, String word1, double numb4, double numb5 ) {     System.out.println("Principal Amount Interest Rate Term Calculation Type Calculated Interest Total Repayment"); System.out.println("---------------- ------------- ---- ---------------- ------------------- ---------------"); System.out.print(numb1+" "); System.out.print(+numb2+" "); System.out.print(+numb3+" "); System.out.print(word1+" "); System.out.print(+numb4+" "); System.out.println(+numb5); }   /** Get principal amount **/ public static double getPrincipalAmount(double numb1) { Scanner input = new Scanner(System.in); double numb2 = 1; do{System.out.print("Enter Loan Amount: "); numb2 = input.nextDouble(); if(numb2 > 0);   else{ System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb2); } }while (numb2 < 0); return numb2; }   /** Get interest rate **/ public static double getInterestRate(double numb1) { Scanner input = new Scanner(System.in); double numb2=1; do{System.out.print("Enter Yearly Interest Rate (1 to 100 percent): "); numb2 = input.nextDouble(); double getInterestRate = 0; if (numb2 >= 0 && numb2 <= 100) getInterestRate = numb2; else{ System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +numb2); } }while (numb2 <= 0 || numb2 >= 100); return numb2; }   /** Get term **/ public static double getTerm(double numb1) { Scanner input = new Scanner(System.in); double numb2=1; do{System.out.print("Enter the Term (in months): "); numb2 = input.nextInt(); double getTerm = 0; if (numb2 > 0) getTerm = numb2; else{ System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb2); } }while (numb2 < 0); return numb2; }   /** Get calculation type **/ public static double getCalculationType(double numb1) { Scanner input = new Scanner(System.in); int numb2=1; do{ System.out.print("Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded):"); numb2 = input.nextInt(); double getCalculationType = 0; if (numb2 ==1|| numb2 ==2 || numb2==3) getCalculationType = numb2; else{ System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +numb2);; } }while (numb2 !=1|| numb2!=2 || numb2 !=3); return numb2; }   /**ask yes or no * @return **/ public static boolean askYesNo( String a ) { Scanner input = new Scanner(System.in); String word1 = a; char b='a'; char word2 = b; do{System.out.println("Calculate another loan interest (Yes/No)?"); word1 = input.nextLine(); if (word1.length() > 0) word2 = word1.charAt(0); if(word2!='y'&& word2!='Y'&& word2!='n'&& word2!='n') { System.out.println("Please enter 'Yes' or 'No'?"); }   }while(word2!='y'&& word2!='Y'&& word2!='n'&& word2!='n'); return false;   } }```
• March 22nd, 2013, 06:34 PM
Norm
Re: Method Help
Quote:

if numb1==1 then it performs the simple interest calc.,2, 3 etc..
switch statements can be used for choosing what method to call based on the value in variable.
• March 22nd, 2013, 06:35 PM
07.350
Re: Method Help
I have a switch statement
Code java:

``` switch(type) { case 1: displayInterest(principle,interest,term,"Simple",round((calculateSimpleInterest(principle, interest, term)),5),round((calculateSimpleInterest(principle, interest, term)+principle),2)); break;   case 2: displayInterest(principle,interest,term,"Compound Monthly", round(calculateCompoundInterest(principle, interest,term ,12.0 ),5), round((calculateCompoundInterest(principle, interest,term ,12.0)+principle),2)); break;   case 3: displayInterest(principle,interest,term,"Compound Daily", round(calculateCompoundInterest(principle, interest, term, 365.0 ),5), round((calculateCompoundInterest(principle, interest, term, 365.0)+principle),2)); break;   default: System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +type); break;```
• March 22nd, 2013, 06:46 PM
Norm
Re: Method Help
Ok.
• March 22nd, 2013, 08:41 PM
07.350
Re: Method Help
Is there a certain way I need to save the users input to use for the other methods
• March 22nd, 2013, 08:55 PM
Norm
Re: Method Help
Do the other methods get the user's input passed to them as arguments?
See the tutorial on how to pass args: Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
• March 22nd, 2013, 09:37 PM
07.350
Re: Method Help
I'm just confused on how to take the users input for the principal amount, interest rate, and term to the other method..
Code java:

``` /** Get calculation type **/ public static double getCalculationType(double numb1) { Scanner input = new Scanner(System.in); int numb2=1;   do{ System.out.print("Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded):"); numb2 = input.nextInt(); double principle = 10; double interest=10; double term=10; switch(numb2) { case 1: displayInterest(principle,interest,term,"Simple",round((calculateSimpleInterest(principle, interest, term)),5),round((calculateSimpleInterest(principle, interest, term)+principle),2)); break;   case 2: displayInterest(principle,interest,term,"Compound Monthly", round(calculateCompoundInterest(principle, interest,term ,12.0 ),5), round((calculateCompoundInterest(principle, interest,term ,12.0)+principle),2)); break;   case 3: displayInterest(principle,interest,term,"Compound Daily", round(calculateCompoundInterest(principle, interest, term, 365.0 ),5), round((calculateCompoundInterest(principle, interest, term, 365.0)+principle),2)); break;   default: System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +numb2); break; } }while (numb2 !=1|| numb2!=2 || numb2 !=3); return numb2; }```
• March 23rd, 2013, 05:49 AM
Norm
Re: Method Help
Did you read the tutorial at the link I posted about how to pass args to a method?

The method: getCalculationType() should do only what its name says: get a type and return it to the caller. I don't see why why the type is a double and not an int.
The caller of the getCalculationType() method should use then use the type to call the methods to do the work.