alright so the last thing i needed to do is rewrite to code so that i can enter values for each question and then have passed to a method for calculation and then passed backed for both to be printed out. Now it all seems to work but there are some exemptions i put in that are to print errors and stop the program. the ones in the annual interest rate if block dont work, they allow values above and below the parameters set to be calculated.
heres my code
import java.text.DecimalFormat;
import java.util.Scanner;
public class MethodVersion {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
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 );
double monthlyPayments2 = MortgageMethod(housePrice, downPayment, annualInterestRate, numberofPayments );
DecimalFormat df = new DecimalFormat("0.00");
if (housePrice > 0 && housePrice2 > 0){
System.out.println( "The price of the house1 is: $ " + (df.format(housePrice)));
System.out.println( "The price of the house2 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(annualInterestRate)));
System.out.println( "Your 2nd annual interest rate would be: " +(df.format(annualInterestRate2)));
}
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)));
}
public static double MortgageMethod(double tempHouse, double tempDownPayment, double tempInterest, double tempNumberofPayments){
//for (double myPercent = 0; myPercent <=1; myPercent+=.1 ){
//double downPayment=(myPercent * tempHouse);
double paymentPass = (((tempHouse - tempDownPayment) * (tempInterest/12))/((1-(Math.pow(1+(tempInterest/12), -tempNumberofPayments)))));
return paymentPass;
}
}
please check
--- Update ---
fixed it needed to use or in stead of and in the if statement
/**
* 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