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 16 of 16

Thread: Attempting to calculate a budget and using public static double method(...)

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Attempting to calculate a budget and using public static double method(...)

    I'm trying to get back the answer in my public static double method. Every time I try to compile, however, it doesn't recognize this: (double kms, double fuelEfficiency, double carIns) and says I am missing a class. I'm completely stuck why I can't get it to work.

    Note: I need to actually do a lot more calculations in there, but first I want to try with it as a basic just to see if it works.
    java.util.Scanner;
    public class budgetCalculator {
    	public static void main(String[] args) {
    		//Scanner
    		Scanner scan = new Scanner(System.in);
    		//Find out number of roommates.
    		System.out.println("How many roommates do you have?:");
    		int occupancy = scan.nextInt();
    		//Find out rent.
    		System.out.println("How much do you pay for rent? (in dollars):");
    		double rent = scan.nextInt();
    		//Find out car insurance.
    		System.out.println("What do you pay for car insurance?:");
    		double carIns = scan.nextInt();
    		//Find out km driven per day.
    		System.out.println("How many kilometres do you drive per day?:");
    		double kms = scan.nextInt();
    		//Find out mileage in car.
    		System.out.println("What is the mileage on your car?:");
    		System.out.println("In case you aren't sure, an average is that:");
    		System.out.println("A very small car's mileage is 7.1L/100km");
    		System.out.println("A small car's mileage is 8.2L/100km.");
    		System.out.println("A sport car's mileage is 14.9L/100km");
    		System.out.println("and an SUV's mileage is 12.3L/100km");
    		double fuelEfficiency = scan.nextInt();
    		//Find out average electric bill.
    		System.out.println("What do you pay for electricity (every two months)?:");
    		double electricity = scan.nextInt();
    		// Find out average cable bill.
    		System.out.println("What do you pay for cable each month?:");
    		double cable = scan.nextInt();
    		//Find out average water bill.
    		System.out.println("What do you pay every 4 months for water?:");
    		double water = scan.nextInt();
    		//Find out average internet bill.
    		System.out.println("What do you pay each month for Internet?:");
    		double internet = scan.nextInt();
    		//Find weekly food costs.
    		System.out.println("What do you pay weekly for food?:");
    		double groceries = scan.nextInt();
    		//Find eating out cost.
    		System.out.println("How much money do you spend eating out per week?:");
    		double resteraunt = scan.nextInt();
    		//Find out mweekly alcohol cost.
    		System.out.println("How much do you spend on alcohol weekly?:");
    		double liquor = scan.nextInt();
     
    		System.out.println(determineTransportationCost(double kms, double fuelEfficiency, double carIns));
    	}
    		public static double determineTransportationCost(double kms, double fuelEfficiency, double carIns) {
    		double transportation = 30 * kms;
    		return transportation;
    	}
    }

    Help?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Attempting to calculate a budget and using public static double method(...)

    The compiler already knows what types your variables are in the method call. No need to include them.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Attempting to calculate a budget and using public static double method(...)

    SO I did that and have been able to continue with my program, but now that it is mostly coded I have run into a problem. The program will not compile around the printReport method.

    import java.util.Scanner;
    public class budgetCalculator {
    	public static void main(String[] args) {
    		//Scanner
    		Scanner scan = new Scanner(System.in);
    		//Find out number of roommates.
    		System.out.println("How many roommates do you have?:");
    		int occupancy = scan.nextInt();
    		occupancy = occupancy + 1;
    		//Find out rent.
    		System.out.println("How much do you pay for rent? (in dollars):");
    		double rent = scan.nextDouble();
    		//Find out car insurance.
    		System.out.println("What do you pay for car insurance?:");
    		double carIns = scan.nextDouble();
    		//Find out km driven per day.
    		System.out.println("How many kilometres do you drive per day?:");
    		double kms = scan.nextDouble();
    		//Find out mileage in car.
    		System.out.println("What is the mileage on your car?:");
    		System.out.println("In case you aren't sure, an average is that:");
    		System.out.println("A very small car's mileage is 7.1L/100km");
    		System.out.println("A small car's mileage is 8.2L/100km.");
    		System.out.println("A sport car's mileage is 14.9L/100km");
    		System.out.println("and an SUV's mileage is 12.3L/100km");
    		double fuelEfficiency = scan.nextDouble();
    		//Find out average electric bill.
    		System.out.println("What do you pay for electricity (every two months)?:");
    		double electricity = scan.nextDouble();
    		// Find out average cable bill.
    		System.out.println("What do you pay for cable each month?:");
    		double cable = scan.nextDouble();
    		//Find out average water bill.
    		System.out.println("What do you pay every 4 months for water?:");
    		double water = scan.nextDouble();
    		//Find out average internet bill.
    		System.out.println("What do you pay each month for Internet?:");
    		double internet = scan.nextDouble();
    		//Find weekly food costs.
    		System.out.println("What do you pay weekly for food?:");
    		double groceries = scan.nextDouble();
    		//Find eating out cost.
    		System.out.println("How much money do you spend eating out per week?:");
    		double resteraunt = scan.nextDouble();
    		//Find out mweekly alcohol cost.
    		System.out.println("How much do you spend on alcohol weekly?:");
    		double liquor = scan.nextDouble();
     
    		printReport(budget, transportation, utility, food, housing);		
    	}
    	public static double determineTransportationCost(double kms,double fuelEfficiency,double carIns) {
    		double transportation = (30 * kms * (fuelEfficiency % 100)) * 1.32; 
    		return transportation;
    	}
    	public static double determineUtilityCost(double electricity, double cable, double water, double internet, int occupancy) {
    		double utility = ((electricity % 2) + cable + (water % 4) + internet) % occupancy;
    		return utility;
    	}
    	public static double determineFoodCost(double groceries, double restaurant, double liquor, int occupancy) {
    		double food = ((groceries + restaurant + liquor) % occupancy) * 4;
    		return food;
    	}
    	public static double determineHousingCost(double rent, int occupancy) {
    		double housing = rent % occupancy;
    		return housing;
    	}
    	public static double calculateTermBudget(double transportation, double utility, double food, double housing) 
    		double budget = transportation + utility + food + housing;
    		return budget;
    	}
    	public static void printReport(double budget, double transportation, double utility, double food, double housing) {
    		System.out.println("Your term budget is:" + budget);
    		System.out.println("The break down is as follows:");
    		System.out.println((transportation % budget) * 100 + "%");
    		System.out.println((utility % budget) * 100 + "%");
    		System.out.println((food % budget) * 100 + "%");
    		System.out.println((housing % budget) * 100 + "%");
    	}
    }

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Attempting to calculate a budget and using public static double method(...)

    We don't read minds. Post the exact error message you get.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Attempting to calculate a budget and using public static double method(...)

    import java.util.Scanner;
    public class budgetCalculator {
    	public static void main(String[] args) {
    		//Scanner
    		Scanner scan = new Scanner(System.in);
    		//Find out number of roommates.
    		System.out.println("How many roommates do you have?:");
    		int occupancy = scan.nextInt();
    		occupancy = occupancy + 1;
    		//Find out rent.
    		System.out.println("How much do you pay for rent? (in dollars):");
    		double rent = scan.nextDouble();
    		//Find out car insurance.
    		System.out.println("What do you pay for car insurance?:");
    		double carIns = scan.nextDouble();
    		//Find out km driven per day.
    		System.out.println("How many kilometres do you drive per day?:");
    		double kms = scan.nextDouble();
    		//Find out mileage in car.
    		System.out.println("What is the mileage on your car?:");
    		System.out.println("In case you aren't sure, an average is that:");
    		System.out.println("A very small car's mileage is 7.1L/100km");
    		System.out.println("A small car's mileage is 8.2L/100km.");
    		System.out.println("A sport car's mileage is 14.9L/100km");
    		System.out.println("and an SUV's mileage is 12.3L/100km");
    		double fuelEfficiency = scan.nextDouble();
    		//Find out average electric bill.
    		System.out.println("What do you pay for electricity (every two months)?:");
    		double electricity = scan.nextDouble();
    		// Find out average cable bill.
    		System.out.println("What do you pay for cable each month?:");
    		double cable = scan.nextDouble();
    		//Find out average water bill.
    		System.out.println("What do you pay every 4 months for water?:");
    		double water = scan.nextDouble();
    		//Find out average internet bill.
    		System.out.println("What do you pay each month for Internet?:");
    		double internet = scan.nextDouble();
    		//Find weekly food costs.
    		System.out.println("What do you pay weekly for food?:");
    		double groceries = scan.nextDouble();
    		//Find eating out cost.
    		System.out.println("How much money do you spend eating out per week?:");
    		double resteraunt = scan.nextDouble();
    		//Find out mweekly alcohol cost.
    		System.out.println("How much do you spend on alcohol weekly?:");
    		double liquor = scan.nextDouble();
     
    		printReport(budget, transportation, utility, food, housing);		
    	}
    	public static double determineTransportationCost(double kms,double fuelEfficiency,double carIns) {
    		double transportation = (30 * kms * (fuelEfficiency % 100)) * 1.32; 
    		return transportation;
    	}
    	public static double determineUtilityCost(double electricity, double cable, double water, double internet, int occupancy) {
    		double utility = ((electricity % 2) + cable + (water % 4) + internet) % occupancy;
    		return utility;
    	}
    	public static double determineFoodCost(double groceries, double restaurant, double liquor, int occupancy) {
    		double food = ((groceries + restaurant + liquor) % occupancy) * 4;
    		return food;
    	}
    	public static double determineHousingCost(double rent, int occupancy) {
    		double housing = rent % occupancy;
    		return housing;
    	}
    	public static double calculateTermBudget(double transportation, double utility, double food, double housing) {
    		double budget = transportation + utility + food + housing;
    		return budget;
    	}
    	public static void printReport(double budget, double transportation, double utility, double food, double housing) {
    		System.out.println("Your term budget is:" + budget);
    		System.out.println("The break down is as follows:");
    		System.out.println((transportation % budget) * 100 + "%");
    		System.out.println((utility % budget) * 100 + "%");
    		System.out.println((food % budget) * 100 + "%");
    		System.out.println((housing % budget) * 100 + "%");
    	}
    }
    It says that thee variables housing, food, utility, budget and transportation don't exist up in the printReport in the main method.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,143
    My Mood
    Mellow
    Thanks
    187
    Thanked 660 Times in 647 Posts

    Default Re: Attempting to calculate a budget and using public static double method(...)

    When someone says, "Post the exact error message you get," they mean copy the errors and stack trace you get when you run the code (show a sample run if possible), and paste what you've copied directly into a post. Use formatting tags if the error/stack trace is long and/or the formatting of the message is important.

    Your filtered translation "It says that thee variables housing, food, utility, budget and transportation don't exist" is almost useless, because you don't really understand what you're reading. We'll help you understand the original, IF you show it to us.

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    taze (October 3rd, 2013)

  8. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Attempting to calculate a budget and using public static double method(...)

    oh, I see. The error after compiling is:
    budgetCalculator.java:49: error: cannot find symbol
                    printReport(budget, transportation, utility, food, housing);
     
                                ^
      symbol:   variable budget
      location: class budgetCalculator
    budgetCalculator.java:49: error: cannot find symbol
                    printReport(budget, transportation, utility, food, housing);
     
                                        ^
      symbol:   variable transportation
      location: class budgetCalculator
    budgetCalculator.java:49: error: cannot find symbol
                    printReport(budget, transportation, utility, food, housing);
     
                                                        ^
      symbol:   variable utility
      location: class budgetCalculator
    budgetCalculator.java:49: error: cannot find symbol
                    printReport(budget, transportation, utility, food, housing);
     
                                                                 ^
      symbol:   variable food
      location: class budgetCalculator
    budgetCalculator.java:49: error: cannot find symbol
                    printReport(budget, transportation, utility, food, housing);
     
                                                                       ^
      symbol:   variable housing
      location: class budgetCalculator
    5 errors

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,143
    My Mood
    Mellow
    Thanks
    187
    Thanked 660 Times in 647 Posts

    Default Re: Attempting to calculate a budget and using public static double method(...)

    It's pointing at line 49. This forum doesn't show us line numbers, so posting code snippets usually requires pointing us to which posted line is being complained about. In this case, it's pretty easy to figure out which line is line 49. And then, it's pretty easy to see that you don't know how to use methods.

    The method calculateTermBudget() returns a value which happens to be represented by a variable local to the method (not visible anywhere else) called 'budget'. So, the proper way to call print report is:

    printReport( calculateTermBudget( transportation, utility, food, housing ), determineTransportationCost( kms, fuelEfficiency, carIns), etc. . . . )

    That will result in new errors, because not all of the variables in those method calls have been defined, like transportation, utility, food, and housing. You'll have to rethink the design a bit. Should the calculateTermBudget() method be called first, or should the other methods be called first with their results used to call the calculateTermBudget() method?

    I hope you can see the answer, because it's pretty clear.

  10. The Following User Says Thank You to GregBrannon For This Useful Post:

    taze (October 3rd, 2013)

  11. #9
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Attempting to calculate a budget and using public static double method(...)

    Unfortunately - I don't see it clearly.

    You state:
    Should the calculateTermBudget() method be called first, or should the other methods be called first with their results used to call the calculateTermBudget() method?

    I understand in what you mean by calling calculateTermBudget(....) first, like in your example, but I do not understand what you mean by calling the other methods first with their results used to call the termBudget method,

    I've tried to rework the program from changing placement of the methods to trying different variations of printReport method (i.e. mainly rewriting what was above in different orders - it still will not recognize housing, transportation, utility and food (the error being that java cannot find the symbol).

    So no, it really isn't as clear to me as one would expect.

  12. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,143
    My Mood
    Mellow
    Thanks
    187
    Thanked 660 Times in 647 Posts

    Default Re: Attempting to calculate a budget and using public static double method(...)

    Which of the following orders of operations will be successful:

    1 -
    c = a + b
    a = 5
    b = 8

    2 -
    a = 5
    c = a + b
    b = 8

    3-
    a = 5
    b = 8
    c = a + b

  13. #11
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Attempting to calculate a budget and using public static double method(...)

    3. So basically, you're saying that what I am attempting to do is do a calculation without stating the variables first - trying to define c before I define a and b.

    So I thought to, in place of my previous printReport statement, do this:

    determineTransportationCost( kms,fuelEfficiency,carIns);		
    		determineUtilityCost(electricity,cable, water, internet,occupancy);
    		determineFoodCost(groceries, restaurant, liquor, occupancy);
    		determineHousingCost(rent,occupancy);
    		calculateTermBudget(transportation,utility,food,housing);
    		printReport(transportation,utility, food, housing, budget);

    The idea was that it would return my values so that calculateTermBudget could go through, however it still returns the same errors as above, so obviously I'm doing this wrong.

  14. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,143
    My Mood
    Mellow
    Thanks
    187
    Thanked 660 Times in 647 Posts

    Default Re: Attempting to calculate a budget and using public static double method(...)

    You're getting it, just not quite there yet. You're calling the methods, and the methods are returning values, but the program is not using the returned values. You can eliminate these variables by calling the methods directly in the printReport() method call, but you can also assign the returned values to variables before the method call, like this:

    double foodCost = determineFoodCost(double groceries, double restaurant, double liquor, int occupancy);

    Do the same for each of the variables that need to be defined before the method printReport() is called to present the results. Substitute those values, like foodCost, into the printReport() method call.

  15. The Following User Says Thank You to GregBrannon For This Useful Post:

    taze (October 3rd, 2013)

  16. #13
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Attempting to calculate a budget and using public static double method(...)

    It compiles and with a few minor tweaks to the basic mathematics it works as it's supposed to! Thank you so much for the help and sorry for being so confused! You really cleared it up, thank you!

  17. #14
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,143
    My Mood
    Mellow
    Thanks
    187
    Thanked 660 Times in 647 Posts

    Default Re: Attempting to calculate a budget and using public static double method(...)

    Glad to have helped and that you figured it out.

  18. #15
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Attempting to calculate a budget and using public static double method(...)

    Quote Originally Posted by GregBrannon View Post
    Glad to have helped and that you figured it out.
    I have the same problem but I still can't work it out after I see your explanations. Please help.

  19. #16
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,143
    My Mood
    Mellow
    Thanks
    187
    Thanked 660 Times in 647 Posts

    Default Re: Attempting to calculate a budget and using public static double method(...)

    I'm sorry. I don't know why taze responded in your thread. I thought it was you.

    Post your updated code and the error message that goes with it.

Similar Threads

  1. Calling A Public Static Void Method from Main
    By Clubs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 27th, 2013, 02:29 AM
  2. How to call a static method within a static method in the same class?
    By EDale in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2013, 04:13 AM
  3. public static void main(NewMember Mr_Bukkake){ }
    By Mr. Bukkake in forum Member Introductions
    Replies: 2
    Last Post: October 13th, 2011, 07:29 AM
  4. Replies: 3
    Last Post: June 11th, 2011, 02:40 PM
  5. Error using public static double Method
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2010, 03:01 PM

Tags for this Thread