Andrew - 7 months ago 39

Java Question

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;

}

}

8 hours/day

5 days/week

50 weeks/year

100 watts/hour

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.

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.

Answer

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.");
```