Troubles with a math formula - Java - java

I'm making a program for my java class that calculates the population for a year given the start year (2011) and increases the population by 1.2% every year. The population for 2011 is 7.000 (I'm using decimals, instead of billions). I currently have this code.
int startYear = 2011;
int endYear = user_input.nextInt();
double t = 1.2; //Population percent increase anually
double nbr = (endYear - startYear); //Number of years increased
double pStart = 7.000; //Starting population of 2011
double pEnd = pStart * Math.exp(nbr * t); // Ending population of user input
DecimalFormat nf = new DecimalFormat("2");
System.out.println("Population in " + endYear + ": " (nf.format(pEnd)));
There's no errors in the code, everything works, but I'm having troubles with the pEnd equation. Currently when I enter 2016 for the end year, i get 22824. I've tried googling a formula, but i can't find anything. Do any of you guys have an idea of the formula? If you enter 2016 for the end year, it should be around 7.433

You're incrementing by a factor of 1.2, which would represent 120% instead of 1.2%. I think what you want is :
double t = 0.012;
This change gives me an exact value of 7.4328558258175175 from 2011 to 2016.
EDIT : here's the code as requested by the author :
public static void main(String args[]){
int startYear = 2011;
int endYear = 2016;
double t = 0.012; //Population percent increase anually
double nbr = (endYear - startYear); //Number of years increased
double pStart = 7.000; //Starting population of 2011
double pEnd = pStart * Math.exp(nbr * t); // Ending population of user input
System.out.println("Population in " + endYear + ": " + pEnd);
}

Use Math.pow(1 + t / 100, nbr) instead of Math.exp(nbr * t) because you need (1+t/100)^nbr (i.e. multiply 1 + t / 100 on itself nbr times), not exp^(nbr*t):
double pEnd = pStart * Math.pow(1 + t / 100, nbr); // Ending population of user input

Try this.
double pEnd = pStart * Math.pow(1.0 + t / 100, nbr);

Related

How can I correct getting NaN as my result?

I am trying to write a payment calculator program, but am receiving "NaN" as my result. The program asks for input on the loan amount and length of loan (in months). The program should then calculate out the monthly payments for each APR (3% -10%). I'm not sure if I have done something wrong with my calculation.
double L, payment;
double APR = 0;
int n;
Scanner input = new Scanner(System.in);
System.out.println("Loan calculator");
System.out.print("Enter the loan amount: ");
L = input.nextDouble();
System.out.print("Enter the number of payments: ");
n = input.nextInt();
double t = APR/1200.0;
for (APR = 3; APR <= 10; APR += .25){
payment = L * (t * (Math.pow((1.0 + t), n)) / (Math.pow((1.0 + t), n) - 1.0));
System.out.println(APR + "\t" + payment);
Define or at least assign t within the loop. Otherwise it will only be computed once before the loop using APR, which is 0 at that time.
for (APR = 3; APR <= 10; APR += .25){
double t = APR/1200.0;
payment = L * (t * (Math.pow((1.0 + t), n)) / (Math.pow((1.0 + t), n) - 1.0));
System.out.println(APR + "\t" + payment);

Java compound interest calculation

my assignment is to calculate the number of years it takes to reach $5000 if you start with $2500 with 7.5% interest. The calculation should be 10 years, but it outputs 11 years. This might be a simple syntax error, but I've been trying to fix this for the past 2 hours with no luck.
final double principle = 2500.00;
double accrued = 0;
final double interest = 0.075;
int year = 0;
double interest1 = 1.0 + interest;
while (accrued < 5000.00)
{
accrued = principle * Math.pow(interest1, year);
year++;
}
System.out.printf("It would take %d years to reach at least $5000 if you start with $2,500 with 7.5%% compound interest.", year);
Try this :
final double principle = 2500.00;
double accrued = 0;
final double interest = 0.075;
int year = 0;
double interest1 = 1.0 + interest;
while (accrued < 5000.00)
{
year++;
accrued = principle * Math.pow(interest1, year);
}
System.out.printf("It would take %d years to reach at least $5000 if you start with $2,500 with 7.5%% compound interest.", year);

Java - Least number of bills and coins for change

I have to do an assignment for my class that allows the user to key in two amounts - the first should be the total sale amount and the next would be the amount of change handed to the cashier. The program needs to calculate the change needed and tell the cashier how many of each monetary amount to return to the customer using the least number of bills and coins. Using $20, 10, 5, 1 and 0.25, 0.10, 0.05, and 0.01. I also need to include a while loop to make sure the cashier is given an amount greater than the amount due.
I have the following so far, but don't know where to go from here:
public class Change {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Enter sale amount less than $100
System.out.println("Enter the sale amount: ");
double price = input.nextDouble();
//Enter amount of money handed to cashier less than $100
System.out.println("Enter the amount of money handed to the cashier: ");
double payment = input.nextDouble();
double difference = payment - price;
int num20 = (int)(difference / 20);
System.out.println("num20 = " + num20);
difference = difference % 20;
System.out.println("difference = " + difference);
int num10 = (int)(difference / 10);
System.out.println("num20 = " + num10);
difference = difference % 10;
System.out.println("difference = " + difference);
int numQtr = (int)(difference / .25);
System.out.println("numqtr = " + numQtr);
int numDime = (int)(difference / .10);
System.out.println("numDime = " + numDime);
}
Use the mod operator and division to find values at each step
29 % 20 -> 9
(int) (29 / 20) -> 1
9 % 10 -> 9
(int) (9 / 10) -> 0
please note that casting the result of a division to an integer will truncate the returned value to a whole number.

Electricity/Energy Bill Calculator: Java

I am having issues figuring out exactly what is wrong with this little Electricity/Energy calculator used to calculate computer energy costs.
I'd appreciate any help.
Program:
import java.util.Scanner;
public class ElectricityCalculations {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
double usageHoursPerDay = 0; // Hours computer is on per day
double usageDaysPerWeek = 0; // Days computer is used per week
double usageWeeksPerYear = 0; // Weeks computer is used per year
double wattsPerHour = 0; // Watts used by computer per hour
final double COST_PER_KWH = 0.145; // Prices of power per kilowatt hour
final double LBS_CO2_PER_KWH = 0.58815; // Pounds of CO2 generated per KWH
double usageHoursPerYear = 0; // Amount of hours on per year
double usageWattHoursPerYear = 0; // Amount of watt hours consumed per year
double usageKWHPerYear = 0; // Amount of KWH used in a year
double costPerYear = 0; // Total cost per year
double lbsCO2PerYear = 0; // Total amount of CO2 in pounds released per year
// Input Values
System.out.println("How many hours is your computer on per day?");
usageHoursPerDay = scnr.nextDouble();
System.out.println("How many days per week is your computer used?");
usageDaysPerWeek = scnr.nextDouble();
System.out.println("How many weeks per year is your computer used?");
usageWeeksPerYear = scnr.nextDouble();
System.out.println("How many watts per hour does your computer use? (Suggestive value for desktop: 100, laptop: 30).");
wattsPerHour = scnr.nextDouble();
// Calculations
usageHoursPerYear = usageHoursPerDay * 365;
usageWattHoursPerYear = wattsPerHour * 8760; // 8760 is the number of hours in a year
usageKWHPerYear = usageWattHoursPerYear / 1000;
costPerYear = usageKWHPerYear * COST_PER_KWH;
lbsCO2PerYear = LBS_CO2_PER_KWH * usageKWHPerYear;
// Printing Energy Audits
System.out.println("Computer Energy Audit");
System.out.println("You use your computer for " + usageHoursPerYear + " hours per year.");
System.out.println("It will use " + usageWattHoursPerYear + " KWH/year.");
System.out.println("Whih will cost " + costPerYear + "$/year for electricity.");
System.out.println("Generating that electricity will produce " + lbsCO2PerYear + " lbs of CO2 pollution.");
return;
}
}
Inputs:
8 hours/day
5 days/week
50 weeks/year
100 watts/hour
My (wrong output):
Computer Energy Audit:
You use your computer for 2920.0 hours per year.
It will use 876000.0 KWH/year.
Whih will cost 127.02$/year for electricity.
Generating that electricity will produce 515.2194 lbs of CO2 pollution.
Correct Output:
Computer Energy Audit:
You use your computer for 2000.0 hours per year.
It will use 200.0 KWH/year.
Which will cost 28.999999999999996 $/year for electricity.
Generating that electricity will produce 117.63 lbs of CO2 pollution.
You take in the number of days per week and weeks per year as input, but forget to use them in your calculations. Also, instead of printing KWH, you are displaying the variable storing Watt Hours.
// Calculations
usageHoursPerYear = usageHoursPerDay * usageDaysPerWeek * usageWeeksPerYear; //calculate based on time used, not 365 days in the year
usageWattHoursPerYear = wattsPerHour * usageHoursPerYear; //use variable from above line
usageKWHPerYear = usageWattHoursPerYear / 1000;
costPerYear = usageKWHPerYear * COST_PER_KWH;
lbsCO2PerYear = LBS_CO2_PER_KWH * usageKWHPerYear;
// Printing Energy Audits
System.out.println("Computer Energy Audit");
System.out.println("You use your computer for " + usageHoursPerYear + " hours per year.");
System.out.println("It will use " + usageKWHPerYear + " KWH/year."); //changed to correct variable
System.out.println("Whih will cost " + costPerYear + "$/year for electricity.");
System.out.println("Generating that electricity will produce " + lbsCO2PerYear + " lbs of CO2 pollution.");

Calculate and Display the Percentage of Time Passed

I'm going through ThinkJava Version 6.1.0 (latest) and in Chapter 2 Exercise 2.3, I'm stuck on #5 which asks "Calculate and display the percentage of the day that has passed. You might run into problems when computing percentages with integers, so consider using floating-point."
I've attempted to get the percentage, but I'm not getting the right result.
I've completed the first 4 questions. Here is what I have so far:
public class Date {
public static void main(String[] args) {
int hour = 13, minute = 58, second = 45;
double percentage;
double secondsSinceMidnight = second + (minute * 60) + (hour * 3600);
double secondsRemainingInDay = (60-second) + ((60-1-minute)*60) + (24-1-hour)*3600;
percentage = (secondsSinceMidnight * 100) / 60;
System.out.println("Number of seconds since midnight:");
System.out.println(secondsSinceMidnight);
System.out.println("Number of seconds remaining in the day:");
System.out.println(secondsRemainingInDay);
System.out.println("Percentage of the day past:");
System.out.println(percentage + "%");
}
}
Thank you for your help and support!
Please check the formula for calculating the percentage of the day already past.
percentage = (secondsSinceMidnight * 100) / 60;
Does not seem right to me. It should be something like
percentage = 100 * secondsSinceMidnight / totalSecondsInDay;
totalSecondsInDay can be the sum of secondsRemainingInDay and secondsSinceMidnight
i think your code have problems with type-casting
in line 3 exchange int with double:
double hour = 13, minute = 58, second = 45;
or there is problem with constant numbers , write numbers in this way : 60.0 instead of 60
Here's an example with a hardcoded time. It's in military time obviously so keep that in mind.
public class Time
{
public static void main(String[] args) {
int startHour = 12; //when you start editing program
int startMinute = 00;
int startSecond = 00;
System.out.print("Number of seconds since midnight: ");
startMinute = (startHour * 60 + startMinute );
startSecond = (startMinute * 60 + startSecond);
System.out.print(startSecond);
System.out.println(" seconds");
int secondInADay = 86400; //number of seconds in a day
System.out.print ("Seconds remaining in the day: ");
System.out.println (secondInADay - startSecond);
System.out.print("Percent of the day that has passed: ");
double startSeconds = 43200; //number of seconds that have passed in a day at start of editing program
System.out.println(startSeconds * 100 / 86400);
int endHour = 16; //time when finished editing program
int endMinute = 00;
int endSecond = 00;
System.out.print ("Difference = "); //difference in time from start to finish
endMinute = (endHour * 60 + endMinute );
endSecond = (endMinute * 60 + endSecond);
System.out.print (endSecond - startSecond);
System.out.print (" seconds");
}
}

Categories

Resources