Calculating & Drawing Tables
hi
im trying to do my assignment which wants me to calculate some stuff as you can see in the code below and print them all in a table, i have tried to calculate the required parameters but im having a problem with priting them out in table and actually i have problems with "distance_km" in the code...i would appreciate it if someone could help me out
Code:
import java.text.NumberFormat;
/**
* Assignment 1, Question 3 for CMPT 125/126 Summer 2010
* <p>
* YOUR DESCRIPTION HERE OF THIS CLASS
*
* @author YOUR-NAME-HERE # STUDENT-NUMBER-HERE
*/
public class CarbonEmissions {
/*
* Constants from the problem definition.
* Note that they are static and final.
* - static makes them common to all objects of this
* class, which is needed because our methods are static.
* - final makes them constants.
*/
static final int DAYS_TRAVELLED_PER_WEEK = 5;
static final int WEEKS_PER_SEMESTER = 13;
static final int CAR_CO2_G_PER_KM = 310;
static final double TREE_CO2_ABSORBED_KG = 236;
static final double CO2_COST_PER_TONNE = 12.50;
static final int KG_PER_TONNE = 1000;
static final int G_PER_KG = 1000;
/**
* Calculate the amount of CO2 emitted by commuting to by car
* in a semester, based on the distance travelled (round trip).
*
* @param distance_km Distance in km travelled for one round-trip to SFU.
* @return CO2 emitted, in kg.
*/
public static double calcKgCO2PerSemester(double distance_km) {
double emittedCO2_kg = 0;
emittedCO2_kg = (double)distance_km/G_PER_KG *(CAR_CO2_G_PER_KM*DAYS_TRAVELLED_PER_WEEK*WEEKS_PER_SEMESTER/(G_PER_KG));
return (long) Math.ceil(emittedCO2_kg);
}
/**
* Calculate the number of trees which, over their 40 year
* life, are required to absorb the CO2 emitted.
*
* @param emittedCO2_kg Amount of CO2 (in kg) to be absorbed.
* @return Number of trees (rounded up) required to absorb the CO2.
*/
public static long calcTreesFromOffset(double emittedCO2_kg) {
return (long) Math.ceil((float)TREE_CO2_ABSORBED_KG/emittedCO2_kg);
}
/**
* Calculates the cost to buy carbon offsets for the amount of
* carbon emissions. Return value, in dollars, must be
* rounded to nearest cent.
*
* @param emittedCO2_kg
* @return Cost in dollars and cents.
*/
public static double calcCostOfOffsets(double emittedCO2_kg) {
return ((float)emittedCO2_kg)/(CO2_COST_PER_TONNE*KG_PER_TONNE);
/**
* Display to the screen a table showing information about the CO2
* emissions from commuting.
*
* @param distance_km
*/
public static void drawCO2Table(double roundTrip_km,
int[] setOfSemesters) {
long emittedCO2_kg=calcKgCO2PerSemester(distance_km);
System.out.println("CO2 Table for Commuting to SFU");
System.out.println("Table for round-trip distance: 80.0km ");
System.out.println("-----------------------------------");
System.out.println("#Sem.\tCO2(kg)\t#Trees\tOffset Cost");
System.out.println("-----------------------------------");
System.out.println(emittedCO2_kg);
}
}
Here is the carbonemissions class
Code:
/**
* Driver class to call the CarbonEmissions class.
* <p>
* This class uses just the main function to call the
* CarbonEmissions.drawCO2Table() method.
* <p>
* You may change the parameters being passed into the drawCO2Table()
* method for your testing; however, you must not change anything
* else in this class.
*
* @author
*
*/
public class CarbonEmissionsDriver {
/**
* Call CarbonEmissions.drawCO2Table() method.
*
* @param args
*/
public static void main(String[] args) {
// Choose the how many semesters to display:
int[] setOfSemesters = {1, 2, 3, 6, 9, 12, 30};
// Call function to display CO2 table
CarbonEmissions.drawCO2Table(80, setOfSemesters);
}
}