CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2018
    Posts
    2

    Unhappy Gross Pay/Net Pay Calculator

    Hello, I'm fairly new to programming in java, and over the course of the months in learning and doing projects on it, have gotten used to a pattern of what every newbie programmer goes through: being overwhelmed doing a project I have no idea on what's it's about, over-exhausting resources in looking for the answer in a commonly overlooked mistake, thinking everything is going right while actually writing code, then compiling the code and hitting a roadblock in one place where everything else is fine. That's how it feels again with this program, that I'm struggling to complete for class.

    Basically, I'm trying to make a gross pay calculator program that determines how much an employee is paid based on how many hours they work and what they're being paid already. Then it takes the gross pay and uses that to calculate the tax pay. The federal, state, local, and fica taxes all have set values and the gross pay is multiplied by each of them to get tax pay values for each. Then all of the tax pay earnings are displayed to the first 2 decimal points. Each calculation is done in its own method and the result is returned for another method to take in.

    My main concern is using different methods and another class to put the separate tax variables in and getting the values to other classes. We're not allowed to use arrays, or getter and setters for methods, and I don't think the professor whats us to use one method for each calculation either. Making a new instance in a class to use its methods may not be allowed either. The tax values with set values all are suggested to be in its own class so their values aren't changed. I know that you can't return no more than one value per method, but I need each calculated tax pay value after being multiplied by the gross pay to be returned to the display method, and it doesn't seem efficient to send each variable to the display method one one-by-one.


    Here's my code so far:

    Code:
    import java.util.Scanner;
    /**
     * Write a description of class GrossToNet here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class GrossToNet
    {
        public static void main()
        {
            Scanner scan = new Scanner(System.in);
            while (true) {  
                System.out.print("Please enter number of hours worked. 0 to quit: ");
                double hoursWorked = scan.nextDouble();
    
                if (hoursWorked == 0)  {
                    break; 
                }
    
                System.out.print("Please enter the payrate. 0 to quit: ");
                double payRate = scan.nextDouble();
    
                if (payRate == 0) {
                    break;
                }
    
                double grossPay = payCalc(hoursWorked, payRate); //send 'hoursworked' and 'payrate' variables to paycalc method
    
                displayTaxes(grossPay); //sends grossPay to the display method for it to be displayed there in text
            }
            System.out.println("Gross Pay Calculator Program Ended");
        }
    
        public static double payCalc(double hoursIn, double payIn)
        {
            double gPay = 0; 
            
            if (hoursIn <= 40)
            {
                gPay = (payIn * hoursIn);
            } 
    
            else
            {
                gPay = (40 * payIn) + ((hoursIn - 40) * (payIn * 1.5));
            }
           
            
            return gPay;
        }
    
        public static void displayTaxes(double grossIn)
    
        {
            System.out.printf("Your gross pay is $%.2f\n", grossIn);
            System.out.print("\n");
    
        }
    }
    
    class TaxVariables //the tax varaibles are being put in another class in order to be referenced (for the usage of other methods)
    //the values shouldn't change when the objects are being referenced to in each method
    {
    
        private static final double FEDERAL = .16, STATE = .06, LOCAL = 3.5, FICA = 4.75;
    
        public static void taxCalc (double grossTaxPay)
        {
            double fedTax = (grossTaxPay * FEDERAL);
            double stateTax = (grossTaxPay * STATE); 
            double locTax = (grossTaxPay * LOCAL); 
            double ficaTax = (grossTaxPay * FICA);
        } //needs to return a value- bring in grossPay from payCalc method into taxCalc method in TaxVariables class
    
    }
    Here's an excerpt of the directions for the assignment :

    Purpose:

    To demonstrate your ability to design and code a project with many constants, variables and calculations with the major work being done in methods. Also to show that you understand how to pass values and references to methods so that a method may "return" multiple results.

    And a hint the instructor gave:

    Hint: declare a class that contains all the pay variables so that they may be passed to the various methods by reference.

    I first need to send grossPay to the TaxVaraiables class in the taxCalc method, and the calculated values of the taxes from the TaxVariables class to the GrossToNet class in the display method in order for this project to work.

    That's whats making me stuck here, can anyone help?

  2. #2
    Join Date
    Feb 2017
    Posts
    677

    Re: Gross Pay/Net Pay Calculator

    Quote Originally Posted by BevyDev View Post
    I know that you can't return no more than one value per method
    You can't but you can return an object (an instance of a class) containing several variables, both primitives and objects.

    So check out classes (like the instructor hinted).

    Also you're declaring everything static. That's not good Java.

    And a hint the instructor gave:

    Hint: declare a class that contains all the pay variables so that they may be passed to the various methods by reference.
    It's important to use the correct terminology. Nothing is passed by reference in Java. Everything is passed by value, both primitives and object references.
    Last edited by wolle; April 29th, 2018 at 11:46 PM.

  3. #3
    Join Date
    Apr 2018
    Posts
    2

    Re: Gross Pay/Net Pay Calculator

    ..? I thought that using a static modifier is what allowed methods to call other methods, also it allowed variables to be passed to them too.

    The main method is static, and removing static from either the main method or the method its passing values to gives an error that says that non-static methods can't pass anything to static methods.

    Also, I updated the code a bit, to accommodate there not being any other pass the return more than one value per method, and I couldn't find any other way to call a class:

    Code:
    import java.util.Scanner;
    /**
     * Write a description of class GrossToNet here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class GrossToNet
    {
        public static void main()
        {
            Scanner scan = new Scanner(System.in);
            while (true) {  
                System.out.print("Please enter number of hours worked. 0 to quit: ");
                double hoursWorked = scan.nextDouble();
    
                if (hoursWorked == 0)  {
                    break; 
                }
    
                System.out.print("Please enter the payrate. 0 to quit: ");
                double payRate = scan.nextDouble();
    
                if (payRate == 0) {
                    break;
                }
    
                double grossIn = payCalc(hoursWorked, payRate); //send 'hoursworked' and 'payrate' variables to paycalc method
    
                TaxVariables passTax = new TaxVariables();
                passTax.taxCalc(grossIn);
                
            }
            System.out.println("Gross Pay Calculator Program Ended");
        }
    
        public static double payCalc(double hoursIn, double payIn)
        {
            double gPay = 0; 
            
            if (hoursIn <= 40)
            {
                gPay = (payIn * hoursIn);
            } 
    
            else
            {
                gPay = (40 * payIn) + ((hoursIn - 40) * (payIn * 1.5));
            }
           
            return gPay;
        }
    
        public static double netGrossPay(double grossIn, double taxIn)
        {
            double netTaxPay = (grossIn - taxIn);
            return netTaxPay;
        }
    
        public static void netCalc(double fedPay, double statePay, double locPay, double ficaPay, double grossPay)
    
        { 
            double taxAdd = (fedPay + statePay + locPay + ficaPay);
            
            double netTaxIn = netGrossPay(grossPay, taxAdd);
            // System.out.printf("Your gross pay is $%.2f\n", grossPay);
            // System.out.print("\n");
    
        }
    }
    
    class TaxVariables //the tax varaibles are being put in another class in order to be referenced (for the usage of other methods)
    //the values shouldn't change when the objects are being referenced to in each method
    {
    
        private static final double FEDERAL = .16, STATE = .06, LOCAL = 3.5, FICA = 4.75;
    
        public static void taxCalc(double grossTaxPay) // method is void because the values aren't being returned, they will be called by passing to GrossToNet class
        {
            double fedTax = (grossTaxPay * FEDERAL);
            double stateTax = (grossTaxPay * STATE); 
            double locTax = (grossTaxPay * LOCAL); 
            double ficaTax = (grossTaxPay * FICA);
            
            GrossToNet netTax = new GrossToNet();
            netTax.netCalc(fedTax, stateTax, locTax, ficaTax, grossTaxPay);
        }
    
    }
    My new problem is invoking the last method, which would be the display method, to display all of the tax values , including gross pay and net pay. The display method has to be invoked by the main method in order to run, but I have no idea how to call it into main(), without passing parameters. The display method is going to be passed the tax variables by the netCalc() method, which means display is going to take in 5 values. But I just want to run the display method from the main, but I can't just put the method name in the main without parameters in order to run it. It'll say an error on how the method trying to be called takes in 5 values, but the method call itself has no variables in the parameters thats being passed. Thing is, I just want to make that method run by invoking it in the main method, there are no variables in main() I need to pass to the display method. But without passing 5 variables from main, because it takes in 5 variables originally from netCalc(), the main() won't call the display method and the method won't run. I'm really stuck on that part and have no idea on what else to do.

  4. #4
    Join Date
    Feb 2017
    Posts
    677

    Re: Gross Pay/Net Pay Calculator

    Quote Originally Posted by BevyDev View Post
    ..? I thought that using a static modifier is what allowed methods to call other methods, also it allowed variables to be passed to them too.

    The main method is static, and removing static from either the main method or the method its passing values to gives an error that says that non-static methods can't pass anything to static methods.
    The main method is declared static for a reason but the application code you write yourself should have very few static methods, if any at all, at least while you're a newbie.

    Has the instructor really never mentioned how central the class concept is to Java programming and how you go about using classes in practice? It's absolutely essential. If you write a Java program that consists of static methods only you're adhering to the procedural style of BASIC, C and Fortran. You can do it but it's not proper Java.

    You don't call a class, you instantiate it using the new keyword giving you an object of the class. You then call methods of the object to do things for you.

    The first thing you do in a Java application is to get out of the static environment of main. It's simple, you just instantiate a class and call a method in it. In principle that method contains what you currently do in main. The static methods you have introduced so far should go into appropriate classes where they shouldn't be static. To call a method you get hold of an object of the class where the method is defined and call it.

    Note that a method can only return one thing but that thing may be an object of a class which can hold many things. It's seldom you introduce a class for the sole purpose of returning stuff from a method but it's not unheard of.

    Program development in Java is based on the introduction of classes. Classes use primitives, other classes you introduce and classes you find in the standard library. To figure out what classes are needed and how they should interact is what constitutes program design with Java. In Java the class is the basic unit of program development.

    It's too tedious to teach Java at a forum so I suggest you locate a beginner's tutorial to get started with classes before continuing with your assignment.

    Good luck!
    Last edited by wolle; May 1st, 2018 at 01:03 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured