14 Replies - 941 Views - Last Post: 09 November 2009 - 01:37 AM Rate Topic: -----

#1 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

printing information about an object

Posted 08 November 2009 - 11:14 AM

i have made an object that represents each family in my program. there are five. the best way to explain would probably to use a code example.
CO2Footprint fp0 = new CO2Footprint(3, 2604.0, 227.29, 0.084, true, true, true, true, 9);
	   CO2Footprint fp1 = new CO2Footprint(6, 3029.0, 213.28, 0.081, false, true, false, true, 3);
	   CO2Footprint fp2 = new CO2Footprint(2, 3590.0, 234.78, 0.085, true, false, true, false, 5);
	   CO2Footprint fp3 = new CO2Footprint(10,3590.0, 256.04, 0.084, false, false, false, false, 1);
	   CO2Footprint fp4 = new CO2Footprint(1, 1362.0, 221.96, 0.086, true, true, true, true, 8);
		
	   fp0.calculateGasEmissions();
	   fp0.calculateElectricityEmissions();
	   fp0.calcWasteReduction();
	   fp0.calcGrossWasteEmission();
	   fp0.calcEmissionReductionTotals(); 
	   fp0.calcCO2Footprint();
	   
	   fp1.calculateGasEmissions();
	   fp1.calculateElectricityEmissions();
	   fp1.calcWasteReduction();
	   fp1.calcGrossWasteEmission();
	   fp1.calcEmissionReductionTotals(); 
	   fp1.calcCO2Footprint();
	   
	   fp2.calculateGasEmissions();
	   fp2.calculateElectricityEmissions();
	   fp2.calcWasteReduction();
	   fp2.calcGrossWasteEmission();
	   fp2.calcEmissionReductionTotals(); 
	   fp2.calcCO2Footprint();
	   
	   fp3.calculateGasEmissions();
	   fp3.calculateElectricityEmissions();
	   fp3.calcWasteReduction();
	   fp3.calcGrossWasteEmission();
	   fp3.calcEmissionReductionTotals(); 
	   fp3.calcCO2Footprint();
	   
	   fp4.calculateGasEmissions();
	   fp4.calculateElectricityEmissions();
	   fp4.calcWasteReduction();
	   fp4.calcGrossWasteEmission();
	   fp4.calcEmissionReductionTotals(); 
	   fp4.calcCO2Footprint();
	   
	   
	   
		//print results
		
		System.out.println("|			   Pounds of CO2			 |	  Pounds of CO2		 |					   |");
		System.out.println("|			   Emmited from			  |	  Reduced from		  |					   |");
		System.out.println("|   Gas   |	  Electricity  |   Waste   |   Recycling  |  New Bulbs  |	CO2 Footprint	  |");
		System.out.println("|"fp0.gasFootPrint "|");
										   ^
											|
											|


my return on my calcualteGasEmissions method is gasFootPrint. im trying to print the return for the first family there where the arrow is pointing. my question is, is there a better way to do this? im doing it and it just doesnt look like a way that it should be done.

Is This A Good Question/Topic? 0
  • +

Replies To: printing information about an object

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4444
  • View blog
  • Posts: 12,317
  • Joined: 18-April 07

Re: printing information about an object

Posted 08 November 2009 - 11:36 AM

Have you considered making an array of C02Footprint objects? Then you could loop through them and won't have so much repeat code. This will also make printing them a breeze...


// Create an array of 5 families
C02Footprint[] footprints = new C02Footprint[5];

// Load up the array
footprints[0] = new CO2Footprint(3, 2604.0, 227.29, 0.084, true, true, true, true, 9);
footprints[1] = new CO2Footprint(6, 3029.0, 213.28, 0.081, false, true, false, true, 3);
footprints[2] = new CO2Footprint(2, 3590.0, 234.78, 0.085, true, false, true, false, 5);
footprints[3] = new CO2Footprint(10,3590.0, 256.04, 0.084, false, false, false, false, 1);
footprints[4] = new CO2Footprint(1, 1362.0, 221.96, 0.086, true, true, true, true, 8);

// Print our header
System.out.println("|			   Pounds of CO2			 |	  Pounds of CO2		 |					   |");
System.out.println("|			   Emmited from			  |	  Reduced from		  |					   |");
System.out.println("|   Gas   |	  Electricity  |   Waste   |   Recycling  |  New Bulbs  |	CO2 Footprint	  |");

// Loop through the array doing the calculations and printing their return values (assuming you DID have them return numbers and strings)
for (int i = 0; i < footprints.length; i++) {
	   System.out.print("|" + footprints[i].calculateGasEmissions() + "|");
	   System.out.print(footprints[i].calculateElectricityEmissions() + "|");
	   System.out.print(footprints[i].calcWasteReduction() + "|");
	   System.out.print(footprints[i].calcGrossWasteEmission() + "|");
	   System.out.print(footprints[i].calcEmissionReductionTotals() + "|");
	   System.out.print(footprints[i].calcCO2Footprint() + "|");
	   System.out.print("\n");
}



So as you can see we loaded up the array with the families and then using a loop we can calculate and then print their return values in a line. We could have made this a bit simpler still by printing in a single line, but I wanted to make it a bit more readable here instead of a huge long line. The beauty of this is that now we can easily expand the array to include more families without changing the loop or the print statements. It also cuts down on your lines and makes things a bit more readable.

Hope this helps!


"At DIC we be carbon footprint friendly code ninjas.... we have left our footprint on the behinds of many carbon lifeforms." :snap:

This post has been edited by Martyr2: 08 November 2009 - 11:38 AM

Was This Post Helpful? 1
  • +
  • -

#3 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: printing information about an object

Posted 08 November 2009 - 12:05 PM

wow, thanks for the great advice. i only have one small problem though, when i changed my program to accommodate the array, i now get an error when i try to compile that says: cannot find symbol class CO2Footprint. i dont know what could have caused this...
Was This Post Helpful? 0
  • +
  • -

#4 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: printing information about an object

Posted 08 November 2009 - 12:15 PM

simple fix, somehow my CO2 got replaced with C02. almost impossible to spot.
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: printing information about an object

Posted 08 November 2009 - 12:17 PM

View Postgibson.nathan, on 8 Nov, 2009 - 01:15 PM, said:

simple fix, somehow my CO2 got replaced with C02. almost impossible to spot.


Nice fix :^:

This post has been edited by Dogstopper: 08 November 2009 - 12:17 PM

Was This Post Helpful? 0
  • +
  • -

#6 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: printing information about an object

Posted 08 November 2009 - 12:33 PM

a;oisfj;sndfglbngsiugh omg. omg. omg.

this is unbelievable.

check out the results;

Quote

| Pounds of CO2 | Pounds of CO2 | |
| Emmited from | Reduced from | |
| Gas | Electricity | Waste | Recycling | New Bulbs | CO2 Footprint |
| 0.0 | NaN | 0.0 | 0.0 | 0.0 | NaN |
| 0.0 | NaN | 0.0 | 0.0 | 0.0 | NaN |
| 0.0 | NaN | 0.0 | 0.0 | 0.0 | NaN |
| 0.0 | NaN | 0.0 | 0.0 | 0.0 | NaN |
| 0.0 | NaN | 0.0 | 0.0 | 0.0 | NaN |


this is truly just warping my mind.

:ohno:

*it just looks like its formatted funky because of the way it does when you post it*
Was This Post Helpful? 0
  • +
  • -

#7 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: printing information about an object

Posted 08 November 2009 - 12:49 PM

  /** 
  * Write a description of class CO2Footprint here. 
  *  
  * @author (your name)  
  * @version (a version number or a date) 
  */  
 public class CO2Footprint  
 {  
		 public int numPeople;  
		 public double electricityEmissions;  
		 public double gas;  
		 public double gasFootPrint;  
		 public double aveElecBill;  
		 public double aveElecPrice;  
		 public boolean paper;  
		 public boolean plastic;  
		 public boolean glass;  
		 public boolean cans;  
		 public int numLights;  
		 public int lightsTotal;  
		 public double paperReductions;  
		 public double plasticReductions;  
		 public double glassReductions;  
		 public double canReductions;  
		 public double emissionReductions;  
		 public double emissionReductionTotal;  
		 public double grossWasteEmission;  
		 public double totalReductionsPaper;
		 public double totalReductionsPlastic;
		 public double totalReductionsGlass;
		 public double totalReductionsCans;
		 public double CO2Footprint;
   
   public CO2Footprint( int numberOfPeople, double gasFootPrint, double averageElectricBill,	  
   double averageElectricPrice, boolean paperRecyled, boolean plasticRecyled, boolean glassRecyled,	 
   boolean cansRecyled, int numLightsRecyled)	   
	 {  
		 numberOfPeople = numPeople;  
		 gasFootPrint = gas;  
		 averageElectricBill = aveElecBill;  
		 averageElectricPrice = aveElecPrice;  
		 paperRecyled = paper;  
		 plasticRecyled = plastic;  
		 glassRecyled = glass;  
		 cansRecyled = cans;  
		 numLightsRecyled = numLights;  
		 totalReductionsPaper = paperReductions;  
		 totalReductionsPlastic = plasticReductions;  
		 totalReductionsGlass = glassReductions;  
		 totalReductionsCans = canReductions;  
		 
	 }   
   
   
public double calculateGasEmissions(){
		gasFootPrint = 19.3565 * gas;
		return gasFootPrint;
		}
			
	  public double calculateElectricityEmissions(){
		  electricityEmissions = (aveElecBill/aveElecPrice) * 1.37 * 12;
		  return electricityEmissions;
			}


	public double calcWasteReduction(){
	   emissionReductions = lightsTotal * 1.37 * 73;
	   return  emissionReductions;
}
	
	   public double calcGrossWasteEmission() {
			grossWasteEmission = numPeople * 1018;
			return grossWasteEmission;
	
}

	   
	public double calcCanReductions() {
			double can = 165.8;
				if (cans){
					canReductions  = (can * numPeople);
				}
				return canReductions;
			}
		
	public double calcGlassReductions() {
			double glassReduce = 46.6;
				if(glass){
					glassReductions = (glassReduce * numPeople);
			  
			}
			return glassReductions;
		}
		
			
	public double calcPlasticReductions() {   
			 double plasticReduce = 25.6;
			 if(plastic){
				plasticReductions = (plasticReduce * numPeople);
			  }
			return plasticReductions;
		}
		
		
			
	public double calcPaperReductions() {   
			   double paperReduce = 184.0;
			   if(paper){
					paperReductions = (paperReduce * numPeople);   
			}
		
		return paperReductions;
		
		}
		
	public double calcEmissionReductionTotals() {   
			 emissionReductionTotal = paperReductions + plasticReductions + glassReductions + canReductions;
			 return emissionReductionTotal;
			} 


	public double calcCO2Footprint() {   
			 CO2Footprint = (gasFootPrint + electricityEmissions + grossWasteEmission) - (emissionReductionTotal + emissionReductions);
			 return CO2Footprint;
			} 
}
   
		   
		   



does anyone see what could be causing this. i have been looking an i really dont know where to start.
Was This Post Helpful? 0
  • +
  • -

#8 arashzz  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 28-September 09

Re: printing information about an object

Posted 08 November 2009 - 01:19 PM

in your constructor it should be like this :


		 numPeople=numberOfPeople;
		 gas = gasFootPrint;
		 aveElecBill = averageElectricBill;
		 aveElecPrice = averageElectricPrice;
		 paper = paperRecyled;
		 plastic = plasticRecyled;
		 glass = glassRecyled;
		 cans = cansRecyled;
		 numLights = numLightsRecyled;




your fields should be equal to the parameters in your constructor :)
since I've checked your codes and got default value for the arrays , but with the change above you will get some numbers ;)

hope it helps.
Was This Post Helpful? 1
  • +
  • -

#9 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: printing information about an object

Posted 08 November 2009 - 02:22 PM

oh okay, i got some numbers. im still getting a little some confusion though,

Quote

| Pounds of CO2 | Pounds of CO2 | |
| Emmited from | Reduced from | |
| Gas | Electricity | Waste | Recycling | New Bulbs | CO2 Footprint |
| 50404.326 | 44483.899999999994 | 0.0 | 3054.0 | 0.0 | 97942.226 |
| 58630.8385 | 43287.94074074074 | 0.0 | 6108.0 | 0.0 | 108026.77924074074 |
| 69489.835 | 45409.21411764706 | 0.0 | 2036.0 | 0.0 | 116935.04911764707 |
| 69489.835 | 50110.68571428572 | 0.0 | 10180.0 | 0.0 | 129780.52071428573 |
| 26363.553 | 42430.493023255825 | 0.0 | 1018.0 | 0.0 | 69812.04602325583 |


are the 0 value also solvable, or is there something else wrong?
Was This Post Helpful? 0
  • +
  • -

#10 arashzz  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 28-September 09

Re: printing information about an object

Posted 08 November 2009 - 03:34 PM

for 3rd column:
this method will return zero coz your lightsTotal's value is 0 by default .

public double calcWasteReduction(){
	   emissionReductions = lightsTotal * 1.37 * 73;
	   return  emissionReductions;


if you recheck you will see that you didn't give any value to lightsTotal.
so emissionReductions will be 0 since 0 * 1.37 * 73 = 0

butfor the 5th column ... I'm kinda lost .. not sure what causes the problem, it should be something to do with numPeople in 4 methods (calcCanReductions, calcGlassReductions, calcPlasticReductions, calcPaperReductions)

 public double calcEmissionReductionTotals() {
			 emissionReductionTotal = paperReductions + plasticReductions + glassReductions + canReductions;
			 return emissionReductionTotal;
			}


since the return value of above method is zero then paperReductions, plasticReductions glassReductions and canReductions should be either zero or some diff values which makes the return value zero ...

I assume it has to do something with the value of numPeople in those 4 methods I mentioned above .
sorry but I'm not that much pro in java :) but will try my best to find the answer .
Was This Post Helpful? 0
  • +
  • -

#11 arashzz  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 28-September 09

Re: printing information about an object

Posted 08 November 2009 - 03:47 PM

ok my bad...

it didn't have anything to do with numPeople... the problem was in this method:

public double calcEmissionReductionTotals() {  
			 emissionReductionTotal = paperReductions + plasticReductions + glassReductions + canReductions;
			 return emissionReductionTotal;
			} 



it should be like this :
	public double calcEmissionReductionTotals() {
			 emissionReductionTotal = calcPaperReductions() + calcPlasticReductions() + calcGlassReductions() + calcCanReductions();
			 return emissionReductionTotal;
			}



your mistake was that you called the value for paperReductions, plasticReductions, glassReductions and canReductions in method calcEmissionRedudtionTotals before calling their mutator methods , so it's obvious that they will return the default value that they have which is 0 .

with that change I got some numbers :) I hope it's exactly what you wanted ;)

Hope it Helps.
Was This Post Helpful? 1
  • +
  • -

#12 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: printing information about an object

Posted 08 November 2009 - 03:56 PM

i hate to be a pest, but i just realized that my output is crazy crooked. how do you line this up? printf? how do you add print f into this?
Was This Post Helpful? 0
  • +
  • -

#13 arashzz  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 28-September 09

Re: printing information about an object

Posted 08 November 2009 - 05:17 PM

Sorry but since I don't have any experience in printf I only could come up with this by just reading the forum by searching about printf.... so gimme a break if it looks noobish -.-

System.out.printf("%1s%13s%18s%6s%14s%20s%9s%14s%24s\n"  , "|" , "|" , "Pounds of CO2" , "|" , "|" , "Pounds of CO2" , "|" , "|" , "|");
System.out.printf("%1s%13s%17s%7s%14s%19s%10s%14s%24s\n" , "|" , "|" , "Emmited from" , "|" , "|" , "Reduced from" , "|" , "|" , "|");
System.out.printf("%1s%6s%7s%16s%8s%9s%5s%17s%12s%11s%3s%18s%6s\n" , "|" , "Gas" , "|" , "Electricity" , "|", "Waste" , "|" , "Recycling" , "|" , "New Bulbs" , "|" , "CO2 Footprint" , "|");

// Loop through the array doing the calculations and printing their return values (assuming you DID have them return numbers and strings)
for (int i = 0; i < footprints.length; i++) {
	   System.out.printf("%1s%10s%3s","|" , footprints[i].calculateGasEmissions() , "|");
	   System.out.printf("%20s%4s",footprints[i].calculateElectricityEmissions() , " |");
	   System.out.printf("%8s%6s",footprints[i].calcWasteReduction() , "|");
	   System.out.printf("%16s%13s",footprints[i].calcGrossWasteEmission()  ,"|");
	   System.out.printf("%10s%4s",footprints[i].calcEmissionReductionTotals() , "|");
	   System.out.printf("%20s%4s", footprints[i].calcCO2Footprint() , "|");
	   System.out.print("\n");





I know it's stupid and noobish but i really couldn't come up with anything better ... I might look into printf a bit more but not sure if I'm gonna be able to help u or not

but if you w8 for a while .. I'm sure some expert will help you with the code :)

Hope it Helps.
Was This Post Helpful? 1
  • +
  • -

#14 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: printing information about an object

Posted 08 November 2009 - 07:08 PM

lol, dude im noobish as they come. i think it would take 20 years to become "un-noobish" with java. thanks for the help. i really appreciate it. im so glad to have this assignment wrapped up :D it has been like a week of trial and error.
Was This Post Helpful? 0
  • +
  • -

#15 arashzz  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 28-September 09

Re: printing information about an object

Posted 09 November 2009 - 01:37 AM

well I'm happy that I could help .. Helping you is helping myself to learn new things.
and I still hope some expert come and give the correct printf for your code :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1