简体   繁体   中英

passing user input to methods java

Hey guys i am a complete beginner in java and i am trying to write a program that goes this way: 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:

  • The number of gallons of paint required
  • The hours of labor required
  • The cost of the paint
  • The labor charges
  • The total cost of the paint job

Then it should display the data on the screen this is what i have so far. i can get the user input and the number of gallons but when i try to calculate the number of hours of labor what it does is it calculates the labor from the result of the previous calculation which is not what i want..this is what i have so far

int resultnumberofhours = hoursoflabor(numberofsquarefeet,115,8);
        System.out.println("the number of hours needed are " + resultnumberofhours);
    }
public static int hoursoflabor(int numberofsquarefeet, int squarefeet,int labor){
        int resultnumberofhours = numberofsquarefeet/115*8;
        return resultnumberofhours;
    }

Rather then taking one by one you can try code like this

Scanner dd = new Scanner(System.in);
int[] vars = new int[5];

for(int i = 0; i < vars.length; i++) {
  System.out.println("Enter next var: ");
  vars[i] = dd.nextInt();
}

The only problem that I can see with the code is that you are not doing anything with the results. Try the following:

int result = numberofsquarefeet(numberofsquarefeet, 115, 1);
System.out.println("Number of square feet: " + result);

When returning something from a java function you can either assign that value to a variable of the same type you specify as the return type of the function, or you can use the output in a print statement for example. If you do nothing with the output, that result is lost.

Hope this helps.

You have to either print the result at the end:

       System.out.println("enter number of square feet");
    numberofsquarefeet = Keyboard.nextInt();
    int resultSqFeet = numberofsquarefeet(numberofsquarefeet, 115, 1);
    System.out.println("number of square feet is" + resultSqFeet);
}

or inside the methods:

public static int numberofsquarefeet(int numberofsquarefeet, int squarefeet, int paint) {
    int result = numberofsquarefeet / 115 * 1;
    System.out.println("number of square feet is " + result);
    return result;
}

The same goes with hoursoflabor

I suggest you step back from writing the code and analyze your problem a bit first.

You have a data model and a process which you can describe in more detail. This will help you come up with a good design for your classes and methods. If you immediately start writing code, especially as a beginner, it usually ends up like a pile of spaghetti.

Here is my suggestion for the data model:

  • PaintingCompany
    • PAINT_GALLONS_PER_SQUARE_FOOT = 1 / 115
    • LABOR_HOURS_PER_SQUARE_FOOT = 8 / 115
    • LABOR_DOLLARS_PER_HOUR = 18
  • Room
    • wallSpaceSquareFeet
  • Paint
    • dollarsPerGallon
    • gallons
    • calculatePaintCostInDollars()
  • Project
    • rooms
    • paint
    • calculateGallonsOfPaintRequired()
    • calculateHoursOfLaborRequired()
    • calculateLaborChargesInDollars()
    • calculateTotalCostInDollars()

Note that the PaintingCompany class only contains constants. You could put the main() method in this class if you like.

The type and amount of paint are modeled as a single class 'Paint'. You could argue this is inaccurate and have a separate PaintType (enum) representing known types of paint and their prices but I figured this was feature creep at this point. So with the model above you need to instantiate a Paint instance with the correct price and then set the amount of paint on it. The Paint class will then have a function to calculate the total paint price.

It would be an idea to model the units explicitly. The model is now fixed on dollars and gallons but in future you might wish to switch to euros and liters in order to sell the application in Europe. For now I didn't model units because it probably over complicates the exercise. To avoid confusion it is important that the units are specified clearly as part of the variable or method name Space rockets have been known to crash due to errors of this genre.

The process could look like this:

  1. Create a new Project
  2. Ask the user for the paint price
  3. Set the paint price on the project's Paint object
  4. Start an iteration:
    • Add a new Room object to the Project
    • Ask the user for the wall space of the Room in square feet
    • Ask the user if there is another room
    • If yes, iterate from step 4
  5. If no, print a report with the required project data

Note that some of these process steps are good candidates for separate classes:

  • Iteration could be put in the PaintingCompany class
  • Getting input from the user. It's good to keep this separate so you can easily change it later without affecting other code.
  • Printing the report. Reporting is usually a bit complex, has output formatting etc and is a nice separate concern. You also want to be able to change the report implementation without affecting other code.

If you post new code based on this (or your own) design I am happy to comment on it or help you with any questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM