# Thread: Calculating & Drawing Tables

1. Member
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 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. 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. 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.

#### Posting Permissions

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