I am trying to develop a program that calculates the sea level all the way up to the year 2100. Based on research I have found that the sea level will be 2.5 feet(which is 30 inches) - 6.5 feet(which is 78 inches). My program asks the user to enter which year they want to calculate how many inches the sea level has risen but I want the information to be random numbers per year between .3488 and .9069 inches because those are the averages per year that will make the sea level 2.5 feet and 6.5 feet. So my question is how can i generate random numbers for each year the user inputs so I can calculate how much the sea level has risen and then output that in the number of gallons.
/takes the input from the user of all the way up to which year
/they want to know the number of inches/ gallons the sea level has risen
/using reiman summs and a chart
/
/
/*******************************************************************************/
import java.util.Random;
import java.util.Scanner;
public class CalcProject
{
public static void main(String[] args)
{
//variables for the program
int year;
int numOfYears;
double leastRise = .3488; //y = .3488x , x is the inches of rainfall
double highRise = .9069; //y= .9069x , x is the inches of rainfall
double seaLevel1;
double seaLevel2;
//Describing what the program does to the user
System.out.println("This program caluclates the number of gallons / inches" +
"the sea level will rise based on your input." + "\n");
//Asking for the users year they want to calculate
System.out.println("Please enter which year you wish to know how much the water level will have risen since 2014");
Scanner scan = new Scanner(System.in);
year = scan.nextInt();
if(year <= 2014 || year > 2100)
{
do
{
System.out.println("Please enter a valid year between 2015 and 2100");
year = scan.nextInt();
} while((year <= 2014 || year > 2100));
}
//puts the year into a number of years from 2014
numOfYears = year-2014;
System.out.println("The number of years between now and the year you chose is: " + numOfYears);
//uses the least inches of sealevel
seaLevel1 = numOfYears * leastRise;
System.out.println("The total sealevel in inches is " + seaLevel1 + " for " + year);
//uses the most inches of seaLevel
seaLevel2 = numOfYears * highRise;
System.out.println("The total sealevel in inches is " + seaLevel2 + " for " + year);
//calculating radnom numbers for each year
Here is a simple way of handling the weird random numbers.
int numOfYears;
double leastRise = .3488;
double highRise = .9069;
double seaLevel3 = 0;
int i = 0;
while (i < numOfYears) {
double rand = Math.random();
if (rand >= leastRise && rand <= highRise) {
// rand is some sea level value representing a year
seaLevel3 += rand;
i++;
}
}
System.out.println("The total sealevel in inches is " + seaLevel3 + " for " + year);
Related
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.
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. What has me stumped is how to add the unique tuition values at years 11, 12, 13 and 14.
double Fee = 10000;
double Year = 1;
double TotalFee;
double Rate = 5;
double TotalCost = 15000 + 15500 + 16000 + 16500;
System.out.println("Year " + " Total Fee ");
System.out.println();
while (Year <= 14) {
TotalFee = Fee + ((Fee * ((Year * Rate) - Rate)) / 100);
System.out.println(Year + " " + " "+ TotalFee);`
Year++;
}
System.out.println("Total cost tuition of 4 years starting 10 years from now is " + TotalCost);
The last while loop is my attempt at adding the 4 years. How could I pull out the unique values of TotalCost at iterations 11 to 14 and add them?
Since you want to increase the amount 5% every year, instead of having rate = 5
You should have rate = 1.05.
With the rate as 1.05 you can do this
FeeAtYear1 = 10000*1.05^0 = 10000
FeeAtYear2 = 10000*1.05^1 = 10500
FeeAtYear3 = 10000*1.05^2 = 11025
FeeAtYear4 = 10000*1.05^3 = 11576.25
...
FeeAtYear10 = 10000*1.05^9 = ~16288.95
You don't even need a while loop.
TotalCost = 10000 *1.05^10 + 10000 *1.05^11 + 10000 *1.05^12 + 10000 *1.05^13;
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);
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.");
I'm trying to make a program that converts inches to feet, and returns the number of feet and the number of leftover inches if any. I tried this:
public class Convertor
{
/**
* Fields
*/
private int inches;
private int feet;
private int yards;
private int leftoverInches;
/**
* Constructor for objects of class Convertor
*/
public Convertor()
{
inches=0;
feet=0;
yards=0;
leftoverInches=0;
}
/**
* Mutator method to convert inches to feet
*/
public void convertValuesInchtoFeet(int anyInches)
{
inches=anyInches;
feet=(anyInches * 0.083);
leftoverInches= inches%feet;
System.out.println(inches+" inches = " +feet+" feet.");
System.out.println("There are " +leftoverinches +" leftover inches");
}
Doesn't work.
Someone help me on this, please! Thank you.
int inches = 34;
int feet = inches / 12;
int leftover = inches % 12;
System.out.println(feet + " feet and " + leftover + " inches");
try this:
public void convertValuesInchtoFeet(int anyInches)
{
inches = anyInches;
feet = Math.floor(inches/12);
//if int than no need for the Math.floor()
leftoverInches = inches%12;
System.out.println(inches + " inches = " + feet + " feet.");
System.out.println("There are " + leftoverInches + " leftover inches");
}
The primary reason your code doesn't work is because you're doing
leftoverInches = inches%feet;
Suppose you gave it 13 inches. You would have feet = 1 (13 * 0.083 rounded down), and inches = 13 % 1 = 0. What you mean to do was
leftoverInches = inches%12;
With 13, 13%12 = 1, which is indeed the number of leftover inches.
A smaller but still important error is that you multiply by 0.083, which is NOT 1/12, and will give you serious inaccuracies. For example, if you enter 1,000,000 inches, you will get
1000000 * 0.083 = 83000 feet
But
1000000 / 12 = 83333 feet rounded down
So you would be 333 feet off.