104k views
2 votes
Write a program HousingCost.java to calculate the amount of money a person would pay in renting an apartment over a period of time. Assume the current rent cost is $2,000 a month, it would increase 4% per year. There is also utility fee between $600 and $1500 per year. For the purpose of the calculation, the utility will be a random number between $600 and $1500.

1. Print out projected the yearly cost for the next 5 years and the grand total cost over the 5 years.
2. Determine the number of years in the future where the total cost per year is over $40,000 (Use the appropriate loop structure to solve this. Do not use break.)

1 Answer

1 vote

Answer:

import java.util.Random;

public class HousingCost {

public static void main(String[] args) {

int currentRent = 2000;

double rentIncreaseRate = 1.04;

int utilityFeeLowerBound = 600;

int utilityFeeUpperBound = 1500;

int years = 5;

int totalCost = 0;

System.out.println("Year\tRent\tUtility\tTotal");

for (int year = 1; year <= years; year++) {

int utilityFee = getRandomUtilityFee(utilityFeeLowerBound, utilityFeeUpperBound);

int rent = (int) (currentRent * Math.pow(rentIncreaseRate, year - 1));

int yearlyCost = rent * 12 + utilityFee;

totalCost += yearlyCost;

System.out.println(year + "\t$" + rent + "\t$" + utilityFee + "\t$" + yearlyCost);

}

System.out.println("\\Total cost over " + years + " years: $" + totalCost);

int futureYears = 0;

int totalCostPerYear;

do {

futureYears++;

totalCostPerYear = (int) (currentRent * 12 * Math.pow(rentIncreaseRate, futureYears - 1)) + getRandomUtilityFee(utilityFeeLowerBound, utilityFeeUpperBound);

} while (totalCostPerYear <= 40000);

System.out.println("Number of years in the future where the total cost per year is over $40,000: " + futureYears);

}

private static int getRandomUtilityFee(int lowerBound, int upperBound) {

Random random = new Random();

return random.nextInt(upperBound - lowerBound + 1) + lowerBound;

}

}

User Jamband
by
8.2k points

No related questions found