Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: HW Help: Returning Values with Methods

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HW Help: Returning Values with Methods

    The code is below. The program runs a series of calculations based on data input by the user. My problem is that for the most important thing I'm looking for, total kg CO2 emissions, I continually get an answer of 0.0. What I need is a sum of the individual total emissions as calculated in each method, i.e. the values which are printed with the following: System.out.println(trans); System.out.println(elec); and System.out.println(food);

    The total should be something like 25040 or whatever, depending on the value of the inputs provided by the user, but I'm constantly getting a total of 0.0., which is obviously false. Could have something to do with the way I've initialized my variables, or something to do with the limitations of returning values from methods. I just don't know what to do. How should I tackle this? All help greatly appreciated!

    import java.util.Scanner;
     
    public class CarbonCalc {
     
    	public static void main(String[] args) {
    		double trans = 0;
    		double elec = 0;
    		double food = 0;
     
    		giveIntro();
     
    		determineTransportationEmission(null);
    		determineElecticityEmission(null);
    		determineFoodEmission(null);
     
    		calculateTotalEmission(trans, elec, food);
     
    		//printReport(trans, elec, food);
    	}
     
    	//Gives a brief introduction to the user.
    	public static void giveIntro() {
    		System.out.println("This program will estimate your carbon footprint");
    		System.out.println("(in metric tons per year) by asking you");
    		System.out.println("to input relevant household data.");
    		System.out.println("");
    	}
     
    	//Determines the user's transportation-related carbon emissions.
    	public static double determineTransportationEmission(Scanner input) {
    		Scanner console = new Scanner(System.in);
    		System.out.println("We will first begin with your transportation-related carbon expenditures...");
    		System.out.print("How many kilometres do you drive per day? ");
    		double kmPerDay = console.nextDouble();
    		System.out.print("What is your car's fuel efficiency (in km/litre)? ");
    		double fuelEfficiency = console.nextDouble();
    		System.out.println("We now know that the numeber of litres you use per year is...");
    		double litresUsedPerYear = 365.00 * (kmPerDay / fuelEfficiency);
    		System.out.println(litresUsedPerYear);
    		System.out.println("...and the kg of transportation-related CO2 you emit must be...");
     
    		//Final calculation of transportation-related kgCO2 emissions.
    		double trans = 2.3 * litresUsedPerYear;
    		System.out.println(trans);
    		System.out.println("");
    		return trans;
    	}
     
    	//Determines the user's electricity-related carbon emissions.
    	public static double determineElecticityEmission(Scanner input) {
    		Scanner console = new Scanner(System.in);
    		System.out.println("We will now move on to your electricity-related carbon expenditures...");
    		System.out.print("What is your monthly kilowatt usage (kWh/mo)? ");
    		double kWhPerMonth = console.nextDouble();
    		System.out.print("How many people live in your home? ");
    		double numPeopleInHome = console.nextDouble();
    		System.out.println("The kg of electricity-related CO2 you emit must be...");
     
    		//Final calculation of electricity-related kgCO2 emissions.
    		double elec = (kWhPerMonth * 12 * 0.257) / numPeopleInHome;
    		System.out.println(elec);
    		System.out.println("");
    		return elec;
    	}
     
    	//Determines the user's food-related carbon emissions.
    	public static double determineFoodEmission(Scanner input) {
    		Scanner console = new Scanner(System.in);
    		System.out.println("We will now move on to your food-related carbon expenditures...");
    		System.out.print("In a given year, what percentage of your diet is meat? ");
    		double meat = console.nextDouble();
    		System.out.print("In a given year, what percentage of your diet is dairy? ");
    		double dairy = console.nextDouble();
    		System.out.print("In a given year, what percentage of your diet is fruits and veggies? ");
    		double fruitVeg = console.nextDouble();
    		System.out.print("In a given year, what percentage of your diet is carbohydrates? ");
    		double carbs = console.nextDouble();
     
    		//Final calculation of food-related kgCO2 emissions.
    		System.out.println("The kg of food-related CO2 you emit must be...");
    		double food = (meat * 53.1 + dairy * 13.8 + fruitVeg * 7.6 + carbs * 3.1);
    		System.out.println(food);
    		System.out.println("");
    		return food;
    	}
     
    	//Calculates total emissions across all sources.
    	public static double calculateTotalEmission(double trans, double elec, double food) {
    		System.out.println("Your total kg of CO2 emitted across all sources is equal to...");
    		double total = trans + elec + food;
    		System.out.println(total);
    		System.out.println("");
    		return total;
    	}
     
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: HW Help: Returning Values with Methods

    Hi,
    you declare and init those variables:
    double trans = 0;
    double elec = 0;
    double food = 0;
    But you never assign the values that your methods return, i.e. you pass three 0.0 into calculateTotalEmission.

Similar Threads

  1. Returning Boolean Values
    By triumvirate1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 7th, 2013, 02:40 AM
  2. Returning multiple values from a method.
    By atar in forum Java Theory & Questions
    Replies: 14
    Last Post: July 31st, 2012, 04:59 PM
  3. Program returning wrong values.
    By cam25 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 11th, 2012, 11:59 PM
  4. methods returning zero's ???
    By fakeClassy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 3rd, 2011, 05:53 PM
  5. [SOLVED] Java Beginner: Help with methods and returning values (hailstone program)
    By alf in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 14th, 2010, 06:28 PM