So in my intro to java book, I was tasked with this:
Suppose that the tuition for a university is $10,000 this year and increases 5% every year. In one year, the tuition will be $10,500. Write a program that computes the tuition in ten years and the total cost of four years' worth of tuition after the tenth year.
I can calculate the tenth year tuition easily enough, but what has me stumped is how to add the unique tuition values at years 11, 12, 13 and 14. If I'm right, it should add up to 73717.764259. What my code is giving me is 158394.52795515177. As code goes, It may just be that i'm thinking about this in the wrong way and that my code did the addition correctly, but I think I'm more right here. Here's the code i'm using now:
double initialTuition = 10000;
final double theRate = 0.05;
for (int i = 1; i < 15; i++) {
initialTuition = ((theRate * initialTuition) + initialTuition);
System.out.println("Year " + i + " tuition is: " + initialTuition);
while (i == 10){
System.out.println("Year " + i + " tuition is: " + initialTuition);
double startOfFourYearTuition = initialTuition;
System.out.println(startOfFourYearTuition);
break;
}
while ((i > 10) && (i < 15)) {
System.out.println("Year " + i + " tuition is: " + initialTuition);
initialTuition += initialTuition;
break;
}
}
The last while loop is my attempt at adding the 4 years.
To reiterate the question, How could I pull out the unique values of initialTuition at iterations 11 to 14 and add them?
You also have a lot of gratuitous code in there, while loops inside of for loops, etc. Try this on for size:
public static void main(String[] args)
{
double initialTuition = 10000;
double summarizedTuition = 0;
final double theRate = 0.05;
for (int i = 1; i < 15; i++) {
initialTuition = ((theRate * initialTuition) + initialTuition);
System.out.println("Year " + i + " tuition is: " + initialTuition);
if ((i > 10) && (i < 15))
{
summarizedTuition += initialTuition;
}
}
System.out.println("Summarized tuition for years 11 - 14: " + summarizedTuition);
}
The output is:
Year 1 tuition is: 10500.0
Year 2 tuition is: 11025.0
Year 3 tuition is: 11576.25
Year 4 tuition is: 12155.0625
Year 5 tuition is: 12762.815625
Year 6 tuition is: 13400.95640625
Year 7 tuition is: 14071.0042265625
Year 8 tuition is: 14774.554437890625
Year 9 tuition is: 15513.282159785156
Year 10 tuition is: 16288.946267774414
Year 11 tuition is: 17103.393581163135
Year 12 tuition is: 17958.56326022129
Year 13 tuition is: 18856.491423232354
Year 14 tuition is: 19799.31599439397
Summarized tuition for years 11 - 14: 73717.76425901074
double theRate = 1.05; // rate of increase +5%
double tuitionCost = 10000; // base cost
double fourYearCost = 0; // four year cost (years 10-13)
for (int i = 0; i <= 13; i++) {
System.out.println("Cost of year: " + i + " is $" + tuitionCost); // print out this years cost
if(i >= 10){ //if its year 10, 11, 12, 13
fourYearCost += tuitionCost; // add this years tuition cost
}
tuitionCost *= theRate; // increment the tuition (mutiply it by 1.05, or 5% increase)
}
System.out.println("Cost of tuition for years 10, 11, 12, 13: $" + fourYearCost); // print the cost for the remaining years
Related
The task is to create a loan calculator based on the user input of min and max years of loan payments, loan amount, and min and max % rate with incremented value given by user as well for rate and number of years.
Desired output should look like this:
Principle: $275000.0
Years to repay: 10
Interest Monthly
Rate Payment
6.25 3087.7
6.75 3157.66
7.25 3228.53
Principle: $275000.0
Years to repay: 15
Interest Monthly
Rate Payment
6.25 2357.91
6.75 2433.5
7.25 2510.37
Principle: $275000.0
Years to repay: 20
Interest Monthly
Rate Payment
6.25 2010.05
6.75 2091.0
7.25 2173.53
Please help me fix errors. Thanks!
public static void main(String[] args){
Scanner console = new Scanner (System.in);
System.out.println("This program computes monthly " + "mortgage payments.");
System.out.print("Enter the loan amount: ");
double loan = console.nextDouble();
System.out.print("Enter the starting number of years to repay the loan: ");
int startingYears = console.nextInt();
System.out.print("Enter the ending number of years to repay the loan: ");
int endingYears = console.nextInt();
System.out.print("Enter the years increment between tables: ");
int incrementYears = console.nextInt();
System.out.print("Enter the starting loan yearly interest rate, %: ");
double startingRate = console.nextDouble();
System.out.print("Enter the ending loan yearly interest rate, %: ");
double endingRate = console.nextDouble();
System.out.print("Enter the increment interest rate, %: ");
double incrementRate = console.nextDouble();
System.out.println();
System.out.println("Principle: $" + (double) loan);
System.out.printf("Years to repay: %d\n", startingYears);
System.out.println("-------- -------");
double payment;
System.out.println("Rate\tPayment");
for (int j = startingYears; j <= endingYears; incrementYears++) {
for (double i = startingRate; i <= endingRate; incrementRate++){
int n = 12 * startingYears;
double c = startingRate / 12.0 / 100.0;
payment = loan * c * Math.pow(1 + c, n) / (Math.pow(1 +c, n) - 1);
System.out.println(i + " " + payment);
// System.out.println(round2(startingRate) + "\t" + round2(payment));
startingYears += incrementYears;
}
}
}
}
for (int j = startingYears; j <= endingYears; incrementYears++) {
for (double i = startingRate; i <= endingRate; incrementRate++) {
You are never incrementing the values of your counters i.e. i and j and hence the values of i and j will always be equal to the initialized values and these loops will run for infinite times.
Increment both the counters to be able to reach the termination condition i.e. j <= endingYears and i <= endingRate respectively.
Here is the code snippet:
System.out.println("Principle: $" + (double) loan);
for (int j = startingYears; j <= endingYears; j+=incrementYears) {
System.out.printf("Years to repay: %d\n", j);
System.out.println("-------- -------");
System.out.println("Rate\tPayment");
for (double i = startingRate; i <= endingRate; i+=incrementRate){
int n = 12 * j;
double c = i / 12.0 / 100.0;
double payment = loan * c * Math.pow(1 + c, n) / (Math.pow(1 + c, n) - 1);
System.out.println(i + "\t" + payment);
}
System.out.println();
}
Ok so this java program I'm working on is supposed to compute an investment value over the course of 30 years. It asks the user where their starting investment is and the percentage rate (in the form of decimals). I thought I had it all figured out but my program is returning some ridiculous values. Can someone take a look at my code and tell me what I did wrong?
These are the sample outputs provided to me
What is the amount invested? 1000
What is the annual interest rate? .09
Years Future Value
----- ------------
1 $1093.81
2 $1196.41
...
29 $13467.25
30 $14730.58
my output is returning values in billions and trillions of dollars...just crazy stuff. The formula I have been provided with is
futureValue = investmentAmmount * (1 + monthlyInterestRate)^numberOfYears*12
here is the code for my program
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;
import java.text.DecimalFormat;
public class InvestmentValue
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
NumberFormat df = DecimalFormat.getCurrencyInstance(Locale.US);
double investmentAmmnt;
// monthly interest rate
double mri;
int years;
System.out.print("What is the ammount invested? ");
investmentAmmnt = input.nextDouble();
System.out.print("What is the annual interest rate? ");
mri = input.nextDouble();
futureInvestmentValue(investmentAmmnt, mri, 30);
}
public static double futureInvestmentValue(double investmentAmmnt, double mri, int years)
{
NumberFormat df = DecimalFormat.getCurrencyInstance(Locale.US);
System.out.println("The amount invested: " + (df.format(investmentAmmnt)));
System.out.println("Annual interest rate: " + mri);
System.out.println("Years \t \t Future Value");
for (int i = 1; i <= years * 12; i++){
investmentAmmnt = investmentAmmnt * Math.pow(1 + (mri / 12),(years * 12));
if (i % 12 == 0){
System.out.println(i / 12 + "\t\t" + (df.format(investmentAmmnt)));
}
}
return investmentAmmnt;
}
}
The problem is that the formula futureValue = investmentAmmount * (1 + monthlyInterestRate)^numberOfYears*12 calculates the value of the investment for any amount of years in the future. The problem is that you for loop is calculating more than it needs to. That formula only needs to be done once. Your function futureInvestmentValue should not have a for loop.
Here's how it works:
public static double futureInvestmentValue(final double investmentAmmnt, double mri, int years)
{
NumberFormat df = DecimalFormat.getCurrencyInstance(Locale.US);
double amount = investmentAmmnt;
System.out.println("The amount invested: " + (df.format(investmentAmmnt)));
System.out.println("Annual interest rate: " + mri);
System.out.println("Years \t \t Future Value");
for (int i = 1; i <= years ; i++){
amount = investmentAmmnt * Math.pow(1 + (mri /100),(i ));
System.out.println(i + "\t\t" + (df.format(amount)));
}
return amount;
}
The problems are many with your code...
Your Increasing the amount with Math.pow() in a loop - that's why it goes wild.
Your loop iterates over years * 12 (months?)
Your doing wild *12 and /12 calculations you don't need.
--> output
Years Future Value
1 $1,030.00
2 $1,060.90
3 $1,092.73
4 $1,125.51
5 $1,159.27
6 $1,194.05
7 $1,229.87
8 $1,266.77
9 $1,304.77
10 $1,343.92
11 $1,384.23
12 $1,425.76
13 $1,468.53
14 $1,512.59
15 $1,557.97
16 $1,604.71
17 $1,652.85
18 $1,702.43
19 $1,753.51
20 $1,806.11
21 $1,860.29
22 $1,916.10
23 $1,973.59
24 $2,032.79
25 $2,093.78
26 $2,156.59
27 $2,221.29
28 $2,287.93
29 $2,356.57
30 $2,427.26
In the textbook it explains how to convert seconds into minutes with how many seconds remain but the question I have is as follows.
Write a program that prompts the user to enter the minutes (e.g., 1
billion), and displays the number of years and days for the minutes.
For simplicity, assume a year has 365 days. Here is an example.
Enter the number of minutes: 100000000
100000000 minutes is approximately 1902 years and 214 days.
What I currently have is as follows:
import java.util.Scanner;
public class Ch2_Exercise2_7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt user for number of minutes
System.out.println("Enter the number of minutes:");
int minutes = input.nextInt();
// Number of minutes in a year
int year = minutes / 525600;
int day = minutes / 1440;
int remainingMinutes = day % 525600;
System.out.println(minutes + " minutes is " + year + " years and " + remainingMinutes + " days ");
}
}
With what I have it isn't giving me the remaining minutes into days. For example, if I put in 525600 minutes it gives me 1 year and 365 days when it should just be 1 year.
I'm using Java Eclipse. Any help would be greatly appreciated! My apologies in advance if I post the code incorrectly.
You screwed up a bit over here:
// Number of minutes in a year
int year = minutes / 525600;
int day = minutes / 1440;
int remainingMinutes = day % 525600;
You took the total number of minutes and divided by 1440, so the number of days you got was wrong. You should have taken the remainder and then divided by 1440.
Another thing was in your print statement. You wrote the number of minutes left after one year as the number of days.
This should work:
// Number of minutes in a year
int year = minutes / 525600;
int remainingMinutes = minutes % 525600;
int day = remainingMinutes / 1440;
System.out.println(minutes + " minutes is approximately " + year + " years and " + day + " days.");
My main method is separate:
public class MinutesToYearsDaysCalculator {
public static void printYearsAndDays(long minutes) {
double days = minutes / 60 / 24;
double years = days / 365;
double remainingDays = days % 365;
System.out.println((minutes < 0 ? "Invalid Value" : minutes + " min = " + (int) years + " y and " + (int) remainingDays + " d"));
}
}
int mins,hours,remainingDays,days,years;
cout << "Enter minutes:";
cin >> mins;
hours = mins/60;
days = hours/24;
years = days/365;
remainingDays = days % 365;
cout << years << " years " << remainingDays << " days " << endl;
return 0;
public class MinutesToYearsDaysCalculator {
public static void printYearsAndDays(long minutes)
{
if(minutes<0) System.out.println("Invalid Value");
else
{
long hours = minutes/60;
long day = hours/24;
long years = day/365;
long remainingDays = day % 365;
System.out.println(minutes +" min = "+ years+" y and "+remainingDays +" d");
}
}
import java.util.Scanner;
public class Prog13 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter The Minute");
int min=sc.nextInt();
int year= min/(365*24*60);
int remaining_min=(min%24*60*60);
int day=remaining_min/24*60;
System.out.println( min + " " + " Minutes is approximately:"+ year + " " +"year"+ ' '+ day + "Day" );
}
}
hello you can use this result
import java.util.Scanner;
public class MinToYearsAndDays {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("number of minutes:");
int minutes = input.nextInt();
int year = minutes / 525600;
int remainingMinutes = minutes % 525600;
int day = (remainingMinutes / 1440);
System.out.println(minutes + " minutes is approximately " + year + " years and " + day + " days ");
}
}
public class Mints_to_days_years {
// Given some numbers of minutes calculate the number of days and years
// from the given minutes.
// 1hr = 60 minutes
// 1 day = 24 hours
// 1 year = 365 days
public static void find_days_years(long minutes) {
if(minutes < 0) {
System.out.println("Invalid number of minutes.");
}
// to find the number of years, divide the nminutes by total numbers of minutes in a year.
long years = minutes / (60 * 24 * 365);
// then find the remainder minutes that does not goes exactly into year
long years_remainder = minutes % (60 * 24 * 365);
// divide the remainder by number of minutes in a day to find the numbers of days
long days = years_remainder / (60 * 24);
System.out.println(minutes + " mints = " + years + " yrs and " + days + " days.");
}
public static void main(String[] args) {
find_days_years(525600);
find_days_years(1051200);
find_days_years(561600);
}
}
I am writing a Java program. It supposed to be print out yearend account balance, invested this year, annual return, and number of the year. EX:
Year 1 – Invest $10,000 # 10% you end up with $11,000
Year 2 – Invest another $10,000 on top of the $11,000 you already have so now you have $21,000 and you make $2,100 in annual return so you end up with $23,100
and keep going until reach 6 years.
My code printed all 6 years but with same value as year 1. So anything wrong with the loop? Thanks so much. Here my code:
import java.io.*;
import java.util.*;
import java.text.*;
public class ExamFive {
public static void main(String[] args) {
final int MAX_INVESTMENT = 5000000;
double goalAmount;
double amountInvested;
double annualRate;
double interest;
double total= 0;
int year = 0;
Scanner myScanner = new Scanner(System.in);
System.out.println("Enter your investment goal amount: ");
goalAmount = myScanner.nextDouble();
if (goalAmount > MAX_INVESTMENT) {
System.out.println("Your goal is outside the allowed 5,000,000 limit");
}
System.out.println("Enter the amount annually invested: ");
amountInvested = myScanner.nextDouble();
System.out.println("Enter the expected annual return rate (ex 6.50): ");
annualRate = myScanner.nextDouble();
do {
interest = amountInvested * (annualRate / 100);
total = amountInvested + interest;
year++;
System.out.println("Yearend account balance " + total +
" Invested this year " + amountInvested + " Annual return " + interest
+ " number of years " + year);
} while (total < MAX_INVESTMENT && year < 6);
System.out.println(" ");
System.out.println("Your investment goal " + goalAmount);
System.out.println("Your annualinvestment amount " + amountInvested);
System.out.println("Number of years to reach your goal " + year);
}
}
Here is the output:
Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 1
Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 2
Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 3
Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 4
Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 5
Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 6
It seems that you're also calculating the interest on the annual savings only.
Change:
do {
interest = amountInvested * (annualRate / 100);
total = amountInvested + interest;
year++;
...
} while (total < MAX_INVESTMENT && year < 6);
To:
do {
total += amountInvested;
interest = total * annualRate / 100;
total += interest;
year++;
...
} while (total < MAX_INVESTMENT && year < 6);
You aren't keeping a running total.
Looks like maybe a 3rd or 4th week tutorial but it's a fair question at least you asked what's wrong.
Try: total += (amountInvested + interest);
parenthesis brackets aren't needed but I usually find them a great logical grouping device.
Check the loop below you are not adding 10,000 as you stated
do {
interest = amountInvested * (annualRate / 100);
total = amountInvested + interest;
year++;
System.out.println("Yearend account balance " + total +
" Invested this year " + amountInvested + " Annual return " + interest
+ " number of years " + year);
}
Add following line
amountInvested += (total + 10000);
as the last line in do while loop
amountInvested, annualRate are always the same. you are not incrementing or decrementing these values in the loop. So, they will always remain the same. Also, the formula to caculate the interest is incorrect, you are not using year variable anywhere. it should be,
do {
interest = amountInvested * (annualRate / 100) * year;
total = amountInvested + interest;
year++;
System.out.println("Yearend account balance " + total +
" Invested this year " + amountInvested + " Annual return " + interest
+ " number of years " + year);
} while (total < MAX_INVESTMENT && year < 6);
I'm working on a program that will calculate the basic interest accrued on a certificate of deposit. The program asks for the amount of money invested and the term (up to five years). Depending on how many years their term is, is what determines how much interest is earned. I use an if/else statement to determine the rate of interest. I then use a loop to print out how much money is in the account at the end of each year. My problem is that when I run the program, the money is not counting.
Here is the entire code.
import java.util.Scanner;
public class CDCalc
{
public static void main(String args[])
{
int Count = 0;
double Rate = 0;
double Total = 0;
Scanner userInput = new Scanner(System.in);
System.out.println("How much money do you want to invest?");
int Invest = userInput.nextInt();
System.out.println("How many years will your term be?");
int Term = userInput.nextInt();
System.out.println("Investing: " + Invest);
System.out.println(" Term: " + Term);
if (Term <= 1)
{
Rate = .3;
}
else if (Term <= 2)
{
Rate = .45;
}
else if (Term <= 3)
{
Rate = .95;
}
else if (Term <= 4)
{
Rate = 1.5;
}
else if (Term <= 5)
{
Rate = 1.8;
}
int count = 1;
while(count <= 5)
{
Total = Invest + (Invest * (Rate) / (100.0));
System.out.println("Value after year " + count + ": " + Total);
count++;
}
}
}
and here is the result I get with a 10 dollar investment, just to keep it simple, and a 5 year investment.
How much money do you want to invest?
10
How many years will your term be?
5
Investing: 10
Term: 5
Value after year 1: 10.18
Value after year 2: 10.18
Value after year 3: 10.18
Value after year 4: 10.18
Value after year 5: 10.18
My main problem is I dont know how to make it continually add the intrest onto the total. I'm not sure if I need to use a different loop or what. Any help would be appreciated.
Total = Invest + (Invest * (Rate) / (100.0));
You are not changing the value of Invest for each year, so it is not compounding. It is like you are getting .18$ of interest each year, retired from the account.
Change Total for Invest.
You need to add the investment interest to your total:
Total = Invest;
int count = 1;
while(count <= 5)
{
Total = Total + (Invest * (Rate) / (100.0));
System.out.println("Value after year " + count + ": " + Total);
count++;
}