PLEASE help me create a Method in Java Program (newbie!)

Badges: 2
Report Thread starter 1 year ago
This is my first time Creating Methods in Java. I need to create THREE methods for this project. Help with ONE would be amazing for me to then reference from and create the other two, if you do not want to help with all three. I will post all 3 for you to choose and the code I have thus far. THANK YOU!!!

Modification One: Change the tax-calculation statement to a method call statement. 1. Currently the program calculates the taxpayer's tax by simply multiplying the gross pay times the tax rate. You need to replace this tax calculation statement with a statement that calls the method calculate Tax and passes the appropriate parameters to the method. You must pass three parameters to the calculate Tax method: (1) gross pay (type double), (2) tax rate (type double), and (3) type of taxpayer (type char).

Write the calculate Tax METHOD named calculate Tax. Place this method definition code below the main method of the existing program class. Include the modifier static in the method header.

Modification Two: Write the calculateNetPay method named calculate NetPay. Place this method definition code below the main method of the existing program class. Include the modifier static in the method header

The calculateNetPay method must: Take two parameters as input: (1) gross pay (type double), and (2) tax (type double). Calculate net pay by simply subtracting the tax amount from the gross pay. Return a result of type double, namely the calculated net pay for the pay period.

Modification Three: Write the getTaxpayer TypeName method. The getTaxpayer TypeName method must: Take one parameter as input, which is the taxpayer type as a value of type char. Determine the full name of the taxpayer type, either Weekly, Biweekly, or Monthly Return the full name of the taxpayer type as a string.

My Code for Reference:

//make sure have correct package modify next line
package lab03;

import javax.swing.*;

public class BasicMethodsLab {

public static void main( String args[] ) {

String grossPayStr; // Value entered by user; string type
String taxRateStr; // Value entered by user; string type
double grossPay; // The double form of the user-entered gross pay
double taxRate = 0; // The double form of the user-entered tax rate
double tax = 0; // The tax for the pay period
int resp = 0; // The user's response as to whether to continue

// Initialize the output string to the empty string
String outputStr = "";

while (true) {
// Read in gross pay from user as a string
grossPayStr = JOptionPane.showInputDialog("Ent er gross pay for the pay period");

// Convert from type String to type double
grossPay = Double.parseDouble(grossPayStr);

// Read in tax rate from user as a string
taxRateStr = JOptionPane.showInputDialog("Ent er tax rate as a percent");

// Convert from type String to type double
taxRate = Double.parseDouble(taxRateStr);

// Echo print the entered values
outputStr = outputStr + "Gross pay: " + grossPay + ", ";
outputStr = outputStr + "Tax rate: " + taxRate + ", ";

// Calculate tax for the pay period
tax = grossPay * (taxRate / 100.0);

// Print the tax
outputStr = outputStr + ", " + "Tax: " + tax + "\n";

// Determine whether the user wants to continue the loop
resp = JOptionPane.showConfirmDialog(nu ll, outputStr + "\nContinue?", "Confirm",

if (resp == JOptionPane.NO_OPTION)

// Display final output to the user
null, outputStr, "Final Results", JOptionPane.INFORMATION_MESSAGE) ;

Badges: 19
Report 1 year ago
Firstly, a method is just a block of code with a name. (Similar to the way that a variable is just an area of memory with a name). Your program already has one method in it called public static void main(), although 'main' is a special case in Java because you don't need a method call statement -- Java automatically does that for you because main is the starting-point. You can (and should) write your own methods that are purely for use within your own program.

Doing this is really useful because it helps you divide your program into smaller pieces, which helps you split the code up into logically-sensible pieces, and can also help avoid needing to write copy+paste code as well, so methods are really useful and important, probably as soon as you find yourself writing programs which are more than about 50 lines long you will find that the code gets really messy without having it split into methods.

A 'method call statement' is just taking the name of a method (either one that you have written, or one which already exists inside Java's imports) and putting rounded-brackets after it. e.g. if you created a method called calculateTax then your method call statement would be calculateTax();. Again, you're already using a method call statement on the line where you've used JOptionPane.showConfirmDialog( ... );. showConfirmDialog is a method which belongs to javax.swing.OptionPane.

(If you were to dig into the guts of how Java works, you would find a line of code somewhere which does something similar with your program's static void main(string[] args) { } method)

Methods in Java can receive 'inputs' (i.e. variables in memory which are copied into the function) -- again, your static void main(string[] args) method is already doing this as well for one input, the variable called "args" is a list(array) of command-line arguments which Java provides your program as an input, so when "main" begins to run the main function has got a variable called args that represents some chunk of memory provided to it when the method was called. (i.e. when you run the program in the console, Java scoops up anything which you type after the name of the program and puts it into that into a list in memory - your main method has a variable called args which is a name for that bit of memory).

Methods in Java can also have an output. static void main(string[] args) does not have an output, because it uses the void keyword. For a method to have an output, it needs to replace the void keyword with the data type of whatever the result is - e.g. double, String, int, etc. (

An method's output is basically a result which is "sent back" when the function ends. (Java's keyword to end a function and send a result back is called return.). The part of your program which calls that method can pick up the output just using simple assignment -- again, this is something you are already doing with resp = JOptionPane.showConfirmDialog( ... );

So if you put those 3 things together, you can treat a method as a way of writing a small, self-contained bit of logic which
  • Has some kind of input (e.g. tax and tax rate)
  • Has some kind of calculation which uses that input
  • sends the result back to the bit of code which "calls" the method.

Before you can use a method call statement, you need to write the code for the method. The basic structure for that is much the same same as public static void main, although if you want an output/result, then you need to substitute "void" for whichever data type you want for your result, and you need to specify inputs (known as arguments or parameters) .

See these:

That's pretty much all there is to methods. the code you write inside your own method is no different to the cod eyou write in main, the only difference is that the variables which exist inside main() will not be visible/accessible inside your own "handwritten" methods, which is why you need to use inputs (arguments/parameters) and outputs (return statements).

here's a quick example:
class Main {
    public static void main( String args[] ) {
        // These variables will be inputs for the method call statement
        int totalChocolates = 10;
        int wantToEat = 12;
        System.out.printf("I have %d total chocolates and want to eat %d.\n", totalChocolates, wantToEat);

        // Method call statement stores the result in a variable
        int howManyChocolatesEaten = getNumberOfChocolatesEaten(totalChocolates, wantToEat);

        // Use the method result
        totalChocolates = totalChocolates - howManyChocolatesEaten;
        System.out.printf("I have eaten %d chocolates, there are %d left in the bag",
                howManyChocolatesEaten, totalChocolates);

    // This is a method which belongs to the program and is used above in 'main'
    // There are two parameters/arguments, which the method treats as local variables
    static int getNumberOfChocolatesEaten(int totalChocolatesInBag, int howManyToEat) {
        if (totalChocolatesInBag > howManyToEat) {
            // The function could end here and the result will be 'howManyToEat'
            return howManyToEat;
        } else {
            // Otherwise, the function could also end here and the result will be 'totalChocolatesInBag'
            return totalChocolatesInBag;
Last edited by winterscoming; 1 year ago

Quick Reply

Attached files
Write a reply...
new posts
to top
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.


Do you have the space and resources you need to succeed in home learning?

Yes I have everything I need (398)
I don't have everything I need (309)

Watched Threads

View All