Results 1 to 20 of 27
- 04-11-2009, 10:51 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 34
- Rep Power
- 0
[SOLVED] cant figure this program out..help
hi im trying to Write a program called PaintersHelperSplit
It does exactly what my previous PaintersHelper program does.
In this version it says do the calculations in a helper method called computeGallons. Here is the method header for it:
static int computeGallons(int height, int length, int width, int doors, int smWindows, int lgWindows) {
Gather the data you need (6 numbers) in the main method. Then use computeGallons to… compute the gallons. ????
I have to Print the answer to the screen in the main method.
but some of the local variables that I have to declare in main will not be needed there now. They will be needed in computeGallons instead.
ok so heres my original painterhelpers program__
/* This program calculates the number of gallons of paint needed to paint a room.
Programmer:
Date: April 4, 2009
*/
import java.util.Scanner;
public class PaintersHelper {
static float DOOR_SIZE = 15.0f;
static float SMALL_WINDOW_SIZE =2.0f;
static float LARGE_WINDOW_SIZE = 5.0f;
static float SQ_FT_PER_GALLON = 375.0f;
static float netsquarefeet;
static float grosssquarefeet;
public static void main(String[] args) {
// declare all variables to be used
int width, length, height, doors, lgwindows, smwindows;
float gallons, subtraction;
// give welcome and instructions to user
System.out.println("Welcome to the painters Helper");
Scanner input = new Scanner (System.in);
// prompt user for the room's width
System.out.println("Please enter the room's width:");
width = input.nextInt();
// prompt user for the room's length
System.out.println("Please enter the room's length:");
length = input.nextInt();
// prompt user for the room's height
System.out.println("Please enter the room's height:");
height = input.nextInt();
// prompt user for the number of doors
System.out.println("Please enter the number of doors:");
doors = input.nextInt();
// prompt user for the number of small windows
System.out.println("Please enter the the number of small windows:");
smwindows = input.nextInt();
// prompt user for the number of large windows
System.out.println("Please enter the number of large windows:");
lgwindows = input.nextInt();
// gross square feet = height * 2 * (length + width)
grosssquarefeet = height * 2 * ( length + width);
// subtractions is the sum of:
subtraction = (doors * DOOR_SIZE) + (lgwindows * LARGE_WINDOW_SIZE) + (smwindows * SMALL_WINDOW_SIZE);
// number of doors * door size
// number of lgWindows * lgWindow size
// number of smWindows * smWindow size
// net square feet = gross square feet - subtractions
netsquarefeet = grosssquarefeet - subtraction;
// gallons = netSqFt / SQ_FT_PER_GALLON rounded to the nearest gallon
gallons = netsquarefeet / SQ_FT_PER_GALLON;
// display the answer.
System.out.println("You will need about " + gallons + "gallons of paint for your room.");
} // end of method main
} // end of class PaintersHelper
- 04-11-2009, 11:48 PM #2
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
Hi there
The method signature: "static int computeGallons(int height, int length, int width, int doors, int smWindows, int lgWindows)" tells you that the Main program will pass 6 values to the "computeGallons" method. after that every calculations will be performed in the method and an integer representing the Gallons will be returned (we know this because the method is of type integer). This means that all the temporary variables you needed to calculate, will now be in the computeGallons method. In main, the variables that will be needed are the ones to capture values from the prompt. It is up to you to decide whether you want to hold the results of the method computeGallons inside a variable before you display them or if you will just call the method in your println function.
Let me know if you understand. Good luckTshegofatso Manakana
a.k.a Untouchable ™
- 04-12-2009, 02:11 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 34
- Rep Power
- 0
i understand a little of what you just explained, however theres still a confusion here, can you start me off.
-
I would recommend that you read up on creating methods in your textbook or a decent tutorial and then give it a try on your own first. Here are some of the Sun tutorials on this subject:
Defining a method
passing arguments to a method
Returning a value from a method
Best of luck
- 04-12-2009, 05:51 AM #5
Einstein... I would suggest starting small and understanding how a method works. Review the links that Furball has in his post. Once you have a couple of successful examples of methods compiled and working, then you can start to tackle on changing your program to include a method.
A sample (color coded):
Calling the method:
The method:Java Code:[B][COLOR="Red"]int myVariable[/COLOR][/B] = [B][COLOR="YellowGreen"]myMethod[/COLOR][/B]([B][COLOR="Blue"]myIntParameter1, myIntParameter2[/COLOR][/B]);
The red is what the method is going to returnJava Code:public static [B][COLOR="Red"]int[/COLOR][/B] [B][COLOR="yellowgreen"]myMethod[/COLOR][/B]([B][COLOR="blue"]int number1, int number2[/COLOR][/B]) { int myResult = number1 + number2; return [B][COLOR="red"]myResult[/COLOR][/B]; }
the blue is the parameters that are passed to the method (going to be six in your case)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-12-2009, 07:39 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 34
- Rep Power
- 0
thanx man, ill look that over and give it a try.
- 04-13-2009, 03:09 AM #7
Member
- Join Date
- Apr 2009
- Posts
- 34
- Rep Power
- 0
wowo dis is hard im stuck on the reading fubarable put up. i dont know wat im doing here.
-
I think that you've got to learn things that the forum just can't teach you. As I've mentioned before, I think that you should to talk to your teacher asap. One-on-one help will offer your best chance of you up to speed as quickly as possible, either that or in all seriousness and not meant as an insult, consider dropping the class.
Best of luck.
- 04-13-2009, 03:21 AM #9
Member
- Join Date
- Apr 2009
- Posts
- 34
- Rep Power
- 0
wow, thats not going to help by trying to discourage me, why cant you make a comment where you can actually help me in finishing this assingment, i dont really want the answer, but juss a lil assistance here would be good.
- 04-13-2009, 03:35 AM #10
Member
- Join Date
- Apr 2009
- Posts
- 34
- Rep Power
- 0
wow, yea i agree but dont you think ur being a lil harsh here, i mean, u no, i dont know, anyways can anyone help here.
-
It's not that I don't want to help you finish the assignment, it's that our doing the assignment won't help you learn, and what you need more than anything is to understand the concepts. The best way to do that fast is either through hard study or through one-on-one tutoring.
Chris has given good method examples above. Does this information help you? Do you have any questions about his examples that we can answer?
- 04-13-2009, 03:48 AM #12
Member
- Join Date
- Apr 2009
- Posts
- 34
- Rep Power
- 0
lol, this person here seems to hate you or somthin is goin on here, anyways, chris is very helpfull, and you juss can seem to give me an example or instead of helping me through this, you insult me by saying i should drop the class, and thats not helpfull at all, so i dont know whats going on?? juss need a lil help here dats all.
-
What specific question do you have? If you pose an answerable question, I'll do my best to answer it, but again it has to be specific.
- 04-13-2009, 03:55 AM #14
Member
- Join Date
- Apr 2009
- Posts
- 34
- Rep Power
- 0
ok so heres my original painterhelpers program__
/* This program calculates the number of gallons of paint needed to paint a room.
Programmer:
Date: April 4, 2009
*/
import java.util.Scanner;
public class PaintersHelper {
static float DOOR_SIZE = 15.0f;
static float SMALL_WINDOW_SIZE =2.0f;
static float LARGE_WINDOW_SIZE = 5.0f;
static float SQ_FT_PER_GALLON = 375.0f;
static float netsquarefeet;
static float grosssquarefeet;
public static void main(String[] args) {
// declare all variables to be used
int width, length, height, doors, lgwindows, smwindows;
float gallons, subtraction;
// give welcome and instructions to user
System.out.println("Welcome to the painters Helper");
Scanner input = new Scanner (System.in);
// prompt user for the room's width
System.out.println("Please enter the room's width:");
width = input.nextInt();
// prompt user for the room's length
System.out.println("Please enter the room's length:");
length = input.nextInt();
// prompt user for the room's height
System.out.println("Please enter the room's height:");
height = input.nextInt();
// prompt user for the number of doors
System.out.println("Please enter the number of doors:");
doors = input.nextInt();
// prompt user for the number of small windows
System.out.println("Please enter the the number of small windows:");
smwindows = input.nextInt();
// prompt user for the number of large windows
System.out.println("Please enter the number of large windows:");
lgwindows = input.nextInt();
// gross square feet = height * 2 * (length + width)
grosssquarefeet = height * 2 * ( length + width);
// subtractions is the sum of:
subtraction = (doors * DOOR_SIZE) + (lgwindows * LARGE_WINDOW_SIZE) + (smwindows * SMALL_WINDOW_SIZE);
// number of doors * door size
// number of lgWindows * lgWindow size
// number of smWindows * smWindow size
// net square feet = gross square feet - subtractions
netsquarefeet = grosssquarefeet - subtraction;
// gallons = netSqFt / SQ_FT_PER_GALLON rounded to the nearest gallon
gallons = netsquarefeet / SQ_FT_PER_GALLON;
// display the answer.
System.out.println("You will need about " + gallons + "gallons of paint for your room.");
} // end of method main
} // end of class PaintersHelper
ok so how do i do the calculations in a helper method called computeGallons. Here is the method header for it:
static int computeGallons(int height, int length, int width, int doors, int smWindows, int lgWindows) {
Gather the data you need (6 numbers) in the main method. Then use computeGallons to… compute the gallons. ????
I have to Print the answer to the screen in the main method.
-
this part:
tells you that you have to create a method with this signature that takes these input values and returns the result in an int. Your method will look like this:Java Code:static int computeGallons(int height, int length, int width, int doors, int smWindows, int lgWindows) { //..
and the calculations in this method are the exact same as the ones used in your previous program in the main methodJava Code:static int computeGallons(int height, int length, int width, int doors, int smWindows, int lgWindows) { int gallons = //.... do calculation here return gallons; // return the result of the calculation }
The main method gets the input just as you did before, but instead of doing the calculations in the main method, you pass all the data input in by the user into a call of this method:
Java Code:public static void main(String[] args) { int width, length, height, doors, lgwindows, smwindows; int gallons, subtraction; System.out.println("Welcome to the painters Helper"); Scanner input = new Scanner(System.in); System.out.println("Please enter the room's width:"); width = input.nextInt(); System.out.println("Please enter the room's length:"); length = input.nextInt(); //........etc.... code deleted for brevity gallons = computeGallons(/* pass all parameters here */); System.out.println("You will need about " + gallons + "gallons of paint for your room."); }
- 04-13-2009, 08:49 AM #16
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
Einstein, do you understand what the program does? I mean your original program. Are you the one who coded it? Do you understand why you did what ever is in that program?
Tshegofatso Manakana
a.k.a Untouchable ™
- 04-13-2009, 09:16 AM #17
Member
- Join Date
- Apr 2009
- Posts
- 34
- Rep Power
- 0
yea the program is suppose to calculates the number of gallons of paint needed to paint a room. i did it myself through hard work and reading many chapters, but now the instructor wants me to do the calculations in a helper method called computeGallons. its complicated but i am recieving help from chris, and fubarble. im getting there, juss need a lil assistance dats all, and im trying very hard here. thanx
- 04-14-2009, 02:27 AM #18
Einstein... all your calculations to find out how many gallons of paint is needed, has to be done in the method. You have call the method. First thing to do is figure out what the method is going to return. According to the specifications, it's going to return the amount of gallons which is an int. So you need to define an int variable to hold the gallons (totalGallons?). Then you need to call the method. You already have the name of the method and what parameters need to be specified so it would look semthing like:
Now, that call above will replace ALL the caculations you did in the main to get the total amount of gallons and all those caculations will be placed in the method.Java Code:int totalGallons = computeGallons(height, length, width, doors, smWindows, lgWindows);
So far so good?
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-14-2009, 06:19 AM #19
Member
- Join Date
- Apr 2009
- Posts
- 34
- Rep Power
- 0
so far so good
- 04-16-2009, 08:50 AM #20
Member
- Join Date
- Apr 2009
- Posts
- 34
- Rep Power
- 0
Similar Threads
-
I couldn’t figure out how to do this please help!
By afrttoh in forum New To JavaReplies: 5Last Post: 11-19-2008, 08:21 PM -
Cannot figure out min and max, without using arrays. if possible.
By loddog2002 in forum New To JavaReplies: 2Last Post: 11-08-2008, 04:58 AM -
Please Help, can't figure out what I'm doing wrong.
By tamik0 in forum New To JavaReplies: 2Last Post: 07-11-2008, 09:41 AM -
Seems so simple yet I can't figure it out!
By adamhaviland in forum New To JavaReplies: 1Last Post: 11-03-2007, 06:26 PM -
I can't figure this out
By silvia in forum New To JavaReplies: 3Last Post: 07-20-2007, 04:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks