Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Many questions

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Many questions

    I have been working on this problem and I have some questions. I have to create a program to calculate values seen on employee pay cheques. I read from a file and write to a file including an output seen in the compiler when I run the program.

    I think I can loop this entire program for each of the four employees. Is that possible?
    I was thinking about creating a for loop to create objects for each class. Is that right?

    i am also unsure about the instance variables in each of the classes, I don't know how to initialize them.

    Are there any other things I should fix?

    this is my error message:
    File: location... \PayCheck.java [line: 20]
    Error: ...PayCheck.java:20: non-static method getGrossTotal() cannot be referenced from a static context
    File: location...\PayCheck.java [line: 21]
    Error:...PayCheck.java:21: non-static method getTotalNet() cannot be referenced from a static context
    File: PayCheck.java [line: 22]
    Error: non-static method getTotalTax() cannot be referenced from a static context

    import java.io.*;
    import java.util.*;
    public class PayCheck
    {
      public static void main (String[] args)
      {
        String names [] = new String [4];
        double payRate [] = new double [4];
        double hours [] = new double [4];
        double grossPays[] = new double[4];
     
        double netPays[] = new double[4];
        double empTaxes [] = new double[4];
        double proTax [] = new double [4];
        double feTax [] = new double [4];
     
        readData(names, payRate, hours);
     
        // outside main?
        double grossT = TotalPay.getGrossTotal();
        double netT = TotalNet.getTotalNet();
        double taxT = TotalTax.getTotalTax();
     
        //output, print info for each employee
        System.out.println("Employee Name" + "\t" + "Hours Worked" + "\t" + "PayRate" + "\t" + "GrossPay" + "\t" + "Federal Tax Deduct." + "\t" + "Provincial Tax Deduct." + "\t" + "Total Taxes" + "/t" + "NetPay" + "/t" + "Total GrossPay" + "/t" + "Total Tax Deduct." + "\t" + "Total NetPay");
        for (int i = 0; i < 11; i++) //11 columns of info
        {
          for (int x = 0; x< 4; x++)// 4 employees
          {
            System.out.println(names [x] + "\t" + hours [x] + "\t" + payRate [x] + "\t" + grossPays[x] + "\t" + feTax [x] + "\t" + proTax [x] + "\t" + empTaxes[x] + "\t" + netPays [x] + "\t" + grossT + "\t" + taxT + "\t" + netT);
          } 
        }
      }//end main
     
      public static void readData(String names [], double payRate [], double hours [])
      {
        File fileRead = new File ("employees.txt");//read from text file
        Scanner inFile = new Scanner (fileRead);
        while (inFile.hasNextLine())
        {
          for (int i = 0; i < names.length; i++)
          {
            names [i] = inFile.nextLine();
          }
          for (int j = 0; j< payRate.length; j++)
          {
            payRate [j] = Double.parseDouble(inFile.nextLine());
          }
          for (int k = 0; k< hours.length; k++)
          {
            hours [k] = Double.parseDouble(inFile.nextLine());
          }
        }
      }//end read method
     
      public static void writeNet(double net[], String names [], double netT)
      {
        PrintWriter out = new PrintWriter (new File("Netpays.txt"));
        out.write("Netpays");
        for (int x =0; x< net.length; x++)
        {
          out.write(names [x] + " " + net[x]);//write each employee's net pay
        }
        out.write("Total NetPay: " + netT);
        out.close();
      }//end write method
     
    }//end first class
     
    class GrossPay
    {
      //how to initialize these (as they are, ignoring the constructor)
      private double hours [];
      private double payRate [];
      private double grossD;
      private double grossPays [];
     
      GrossPay (//double h [], double p [], double gross [], double grossM)
      )
      {
        //hours = h;
        //payRate = p;
        //grossPays = gross;
        //grossD = grossM;
        gross (hours, payRate, grossPays);
      }
     
      private void gross (double hours [], double payRate [], double grossPays [])
      {  
        for (int x = 0; x <grossPays.length; x++)
        {
          grossD = hours [x] * payRate [x];
          Math.round(grossD);
          grossPays [x] = grossD;
        }
      }
     
      double getGross ()
      {
        for (int g = 0; g< grossPays.length; g++)
        {
          return grossPays[g];
        }
      }
    }//end gross class
     
    class EmployeeTax //individual employee
    {
      private double fedTax = 0.17;
      private double provTax = 0.14;
      private double grossPays [];
      private double empTaxes[];
      private double proTax[];
      private double feTax[];
      private double total = 0.0;
     
      EmployeeTax ()
      {
        tax (grossPays, empTaxes, proTax, feTax, fedTax, provTax);
      }
     
      private void tax (double grossPays [], double empTaxes[], double [] proTax, double [] feTax, double fedTax, double provTax)
      {
     
        for (int y = 0; y <4; y++)
        {
          double pTax = grossPays[y] * provTax;
          double fTax = grossPays[y] * fedTax;
          double taxDeduct = pTax + fTax;
          empTaxes [y] = taxDeduct; //total
          proTax[y] = pTax; //provincial tax
          feTax[y] = fTax; //federal tax
        }
      }
     
      double getTax()
      {
        for (int j = 0; j < 4; j++)
        {
          return empTaxes [j];
          return proTax [j];
          return feTax [j];
        }
      }
     
    }//end tax class
     
     
    class EmployeeNet
    {
      private double grossPays[]; 
      private double empTaxes[];
      private double netPays[];
     
      EmployeeNet ()
      {
        net(grossPays, empTaxes, netPays);
      }
     
      private void net(double grossPays [], double empTaxes [], double netPays [])
      {
        for (int z = 0; z < 4; z++)
        {
          netPays [z] = grossPays[z] - empTaxes[z];
        }
      }
     
      double getNet()
      {
        for (int k =0; k<4; k++)
        {
          return netPays[k];
        }
      }
    }//end net
     
    class TotalPay//calculate total gross of all employees
    {
      private double grossPays[];
      private double grossT = 0.0;
     
      TotalPay()
      {
        grossTotal(grossPays);
      }
     
      private void grossTotal(double grossPays [])
      {
        for (int i = 1; i < 5; i++)
        {
          grossT+= grossPays[i];
        }
      }
      double getGrossTotal()
      {
        return grossT;
      }
    }//end total gross
     
    class TotalTax
    {
      private double empTaxes[];
      private double totalTax = 0.0;
     
      TotalTax ()
      {
        taxTotal(empTaxes);
      }
     
      private void taxTotal(double [] empTaxes)
      {
        for (int e = 1; e < 5; e++)
        {
          totalTax += empTaxes [e];
        }
      }
      double getTotalTax()
      {
        return totalTax;
      }
     
    }//end total tax
     
    class TotalNet
    {
      private double netPays[];
      private double totalNet = 0.0;
     
      TotalNet ()
      {
        netTotal(netPays);
      }
     
      private void netTotal( double [] empNet)
      {
        for (int j = 1; j < 5; j++)
        {
          totalNet += netPays[j];
        }
      }
     
      double getTotalNet ()
      {
        return totalNet;
      }
     
    }


    Here is the text file I read from, first the names, then the pay per hour and hours they work.

    Jim
    Tom
    Gary
    Phil
    10.0
    15.0
    20.0
    17.50
    40.0
    30.0
    35.0
    40.0

    Thank you for any help you can give me.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Many questions

    Set them to zero if numbers or null if Objects and then reset them to the values read in later.

    Also, you're problem also wasn't initializing. Your methods you cal with the class name have to be static methods. If you make those three methods static, that error should go away.
    Last edited by javapenguin; January 17th, 2011 at 10:14 PM.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Many questions

    Also, your array grossPay should be changed to

    private double[] grossPay I think.
    Last edited by javapenguin; January 17th, 2011 at 10:22 PM.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Many questions

     double getGross ()
      {
        for (int g = 0; g< grossPays.length; g++)
        {
          return grossPays[g];
        }
      }

    This is not a good idea. This will return grossPays[0] then exit the method.

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Many questions

    Thanks for your help.
    Instead, I think I should just have the array returned when I create an object.
    So as long as I make my methods static, looping through the whole program is no issue?

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Many questions

      private double fedTax = 0.17;
      private double provTax = 0.14;
      private double grossPays [];
      private double empTaxes[];
      private double proTax[];
      private double feTax[];
      private double total = 0.0;
     
      EmployeeTax ()
      {
        tax (grossPays, empTaxes, proTax, feTax, fedTax, provTax);
      }

    Here you'll have to initialize those array if you're gonna pass them as parameters to that method in your constructor.

    Typically stuff like
    private double fedTax = 0.17;
    private double provTax = 0.14;
    that's known and won't change are best left as static final named constants.
    You could pass your arrays as parameters to the constructor to deal with the initialization problem.
     
      EmployeeTax (double[] grossPays, double[] empTaxes, double[] proTax, double[] feTax )
    {
    this.grossPays = grossPays;
    this.empTaxes = empTaxes;
    this.proTax = proTax;
    this.feTax = feTax;
     tax (grossPays, empTaxes, proTax, feTax, fedTax, provTax);
    }

    Make sure to change the [] to go after the variable type instead of after the variable name.


     double getTax()
      {
        for (int j = 0; j < 4; j++)
        {
          return empTaxes [j];
          return proTax [j];
          return feTax [j];
        }
      }

    A method can only return at most 1 thing. None if it's a void method.

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Many questions

    Thanks again.
    I had things in my constructors before, but I was getting error messages that I tried to fix while working on this. Guess I overlooked that.

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Many questions

    Quote Originally Posted by SharpT View Post
    Thanks for your help.
    Instead, I think I should just have the array returned when I create an object.
    So as long as I make my methods static, looping through the whole program is no issue?
    Not necessarily always. What the compiler didn't like was calling a non-static method in a static way.

    For instance

    System.out.prinltn() calls the static variable out of the System class which calls the method println.

    Another example is Math.pow(2,3) Which return to 2^3 = 8.

    However

    AClass variable = new AClass();

    (or

    AClass variable = someVariable.methodThatReturnsAnAClass()



    and then


    aClass.aMethod(); is a non-static way of calling a method.

  9. The Following User Says Thank You to javapenguin For This Useful Post:

    SharpT (January 17th, 2011)

  10. #9
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Many questions

    Your explanation was great. I understand that now.

  11. #10
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Many questions

    I have been fixing this code a bit, perhaps this is more correct? I have not fixed the output completely, but I will do that soon.

    I currently get one error, incompatible types in the getGross method. I left the other accessor methods as is, but I get no error message for them. Can anyone explain why this is happening, and how to return the arrays correctly?

    I've been looking for information to help me with this, but if this code is not correct, could anyone please tell me about things I should consider?

    I was also wondering if I should put my write method in my main class.

    import java.io.*;
    import java.util.*;
    public class PayCheck
    {
      public static void main (String[] args)
      {
        //outputs
      }
    }
     
    class ReadData
    {
    //input from different file
      private String names [];
      private double payRate [];
      private double hours [];
     
      ReadData(String[] names, double [] payRate, double [] hours) throws IOException
      {
        names = new String [4];
        payRate = new double [4];
        hours = new double [4];
        readFile(names, payRate, hours);
      }
     
      public static void readFile(String names [], double payRate [], double hours []) throws IOException
      {
        File nameRead = new File ("names.txt");//read from text file
        Scanner inName = new Scanner (nameRead);
     
        File hourRead = new File ("hours.txt");
        Scanner inHours = new Scanner (hourRead);
     
        File payRead = new File ("payrates.txt");
        Scanner inPays = new Scanner (payRead);
     
        while (inName.hasNextLine())
        {
          for (int i = 0; i < names.length; i++)
          {
            names [i] = inName.nextLine();
          }
        }
        while (inPays.hasNextLine())
        {
          for (int j = 0; j< payRate.length; j++)
          {
            payRate [j] = Double.parseDouble(inPays.nextLine());
          }
        }
        while (inHours.hasNextLine())
        {
          for (int k = 0; k< hours.length; k++)
          {
            hours [k] = Double.parseDouble(inHours.nextLine());
          }
        }
      }
    }//end class
     
    //put this in main?
    class WriteNet
    {
      private double netPays[];
      private double netTotal = 0.0;
      private String names[];
      WriteNet(double [] netPays, String [] names, double netT) throws IOException
      {
        this.netPays = netPays;
        this.names = names;
        netTotal = netT;
        calcTotal(netPays);
        writeFile(netPays, names, netTotal);
      }
     
      private double calcTotal(double netPays[])
      {
        for (int c = 0; c< netPays.length; c++)
        {
          netTotal += netPays[c];
        }
        return netTotal;
      }
      public static void writeFile(double netPays[], String names [], double netTotal) throws IOException
      {
        PrintWriter out = new PrintWriter (new FileWriter("Netpays.txt"));
        out.println("Netpays");
        for (int x =0; x< netPays.length; x++)
        {
          out.println(names [x] + " " + netPays[x]);//write each employee's net pay
        }
        out.println("Total NetPay: " + netTotal);
        out.close();
      }//end write method
     
    }//end class
     
    class GrossPay
    {
      private double hours [];
      private double payRate [];
      private double grossPays [];
     
      GrossPay (double [] hours, double [] payRate, double [] grossPays)
      {
        this.hours = hours;
        this.payRate = payRate;
        grossPays = new double [4];
        gross (hours, payRate, grossPays);
      }
     
      private void gross (double hours [], double payRate [], double grossPays [])
      {  
        for (int x = 0; x <grossPays.length; x++)
        {
          grossPays[x] = hours [x] * payRate [x];
          Math.round(grossPays[x]);
        }
      }
     
      double getGross ()
      {
        //how to return?
        return grossPays;
      }
    }//end gross class
     
    class EmployeeTax //individual employee
    {
      private double fedTax = 0.17;
      private double provTax = 0.14;
      private double grossPays [];
      private double empTaxes[];
      private double proTax[];
      private double feTax[];
     
      EmployeeTax (double fTax, double pTax, double [] grossPays, double [] empTaxes, double [] proTax, double []feTax)
      {
        fedTax = fTax;
        provTax = pTax;
        this.grossPays = grossPays;
        this.empTaxes = empTaxes;
        this.proTax = proTax;
        this.feTax = feTax;
        tax (grossPays, empTaxes, proTax, feTax, fedTax, provTax);
      }
     
      private void tax (double grossPays [], double empTaxes[], double [] proTax, double [] feTax, double fedTax, double provTax)
      {
        for (int y = 0; y < empTaxes.length; y++)
        {
          double pTax = grossPays[y] * provTax;
          double fTax = grossPays[y] * fedTax;
          double taxDeduct = pTax + fTax;
          empTaxes [y] = Math.round(taxDeduct); //total
          proTax[y] = Math.round(pTax); //provincial tax
          feTax[y] = Math.round(fTax); //federal tax
        }
      }
     
      double getTax()
      {
        return;
      }
     
    }//end tax class
     
     
    class EmployeeNet
    {
      private double grossPays[]; 
      private double empTaxes[];
      private double netPays[];
     
      EmployeeNet (double [] grossPays, double [] empTaxes, double [] netPays)
      {
        this.grossPays = grossPays;
        this.empTaxes = empTaxes;
        this.netPays = netPays;
        net(grossPays, empTaxes, netPays);
      }
     
      private void net(double grossPays [], double empTaxes [], double netPays [])
      {
        for (int z = 0; z < 4; z++)
        {
          netPays [z] = grossPays[z] - empTaxes[z];
        }
      }
     
      double getNet()
      {
        //
      }
    }//end net

  12. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Many questions

    Actually, the point of passing something in a parameter is so that the class itself doesn't have to define and initialize the array, usually. Usually the main method is where you define it all and pass it as parameters.

    I'm not sure what you're trying to return for getGross(). A total gross for all Workers, a gross for a certain Worker, something else... All you have to do is set that value to something. As you've never set it whatever it is, you can't return it.
    GrossPay (double [] hours, double [] payRate, double [] grossPays)
      {
        this.hours = hours;
        this.payRate = payRate;
       this.grossPays = grossPays;
        gross (hours, payRate);
      }
     
    // you already have defined grossPays in constructor.  
      private void gross (double hours [], double payRate [])
      {  
        for (int x = 0; x <grossPays.length; x++)
        {
          grossPays[x] = hours [x] * payRate [x]; // hopefully hours and payRate have enough indexes, or preferably the same
     // other wise you'll get a null pointer exception.  You define all this in the main method.  
          Math.round(grossPays[x]); // what are you doing with this value.  It's going nowhere.  
        }
    // how about this
    this.grossPays = grossPays;
      }
     
      double[] getGross ()
      {
        //how to return?
    // will return an entire array.  
        return grossPays;
      }
     
    // in case you'd like the gross of a single Worker or whatever
     
    public double getGrossAt(int index)
    {
    if (index < 0 || index >= grossPays.length)
    {
    System.out.println("This cannot be done.");
    return null;
    }
     
    return (grossPays[index]);
    }
    // Also, it just occurred to me that you could possibly initialize an array in a class, but this usually would be done if you were
    // storing some value that you didn't read in or get from the user and that was intended to be constant.

  13. #12
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Many questions

    Quote Originally Posted by javapenguin View Post
    Actually, the point of passing something in a parameter is so that the class itself doesn't have to define and initialize the array, usually. Usually the main method is where you define it all and pass it as parameters.

    I'm not sure what you're trying to return for getGross(). A total gross for all Workers, a gross for a certain Worker, something else... All you have to do is set that value to something. As you've never set it whatever it is, you can't return it.
    GrossPay (double [] hours, double [] payRate, double [] grossPays)
      {
        this.hours = hours;
        this.payRate = payRate;
       this.grossPays = grossPays;
        gross (hours, payRate);
      }
     
    // you already have defined grossPays in constructor.  
      private void gross (double hours [], double payRate [])
      {  
        for (int x = 0; x <grossPays.length; x++)
        {
          grossPays[x] = hours [x] * payRate [x]; // hopefully hours and payRate have enough indexes, or preferably the same
     // other wise you'll get a null pointer exception.  You define all this in the main method.  
          Math.round(grossPays[x]); // what are you doing with this value.  It's going nowhere.  
        }
    // how about this
    this.grossPays = grossPays;
      }
     
      double[] getGross ()
      {
        //how to return?
    // will return an entire array.  
        return grossPays;
      }
     
    // in case you'd like the gross of a single Worker or whatever
     
    public double getGrossAt(int index)
    {
    if (index < 0 || index >= grossPays.length)
    {
    System.out.println("This cannot be done.");
    return null;
    }
     
    return (grossPays[index]);
    }
    // Also, it just occurred to me that you could possibly initialize an array in a class, but this usually would be done if you were
    // storing some value that you didn't read in or get from the user and that was intended to be constant.
    Thank you again for your help.
    For getGross, I was trying to return the entire array of GrossPays that contains the grosses of all the workers. And yes, the pay per hour and hour arrays have the same index. I was attempting to populate the GrossPay array while calculating. I guess that I will round the grosses when I am outputting.

    I knew there was something I was forgetting with the accessor method, thank you.

Similar Threads

  1. Inheritance questions....
    By smellyhole85 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 26th, 2010, 06:11 PM
  2. A few questions
    By adenverd in forum Java Theory & Questions
    Replies: 3
    Last Post: May 26th, 2010, 03:34 AM
  3. [SOLVED] Some serious questions,
    By Time in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 17th, 2010, 02:52 AM
  4. [SOLVED] Questions About Threads
    By neo_2010 in forum Threads
    Replies: 4
    Last Post: March 15th, 2010, 09:04 AM
  5. Some basic questions.
    By trips in forum Java Theory & Questions
    Replies: 5
    Last Post: July 21st, 2009, 02:15 AM