6 Replies - 2022 Views - Last Post: 05 July 2009 - 10:59 AM Rate Topic: -----

#1 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Problem with methods

Post icon  Posted 04 July 2009 - 05:00 PM

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:

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with methods

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Problem with methods

Posted 04 July 2009 - 05:21 PM

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.

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! :)

This post has been edited by Locke: 04 July 2009 - 05:28 PM

Was This Post Helpful? 1
  • +
  • -

#3 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Re: Problem with methods

Posted 04 July 2009 - 07:01 PM

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!
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Problem with methods

Posted 04 July 2009 - 07:53 PM

Oh yes, I forgot about the Scanner object. Sorry about that. :)

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! :)
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem with methods

Posted 04 July 2009 - 08:08 PM

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:

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
   }
}


Was This Post Helpful? 0
  • +
  • -

#6 Get.carter()  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 31
  • Joined: 17-June 09

Re: Problem with methods

Posted 05 July 2009 - 06:29 AM

Tell the boss.

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


Soz couldn't resist that one. :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Problem with methods

Posted 05 July 2009 - 10:59 AM

View PostGet.carter(), on 5 Jul, 2009 - 07:29 AM, said:

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


Soz couldn't resist that one. :rolleyes:


Improper syntax...;) :P (in both lines that contain actual code (not braces))

This post has been edited by Locke: 05 July 2009 - 10:59 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1