Results 1 to 9 of 9

Thread: Questions

  1. #1
    Csharks is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default 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

    Java Code:
    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
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    The message tells you what is wrong. The code is in a static method (belongs to a class and not an instance of the class) and is calling a non-static method (belongs to an instance of the class and not the class itself). So how does it know which method to call? For example if you have 5 Strings declared and your code called
    Java Code:
    System.out.println(charAt(0));
    All 5 Strings have a charAt method so which method is it supposed to call?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    By the way, have you learnt about classes yet? Parallel arrays are a nightmare to maintain. All your information would be better if it was stored in an Employee class.

  4. #4
    Csharks is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    I don't know a lot about classes, but I guess you are right. I have to use multiple classes though. I'm working on fixing this now.
    Thanks for your definitions.

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by Csharks View Post
    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
    All the three methods, which are in error, are non-static methods that you are trying to access from your main method.

    When you are accessing your methods using the class name instead of creating a class object, then you need to make sure that those methods are static, which is not the case here.

    I hope, now you know where you need to make the change.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #6
    Csharks is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by goldest View Post
    All the three methods, which are in error, are non-static methods that you are trying to access from your main method.

    When you are accessing your methods using the class name instead of creating a class object, then you need to make sure that those methods are static, which is not the case here.

    I hope, now you know where you need to make the change.

    Goldest
    Thanks for your explanation.
    So by creating class objects, I can loop through the whole program?

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by Csharks View Post
    So by creating class objects, I can loop through the whole program?
    You can create a class object and call your non-static methods.

    OR

    You can declare those methods as static if you want to call them by the class name itself.

    Either can be done, but that depends on your requirements.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    Csharks is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    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.

    Java Code:
    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

  9. #9
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    When you define your accessor methods, you are supposed to "get" something. Which means that your method is supposed to "return" something. Which means that your methods "return type" should be mentioned in the method signature.

    In your case, your method getGross is returning a array and so, it should declare the return type as arrays, like,

    Java Code:
    double[] getGross () { }

    For more details about defining methods, refer : Methods Declaration

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Three questions
    By an24 in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 09-15-2010, 08:36 AM
  2. Some Questions
    By MuslimCoder in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 04:01 PM
  3. I have Questions -_-
    By ChazZeromus in forum New To Java
    Replies: 5
    Last Post: 09-13-2008, 08:08 PM
  4. Questions About JSP?
    By mtz1406 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-19-2008, 07:56 PM
  5. Just a Few Questions
    By pringle in forum New To Java
    Replies: 21
    Last Post: 01-09-2008, 06:21 PM

Posting Permissions

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