Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,509 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,912 people online right now. Registration is fast and FREE... Join Now!




Problem with methods

 

Problem with methods

villalandron

4 Jul, 2009 - 04:00 PM
Post #1

D.I.C Head
**

Joined: 24 Sep, 2008
Posts: 124

Hello! I am just starting to learn about methods and am somewhat confused. I am doing an exercise program with a scenario. A painting company has determined that for every 115 square feet of wall space, one gallon of
paint and eight hours of labor will be required. The company charges $18.00 per hour for labor. Write a program
that allows the user to enter the number of rooms to be painted and the price of the paint per gallon. It should
also ask for the square feet of wall space in each room. The program should have methods that return the following
data:
1. The number of gallons of paint required
2. The hours of labor required
3. The cost of the paint
4. The labor charges
5. The total cost of the paint job

I am getting a lot of errors in this. This is my first try at doing methods and am having a extremely hard time. Here's what I have so far:

CODE

import java.util.Scanner;

public class paint
{
   public static void main (String[ ] args)
   {
        //Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);
        
        rooms ();
        price ();
        square();

                int room, price, sqtft, gallon, labor, cost, laborc, total;
        
        gallon = sqtft / 115;
        labor = (sqtft * 8) / 115;
        cost =  (sqtft * price) / 115;
        laborc = labor * 18;
        total = laborc + cost;
            
            while (room < 1 || price < 1 || sqtft < 1)
        {
        System.out.println("Please enter correct amount.");
        System.out.println("How many rooms are in the house?");    
        room = keyboard.nextInt();
        System.out.println("What is the price of the paint per gallon?");    
        price = keyboard.nextInt();        
        System.out.println("What is the square feet of the wall space per room?");
        sqtft = keyboard.nextInt();
        }
        
        System.out.println("The number of gallons of paint required are: " + gallon);
        System.out.println("The hours of labor required are: " + labor);
        System.out.println("The cost of the paint is: $" + cost);
        System.out.println("The labor charges are: $" + laborc);
        System.out.println("The total cost of the paint job is: $" + total);
            
   }
  
   public static void rooms()
   {
        System.out.println("How many rooms are in the house?");    
        room = keyboard.nextInt();
   }    
        
    public static void price()
    {
        System.out.println("What is the price of the paint per gallon?");    
        price = keyboard.nextInt();    
    }    
        
    public static void square()
    {
        System.out.println("What is the square feet of the wall space per room?");
        sqtft = keyboard.nextInt();
    }

}//end class



Every single one of the examples in that I see in the book are done with strings and not integers. I don't know if that'll make a difference but I don't know exactly how to correct the problem. Any help will be appreciatted!

User is offlineProfile CardPM
+Quote Post


Locke

RE: Problem With Methods

4 Jul, 2009 - 04:21 PM
Post #2

Treasure Hunter
Group Icon

Joined: 20 Mar, 2008
Posts: 3,943



Thanked: 292 times
Dream Kudos: 325
Expert In: Java

My Contributions
Ok.

So, you need to have some class variables, since you try to use some variables in the methods that don't exist--room, price, sqtft for example, you use those in the methods, but they are only created in the main method. Instead of declaring those in the main method, declare them in the class...like this. Then, you'll be able to access the same variables over the entire class, no matter what method they are in.

Java
public class paint
{
static int room;
static int price;
static int sqtft;
// remove those variables from the main method and put them there.

public static void main(String[ ] args)
{
// ...your code here.
}

// other methods here.
}


Other than that, I can't see any errors. (I haven't compiled it for myself)

But remember to post the errors you get too. Those will help us tremendously.

Hope this helps! smile.gif

This post has been edited by Locke: 4 Jul, 2009 - 04:28 PM
User is offlineProfile CardPM
+Quote Post

villalandron

RE: Problem With Methods

4 Jul, 2009 - 06:01 PM
Post #3

D.I.C Head
**

Joined: 24 Sep, 2008
Posts: 124

This helped me greatly. I was not able to understand a way to do static variables until. Now, When I tried it, there were 3 errors but I was able to fix them since now I understand what static variables are. Created static Scanner keyboard = new Scanner(System.in); Thank you for your help!
User is offlineProfile CardPM
+Quote Post

Locke

RE: Problem With Methods

4 Jul, 2009 - 06:53 PM
Post #4

Treasure Hunter
Group Icon

Joined: 20 Mar, 2008
Posts: 3,943



Thanked: 292 times
Dream Kudos: 325
Expert In: Java

My Contributions
Oh yes, I forgot about the Scanner object. Sorry about that. smile.gif

Static variables are for accessing the same variable throughout the class, no matter what instance is used. In essence, it's a variable that is only created ONCE. Only one is created no matter how many instances of the class are used.

Also, if you're going to access a class variable in a static method, the variable MUST be static. If it's not declared static, the method would see it as an instance variable, and the static method would not know which instance's value to use. (Hope that isn't too confusing)

Glad I could help! smile.gif
User is offlineProfile CardPM
+Quote Post

pbl

RE: Problem With Methods

4 Jul, 2009 - 07:08 PM
Post #5

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
Actually the main(() method should only be used to access the class

What is the puprose to create a paint class if there is no piant object created
your code should rather look like this:

CODE

class paint {
   int gallon;                     // the gallon for THIS paint job
   ...

   // constructor of paint
   paint() {
      Scanner scan = new Scanner(System.in);
      ... do all your stuff here
   }

   void otherMethod() {    // of paint not static
   ....
   }

   // the ONLY static method il all the class
   public static void main(String[] args) {
      // the ONLY line in main() method
      new paint();    // create paint object that will do what it has to do
   }
}

User is offlineProfile CardPM
+Quote Post

Get.carter()

RE: Problem With Methods

5 Jul, 2009 - 05:29 AM
Post #6

New D.I.C Head
*

Joined: 17 Jun, 2009
Posts: 14

Tell the boss.

CODE
if (timeAllowed == 8hrs) && (workExpected == 1Gallon) && (ratePerHour == $18.00)
{
exPainterCarter = System.out.print("Plz gimme the job!");
}

Soz couldn't resist that one. rolleyes.gif
User is offlineProfile CardPM
+Quote Post

Locke

RE: Problem With Methods

5 Jul, 2009 - 09:59 AM
Post #7

Treasure Hunter
Group Icon

Joined: 20 Mar, 2008
Posts: 3,943



Thanked: 292 times
Dream Kudos: 325
Expert In: Java

My Contributions
QUOTE(Get.carter() @ 5 Jul, 2009 - 07:29 AM) *

CODE
if (timeAllowed == 8hrs) && (workExpected == 1Gallon) && (ratePerHour == $18.00)
{
exPainterCarter = System.out.print("Plz gimme the job!");
}

Soz couldn't resist that one. rolleyes.gif


Improper syntax...wink2.gif tongue.gif (in both lines that contain actual code (not braces))

This post has been edited by Locke: 5 Jul, 2009 - 09:59 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 05:10AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month