Results 1 to 9 of 9
  1. #1
    sssss is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default Help with java gui and inheritance

    Hi. I am trying to use inheritance in my payroll program. I want to calculate the CPP amount in a different class, and send it back to the main program. But I keep getting th error:

    1 error found:
    File: E:\Payroll Program\Payrollclear.java [line: 146]
    Error: E:\Payroll Program\Payrollclear.java:146: cannot find symbol
    symbol : class CandaPensionPlanD
    location: class Payrollclear.NetPay

    Here is my code:

    Java Code:
    //CPP class
    public class CandaPensionPlanDeduction {
      public double CandaPensionPlanD(double x, double cppAmount){ 
      if (x >= 1513.10 && x <=1523.09) {
        cppAmount = 59.49;
        
      } else if (x >= 1523.10 && x <=1533.09) {
        cppAmount = 59.92;
        
      } else if (x >= 1533.10 && x <=1543.09 ){
        cppAmount = 60.35;
        
      } return(cppAmount);
    } 
    }
    
    
    
    //Main Program: 
    
    //Gross and Net biweekely salary
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;  
    import java.lang.String; 
    import java.io.*;
    //Net = after
    //gross = before
    public class Payrollclear {
      JFrame frame;
      JPanel contentPane;
      JLabel prompt1, prompt2, prompt3, prompt4, prompt5, stat;
      JTextField name, wageT, hoursT, charityT;
      JButton gross, net, cpp, eI, charityB, save;
      
      public Payrollclear(){
        /* Create and set up the frame */
        frame = new JFrame("Payroll Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        /* Create a content pane with a GridLayout and empty borders */
        contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(0, 2, 10, 5));
        contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        contentPane.setBackground(Color.white);
        
        /* Create and add a prompt and then a text field */
        prompt1 = new JLabel("Enter the employee name: ");
        contentPane.add(prompt1);
        name = new JTextField(10);
        contentPane.add(name);
        
        /* Create and add a prompt and then a text field */
        prompt1 = new JLabel("Enter the hourly wage: ");
        contentPane.add(prompt1);
        
        wageT = new JTextField(10);
        contentPane.add(wageT);
        
        /* Create and add a second prompt and then a text field */
        prompt2 = new JLabel("Enter the hours worked: ");
        contentPane.add(prompt2);
        
        hoursT = new JTextField(10);
        contentPane.add(hoursT);
        
        /* Create and add a third prompt and then a text field */
        prompt3 = new JLabel("Enter the charity percentage: ");
        contentPane.add(prompt3);
        
        charityT = new JTextField(10);
        contentPane.add(charityT);
        
        /* Create and add button that will display the gross pay */
        gross = new JButton("Gross Pay");
        gross.addActionListener(new PayCalculator());
        contentPane.add(gross);
        
        /*Create and add button that will display the net pay*/
        net = new JButton("Net Pay");
        net.addActionListener(new NetPay());
        contentPane.add(net);
        
        /* Create and add button that will display the charity */
        charityB = new JButton("Charity");
        charityB.addActionListener(new Charity());
        contentPane.add(charityB);
        
        
        /* Create and add a label that will display stats(whatever is selected) */
        stat = new JLabel(" ");
        stat.setBorder(BorderFactory.createEmptyBorder(15, 0, 15, 0));
        contentPane.add(stat);
        
        /* Add content pane to frame */
        frame.setContentPane(contentPane);
        /* Size and then display the frame. */
        frame.pack();
        frame.setVisible(true);
      }
      
      
      /*
       * PayCalculator class that calculates the gross pay
       */ 
      class PayCalculator implements ActionListener {
        public void actionPerformed(ActionEvent event) {
          String g1 = name.getText();
          String g2 = wageT.getText();
          String g3 = hoursT.getText();
          String g4 = charityT.getText();
          
          double gross = 0;
     
          gross = (Double.parseDouble(g2) * Double.parseDouble(g3))*2.00;
          stat.setText(" Gross Pay = $" + Double.toString(gross));
          
        }
      }
     
       /* 
       * Charity class that calculates and displays the Charity
       */
    class Charity implements ActionListener {
        public void actionPerformed(ActionEvent event) {
          String g8 = wageT.getText();
          String g9 = hoursT.getText();
          String g10 = charityT.getText();
          
          double charity = 0.0;
          double y = (Double.parseDouble(g8) * Double.parseDouble(g9)) * 2.00;
          charity = (Double.parseDouble(g10)/100) * y;
          stat.setText(" Charity = $" + Double.toString(charity));
        } 
       }
    
    
       /* 
       * NetPay class that calculates and displays the Net Pay
       */
      class NetPay implements ActionListener {
        public void actionPerformed(ActionEvent event) {
          String g5 = wageT.getText();
          String g6 = hoursT.getText();
          String g7 = charityT.getText();
          
          double net = 0.0;
          double cppAmount = 0;
          double charity = 0.0;
          double x = (Double.parseDouble(g5) * Double.parseDouble(g6)) * 2.00;
          
          cppAmount = new CandaPensionPlanD(x, cppAmount);
           
          //Calculate EI 
          double s = x * 2.00;
          double ei = 0.0;
          if (s >=2176.23 && s <=2176.66 ) {
            ei = 48.97;
          }else if (s >= 2167.67&& s <= 2177.11) {
            ei = 48.98;
          }else if (s >= 2177.12&& s <=2177.55 ) {
            ei = 48.99;
          }else if (s >= 2177.56&& s <=2177.99 ) {
            ei = 49.00;
          }else if (s >= 2178.00&& s <=2178.44 ) {
            ei = 49.01;
          }
          charity = (Double.parseDouble(g7)/100) * x;
          net = (x - charity);
          net = (net - cppAmount);
          net = (net - ei);
          stat.setText(" Net Pay = $" + Double.toString(net));
        }
      }
    
       
       
    /**
     * Create and show the GUI.
     */
    private static void runGUI() {
      JFrame.setDefaultLookAndFeelDecorated(true);
      Payrollclear myGrade = new Payrollclear();
    }
    
      
      /*
       * main method of the program
       */ 
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            runGUI();
          }
        });
      }
    }
    Last edited by sssss; 01-18-2011 at 08:32 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    28

    Default

    This class has a pseudo-constructor:

    Java Code:
    public class CandaPensionPlanDeduction {
      public double CandaPensionPlanD(double x, double cppAmount){ 
      if (x >= 1513.10 && x <=1523.09) {
        cppAmount = 59.49;
        
      } else if (x >= 1523.10 && x <=1533.09) {
        cppAmount = 59.92;
        
      } else if (x >= 1533.10 && x <=1543.09 ){
        cppAmount = 60.35;
        
      } return(cppAmount);
    } 
    }
    Remember that a constructor has no return type, not double, not void, but nothing. This is likely contributing to your problem.

  3. #3
    sssss is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    So how would I remove the constructor? I have never worked with inheritance before....

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    28

    Default

    Quote Originally Posted by sssss View Post
    So how would I remove the constructor? I have never worked with inheritance before....
    ???
    Who said anything about removing a constructor?

    You need to change the pseudo-constructor into a true constructor by getting rid of the return type and making sure that it doesn't try to return anything.

  5. #5
    sssss is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    But if it does not return anything, then how will i get the value from the CanadaPensionPlanD() class into the main program????

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    28

    Default

    Quote Originally Posted by sssss View Post
    But if it does not return anything, then how will i get the value from the CanadaPensionPlanD() class into the main program????
    But that is not the constructor's job. A constructor's job is to create an object, period. I think you need to rethink your code a bit. If all you want is a class to hold a method that returns a value, say acts on the parameters calculates a new value based on the parameters and returns it, and the class has no state (no fields that hold values), then perhaps all you want is a static method not a constructor, and you'll need to call it as a method on a class, not as a constructor, but this decision must come from you based on your needs.

  7. #7
    sssss is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    kkk. I think I get it. So is this what the code for the other class would look like:

    Java Code:
    public static double CandaPensionPlanDeduction() {
      public double CandaPensionPlanD(double x, double cppAmount){ 
      if (x >= 1513.10 && x <=1523.09) {
        cppAmount = 59.49;
        
      } else if (x >= 1523.10 && x <=1533.09) {
        cppAmount = 59.92;
        
      } else if (x >= 1533.10 && x <=1543.09 ){
        cppAmount = 60.35;
        
      } else if (x >= 1543.10 && x <=1553.09 ){
        cppAmount = 60.78;
        
      }
    return(cppAmount);
    }
    }

    and to call the class from the main class, I would have to use:

    cppAmount = new CandaPensionPlanD(x, cppAmount);

    can you please correct me if I am wrong?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    28

    Default

    Well, first ask the compiler before asking us -- does it compile? Also, does it match examples of methods that you've seen, that you should be basing your methods on?

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

    Default

    Also you have called your class CandaPensionPlanDeduction and not CandaPensionPlanD.

    You will have to sort that out as well as deciding if you are trying to write a method or a constructor.

Similar Threads

  1. using inheritance
    By senca in forum New To Java
    Replies: 4
    Last Post: 08-15-2010, 07:33 PM
  2. Replies: 4
    Last Post: 07-22-2010, 11:22 AM
  3. multiple inheritance in java
    By pawanspace in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 05:08 AM
  4. Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 07:08 PM
  5. Multiple Inheritance in java
    By paty in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 02:25 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
  •