Results 1 to 3 of 3

Thread: Method Help

  1. #1
    07.350 is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    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.Any help would be greatly appreciated, thanks!

    Java Code:
    import java.util.Scanner;
    public class InterestCalculator5 {
    	public static void main(String[] args )
    	{
    		Scanner input = new Scanner(System.in);
    
    		String strAnsYesNo;
    		char ansYesNo = 0;
    		getPrincipalAmount(1);
    		getInterestRate(1);
    		getTerm(1);
    	}
    				    
    			    			      
    			    			  
    			    			  
    		
    	
    
    	/** 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);
    		 do{System.out.print("Enter Loan Amount: ");
    		   double numb2 = input.nextDouble();
    		  double getPrincipalAmount = 0;
    		   if(numb2 > 0)
    		  getPrincipalAmount = numb2;
    		   		else{	
    			    		System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb1);
    		   		}		
    			   }while (numb2 < 0);
    		return getPrincipalAmount;
    	  }
    	  
    	  /** Get interest rate **/
    	  public static double getInterestRate(double numb1) {
    		  Scanner input = new Scanner(System.in);
    		  do{System.out.print("Enter Yearly Interest Rate (1 to 100 percent): ");
    		    double 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 " +numb1);
    		   		}		
    			   }while (numb2 <= 0 && numb2 >= 100);
    		return getInterestRate;
    	  }
    	  
    	  /** Get term **/
    	  public static double getTerm(double numb1) {
    		  Scanner input = new Scanner(System.in);
    		  do{System.out.print("Enter the Term (in months): ");
    		    double 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 " +numb1);
    		   		}		
    			   }while (numb2 < 0);
    		return getTerm;
    	  }
    	  
    	  /** Get calculation type **/
    	  public static double getCalculationType(double numb1) {
    		  Scanner input = new Scanner(System.in);
    		  System.out.print("Enter Interest Calculation Type (1  Simple, 2  Monthly Compounded, 3  Daily Compounded):");
    		    int numb2 = input.nextInt();
    		  double getCalculationType = 0;
    		  do{if (numb2 <1||numb2 >3)
    			  getCalculationType = numb2;
    		   		else{	
    			    		System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +numb1);;
    		   		}		
    			   }while (numb2 > 1||numb2 < 3);
    		return getCalculationType;
    	  }
    }
    I'm getting the error, I'm not sure if I have the do loop in the wrong spot in the methods or what?

    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	numb2 cannot be resolved to a variable
    	getPrincipalAmount cannot be resolved to a variable
    
    	at InterestCalculator5.getPrincipalAmount(InterestCalculator5.java:69)
    	at InterestCalculator5.main(InterestCalculator5.java:15)

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Method Help

    You have declared the variable numb2 inside the do/while loop and therefore cannot use it outside the loop. In a do/while loop the condition is deemed to be outside the loop.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

Similar Threads

  1. Replies: 2
    Last Post: 03-23-2012, 05:53 AM
  2. Replies: 1
    Last Post: 10-27-2011, 08:16 AM
  3. Replies: 1
    Last Post: 10-17-2011, 02:00 AM
  4. Replies: 18
    Last Post: 04-18-2011, 06:39 AM
  5. Replies: 2
    Last Post: 03-26-2010, 06:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •