Results 1 to 17 of 17
  1. #1
    badness is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default Beginner; Create a class to store info and constructor to initialize

    Hey everyone. I love that this forum exists and we can learn from one another. At this point I am the one doing all of the learning, but I will someday be able to return the favor for other beginners.

    I need a little help. My time, of late, is very limited for my studying of Java and I am saddened by this because I am interested in learning more. I have a project I need to submit for school and I thought I could enlist some help in gaining understanding of how to do it.

    I have a payroll program:

    import java.util.Scanner;

    public class Payroll3
    {
    public static void main( String[] args )
    {
    Scanner input = new Scanner( System.in );

    boolean enterMoreData = true;
    while(enterMoreData)
    {
    System.out.print ( "Enter employee name: " );
    String theName = input.nextLine();
    if(theName.equalsIgnoreCase("stop"))
    {
    input.close();
    break;
    }

    int hoursWorked;
    do
    {
    System.out.println("Enter hours worked: ");
    String line = input.nextLine();
    hoursWorked = Integer.parseInt(line);
    if(hoursWorked < 0)
    System.out.println("number must be positive");
    }
    while(hoursWorked < 0);

    int hourlyPayRate;
    do
    {
    System.out.println("Enter hourly pay rate: ");
    String line = input.nextLine();
    hourlyPayRate = Integer.parseInt(line);
    if(hourlyPayRate < 0)
    System.out.println("number must be positive");
    }
    while(hourlyPayRate < 0);

    // multiply hours worked * hourly pay rate
    int weeklyPay = hoursWorked * hourlyPayRate;

    // display employee name and weekly pay total
    System.out.printf( "Employee Name: %s, Weekly pay is $%d%n",
    theName, weeklyPay );
    }
    }
    }


    I got help from some of you to get this far and I learned a lot. Thanks to those who helped. Now I need to modify the program for these requirements:

    Modify the Payroll Program so that it uses a class to store and retrieve the employee's
    name, the hourly rate, and the number of hours worked. Use a constructor to initialize the
    employee information, and a method within that class to calculate the weekly pay. Once
    stop is entered as the employee name, the application should terminate.


    I am unsure of how to put a class in that will store this info until called upon by the constructor. I will be doing some reading this afternoon trying to find this in my text, I am not just being lazy. I would, though appreciate any help and explanations that you may offer. I really do appreciate this.

    Badness

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.util.Scanner;
    
    public class PayrollRx
    {
        public static void main( String[] args )
        {
            Scanner input = new Scanner( System.in );
    
            boolean enterMoreData = true;
            while(enterMoreData)
            {
                System.out.print ( "Enter employee name: " );
                String theName = input.nextLine();
                if(theName.equalsIgnoreCase("stop"))
                {
                    input.close();
                    break;
                }
    
                int hoursWorked;
                do
                {
                    System.out.println("Enter hours worked: ");
                    String line = input.nextLine();
                    hoursWorked = Integer.parseInt(line);
                    if(hoursWorked < 0)
                        System.out.println("number must be positive");
                }
                while(hoursWorked < 0);
    
                int hourlyPayRate;
                do
                {
                    System.out.println("Enter hourly pay rate: ");
                    String line = input.nextLine();
                    hourlyPayRate = Integer.parseInt(line);
                    if(hourlyPayRate < 0)
                        System.out.println("number must be positive");
                }
                while(hourlyPayRate < 0);
    
                Employee employee = new Employee(theName, hoursWorked, hourlyPayRate);
    
                System.out.printf( "Employee Name: %s,  Weekly pay is $%d%n",
                                    employee.name,  employee.getWeeklyPay() );
            }
        }
    }
    
    class Employee {
        String name;
        int hours;
        int hourlyPayRate;
        int weeklyPay;
    
        public Employee(String name, int hours, int rate) {
            this.name = name;
            this.hours = hours;
            hourlyPayRate = rate;
            computeWeeklyPay();
        }
    
        private void computeWeeklyPay() {
            weeklyPay = hours * hourlyPayRate;
        }
    
        public int getWeeklyPay() {
            return weeklyPay;
        }
    }

  3. #3
    Azzia is offline Member
    Join Date
    May 2008
    Location
    Ohio
    Posts
    20
    Rep Power
    0

    Default

    Hello,

    I know this is an old post and sorry to bring it back to life but I have the same exact assignment. I read through your code, hardwired, and it seems I have made mine work in a different way. I understand a little able what you have done above but not enough to be able to implement it into my code. Without adding the code, can you explain a little bit or point me in the correct direction as to where I can understand better on where the constructors would appear and/or how to create a class to store the information?

    Java Code:
    import java.util.Scanner;
    
    public class Main 
    {
    
         double rate;
         double hours;
         String Empname;
     
        
        // main method begins execution of Java application
        public static void main(String args[])
     
        {
            boolean stop = false; // controls if loop below is executed
            while (!stop)
            {
                // create Scanner to obtain input from command window
                Scanner input = new Scanner(System.in);
     
                System.out.print("Enter employee's name or stop to quit: "); // prompt for name
                String EmpName = input.nextLine(); // read employee's name entered
     
                if (EmpName.equals("stop")) 
                {
                    System.out.println("This program has ended successfully.");
                    stop = true;
                }
                else
                {
                    double PayRate; // First number multiplied
                    double HoursWorked; // Second number multiplied
                    double total; // Total of PayRate * HoursWorked
     
                    System.out.print("Enter the pay rate per hour of the employee: "); // prompt for rate
                    PayRate = input.nextDouble(); // read payrate of employee
                    while (PayRate <= 0) // Validate for positive number
                    {
                        System.out.println(); 
                        System.out.println("Pay rate must be a positve number. ");
                        System.out.print("Please enter pay rate per hour again: ");
                        PayRate = input.nextDouble(); 
                        System.out.println();
                    }
                    System.out.print("Enter the employee's hours worked this pay week: "); // prompt for Hours
                    HoursWorked = input.nextDouble(); 
                    while (HoursWorked <= 0) // validate for positive number
                    {
                        System.out.println();
                        System.out
                                .println("Hours worked must be a positive number. ");
                        System.out.print("Please enter hours worked this pay week again: ");
                        HoursWorked = input.nextDouble(); //read hourly rate of pay
                        System.out.println(); 
                    }
                    total = (double) PayRate * HoursWorked; // Multiply PayRate by HoursWorked
                    System.out.println(); 
                    System.out.println("Employee: " + EmpName); //display name
                    System.out.println("Number of hours worked this pay week: " + HoursWorked); //display hours worked
                    System.out.printf("Employee's Pay: $%,.2f\n", total); //display pay rate
                    System.out.println();
                    System.out.println();  
                } // end else
            } //end while 
        } // end main 
    } // end class
    Last edited by Azzia; 05-06-2008 at 09:45 PM.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Are you looking complete explanation for the complete code? Best things is asked question by steps, where you stuck with. Then you can found the best solution for it.

  5. #5
    Azzia is offline Member
    Join Date
    May 2008
    Location
    Ohio
    Posts
    20
    Rep Power
    0

    Default

    Java Code:
    Employee employee = new Employee(theName, hoursWorked, hourlyPayRate);
    
                System.out.printf( "Employee Name: %s,  Weekly pay is $%d%n",
                                    employee.name,  employee.getWeeklyPay() );
            }
        }
    }
    
    class Employee {
        String name;
        int hours;
        int hourlyPayRate;
        int weeklyPay;
    
        public Employee(String name, int hours, int rate) {
            this.name = name;
            this.hours = hours;
            hourlyPayRate = rate;
            computeWeeklyPay();
        }
    
        private void computeWeeklyPay() {
            weeklyPay = hours * hourlyPayRate;
        }
    
        public int getWeeklyPay() {
            return weeklyPay;
        }
    }
    This is the part I am unsure about. I have created the class but when I try and implement the constructor in my above code it will not work. The error is about variables but the variables I have used are declared in my code which again makes no sense to me.

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Looking at your Main class you declare three variables of (the primitive data) type double.
    Java Code:
        double PayRate;     // First number multiplied
        double HoursWorked; // Second number multiplied
        double total;       // Total of PayRate * HoursWorked
        ...
        PayRate = input.nextDouble();
        ...
        HoursWorked = input.nextDouble(); 
        ...
        total = (double) PayRate * HoursWorked;
        // Create an instance of Employee with this data:
        Employee employee = new Employee(PayRate, HoursWorked, total);
    For this to work your Employee class must have a constructor that accepts three double arguments, viz,
    Java Code:
    class Employee {
        ...
        public Employee(double arg1, double arg2, double arg3) {
            ...
        }
        ...
    }
    If java cannot find such a constructor in the Employee class it will give a compile error saying it cannot find a constructor with this signature.
    So you would want to design your Employee (or whatever name you choose to give it) class to match your Main class data that you are collecting, computing and want to save (in the Employee class).
    Java Code:
    class Employee {
        double payRate;
        double hours;
        double total;
    
        Employee(double payRate, double hours, double total) {
            this.payRate = payRate;
            this.hours = hours;
            this.total = toal;
        }
    
        ...
    }
    The argument types must match up. Java is very prissy about type matching. It all seems silly in the beginning but it's a way to make everything clear and easy to read and understand.

  7. #7
    Azzia is offline Member
    Join Date
    May 2008
    Location
    Ohio
    Posts
    20
    Rep Power
    0

    Default

    Oh wow. Well that explains a lot that the text book doesnt cover. When explaining floats and doubles, the text just says when to use them and why. Looking though it now, it never mentions that classes won't be compatible if their type isn't the same.

    I am still having an issue though. I declared the string "empName" but when I use the constructor it is still giving me an error. Here is a copy of how I implemented your code into mine to see how it worked. (I also tried several different approaches by changing some things but each seem to bite the dust.)


    Java Code:
    Employee employee = new Employee ( empName, hoursWorked, payRate);
    
                System.out.printf( "Employee Name: %s,  Weekly pay is $%d%n",
                                    employee.empName,  employee.getWeeklyPay() );
    
                   
                    
    //              total = (double) PayRate * HoursWorked; // Multiply PayRate by HoursWorked
    //              System.out.println(); 
    //              System.out.println("Employee: " + empName); //display name
    //              System.out.println("Number of hours worked this pay week: " + HoursWorked); //display hours worked
    //              System.out.printf("Employee's Pay: $%,.2f\n", total); //display pay rate
    //              System.out.println();
    //              System.out.println();  
                } // end else
            } //end while 
        } // end main 
    } // end class
    Last edited by Azzia; 05-07-2008 at 07:36 PM.

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
        String Empname;
        double PayRate;
        double HoursWorked;
        double total;
        ...
        // Create an instance of Employee with this data:
        Employee employee = new Employee(Empname, hoursWorked, payRate);
    Java will look for a constructor in Employee that matches this type signature:
    Java Code:
    class Employee {
        ...
        public Employee(String arg1, double arg2, double arg3) {
            ...
        }
        ...
    }
    Your Employee class can look like this:
    Java Code:
    class Employee {
        String name;
        double payRate;
        double hours;
        double total;
    
        Employee(String name, double payRate, double hours) {
            this.name    = name
            this.payRate = payRate;
            this.hours   = hours;
        }
    
        ...
    }

  9. #9
    Azzia is offline Member
    Join Date
    May 2008
    Location
    Ohio
    Posts
    20
    Rep Power
    0

    Default

    This seems to be my issue right now. (I have changed things around and added new things but seems I am just making it worse)

    Java Code:
     Employee employee = new [COLOR="Red"]employee[/COLOR] ( empName, hoursWorked, payRate);
    It is a symbol error. I am going to research a little more to see if I can find the issue. Here is the rest of the code.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package payrollprogram;
    
    /**
     *
     * @author MKAY
     */
    import java.util.Scanner;
    
    public class Main 
    {
         String empName;
         double payRate;
         double hoursWorked;
     
        
        // main method begins execution of Java application
        public void main(String args[])
     
        {
            boolean stop = false; // controls if loop below is executed
            while (!stop)
            {
                // create Scanner to obtain input from command window
                Scanner input = new Scanner(System.in);
     
                System.out.print("Enter employee's name or stop to quit: "); // prompt for name
                String EmpName = input.nextLine(); // read employee's name entered
     
                if (EmpName.equals("stop")) 
                {
                    System.out.println("This program has ended successfully.");
                    stop = true;
                    input.close();
                    break;
                }
                else
                {
                    double PayRate; // First number multiplied
                    double HoursWorked; // Second number multiplied
                    double total; // Total of PayRate * HoursWorked
     
                    System.out.print("Enter the pay rate per hour of the employee: "); // prompt for rate
                    PayRate = input.nextDouble(); // read payrate of employee
                    while (PayRate <= 0) // Validate for positive number
                    {
                        System.out.println(); 
                        System.out.println("Pay rate must be a positve number. ");
                        System.out.print("Please enter pay rate per hour again: ");
                        PayRate = input.nextDouble(); 
                        System.out.println();
                    }
                    System.out.print("Enter the employee's hours worked this pay week: "); // prompt for Hours
                    HoursWorked = input.nextDouble(); 
                    while (HoursWorked <= 0) // validate for positive number
                    {
                        System.out.println();
                        System.out
                                .println("Hours worked must be a positive number. ");
                        System.out.print("Please enter hours worked this pay week again: ");
                        HoursWorked = input.nextDouble(); //read hourly rate of pay
                        System.out.println(); 
                    }
    
                Employee employee = new employee ( empName, hoursWorked, payRate);
    
                System.out.printf( "Employee Name: %s,  Weekly pay is $%d%n",
                                    employee.empName,  employee.getWeeklyPay() );
                
    //              total = (double) PayRate * HoursWorked; // Multiply PayRate by HoursWorked
    //              System.out.println(); 
    //              System.out.println("Employee: " + EmpName); //display name
    //              System.out.println("Number of hours worked this pay week: " + HoursWorked); //display hours worked
    //              System.out.printf("Employee's Pay: $%,.2f\n", total); //display pay rate
    //              System.out.println();
    //              System.out.println();  
                } // end else
            } //end while 
        } // end main 
    } // end class
    
              
     class Employee
    {
        String empName;
        double hoursWorked;
        double payRate;
        double total;
        
        public void Employee (String name, double hours, double rate){
            this.empName = name;
            this.hoursWorked = hours;
            this.payRate = rate;
            computeWeeklyPay();
        }
        
        private void computeWeeklyPay() {
            total = (double) hoursWorked * (double) payRate;
        }
        public double getWeeklyPay(){
            return total;
            
        }
    }

  10. #10
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Typo:
    Java Code:
    // Change this
    Employee employee = new employee ( empName, hoursWorked, payRate);
    // to this
    Employee employee = new Employee ( empName, hoursWorked, payRate);

  11. #11
    Azzia is offline Member
    Join Date
    May 2008
    Location
    Ohio
    Posts
    20
    Rep Power
    0

    Default

    Java Code:
    public [COLOR="Red"]void[/COLOR] Employee (String name, double hours, double rate){
            this.empName = name;
            this.hoursWorked = hours;
            this.payRate = rate;
            computeWeeklyPay();
    Got it. Not sure what I was thinking. Thanks a lot for your help! I am sure ill be needing you again soon!

  12. #12
    Azzia is offline Member
    Join Date
    May 2008
    Location
    Ohio
    Posts
    20
    Rep Power
    0

    Default

    Alright, well I thought I had it all good and corrected but then it was giving me an error of <no main class found>. So, I went back through and edited it some mroe and got it to where there were no errors BUT the program wouldnt run, it just told me build successful and thats it. So, I played around again with it and now I am back to square one. Here is my whole source code. I am at my end with this. :eek:

    Java Code:
    import java.util.Scanner;
    
    public class Main
    {    
        // main method begins execution of Java application
        public static void Main(String empName, double hoursWorked, double payRate, double total)    
        {
        
                     boolean stop = false; // controls if loop below is executed 
                     while (!stop)
            {
                // create Scanner to obtain input from command window
                Scanner input = new Scanner(System.in);
     
                System.out.print("Enter employee's name or stop to quit: "); // prompt for name
                String EmpName = input.nextLine(); // read employee's name entered
                
                  
                if (EmpName.equals("stop")) 
                {
                    System.out.println("This program has ended successfully.");
                    stop = true;
                    input.close();
                    break;
                }
                else
                {
                    double PayRate; // First number multiplied
                    double HoursWorked; // Second number multiplied
                    //double total; // Total of PayRate * HoursWorked
     
                    System.out.print("Enter the pay rate per hour of the employee: "); // prompt for rate
                    PayRate = input.nextDouble(); // read payrate of employee
                    while (PayRate <= 0) // Validate for positive number
                    {
                        System.out.println(); 
                        System.out.println("Pay rate must be a positve number. ");
                        System.out.print("Please enter pay rate per hour again: ");
                        PayRate = input.nextDouble(); 
                        System.out.println();
                    }
                    System.out.print("Enter the employee's hours worked this pay week: "); // prompt for Hours
                    HoursWorked = input.nextDouble(); 
                    while (HoursWorked <= 0) // validate for positive number
                    {
                        System.out.println();
                        System.out
                                .println("Hours worked must be a positive number. ");
                        System.out.print("Please enter hours worked this pay week again: ");
                        HoursWorked = input.nextDouble(); //read hourly rate of pay
                        System.out.println(); 
                    }
                //Employee employee = new Employee();
                Employee employee = new Employee(empName, hoursWorked, payRate, total);
    
                System.out.printf( "Employee Name: %s,  Weekly pay is $%d%n",
                                    employee.empName,  employee.getWeeklyPay() );
                
    //              total = (double) PayRate * HoursWorked; // Multiply PayRate by HoursWorked
                  System.out.println(); 
    //              System.out.println("Employee: " + EmpName); //display name
    //              System.out.println("Number of hours worked this pay week: " + HoursWorked); //display hours worked
    //              System.out.printf("Employee's Pay: $%,.2f\n", total); //display pay rate
                  System.out.println();
                  System.out.println();  
                } // end else
            } //end while 
        } // end main 
    } // end class
    
              
    class Employee
    {
        
        public String empName;
        double hoursWorked;
        double payRate;
        double total;
        
        public  Employee(String name, double hours, double rate)
       {
            this.empName = name;
            this.hoursWorked = hours;
            this.payRate = rate;
            computeWeeklyPay();
        }
        
        private void computeWeeklyPay() {
            total = (double) hoursWorked * (double) payRate;
        }
        public double getWeeklyPay(){
            return total;
            
        }
    }

  13. #13
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    // change this
    public static void Main(String empName, double hoursWorked, double payRate, double total)
    // to this
    public static void main()
    ...
    // To get your class to work with these two statements
    Employee employee = new Employee();
    Employee employee = new Employee(empName, hoursWorked, payRate, total);
    // you would have to add more constructors:
    class Employee
    {
        public String empName;
        double hoursWorked;
        double payRate;
        double total;
    
        public Employee()
        {
            this(null, 0.0, 0.0);
        }
    
        public Employee(String name, double hours, double rate)
        {
            this(name, hours, rate, 0.0);
            computeWeeklyPay();
        }
    
        public Employee(String name, double hours, double rate, double total)
        {
            this.empName = name;
            this.hoursWorked = hours;
            this.payRate = rate;
            this.total = total;
        }
        ...
    or you could change the constructor in the Main class to
    Java Code:
    Employee employee = new Employee(empName, hoursWorked, payRate);
    and call computeWeeklyPay() in the Employee constructor to calculate total as you had it.
    Last edited by hardwired; 05-08-2008 at 06:13 AM. Reason: more to say after a second look

  14. #14
    Azzia is offline Member
    Join Date
    May 2008
    Location
    Ohio
    Posts
    20
    Rep Power
    0

    Default

    I apologize for this all in advance. I am at the point where I have messed with it so much that its all a big mumbo jumbo in my head. I am going to read up a little more on contructors cause that seems to be my issue. I can't seem to get away from this error and its irritating.

    Java Code:
    symbol  : constructor Employee(java.lang.String,double,double[],double)
    location: class payrollprogram.Employee
                Employee employee = new Employee(empName, hoursWorked, payRate, total);
    1 error
    BUILD FAILED (total time: 0 seconds)
    I will let you know what I figure out.

  15. #15
    Azzia is offline Member
    Join Date
    May 2008
    Location
    Ohio
    Posts
    20
    Rep Power
    0

    Default

    Well, I am laughing now. :o
    My compiler is messed up and I have to completely make a whole new package cause the error just wouldn't budge.

    Well, the program compiles and runs...

    Java Code:
    /*
    
    import java.util.Scanner;
    
    public class Main 
    {
         double rate;
         double hours;
         String Empname;
    
        // main method begins execution of Java application
        public static void main( String [] args)
     
        {
            boolean stop = false; // controls if loop below is executed
            while (!stop)
            {
                // create Scanner to obtain input from command window
                Scanner input = new Scanner(System.in);
     
                System.out.print("Enter employee's name or stop to quit: "); // prompt for name
                String EmpName = input.nextLine(); // read employee's name entered
     
                if (EmpName.equals("stop")) 
                {
                    System.out.println("This program has ended successfully.");
                    stop = true;
                    input.close();
                    break;
                }
                else
                {
                    double PayRate; // First number multiplied
                    double HoursWorked; // Second number multiplied
                    double total; // Total of PayRate * HoursWorked
     
                    System.out.print("Enter the pay rate per hour of the employee: "); // prompt for rate
                    PayRate = input.nextDouble(); // read payrate of employee
                    while (PayRate <= 0) // Validate for positive number
                    {
                        System.out.println(); 
                        System.out.println("Pay rate must be a positve number. ");
                        System.out.print("Please enter pay rate per hour again: ");
                        PayRate = input.nextDouble(); 
                        System.out.println();
                    }
                    System.out.print("Enter the employee's hours worked this pay week: "); // prompt for Hours
                    HoursWorked = input.nextDouble(); 
                    while (HoursWorked <= 0) // validate for positive number
                    {
                        System.out.println();
                        System.out
                                .println("Hours worked must be a positive number. ");
                        System.out.print("Please enter hours worked this pay week again: ");
                        HoursWorked = input.nextDouble(); //read hourly rate of pay
                        System.out.println(); 
                    }
    
                Employee employee = new Employee ();
    
                System.out.printf( "Employee Name: %s,  Weekly pay is $%,.2f",
                                    employee.empName,  employee.getWeeklyPay() );
                
    //              total = (double) PayRate * HoursWorked; // Multiply PayRate by HoursWorked
                  System.out.println(); 
    //              System.out.println("Employee: " + EmpName); //display name
    //              System.out.println("Number of hours worked this pay week: " + HoursWorked); //display hours worked
    //              System.out.printf("Employee's Pay: $%,.2f", total); //display pay rate
                 System.out.println();
                 System.out.println();  
                } // end else
            } //end while 
        } // end main 
    } // end class
    class Employee
    {
        String empName;
        double hoursWorked;
        double payRate;
        double total;
        
        public void Employee (String name, double hours, double rate)
        {
            this.empName = name;
            this.hoursWorked = hours;
            payRate = rate;
            computeWeeklyPay();
        }
        
        private void computeWeeklyPay() 
        {
            total = hoursWorked *  payRate;
        }
        public double getWeeklyPay()
        {
            return total;
            
        }
    }

    but now... dun dun dun...

    "Employee Name: null, Weekly pay is $0.00


    Enter employee's name or stop to quit: "
    Last edited by Azzia; 05-08-2008 at 08:27 PM.

  16. #16
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    C:\jexp>javac employeetest2.java
    employeetest2.java:57: cannot find symbol
    symbol  : constructor Employee2()
    location: class Employee2
                    Employee2 employee = new Employee2();
                                         ^
    1 error
    There is no explicit constructor declared in the Employee2 class so the jvm calls the default class constructor which the jvm gives to any class without a constructor.
    Java Code:
        // This is a method with a [i]void[/i] return type, ie, the method
        // does not return anything to the caller.
        public void Employee2(String name, double hours, double rate)
    
        // This is a constructor: a special method that does not have a
        // return type int its declaration and whose name is the same as
        // its enclosing class.
        public Employee2(String name, double hours, double rate)
    For more on constructors see Providing Constructors for Your Classes.
    Works okay now:
    Java Code:
    import java.util.Scanner;
    
    public class EmployeeTest2 
    {
        public static void main( String[] args)
        {
            boolean stop = false;
            while (!stop)
            {
                Scanner input = new Scanner(System.in);
     
                System.out.print("Enter employee's name or stop to quit: ");
                String EmpName = input.nextLine();
     
                if (EmpName.equals("stop")) 
                {
                    System.out.println("This program has ended successfully.");
                    stop = true;
                    input.close();
                    break;
                }
                else
                {
                    double PayRate;
                    double HoursWorked;
                    double total;
     
                    System.out.print("Enter the pay rate per hour of " +
                                     "the employee: ");
                    PayRate = input.nextDouble();
                    while (PayRate <= 0)
                    {
                        System.out.println(); 
                        System.out.println("Pay rate must be a positve number. ");
                        System.out.print("Please enter pay rate per hour again: ");
                        PayRate = input.nextDouble(); 
                        System.out.println();
                    }
                    System.out.print("Enter the employee's hours " +
                                     "worked this pay week: ");
                    HoursWorked = input.nextDouble(); 
                    while (HoursWorked <= 0)
                    {
                        System.out.println();
                        System.out.println("Hours worked must be a " +
                                           "positive number. ");
                        System.out.print("Please enter hours worked " +
                                         "this pay week again: ");
                        HoursWorked = input.nextDouble();
                        System.out.println(); 
                    }
    
                    Employee2 employee =
                        new Employee2(EmpName, PayRate, HoursWorked);
    
                    System.out.printf( "Employee Name: %s,  Weekly pay is $%,.2f%n",
                                        employee.empName,  employee.getWeeklyPay() );
                }
            } 
        } 
    }
    
    class Employee2
    {
        String empName;
        double hoursWorked;
        double payRate;
        double total;
        
        public Employee2(String name, double hours, double rate)
        {
            this.empName = name;
            this.hoursWorked = hours;
            payRate = rate;
            computeWeeklyPay();
        }
        
        private void computeWeeklyPay() 
        {
            total = hoursWorked *  payRate;
        }
    
        public double getWeeklyPay()
        {
            return total;
            
        }
    }

  17. #17
    Azzia is offline Member
    Join Date
    May 2008
    Location
    Ohio
    Posts
    20
    Rep Power
    0

    Default

    I understand. I had to completely delete my file about 3 or 4 times because it was showing an error when there wasn't supposed to be one. I think I confused myself more because of this problem due to thinking there were problems and changing things when there really wasn't a problem at all. I really appriciate your help. Thank you!

Similar Threads

  1. Replies: 5
    Last Post: 07-30-2008, 09:30 AM
  2. Modify/create AVI's INFO BLOCK
    By Agus211 in forum New To Java
    Replies: 0
    Last Post: 02-11-2008, 03:20 PM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  5. Replies: 2
    Last Post: 12-12-2007, 05:01 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
  •