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

    Default Very simple problem - calculation. Please help.

    I'm having a problem with the calculation for calcTotal when I do the final output message. Program runs, just number is not correct. Maybe I have been looking at this for too long. I can't figure out what's wrong with the equation/calculation.

    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;
            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 = getHours() * getWage() * expPer;
            Double netTotal = (getHours() * getWage()) - netExp;
            return String.valueOf(netTotal);
        }
    
    }

  2. #2
    Estrella is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    16
    Rep Power
    0

    Default Re: Very simple problem - calculation. Please help.

    Which calculation is wrong? Posting sample input and output might help.

  3. #3
    Raigeki is offline Nocturnal Beast
    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Default Re: Very simple problem - calculation. Please help.

    It's hard for anybody to know what the error is when there is no information about the "wrong number". What did you expect? What did you get instead? Have you tried messing around with the code to see where the error might be? :)

    Anyway, I'm just wondering why you are dividing your set expense percent with 100 at line 105 in your code.
    Are you sure you get a persons height in centimeters by multiplying his height in feet with 30.48? It is things like those you have to look at. But hey, I might be wrong.
    Last edited by Raigeki; 02-01-2013 at 11:35 AM.

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

    Default Re: Very simple problem - calculation. Please help.

    There are 30.48 cm to a foot, so that is correct.
    An inch is approx 2.54cm.

    As for the expense %, I suspect the user is going to type in '20', so it makes sense to turn that into 0.2 since it makes the later calculation a little easier.

    But yes, we need to be told what is happening and why that is wrong.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 18
    Last Post: 05-22-2011, 10:00 PM
  2. Simple calculation method problem.
    By carman12 in forum New To Java
    Replies: 11
    Last Post: 12-29-2010, 01:56 AM
  3. Problem with calculation
    By ibrafiqb in forum New To Java
    Replies: 3
    Last Post: 11-24-2010, 05:02 PM
  4. Parsing string and simple calculation
    By sapina007 in forum Advanced Java
    Replies: 4
    Last Post: 08-21-2009, 12:07 PM
  5. Problem with Calculation ....
    By danny000 in forum New To Java
    Replies: 1
    Last Post: 04-15-2008, 02:42 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
  •