Results 1 to 3 of 3
Thread: Calculating & Drawing Tables
 06032010, 05:28 AM #1Member
 Join Date
 Jun 2010
 Posts
 3
 Rep Power
 0
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
Java Code:import java.text.NumberFormat; /** * Assignment 1, Question 3 for CMPT 125/126 Summer 2010 * <p> * YOUR DESCRIPTION HERE OF THIS CLASS * * @author YOURNAMEHERE # STUDENTNUMBERHERE */ 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 roundtrip 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 roundtrip distance: 80.0km "); System.out.println(""); System.out.println("#Sem.\tCO2(kg)\t#Trees\tOffset Cost"); System.out.println(""); System.out.println(emittedCO2_kg); } }
Java 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); } }
 06032010, 10:13 PM #2i have problems with "distance_km"
I don't see where you try to compute it? Only references I see are to parms to methods.
 06032010, 11:26 PM #3
something like that?
Java Code:public static void drawCO2Table(int Semester, int roundTrip_km) { System.out.println("CO2 Table for Commuting to SFU"); System.out.println("Table for roundtrip distance: " + roundTrip_km + "km"); System.out.println(""); System.out.println("#Sem.\tCO2(kg)\t#Trees\tOffset Cost"); System.out.println(""); long emittedCO2_kg = (long) calcKgCO2PerSemester(roundTrip_km); System.out.print(Semester + "\t" + emittedCO2_kg + "\t" + calcKgCO2PerSemester(roundTrip_km) + "\t" + calcTreesFromOffset(emittedCO2_kg) + "\t" + calcCostOfOffsets(emittedCO2_kg)); System.out.println("\n\n"); }
called with
CarbonEmissions.drawCO2Table(1, 80);
CarbonEmissions.drawCO2Table(2, 90);
CarbonEmissions.drawCO2Table(3, 110);
will produce
Java Code:[FONT="Courier New"]CO2 Table for Commuting to SFU Table for roundtrip distance: 80km  #Sem. CO2(kg) #Trees Offset Cost  1 2 2.0 118 1.6E4 CO2 Table for Commuting to SFU Table for roundtrip distance: 90km  #Sem. CO2(kg) #Trees Offset Cost  2 2 2.0 118 1.6E4 CO2 Table for Commuting to SFU Table for roundtrip distance: 110km  #Sem. CO2(kg) #Trees Offset Cost  3 3 3.0 79 2.4E4[/FONT]
no idea if the numbers are ok, but you can see how to format your numbers for a table. hope this help.
Similar Threads

need help with calculating something
By mikec420 in forum New To JavaReplies: 13Last Post: 09292011, 09:14 PM 
Calculating average
By clocksaysits9 in forum New To JavaReplies: 4Last Post: 04062010, 05:03 AM 
calculating exponents
By GPB in forum New To JavaReplies: 2Last Post: 03212010, 12:44 PM 
calculating with dates
By hannes in forum New To JavaReplies: 1Last Post: 01142010, 09:22 PM 
Calculating sin of a double value
By Java Tip in forum Java TipReplies: 0Last Post: 01132008, 09:13 PM
Bookmarks