Results 1 to 3 of 3
  1. #1
    kassoli is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default 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 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

    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);
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    i have problems with "distance_km"
    Can you explain what the problem is?
    I don't see where you try to compute it? Only references I see are to parms to methods.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    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 round-trip 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 round-trip distance: 80km
    -----------------------------------
    #Sem.	CO2(kg)	#Trees	Offset Cost
    -----------------------------------
    1	2	2.0	118	1.6E-4
    
    
    CO2 Table for Commuting to SFU
    Table for round-trip distance: 90km
    -----------------------------------
    #Sem.	CO2(kg)	#Trees	Offset Cost
    -----------------------------------
    2	2	2.0	118	1.6E-4
    
    
    CO2 Table for Commuting to SFU
    Table for round-trip distance: 110km
    -----------------------------------
    #Sem.	CO2(kg)	#Trees	Offset Cost
    -----------------------------------
    3	3	3.0	79	2.4E-4[/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

  1. need help with calculating something
    By mikec420 in forum New To Java
    Replies: 13
    Last Post: 09-29-2011, 09:14 PM
  2. Calculating average
    By clocksaysits9 in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 05:03 AM
  3. calculating exponents
    By GPB in forum New To Java
    Replies: 2
    Last Post: 03-21-2010, 11:44 AM
  4. calculating with dates
    By hannes in forum New To Java
    Replies: 1
    Last Post: 01-14-2010, 08:22 PM
  5. Calculating sin of a double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •