Page 3 of 3 First 123
  • Jump to page:
    #31
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    41
    Rep Power
    2
    So i think i got it heres thre version of the code the first is the calulator, the second is the calulator with the for loop determining the down payment . The third is turns the calulation into a method and then computes two arguments passed to it.

    This code all works with the exception of the third set of code. The if statements for the annual interest rate dont work. it is supposed to print an error and stop the program if interest rate is under .025 or over .05 for either value. If you enter both as above or both as below it runs the error, but if its a mix of the two or a valid value is entered it doesnt work
    code1:
    Code:
    import java.text.DecimalFormat;// allows for decimal formatting
    import java.util.Scanner;
    
    	public class Calculator{
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		// questions for the user
    		System.out.println("Enter the price of the house: ");
    		double housePrice = input.nextDouble();
    		System.out.println("Enter the down payment: ");
    		double downPayment = input.nextDouble();
    		System.out.println("Enter the annual interest rate: ");
    		double annualInterestRate = input.nextDouble();
    		System.out.println("Enter the number of payments: ");
    		double numberofPayments = input.nextDouble();
    		System.out.println("");System.out.println("");
    		
    		
    		// formula for payment
    		double monthlyPayment = (((housePrice - downPayment) * (annualInterestRate/12))/((1-(Math.pow(1+(annualInterestRate/12), -numberofPayments)))));
    		
    		DecimalFormat df = new DecimalFormat("0.00"); //decimal format
    		//if statements to meet conditions
    		if (housePrice > 0){
    		System.out.println( "The price of the house would be: $" + (df.format(housePrice)));
    		
    		}
    		else {
    			System.out.println("Error: Price must be greater than 0! ");
    			System.exit(0);
    		}
    		if (downPayment > 0){
    		System.out.println( "Your down payment would be: $" + (df.format(downPayment)));
    		
    		}
    		else {
    			System.out.println("Error: Price must be greater than 0! "); //error message if under 1
    			System.exit(0);
    		}
    		
    		if (annualInterestRate < .025 ){
    			System.out.println("Error: Interest rate too low! ");//error message if under .025
    			System.exit(0);
    		}
    			else if  (annualInterestRate > .05 ){
    				System.out.println("Error: Interest rate too high! ");//error message if over .05
    				System.exit(0);
    		}
    		
    			else {
    				System.out.println( "Your annual interest rate would be: " + (df.format(annualInterestRate)));
    			}		
    		if (numberofPayments > 0 ){
    			
    		System.out.println( "Your number of payments would be: " + (int)numberofPayments);	
    		}
    		else {
    			System.out.println( "Error: must be higher than 0");	//error message if under 1
    			System.exit(0);
    		}
    		
    		System.out.println( "Your monthly payment would be: $" + (df.format(monthlyPayment)));
    		
    		
    	
    	}	
    	}//end
    code 2:
    Code:
    /**
    * Project 1-ReSubmittion
    * File: PossibleDownPayments
    * Date of creation:3/25/2013
    * Date of modification: 3/25/2013
    * Author: Adam Gonia
    **/
      
    
    import java.text.DecimalFormat; // allows for decimal formatting
    import java.util.Scanner; // allows for user input
     
    	public class PossibleDownPayments {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		//beginning of user input
    		System.out.println("Enter the price of the house: ");
    		double housePrice = input.nextDouble();
    		System.out.println("Enter the annual interest rate: ");
    		double annualInterestRate = input.nextDouble();
    		System.out.println("Enter the number of payments: ");
    		double numberofPayments = input.nextDouble();
     		
    		DecimalFormat df = new DecimalFormat("0.00");// decimal formatting change
    		
    		//beginning of if statements
    		if (annualInterestRate < .025 ){
    			System.out.println("Error: Interest rate too low! ");
    			System.exit(0);
    		}
    			else if  (annualInterestRate > .05 ){
    				System.out.println("Error: Interest rate too high! ");
    				System.exit(0);
    		}
     
    			else {
    				System.out.println( "Your annual interest rate is:  " +(df.format(annualInterestRate)));
    			}		
    		if (numberofPayments > 0 ){
     
    		System.out.println( "Your number of payments are: " + (int)numberofPayments);	
    		}
    		else {
    			System.out.println( "Error: must be higher than 0");	
    			System.exit(0);
    		}
    		
    		if (housePrice > 0){
    		System.out.println( "The price of the house is: $ " + (df.format(housePrice)));
    		System.out.println("");
    		}
    		else {
    			System.out.println("Error: Price must be greater than 0! ");
    			System.exit(0);
    		}
    		
    		
    		for (double myPercent = 0; myPercent <=1; myPercent+=.1 ){ 	//for loop calculate 11 cases of down payments
    			
    				double downPayment=(myPercent * housePrice);
    				double monthlyPayment = (((housePrice - downPayment) * (annualInterestRate/12))/((1-(Math.pow(1+(annualInterestRate/12), -numberofPayments)))));
    				//calculation done in the loop
    		
    		System.out.println( "Your down payment is: $ " + (df.format(downPayment)));
    		System.out.println( "Your monthly payment is: $ " + (df.format(monthlyPayment)));
    		System.out.println("");
    		}
    		
     
    	}	
    	}
    //end
    code 3:

    Code:
    
     
    /**
    * Project 1-ReSubmittion
    * File: MethodVersion
    * Date of creation:3/25/2013
    * Date of modification: 3/25/2013
    * Author: Adam Gonia
    **/
    
    
    import java.text.DecimalFormat;  //allows for decimal formatting
    import java.util.Scanner;  //allows for user input
     
    	public class MethodVersion {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);// where user enters values
    		System.out.println("Please enter two values for each question: ");
    		System.out.println("");
    		System.out.println("Enter the price of the house: ");
    		double housePrice = input.nextDouble();
    		double housePrice2 = input.nextDouble();
    		System.out.println("Enter the down payment: ");
    		double downPayment = input.nextDouble(); 
    		double downPayment2 = input.nextDouble(); 
    		System.out.println("Enter the annual interest rate: ");
    		double annualInterestRate = input.nextDouble();
    		double annualInterestRate2 = input.nextDouble();
    		System.out.println("Enter the number of payments: ");
    		double numberofPayments = input.nextDouble();
    		double numberofPayments2 = input.nextDouble();
    		System.out.println("");System.out.println("");
    		double monthlyPayments = MortgageMethod(housePrice, downPayment, annualInterestRate, numberofPayments );//the return of the method
    		double monthlyPayments2 = MortgageMethod(housePrice2, downPayment2, annualInterestRate2, numberofPayments2 );// the return of the method
    		double annualInterestRatePercent = (annualInterestRate * 100);
    		double annualInterestRatePercent2 = (annualInterestRate2 * 100);
    		DecimalFormat df = new DecimalFormat("0.00"); // decimal formatting change
    		
    		//beginning of if statements
    		if (housePrice > 0 && housePrice2 > 0){
    			System.out.println( "The 1st price of the house is:$" + (df.format(housePrice)));
    			System.out.println( "The 2nd price of the house is: $" + (df.format(housePrice2)));
    			}
    			else {
    				System.out.println("Error: Price must be greater than 0! ");
    				System.exit(0);
    			}
    		System.out.println("");
    		if (downPayment > 0 && downPayment2 > 0){
    			System.out.println( "Your 1st down payment would be: $" + (df.format(downPayment)));
    			System.out.println( "Your 2nd down payment would be: $" + (df.format(downPayment2)));
    			}
    			else {
    				System.out.println("Error: Price must be greater than 0! "); //error message if under 1
    				System.exit(0);
    			}
    		System.out.println("");
    		
    		if (annualInterestRate < .025 && annualInterestRate2 < .025 ){  //this block isnt working properly the if and th else if
    			                                                             //dont seem to work if you enter above or below the requirements it still runs the computation.
    			System.out.println("Error: Interest rate too low! ");
    			System.exit(0);
    		}
    			else if  (annualInterestRate > .05 && annualInterestRate2 > .05 ){
    				System.out.println("Error: Interest rate too high! ");
    				System.exit(0);
    		}
     
    			else {
    				System.out.println( "Your 1st annual interest rate would be: " +(df.format(annualInterestRatePercent))+" %");
    				System.out.println( "Your 2nd annual interest rate would be: " +(df.format(annualInterestRatePercent2))+" %");
    			}		
    		System.out.println("");
    		
    		if (numberofPayments > 0 && numberofPayments2 > 0){
    			System.out.println( "Your 1st number of payments would be: " + (int)numberofPayments);	
    			System.out.println( "Your 2nd number of payments would be: " + (int)numberofPayments2);	
    			}
    			else {
    				System.out.println( "Error: must be higher than 0");	
    				System.exit(0);
    		}
    				
    		System.out.println("");
    		System.out.println( "Your 1st monthly payment would be: $" + (df.format(monthlyPayments)));
    		System.out.println( "Your 2nd monthly payment would be: $" + (df.format(monthlyPayments2)));
    		}
    	
    	//method MortgageMethod passes the input values so they can be calculated.
    		public static double MortgageMethod(double tempHouse, double tempDownPayment,  double tempInterest, double tempNumberofPayments){
    		
    				double paymentPass = (((tempHouse - tempDownPayment) * (tempInterest/12))/((1-(Math.pow(1+(tempInterest/12), -tempNumberofPayments)))));
    				//formula used for payment
    			
    				return paymentPass;
    		}
    	}
    //end

    if someone could please check
  2. #32
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    41
    Rep Power
    2
    fixed it! I needed to use ||or instead of && in the if statement
Page 3 of 3 First 123
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo