Problem w/ Parameters & Calling Methods

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 689 Views - Last Post: 30 September 2012 - 10:47 PM Rate Topic: -----

#1 Collectionize  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-September 12

Problem w/ Parameters & Calling Methods

Posted 30 September 2012 - 04:46 PM

Okay, I've just recently started programming and I'm already having trouble. I've been trying to call the method printReport into my main method however it hasn't been working. These are ways I've already tried, but haven't been working.
printReport();
printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal);
double x = printReport();
double x = printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal);


import java.util.Scanner;

public class FootprintCalculator {
	
	public static void main(String[] args) {
		System.out.println("We will calculate the CO2 emissions for your transportation in a year. After answering a few of the following questions that appear. Please answer with numbers.");
		carbonPrivateAutoUseage();
		carbonPrivateAutoOwnership();
		carbonPublicTransport();
		carbonAirTransport();
		printReport();
		
	}
	
	public static double carbonPrivateAutoUseage() {
		Scanner userInput = new Scanner (System.in);
		System.out.println("How many liters of gasoline do you use per day?");
		double litersPerDay = userInput.nextDouble();
		System.out.println("How many kilometers do you drive per day?");
		double kmPerDay = userInput.nextDouble();
		double fuelEfficiency = kmPerDay/litersPerDay;
		double litresUsedPerYear = 365 * (kmPerDay/fuelEfficiency);
		double kgCO2 = 12.85 * litresUsedPerYear;
		return kgCO2;
	}
	
	public static double carbonPrivateAutoOwnership() {
		Scanner userInput = new Scanner (System.in);
		System.out.println("How old is your car?");
		double ageOfCar = userInput.nextDouble();
		double kgC02PerGJ = 1.94;
		double carbonManufactureCar = (kgC02PerGJ*120) + ((3.8*kgC02PerGJ)*ageOfCar);
		double kgCO2Car = carbonManufactureCar/ageOfCar;
		return kgCO2Car;
	}
	
	public static double carbonPublicTransport() {
		Scanner userInput = new Scanner (System.in);
		System.out.println("How many kilometers do you ride the bus per day?");
		double busRide = userInput.nextDouble();
		double kgC02OnBus = (0.18*busRide) * 365;
		return kgC02OnBus;
	}
	
	public static double carbonAirTransport() {
		Scanner userInput = new Scanner (System.in);
		System.out.println("How many kilometers in total have you flown on domestic flights this year?");
		double domesticFlight = userInput.nextDouble();
		System.out.println("How many kilometers in total have you flown on international flights this year?");
		double internationalFlight = userInput.nextDouble();
		double smallHaul = 0.10*domesticFlight;
		double bigHaul = 0.2*internationalFlight;
		double totalHaul = smallHaul+bigHaul;
		return totalHaul;
	}
	
	public static double carbonTotal(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul) {
		double overallTotal = (kgCO2 + kgCO2Car + kgC02OnBus + totalHaul)/1000;
		return overallTotal;
	}
	
// This is the one I'm having trouble calling to my main.
	public static void printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal){
		System.out.println("You produce an annual total of metric" + overallTotal + "tons of CO2 per year for your personal transport.");
		System.out.println("The breakdown is as follows:");
		System.out.println("Private Automobile Usage	" + kgCO2);
		System.out.println("Private Automobile Ownership	" + kgCO2Car);
		System.out.println("Public Transport	" + kgC02OnBus);
		System.out.println("Air Transport	" + totalHaul);
	}
}

Yeah, and I realize my code is a little redundant with a Scanner in the first 4 methods, but I can't figure out how to put the Scanner userInput into the parameters. Thank you for taking a look over this!

Is This A Good Question/Topic? 0
  • +

Replies To: Problem w/ Parameters & Calling Methods

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Problem w/ Parameters & Calling Methods

Posted 30 September 2012 - 06:07 PM

First off, what class does printReport belong to? On your code block above there's an empty one and another with parameters, yet on your main method you call the one with no parameters. Is it from an abstract class? Well, I see your printReport on Footprint but it's the one with the parameters. Second, you can declare your scanner as a static field outside your methods like so:

public static Scanner userInput = new Scanner(System.in);



Which is okay and good for a single class but if you're planning to use Footprint on other classes and change the methods not to static then your design would work out too.
Was This Post Helpful? 1
  • +
  • -

#3 Collectionize  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-September 12

Re: Problem w/ Parameters & Calling Methods

Posted 30 September 2012 - 06:56 PM

Oh, I made a mistake. I was suppose to put the parameters inside when I was calling it, but however, that doesn't work and I get an error. As for the class, I only have one which is the FootprintCalculator. And thank you so much for helping me.
Was This Post Helpful? 0
  • +
  • -

#4 Collectionize  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-September 12

Re: Problem w/ Parameters & Calling Methods

Posted 30 September 2012 - 07:11 PM

When I try to call it by
printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal);

I get a long error that looks like this.
FootprintCalculator.java:11: error: '.class' expected
printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal);
^
FootprintCalculator.java:11: error: ';' expected
printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal);
^
FootprintCalculator.java:11: error: <indenitifer> expected
printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal);
^
FootprintCalculator.java:11: error: not a statement
printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal);
^
FootprintCalculator.java:11: error: ';' expected
printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal);
^
FootprintCalculator.java:11: error: <indenitifer> expected
printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal);
^
FootprintCalculator.java:11: error: not a statement
printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal);
^
FootprintCalculator.java:11: error: ';' expected
printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal
^

8 errors
Was This Post Helpful? 0
  • +
  • -

#5 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Problem w/ Parameters & Calling Methods

Posted 30 September 2012 - 07:38 PM

What's your complete code now? Your initial code looks fine to me.
Was This Post Helpful? 0
  • +
  • -

#6 Collectionize  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-September 12

Re: Problem w/ Parameters & Calling Methods

Posted 30 September 2012 - 07:40 PM

View PostfromTheSprawl, on 30 September 2012 - 07:38 PM, said:

What's your complete code now? Your initial code looks fine to me.
Looks like this.
import java.util.Scanner;

public class FootprintCalculator {
	
	public static void main(String[] args) {
		System.out.println("We will calculate the CO2 emissions for your transportation in a year. After answering a few of the following questions that appear. Please answer with numbers.");
		carbonPrivateAutoUseage();
		carbonPrivateAutoOwnership();
		carbonPublicTransport();
		carbonAirTransport();
		printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal);
	}
	
	public static Scanner userInput = new Scanner(System.in);
	
	public static double carbonPrivateAutoUseage() {
		System.out.println("How many liters of gasoline do you use per day?");
		double litersPerDay = userInput.nextDouble();
		System.out.println("How many kilometers do you drive per day?");
		double kmPerDay = userInput.nextDouble();
		double fuelEfficiency = kmPerDay/litersPerDay;
		double litresUsedPerYear = 365 * (kmPerDay/fuelEfficiency);
		double kgCO2 = 12.85 * litresUsedPerYear;
		return kgCO2;
	}
	
	public static double carbonPrivateAutoOwnership() {
		System.out.println("How old is your car?");
		double ageOfCar = userInput.nextDouble();
		double kgC02PerGJ = 1.94;
		double carbonManufactureCar = (kgC02PerGJ*120) + ((3.8*kgC02PerGJ)*ageOfCar);
		double kgCO2Car = carbonManufactureCar/ageOfCar;
		return kgCO2Car;
	}
	
	public static double carbonPublicTransport() {
		System.out.println("How many kilometers do you ride the bus per day?");
		double busRide = userInput.nextDouble();
		double kgC02OnBus = (0.18*busRide) * 365;
		return kgC02OnBus;
	}
	
	public static double carbonAirTransport() {
		System.out.println("How many kilometers in total have you flown on domestic flights this year?");
		double domesticFlight = userInput.nextDouble();
		System.out.println("How many kilometers in total have you flown on international flights this year?");
		double internationalFlight = userInput.nextDouble();
		double smallHaul = 0.10*domesticFlight;
		double bigHaul = 0.2*internationalFlight;
		double totalHaul = smallHaul+bigHaul;
		return totalHaul;
	}
	
	public static double carbonTotal(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul) {
		double overallTotal = (kgCO2 + kgCO2Car + kgC02OnBus + totalHaul)/1000;
		return overallTotal;
	}
	
	public static void printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal){
		System.out.println("You produce an annual total of metric" + overallTotal + "tons of CO2 per year for your personal transport.");
		System.out.println("The breakdown is as follows:");
		System.out.println("Private Automobile Usage	" + kgCO2);
		System.out.println("Private Automobile Ownership	" + kgCO2Car);
		System.out.println("Public Transport	" + kgC02OnBus);
		System.out.println("Air Transport	" + totalHaul);
	}
}

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Problem w/ Parameters & Calling Methods

Posted 30 September 2012 - 07:49 PM

You don't specify the parameter type on a method call
printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal); 
should be:
printReport(kgCO2, kgCO2Car, kgC02OnBus, totalHaul, overallTotal); 


Was This Post Helpful? 1
  • +
  • -

#8 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Problem w/ Parameters & Calling Methods

Posted 30 September 2012 - 07:52 PM

What he said. ^^
Was This Post Helpful? 0
  • +
  • -

#9 Collectionize  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-September 12

Re: Problem w/ Parameters & Calling Methods

Posted 30 September 2012 - 07:56 PM

Ah, I tried that too, but I get an error. It looks something like this...

FootprintCalculator.java:11: error: cannot find symbol
printReport(kgCO2, kgCO2Car, kgC02OnBus, totalHaul, overallTotal);
^
FootprintCalculator.java:11: error: cannot find symbol
printReport(kgCO2, kgCO2Car, kgC02OnBus, totalHaul, overallTotal);
^
FootprintCalculator.java:11: error: cannot find symbol
printReport(kgCO2, kgCO2Car, kgC02OnBus, totalHaul, overallTotal);
^
FootprintCalculator.java:11: error: cannot find symbol
printReport(kgCO2, kgCO2Car, kgC02OnBus, totalHaul, overallTotal);
^
FootprintCalculator.java:11: error: cannot find symbol
printReport(kgCO2, kgCO2Car, kgC02OnBus, totalHaul, overallTotal);
^

symbol: variable overallTotal
location: class FootprintCalculator
5 errors
Was This Post Helpful? 0
  • +
  • -

#10 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Problem w/ Parameters & Calling Methods

Posted 30 September 2012 - 07:59 PM

I see it now, you're passing variables to printReport with variables that aren't even initialized on your main method. Declare those variables, give them the values from the appropriate methods where they're supposed to get them, then all will be fine. So it's line 7 to 10.
Was This Post Helpful? 0
  • +
  • -

#11 Collectionize  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-September 12

Re: Problem w/ Parameters & Calling Methods

Posted 30 September 2012 - 08:04 PM

Uhm, how would I do that? ^^; I'm still a newbie to this so I haven't gotten all the lingo down for programming yet.
Was This Post Helpful? 0
  • +
  • -

#12 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Problem w/ Parameters & Calling Methods

Posted 30 September 2012 - 08:11 PM

No problem, here's what you have in your main method:
	carbonPrivateAutoUseage();
		carbonPrivateAutoOwnership();
		carbonPublicTransport();
		carbonAirTransport();
		printReport();


Upon inspecting these methods, they return a value, here's an example:
public static double carbonPrivateAutoOwnership()

This method declaration says that this method is public, can be accessed in a static context, and returns a double value.

Now to assign a value, we put a variable on the left side to receive the value of the right side. We can get values from other variables, expressions, or from methods that return values. Here, we have the third:
double carbonPrivateAutoUseage = carbonPrivateAutoUseage();


That will give the variable carbonPrivateAutoUseage the return value of the method carbonPrivateAutoUseage. Now you only need to assign proper variables to receive the values from the method, then pass them on to printReport. Good luck. ^^
Was This Post Helpful? 0
  • +
  • -

#13 Collectionize  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-September 12

Re: Problem w/ Parameters & Calling Methods

Posted 30 September 2012 - 08:19 PM

	public static double carbonPrivateAutoOwnership() {
		System.out.println("How old is your car?");
		double ageOfCar = userInput.nextDouble();
		double kgC02PerGJ = 1.94;
		double carbonManufactureCar = (kgC02PerGJ*120) + ((3.8*kgC02PerGJ)*ageOfCar);
		double kgCO2Car = carbonManufactureCar/ageOfCar;
		return kgCO2Car;
	}

So by doing what's below the main method receives kgCO2Car which is returned?
double carbonPrivateAutoUseage = carbonPrivateAutoUseage();


Was This Post Helpful? 0
  • +
  • -

#14 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Problem w/ Parameters & Calling Methods

Posted 30 September 2012 - 08:21 PM

Yes, that is correct. ^^
Was This Post Helpful? 0
  • +
  • -

#15 Collectionize  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-September 12

Re: Problem w/ Parameters & Calling Methods

Posted 30 September 2012 - 08:32 PM

It runs now, but it posts my set of questions 3 times.

Hm, here's what my code looks like now.
import java.util.Scanner;

public class FootprintCalculator {
	
	public static void main(String[] args) {
		System.out.println("We will calculate the CO2 emissions for your transportation in a year. After answering a few of the following questions that appear. Please answer with numbers.");
		carbonPrivateAutoUseage();
		carbonPrivateAutoOwnership();
		carbonPublicTransport();
		carbonAirTransport();
		
		double kgCO2 = carbonPrivateAutoUseage();
		double kgCO2Car = carbonPrivateAutoOwnership();
		double kgC02OnBus = carbonPublicTransport();
		double totalHaul = carbonAirTransport();
		double overallTotal = carbonTotal(kgCO2, kgCO2Car, kgC02OnBus, totalHaul);
		printReport(kgCO2, kgCO2Car, kgC02OnBus, totalHaul, overallTotal);
	}
	
	public static Scanner userInput = new Scanner(System.in);
	
	public static double carbonPrivateAutoUseage() {
		System.out.println("How many liters of gasoline do you use per day?");
		double litersPerDay = userInput.nextDouble();
		System.out.println("How many kilometers do you drive per day?");
		double kmPerDay = userInput.nextDouble();
		double fuelEfficiency = kmPerDay/litersPerDay;
		double litresUsedPerYear = 365 * (kmPerDay/fuelEfficiency);
		double kgCO2 = 12.85 * litresUsedPerYear;
		return kgCO2;
	}
	
	public static double carbonPrivateAutoOwnership() {
		System.out.println("How old is your car?");
		double ageOfCar = userInput.nextDouble();
		double kgC02PerGJ = 1.94;
		double carbonManufactureCar = (kgC02PerGJ*120) + ((3.8*kgC02PerGJ)*ageOfCar);
		double kgCO2Car = carbonManufactureCar/ageOfCar;
		return kgCO2Car;
	}
	
	public static double carbonPublicTransport() {
		System.out.println("How many kilometers do you ride the bus per day?");
		double busRide = userInput.nextDouble();
		double kgC02OnBus = (0.18*busRide) * 365;
		return kgC02OnBus;
	}
	
	public static double carbonAirTransport() {
		System.out.println("How many kilometers in total have you flown on domestic flights this year?");
		double domesticFlight = userInput.nextDouble();
		System.out.println("How many kilometers in total have you flown on international flights this year?");
		double internationalFlight = userInput.nextDouble();
		double smallHaul = 0.10*domesticFlight;
		double bigHaul = 0.2*internationalFlight;
		double totalHaul = smallHaul+bigHaul;
		return totalHaul;
	}
	
	public static double carbonTotal(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul) {
		double overallTotal = (kgCO2 + kgCO2Car + kgC02OnBus + totalHaul)/1000;
		return overallTotal;
	}
	
	public static void printReport(double kgCO2, double kgCO2Car, double kgC02OnBus, double totalHaul, double overallTotal){
		System.out.println("You produce an annual total of metric" + overallTotal + "tons of CO2 per year for your personal transport.");
		System.out.println("The breakdown is as follows:");
		System.out.println("Private Automobile Usage	" + kgCO2);
		System.out.println("Private Automobile Ownership	" + kgCO2Car);
		System.out.println("Public Transport	" + kgC02OnBus);
		System.out.println("Air Transport	" + totalHaul);
	}
}

I have no clue why it repeats my set of questions so many times. xP
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2