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.
Related
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))
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
fix me plz. i get multiple error messages
"variable airSpeed_km might not have been initialized"
"variable width might not have been initialized"
"variable length might not have been initialized"
import java.util.Scanner;
public class V4_________1{
public static void main (String args[])
{
Scanner keyboard = new Scanner(System.in);
double KNOTS_TO_KMPHR;
double airSpeed_km;
double airSpeed_knots;
double width;
double length;
***// need to do something in the main but not sure what exactly***
airSpeed_knots = keyboard.nextDouble();
System.out.println("what is your current airspeed in knots?");
System.out.println("your current airspeed in km is: " + airSpeed_km + "your holding pattern width is: " + width + "your holding patter length is: " + length);
}
public static double getAirSpeed(double airSpeed_knots, double KNOTS_TO_KMPHR, double airSpeed_km)
{
KNOTS_TO_KMPHR = 1.852;
airSpeed_km = airSpeed_knots * KNOTS_TO_KMPHR ;
return airSpeed_km;
}
public static double calcPatternWidth(double width, double airSpeed_km)
{
width = (airSpeed_km) / (60 * Math.PI) * 2;
return width;
}
public static double calcPatternLength(double airSpeed_km, double length)
{
length = (airSpeed_km) / (60 * Math.PI) * 2 + ((airSpeed_km) / 60);
return length;
}
}
You declare:
double airSpeed_km;
And after use it:
System.out.println("your current airspeed in km is: " + airSpeed_km + "your holding pattern width is: " + width + "your holding patter length is: " + length);
without any assignment. So you get an error, you can prevent this by giving it a default value of 0 for example.
double airSpeed_km = 0;
(same goes for your other errors)
In Java, the compiler gets upset if a variable even MIGHT be used without a value.
So it is best practice to always give a value to variables when you first declare them.
Since you normally don't know the value a variable will have at the time of declaration, it is common practice to give it a value of zero.
So your declarations should look like this:
double KNOTS_TO_KMPHR=0;
double airSpeed_km=0;
double airSpeed_knots=0;
double width=0;
double length=0;
This will take care of all your "[] might not have been initialized" compiler errors.
Your code is in correct. Your cannot set variable when you pass them into a function. See both approaches and understand what going on.
You can do this:
public static void main (String args[])
{
Scanner keyboard = new Scanner(System.in);
double KNOTS_TO_KMPHR=1.852;
double airSpeed_knots;
System.out.println("what is your current airspeed in knots?");
airSpeed_knots = keyboard.nextDouble();
System.out.println("your current airspeed in km is: " + getAirSpeed(airSpeed_knots) + "your holding pattern width is: " + calcPatternWidth(getAirSpeed(airSpeed_knots)) + "your holding patter length is: " + calcPatternLength(getAirSpeed(airSpeed_knots));
}
public static double getAirSpeed(double airSpeed_knots)
{
return airSpeed_knots * KNOTS_TO_KMPHR ;
}
public static double calcPatternWidth(double airSpeed_km)
{
return (airSpeed_km) / (60 * Math.PI) * 2;
}
public static double calcPatternLength(double airSpeed_km)
{
return (airSpeed_km) / (60 * Math.PI) * 2 + ((airSpeed_km) / 60);
}
Or do this if you want to set variables:
public static void main (String args[])
{
Scanner keyboard = new Scanner(System.in);
double KNOTS_TO_KMPHR=1.852;
double airSpeed_knots;
System.out.println("what is your current airspeed in knots?");
airSpeed_knots = keyboard.nextDouble();
double airSpeed_km=getAirSpeed(airSpeed_knots);
double width=calcPatternWidth(airSpeed_km);
double length= calcPatternLength(airSpeed_km);
System.out.println("your current airspeed in km is: " + airSpeed_km + "your holding pattern width is: " + width + "your holding patter length is: " + length);
}
public static double getAirSpeed(double airSpeed_knots)
{
return airSpeed_knots * KNOTS_TO_KMPHR ;
}
public static double calcPatternWidth(double airSpeed_km)
{
return (airSpeed_km) / (60 * Math.PI) * 2;
}
public static double calcPatternLength(double airSpeed_km)
{
return (airSpeed_km) / (60 * Math.PI) * 2 + ((airSpeed_km) / 60);
}
I have a method which calulates intrests.
public static float calculateIntrests (float Principal, float RateInPercentage){
float result;
result=Principal*RateInPercentage;
return result;
Now I need a loop to print me how much "money" do I have each year. I have done something like this.
import java.util.Scanner;
public class Aplikacija {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int years = 1;
System.out.println("Principal!");
float Principal = input.nextInt();
System.out.println("Rate!");
int Rate = input.nextInt();
float RateInPercentage = Rate / 100.0f;
float calculate = Caluclateintrests(Principal, rateinPercentage);
while (year <= 5) {
System.out.println("It is: " + (calculate) + " dollars in year" + year + "on your account");
year++;
}
input.close();
}
}
Java is a case sensitive, so if you declared variable, method or class with a specific name then you have to sick with the declared name in order to use it,
So you have declared years but you used year instead
int years = 1;
But used as:
while (year <= 5) {//
So here you can do two of thins:
change the declaration of years to be year.
change the using of that variable to years.
This also goes with RateInPercentage variable, Caluclateintrests method.
You have lots and lots of mistakes. Remember to spell it right and that Java is case-sensitive, therefore you have to keep eye on capital letters.
And few mistakes are not syntax, but in logical matter.
Also the naming convention for Java suppose to start naming variables and methods with lower-case letter.
This is working code, try to learn from it :
import java.util.Scanner;
public class Aplikacija {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 1;
System.out.println("Principal!");
float principal = input.nextInt();
System.out.println("Rate!");
int rate = input.nextInt();
float rateInPercentage = rate / 100.0f;
while (year <= 5) {
principal += calculateIntrests(principal, rateInPercentage);
System.out.println("It is: " + principal + " dollars in year" + year + " on your account");
year++;
}
input.close();
}
public static float calculateIntrests(float principal, float rateInPercentage) {
float result;
result = principal * rateInPercentage;
return result;
}
}
Sample output :
Principal!
100
Rate!
1
It is: 101.0 dollars in year1 on your account
It is: 102.01 dollars in year2 on your account
It is: 103.030106 dollars in year3 on your account
It is: 104.06041 dollars in year4 on your account
It is: 105.10101 dollars in year5 on your account
I am having an issue with a method returning to the main method. It is saying that amount in "return amount" cannot be resolved to a variable. Where am I off on this??
This is the message I get:
Multiple markers at this line
- Void methods cannot return a
value
- amount cannot be resolved to a
variable
Here is the code:
import java.util.Scanner;
public class Investment {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount invested: ");
double amount = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
double interest = input.nextDouble();
int years = 30;
System.out.print(futureInvestmentValue(amount, interest, years)); //Enter output for table
}
public static double futureInvestmentValue(double amount, double interest, int years) {
double monthlyInterest = interest/1200;
double temp;
double count = 1;
while (count < years)
temp = amount * (Math.pow(1 + monthlyInterest,years *12));
amount = temp;
System.out.print((count + 1) + " " + temp);
}
{
return amount;
}
}
You curly braces are not correct. The compiler - and me - was confused about that.
This should work (at least syntactically):
import java.util.Scanner;
public class Investment {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount invested: ");
double amount = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
double interest = input.nextDouble();
int years = 30;
System.out.print(futureInvestmentValue(amount, interest, years));
}
public static double futureInvestmentValue(
double amount, double interest, int years) {
double monthlyInterest = interest / 1200;
double temp = 0;
double count = 1;
while (count < years)
temp = amount * (Math.pow(1 + monthlyInterest, years * 12));
amount = temp;
System.out.print((count + 1) + " " + temp);
return amount;
}
}
Remove amount from its own scope As a start. Also from the method futureInvestmentValue, you take in amount as an argument but the value is never modified so you're returning the same value being passed which is most likely not the desired outcome.
remove return amount from its own scope
the method futureInvestmentValue... You can't modify any of the parameters inside the method so you have to declare another variable besides amount inside the method (maybe it's the temp variable you keep using) and return that instead
when you return something, the return statement is always inside the method. Never outside it while inside its own braces (never seen this before...)
import java.util.Scanner;
public class Investment {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount invested: ");
double amount = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
double interest = input.nextDouble();
int years = 30;
System.out.print(futureInvestmentValue(amount, interest, years)); //Enter output for table
}
public static double futureInvestmentValue(double amount, double interest, int years) {
double monthlyInterest = interest/1200;
double temp;
double count = 1;
while (count < years) {
temp = amount * (Math.pow(1 + monthlyInterest,years *12));
System.out.print((count + 1) + " " + temp);
}
return amount;
}
}