Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Method Help

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Method Help

    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:
    	System.out.print("Enter Loan Amount: ");    //  ask the user to enter data
    	double principle = input.nextInt();           //  read the data that the user entered
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method Help

    Quote Originally Posted by Norm View Post
    The following statements from your posted code does that:
    	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?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method Help

    When I do that
     /** 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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Method Help

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Method Help

    Copy the definition for the variable: input from the main() method to the method. This is where input is defined:
    		Scanner input = new Scanner(System.in);

    I'm done for tonight.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method Help

    Thanks for your help!

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Method Help

    Method Help

    Duplicate post.
    Improving the world one idiot at a time!

  11. #11
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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..

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

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Method Help

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method Help

    I have a switch statement
     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;

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Method Help

    Ok.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method Help

    Is there a certain way I need to save the users input to use for the other methods

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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)
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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..
     /** 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;
    	  }

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: January 23rd, 2013, 07:29 AM
  2. [SOLVED] How to create a Java generic method, similar to a C++ template method?
    By Sharmeen in forum Object Oriented Programming
    Replies: 3
    Last Post: October 18th, 2012, 02:33 AM
  3. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  4. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  5. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM