Results 1 to 7 of 7
  1. #1
    blink16j is offline Member
    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Default Help with simple program - classes and methods

    I need some guidance with this simple program.
    I need all input/output to occur in the main method
    I need the Employee class to contain methods getName, setName, calcCentimeters, calcAmount, and a constructor that accepts the name. I need the calculations for calcCentimeters and calcAmount to occur in their respective method.
    I am having trouble calling the methods and setting the variables to their proper values.
    I am not sure as to what to leave in the main method of place into other method.


    Java Code:
    package employee;
    
    import java.util.Scanner; //import scanner package
    import javax.swing.JOptionPane; //import JOption package
    
    public class EmployeeProgram1
    {
        public static void main(String[] args) 
        {
            //Start Function 1
        
            Scanner keyboard = new Scanner(System.in); //Create new scanner class object
            
            System.out.print("Enter your name: ");
            String userName = keyboard.nextLine(); //Capture userName as string
            System.out.print("Enter height in feet: ");
            String userHeight = keyboard.nextLine(); //Capture userHeight as string
            
            Double dblHeight = Double.valueOf(userHeight); //Convert userHeight to type double
            String cmHeight = Double.toString(dblHeight*30.48); //Calculate user's height in centimeters
            
            System.out.println(userName + " has height in centimeters of " + cmHeight); 
     
            //Start Function 2  
            JOptionPane.showMessageDialog(null, "Part 2 will use a JOption Pane");
            String employeeName = JOptionPane.showInputDialog("Enter employee name");
            
            String hoursWorkedStr = JOptionPane.showInputDialog("Enter hours worked");
            Double hoursWorked = Double.parseDouble(hoursWorkedStr); //Convert string to double
            
            String payRateStr = JOptionPane.showInputDialog("Enter hourly pay rate");
            Double payRate = Double.parseDouble(payRateStr);  //Convert string to double
              
            String percentageStr = JOptionPane.showInputDialog("Enter expense as a percentage (10% should be entered as 10)");
            Double percentage = Double.parseDouble(percentageStr);  //Convert string to double
            Double totalAmt = hoursWorked*payRate*((100-percentage)/100); //Calculate net total amount
              
             //Display results
            String output = "Written by Joe Benner" +
                            "\nName: " + employeeName +
                            "\nHours Worked: " + hoursWorkedStr +
                            "\nHourly Pay Rate: $" + payRateStr +
                            "\nExpense Percent " + percentageStr +
                            "\nTotal Amount after Expenses: $" + totalAmt;
            
            JOptionPane.showMessageDialog(null, output);
     
       
        }
    }
    
    class Employee
    {
        
    }
    Last edited by blink16j; 01-28-2013 at 10:47 PM.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: Help with simple program - classes and methods

    Take it a step at a time. You have already created an empty Employee class. Now build it up.
    Most classes are made up of attributes, a constructor, and methods.

    You know that you need a variable to hold the value of name.
    Why not go ahead and declare that variable inside your Employee class?
    That's a start.

  3. #3
    blink16j is offline Member
    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with simple program - classes and methods

    Is it possible to do something like this. I get an error message saying "non-static method getName() cannot be referenced from a static context."

    in main method:
    Java Code:
    String employeeName = Employee.getName();
    then,
    Java Code:
    class Employee
    {
        String getName()
        {
            String result;
            result = JOptionPane.showInputDialog("Enter employee name");
            return result;
        }
    }

  4. #4
    blink16j is offline Member
    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with simple program - classes and methods

    Ok I made it "public static String getName()" and it works now.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Help with simple program - classes and methods

    No.
    You don't want to make it static.
    What if you had more than one Employee?
    They'd all have the same name.

    You want to create an Employee object for each Employee:
    Java Code:
    Employee employee = new Employee();
    employee.setName("Fred");
    System.out.println(employeee.getName());
    That creates an Employee object (the 'new' command), and sticks the reference to it in the 'employee' variable.
    You can then use that variable to set the name and print the name.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    blink16j is offline Member
    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with simple program - classes and methods

    Ok thanks for everyone's help. The program is running but I am having a problem when I display the output message at end. My netTotal will not calculate properly.
    Maybe I've been looking at this for too long.

    Here's the code
    Java Code:
    package lab1;
    
    /**
     *
     * @author James Doyle
     */
    import java.util.Scanner; //import scanner package
    import javax.swing.JOptionPane; //import JOption package
    
    public class JamesDoyleLab1
    {
        public static void main(String[] args) 
        {
            //Start Function 1
            System.out.println("Lab 1 written by James Doyle");
            //Create new scanner class object
            Scanner keyboard = new Scanner(System.in); 
            //Capture and set userName as string
            System.out.print("Enter your name: ");
            String userName = keyboard.nextLine(); 
            //Create new employee class object
            Employee employee = new Employee(userName); 
            //Capture and set employee height in feet
            System.out.print("Enter height in feet: ");
            employee.setFtHeight(keyboard.nextDouble());
            //Display full scanner output
            System.out.print(employee.getName() + " has height in centimeters of " + employee.calcCentimeters());
           
            //Start Function 2  
            JOptionPane.showMessageDialog(null, "Part 2 will use a JOption Pane");
            //Request and assign employee name
            employee.setName(JOptionPane.showInputDialog("Enter employee name"));
            //Request and set employee hours worked
            employee.setHours(JOptionPane.showInputDialog("Enter hours worked"));
            //Request and set hourly pay rate
            employee.setWage(JOptionPane.showInputDialog("Enter hourly pay rate"));
            //Request and set expense percent
            employee.setExp(JOptionPane.showInputDialog("Enter expense as a percentage (10% should be entered as 10)"));
             
            //Display results
            String output = ("Written by James Doyle" +
                            "\nName: " + employee.getName() +
                            "\nHours Worked: " + employee.getHours() +
                            "\nHourly Pay Rate: $" + employee.getWage() +
                            "\nExpense Percent: " + employee.getExp() +
                            "\nTotal Amount after Expenses: $" + employee.calcTotal());
            
            JOptionPane.showMessageDialog(null, output);
            JOptionPane.showMessageDialog(null, "James Doyle Lab1 has ended");
       
        }
    }
    
    class Employee
    {
        private String name;
        private double ftHeight;
        private double hrsWorked;
        private double wage;
        private double expPer;
        
        public Employee(String name)
        {
            this.name = name;
        }
        public void setName(String name)
        {
             this.name = name;
        }
        public String getName()
        {
            return name;
        }
        public void setFtHeight(double ftHeight)
        {
            this.ftHeight = ftHeight;
        }
        public double calcCentimeters()
        {
            return ftHeight*30.48;
        }
        public void setHours(String hrsWorked)
        {
            this.hrsWorked = Double.parseDouble(hrsWorked);
        }
        public double getHours()
        {
            return hrsWorked;
        }
        public void setWage(String wage)
        {
            this.wage = Double.parseDouble(wage);
        }
        public double getWage()
        {
            return wage;
        }
        public void setExp(String expPer)
        {
            this.expPer = Double.parseDouble(expPer);
        }
        public void setExp(double expPer)
        {
            this.expPer = expPer/100.0;
        }
        public double getExp()
        {
            return expPer;
        }
        public String calcTotal()
        {
            Double netExp = hrsWorked * wage * expPer;
            Double netTotal = (hrsWorked * wage) - netExp;
            return String.valueOf(netTotal);
        }
    
    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Help with simple program - classes and methods

    Can you show us a sample input and the results of the final println()?
    Also say what you expect to see (if it's not obvious).

    I would suggest not using the Double class, but the double primitive. There's no point using the class in this case.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 07-13-2012, 12:04 AM
  2. Replies: 2
    Last Post: 07-13-2012, 12:04 AM
  3. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  4. Replies: 3
    Last Post: 10-22-2011, 01:25 PM
  5. using methods between classes
    By soccer_kid_6 in forum New To Java
    Replies: 2
    Last Post: 04-18-2010, 04:14 AM

Posting Permissions

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