Monthly payment input calculator - java

I'm pretty new to java, and I'm trying to write a program that will give me the monthly payments, interest total and total amount paid on a bank loan, but I believe either my math is wrong or is incorrectly formatted, because when I run it I get numbers in the negatives, and payments that I know are wrong. Could you point out where I have made the mistake?
Example :
7500 (amount borrowed)
14.5 (loan rate)
3 (# of years)
The expected output would be
258.16 (monthly payment)
1793.66 (interest paid)
9293.66 (total paid).
Code :
import java.io.*;
import java.util.*;
public class Prog58i
{
public static void main(String args[])
{
Scanner numberReader = new Scanner(System.in);
System.out.print("The amount I wish to borrow is? ");
int p = numberReader.nextInt();
System.out.print("The loan rate I can get is? ");
double r = numberReader.nextDouble();
System.out.print("How mny years will it take me to pay off the loan? ");
int m = (numberReader.nextInt())*12;
double MP = (1 +(r/1200));
MP = Math.pow(MP, m);
double payment = p *(r/1200) * (MP/(MP-1));
payment = (int)(m * 100+0.5)/100.0;
double total = (int)((m * payment)*100)/100.0;
double intetotal = (int)((total - p)*100)/100.0;
System.out.println("My monthly payments will be " + payment);
System.out.println("Total Interest Paid is " + intetotal);
System.out.println("Total amount paid is " + total);
}
}

According to your formula, this statement seems to be wrong
double MP = (1 + (r / 1200));
MP = Math.pow(MP, m);
The power is only on (r / 1200) not on (1 + (r / 1200))

Related

Java ROI Compound Calculator

I have been trying to make a program that takes in:
Initial Value (in billions)
Growth Rate / Year (in billions)
Purchase Price (in billions)
And then is able to calculate the number of years it would take to break even on the investment. I have been able to accomplish this with a brute force algorithm.
I was wondering if there was a way to do this more efficiently (in a way that is more similar to standard algebra).
My Code:
import java.util.Scanner;
public class ReturnOnInvestment {
public static double initialValue;
public static double growthRate;
public static double purchasePrice;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(" Return on Investment Calculator ");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.print(" Starting Value (In Billions): ");
initialValue = input.nextDouble();
System.out.print(" Growth Rate (Per Year in Billions): ");
growthRate = input.nextDouble();
System.out.print(" Purchase Price (In Billions): ");
purchasePrice = input.nextDouble();
input.close();
System.out.println("-----------------------------------------");
System.out.println(" ROI Period: " + calculateYears(0) + " Years");
}
public static double calculateMoney(double years) {
if(years < 1) return 0;
return calculateMoney(years - 1) + initialValue + (growthRate * (years - 1));
}
public static double calculateYears(double years) {
if(calculateMoney(years) >= purchasePrice) return Math.round(years * 100) / 100.0;
return calculateYears(years + 0.01);
}
}
Yes - you can use the logarithm function for that.
Given your Java code you can write:
public static double yearsNeeded(double initialValue, double growthRate, double purchasePrice) {
return Math.log(purchasePrice / initialValue) / Math.log(1 + growthRate);
}
with an example:
public static void main(String[] args) {
System.out.println("Years to go from 100 to 150 with a growth rate of 5%: "
+ yearsNeeded(100, .05, 150));
}
Basically you're trying to solve for "years":
initialValue * (1 + growthRate) ^ years = purchasePrice
where ^ means exponentiation.
You can rewrite that to:
(1 + growthRate) ^ years = purchasePrice / initialValue
which turns into:
years = [1 + growthRate] log (purchasePrice / initialValue)
where the base of the log is "1 + growthRate". And a log of another base is the same as the log in any base divided by the log of the base.

How to store 2 variables and compare them

I am trying to accept user input for two people's hourly wage and the amount of hours of overtime they work per year.
using an algorithm I have researched, the program will tell both people the amount of money they make per year and the amount of taxes they pay, which is based on the amount that they make.
This is all fine and dandy. However, what I am now trying to do is to add a line at the end of the program which states who is paying more taxes. This would be accomplished with the method whoPaysMoreTaxes, but I have no idea what to include in that method. I know I would need a simple if/ else if/ else statement to get the job done, but I do not know how I would go about storing the taxes of person 1 and the taxes of person 2 and compare them. The output should be as follows I believe. The numbers 22, 100, 58, and 260 are user input:
Person 1's hourly wage: 22
Person 1's overtime hours for the year: 100
You will make $45540 this year
And you will pay $9108 in taxes
Person 2's hourly wage: 58
Person 2's overtime hours for the year: 260
You will make $133980 this year
And you will pay $40194 in taxes.
Person 2 is paying more taxes.
The issue I am having is finding a way to produce that final line that says who is paying more taxes.
public class conditionalsAndReturn
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
taxes(console, 1);
taxes(console, 2);
}
public static void taxes(Scanner console, int personNum)
{
System.out.print("Person " + personNum + "'s hourly wage: ");
int wage = console.nextInt();
System.out.print("Person " + personNum + "'s overtime hours for the year: ");
double totalOvertimeHours = console.nextInt();
int salary = annualSalary(wage, totalOvertimeHours);
System.out.println("You will make $" + salary + " this year");
System.out.println("And you will pay $" + taxation(salary) + " in taxes");
System.out.println();
}
public static int annualSalary(int wage, double totalOvertimeHours)
{
double workHoursPerWeek = 40 + totalOvertimeHours / 48;
return (int)(weeklyPay(wage, workHoursPerWeek) * 48);
}
public static double weeklyPay(int wage, double workHoursPerWeek)
{
if (workHoursPerWeek > 40)
{
return (wage * 40) + ((wage + wage / 2.0) * (workHoursPerWeek - 40));
}
else
{
return wage * workHoursPerWeek;
}
}
public static int taxation(int salary)
{
if (salary < 20000)
{
return 0;
}
else if (salary > 100000)
{
return salary * 3 / 10;
}
else
{
return salary * 2 / 10;
}
}
public static String whoPaysMoreTaxes(
}
The OOP conform coding would be, to have a class person (or better employee), with the fields: personNum, one or more of the three wage/salary variables, taxation. Add name and such if needed.
Now you can use instances of those class to store the accumulated data, and compare the objects with a compareTo.
If you were to follow true Object Oriented programming principles, then you might create a separate class which represents a Person object (or consider a nested class). Then each Person instance could have the attributes:
hourly_wage
overtime_hours
income
taxes_owed
You would then want to create as many People classes as you need, using the class instances to store data. You could then modify your method header to be:
public Person who_payes_more_taxes(Person p1, Person p2): { ... }
Inside the method you would need to decide how to compare taxes, but most likely it will look something like:
if (p1.taxes_owed > p2.taxes_owed) { return p1 }
You're definitely on the right track. I would use more variables to simplify comparing the taxes:
import java.util.Scanner;
public class ConditionalsAndReturn
{
public static void main(String[] args)
{
int personOneWage;
int personOneOvertime;
double personOnePayBeforeTax;
double personOneTaxes;
double personOneNetIncome;
int personTwoWage;
int personTwoOvertime;
double personTwoPayBeforeTax;
double personTwoTaxes;
double personTwoNetIncome;
Scanner scan = new Scanner(System.in);
System.out.print("Person 1's hourly wage: ");
personOneWage = scan.nextInt();
System.out.print("Person 1's overtime hours for the year: ");
personOneOvertime = scan.nextInt();
personOnePayBeforeTax = (40 * personOneWage) + (personOneOvertime * personOneWage * 1.5);
personOneTaxes = taxes(personOnePayBeforeTax);
personOneNetIncome = personOnePayBeforeTax - personOneTaxes;
System.out.println("You will make $" + personOneNetIncome + " this year");
System.out.println("And you will pay $" + personOneTaxes + " in taxes");
System.out.print("Person 2's hourly wage: ");
personTwoWage = scan.nextInt();
System.out.print("Person 2's overtime hours for the year: ");
personTwoOvertime = scan.nextInt();
personTwoPayBeforeTax = (40 * personTwoWage) + (personTwoOvertime * personTwoWage * 1.5);
personTwoTaxes = taxes(personTwoPayBeforeTax);
personTwoNetIncome = personTwoPayBeforeTax - personTwoTaxes;
System.out.println("You will make $" + personTwoNetIncome + " this year");
System.out.println("And you will pay $" + personTwoTaxes + " in taxes");
if (personOneTaxes > personTwoTaxes)
{
System.out.println("Person 1 is paying more in taxes.");
}
else
{
System.out.println("Person 2 is paying more in taxes.");
}
scan.close();
}
private static double taxes(double payBeforeTax)
{
if (payBeforeTax < 20000)
{
return 0;
}
else if (payBeforeTax > 100000)
{
return payBeforeTax * 3 / 10;
}
else
{
return payBeforeTax * 2 / 10;
}
}
}

Code produces no output java

Im trying to write a code, that computes CD value, for every month.
Suppose you put 10,000 dollars into a CD with an annual percentage yield of 6,15%.
After one month the CD is worth:
10000 + 10000 * 6,15 / 1200 = 10051.25
After the next month :
10051.25 + 10051.25 * 6,15 / 1200 = 10102.76
Now I need to display all the results for the specific number of months entered by the user,
So
month1 =
month2 =
But whth this code I wrote, nothing is printed.
Can you see what's wrong?
Thanks in advance!
import java.util.Scanner;
public class CDValue {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter an amount");
double amount = input.nextInt();
System.out.println ("Enter the annual percentage yield");
double percentage = input.nextDouble();
System.out.println ("Enter the number of months");
int months = input.nextInt();
double worth = amount + amount * percentage / 1200;
for (int i = 1; i < months; i++) {
while (i != months) {
amount = worth;
worth = amount + amount * percentage / 1200;
}
System.out.print(worth);
You do not modify neither i nor months in
while (i != months) {
....
}
so if the (i != months) condition is satisfied, the loop runs forever, and you never get to System.out.print statement.
for (int i = 1; i < months; i++) {
while (i != months) {
//you have to modify i or to modify the while condition.
}
if you don't modify i in the while you can't exit from the loop
Corrected code-
import java.util.Scanner;
public class CDValue {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter an amount");
double amount = input.nextInt();
System.out.println ("Enter the annual percentage yield");
double percentage = input.nextDouble();
System.out.println ("Enter the number of months");
int months = input.nextInt();
double worth = amount + amount * percentage / 1200;
for (int i = 1; i <= months; i++)
{
System.out.print("Month " + i + " = " + worth);
amount = worth;
worth = amount + amount * percentage / 1200;
}
Note: If you want to print values for each month then the print statement should be inside the loop. You don't need two loops for the objective that you have mentioned above.
As you have been told your code won't get out of the while loop if you don't modify it. Simply remove the while loop. Your code should be like this:
import java.util.Scanner;
public class CDValue {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter an amount");
double amount = input.nextDouble();
System.out.println ("Enter the annual percentage yield");
double percentage = input.nextDouble();
System.out.println ("Enter the number of months");
int months = input.nextInt();
double worth = amount + amount * percentage / 1200;
for (int i = 1; i < months; i++) {
amount = worth;
worth = amount + amount * percentage / 1200;
}
System.out.print(worth);
}
}
Thanks! Solved it by using
{
System.out.print("Month " + i + " = " + worth);
amount = worth;
worth = amount + amount * percentage / 1200;
instead of while loop.
It works now :) Thanks so much!

What are the two errors in my program?

Can you help me to determine the error here ?
Please please can you fix it please
import java.util.*;
import java.lang.*;
class Loan_payment
{
public static void main(String args[])
{
double inst=0.4, loan_amt=0.56, pay=0.34;
double mon = (inst/1200);
double quarterly = (inst/400);
double Half = (inst/200);
double annual = (inst/100);
//Mothly
double ad = 1 + mon;
double amt,amt_mon,amt2;
amt_mon = -1*((Math.log( 1 - ((mon * loan_amt) / pay))));
amt2 =(Math.log(ad));
amt = (amt_mon / amt2);
System.out.println("Number of Payments based on monthly : "+amt);
//Quarterly
ad = 1 + quarterly;
double amt_quart;
amt_quart = (( -Math.log( 1 - ((quarterly * loan_amt) / pay))));
amt = (amt_quart / amt2);
System.out.println("Number of Payments based on quarterly : "+amt);
//HalfYearly
ad = 1 + Half;
double amt_half;
amt_half = (( -Math.log( 1 - ((Half * loan_amt) / pay))));
amt = (amt_half / amt2);
System.out.println("Number of Payments based on HalfYearly : "+amt);
//Annually
ad = 1 + annual;
double amt_ann;
amt_ann = (( -Math.log( 1 - ((annual * loan_amt) / pay))));
amt = (amt_ann / amt2);
System.out.println("Number of Payments based on Annually : "+amt);
}
}
Can you help me to determine the error here?
No errors whatsoever, giving output :-
Number of Payments based on monthly : 1.6477856928143282
Number of Payments based on quarterly : 4.94607431098997
Number of Payments based on HalfYearly : 9.900315277456814
Number of Payments based on Annually : 19.833405363765696

Proper Formatting

I have the program working I just need help cutting off the extra numbers, Im not very skilled at using the printf statements when printing in Java. When I run it I get output like 1225.043 Here is what I have:
import java.util.Scanner;
public class Comparison {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
float amount;
double principal = 1000.00;
double rate;
System.out.println("Enter interest rate");
rate = keyboard.nextDouble();
System.out.println("Year" +" "+ "Amount on deposit");
for(int year = 1; year <= 10; ++year)
{
amount = (float) (principal * Math.pow(1.0 + rate, year));
System.out.println(year+ " "+ amount);
System.out.println();
}
}
}
Try
System.out.printf("%2d %.2f%n", year, amount);
Output:
Enter interest rate
0.1
Year Amount on deposit
1 1100.00
2 1210.00
3 1331.00
4 1464.10
5 1610.51
6 1771.56
7 1948.72
8 2143.59
9 2357.95
10 2593.74

Categories

Resources