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

Thread: Returning methods to main() and other methods

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

    Default Returning methods to main() and other methods

    This is a simple paint calculator program I built. I am an absolute beginner and I am having problems returning and displaying methods to the main. I keep trying to call the paintFormula() method back to the main but I keep getting errors. Also I am not sure if I am passing the wallArea() methods "calculated wallArea" to the paintFormula(). I keep trying but nothing works.

    The instructions ask me to:

    Assume that a gallon of paint covers about 350 square feet of wall space. Create an application with a main() method that prompts the user for the length, width, and height of a rectangular room. Pass these three values to a method that does the following:

    * Calculates the wall area for a room

    * Passes the calculated wall area to another method that calculates and returns the number of gallons of paint needed

    * Displays the number of gallons needed

    * Computes the price based on a paint price of $32 per gallon, assuming that the painter can buy any fraction of a gallon of paint at the same
    price as a whole gallon.

    * Returns the price to the main() method

    * The main() method displays the final price.

     
    import java.util.Scanner;
     
    public class PaintCalculator2
    {
    	public static void main(String [] args)
    	{
    	 Scanner keyboard = new Scanner(System.in);
     
             double wallArea;                            
    	 double height; 
    	 double length; 
    	 double width;
             double price;
             double WallArea;
             double paintQuantity;
     
     
             //Prompts user for the dimensions of the room                         
    	 System.out.print("Please enter the height of the room:   ");
    	 height = keyboard.nextDouble();
     
             System.out.print("Please enter the length of the room:   ");
    	 length = keyboard.nextDouble();
     
             System.out.print("Please enter the width of the room:   ");
    	 width = keyboard.nextDouble();
     
             WallAreaMethod(height, length, width);    
             }
     
     
     
                //Calulates the area of the wall in a room
                public static double WallAreaMethod(double height, double length, double width)
                {
                 double wallArea;
                 wallArea = length * height * width * height;
     
                 return wallArea;
                }
     
     
     
                    //Computes the quanity of paint needed
                    public static double paintFormula(double wallAreaMethod, double price, double height, double length,double width)
                    {
     
                      double wallArea;
                      double paintQuantity;
     
                      paintQuantity = wallAreaMethod * 2 / 350;
     
     
                      System.out.println("For a room of  height " + height + "feet, length " + 
    	          length + " feet, and width " + width + " feet you need to purchase "
    	          + paintQuantity + " gallons of paint.");
     
                      System.out.println("The price will be $" + price + ".");
     
                      price = paintQuantity * 32.0;
     
                      return price; 
                     }
     
    }
    Last edited by jonmackey22; September 25th, 2014 at 05:23 AM.


  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: Returning methods to main() and other methods

    what errors are you getting? Also you might think of better method names. Also method names start with lower case letters.

  3. The Following User Says Thank You to PhHein For This Useful Post:

    jonmackey22 (September 25th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Returning methods to main() and other methods

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

    Post the entire text of message you're receiving.

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

    jonmackey22 (September 25th, 2014)

  6. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Returning methods to main() and other methods

    I realize there was some errors in my code but I fixed them. Right now, I am trying to return the price from the paintformula ex: price = paintFormula(wallArea,price,height,length,width); or paintFormula(wallArea,price,height,length,width);. When I call the method I keep getting errors like: PaintCalculator2.java:33: error: variable price might not have been initialized.

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Returning methods to main() and other methods

    variable price might not have been initialized.
    If you give the variable an initial value the compiler won't warn you about the possibility that the variable might not have a value when you use it.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    jonmackey22 (September 25th, 2014)

Similar Threads

  1. HW Help: Returning Values with Methods
    By santafebound in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 25th, 2014, 02:33 AM
  2. Calling up array methods from main
    By my21 in forum Collections and Generics
    Replies: 12
    Last Post: November 6th, 2013, 05:15 AM
  3. hey i need some help with the main of this methods that i made pls help
    By taujvprogramer in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 14th, 2012, 03:28 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