Hi all,
finally following the advice on this forum I got hold of the Deitel and Deitel java how to program 9th edition! Great book I must say so far (only got to chapter 3). Now, I am doing some of the exercises as I go along, and today I have done 3.14:

p 137 ex 3.14 EMPLOYEE CLASS: create a class called Employee that includes three instances variables - a first name (string), a last name (string) and a monthly
salary (double). Provide a constructor that initializes the three instance variable. Provide a set and get method for each instance variable. If the monthly salary is
not positive do not set its value. Write a test application named employeeTest that demonstrates class Employee's capabilities. Create two Employee objects and
display each object's yearly salary. Then give each employee a 10% raise and display each Employee's yearly salary

It all went quite well, but there are a few things I had some problems with. My program has 2 files Employee.java and EmployeeClass.java:

//Employee.java
public class Employee{
    public static void main(String[] args){
        EmployeeClass employee1 = new EmployeeClass("your name 1", "your surname 1", 0.0);
        System.out.println("Printing values '1' initialized by the constructor.");
        System.out.printf("Name:%s ", employee1.getName());
        System.out.printf("\nSurnname:%s ", employee1.getLastName());
        System.out.printf("\nSalary:" + employee1.getSalary());

        EmployeeClass employee2 = new EmployeeClass("your name 2", "your surname 2", 0.0);
        System.out.println("\n\nPrinting values '2' initialized by the constructor.");
        System.out.printf("Name:%s ", employee2.getName());
        System.out.printf("\nSurnname:%s ", employee2.getLastName());
        System.out.printf("\nSalary: " + employee2.getSalary());

        //setting the values of the instance variables of first employee
        employee1.setName("John");
        employee1.setLastName("Smith");
        employee1.setSalary(19000.50);

        //getting the values of the instance variables of first employee
        System.out.println("\n\nPrinting the values of first employee's details after having set them");
        System.out.printf("Name:%s ", employee1.getName());
        System.out.printf("\nSurnname:%s ", employee1.getLastName());
        System.out.println("\nSalary: \u00A3" + employee1.getSalary());
        //increase 10% salary
        System.out.print("\na 10% rise will give a new salary of \u00A3" + employee1.salaryIncrease()); 

        //setting the values of the instance variables of second employee
        employee2.setName("Michael");
        employee2.setLastName("McWry");
        employee2.setSalary(23000.50);

        //getting the values of the instance variables of second employee
        System.out.println("\n\nPrinting the values of second employee's details after having set them");
        System.out.printf("Name:%s ", employee2.getName());
        System.out.printf("\nSurnname:%s ", employee2.getLastName());
        System.out.println("\nSalary: \u00A3" + employee2.getSalary());
        //increase 10% salary
        System.out.print("\na 10% rise will give a new salary of \u00A3" + employee2.salaryIncrease() + "\n");  
    }
}

and

//EmployeeClass.java
public class EmployeeClass{ 
    private String firstName;
    private String lastName;
    private double salary;

    //constructor taking 3 parameters
    public EmployeeClass(String name, String surname, double money){
        firstName = name;
        lastName = surname;
        salary = money;
    }

    //setters to set the instance variables
    public void setName(String name){
        firstName = name;
    }
    public void setLastName(String surname){
        lastName = surname;
    }
    public void setSalary(double money){
        if(money >= 0.0){
            salary = money;
        }
    }

    //getters to get the values back
    public String getName(){
        return firstName;
    }
    public String getLastName(){
        return lastName;
    }
    public double getSalary(){
        return salary;
    }

    //salary rise of 10%
    public double salaryIncrease(){
        salary += ((salary / 100) * 10);
        return salary;
    }
}

Now, first problem: format specifier: I was looking for a double format specifier and I thought I found it in %l but I had a runtime error - sorry I amended the code now and I didn't take a screenshot of the error(I am using the ubuntu terminal by the way). I had something like System.out.printf("\nSalary:%l ", employee1.getSalary()); but I had to change it to System.out.println("\nSalary: \u00A3" + employee1.getSalary()); ANy idea if there is such a thing as a double format specifier?

Other thing, the £ sign: that was driving me insane. I have finally managed to find this \u00A3 that prints a £ sign, but I am really sure that there's got to be another way to do this...I had a look online but I didn't find much help, any advice?

thanks

You shouldn't use floating point types to represent money because they are imprecise. They are designed to give you a close approximation of the correct answer. Doubles especially can give you a very close approximation because they are large enough to store a large amount of precision, which is great for most scientific calculations, simulations, and games. For money, people don't just want a large amount of precision; they want total precision; they won't accept the calculated answer being different from the true answer by even a tenth of a penny, and with floating point arithmetic it is difficult to be sure exactly how far off your answer might be. You might accidentally gain or lose entire dollars. It's just not safe.

I see what you mean thanks, so which type should I declare my variable if it holds money?
thanks

Edited 3 Years Ago by Violet_82

You can usually hold money as an integer number of cents - very few applications ever neen to handle a fraction of cent. int will usually do, or long if you need amounts over 20 million dollars. If you write a simple method to take a value in cents and format it as a String with dollars and cents, dollar sign, commas etc then it's no harder than working in floating point dollars.

oh right, that's interesting. So basically it's a bit like the price of petrol you mean, say £1,489.00 will be 148900. But how do I then format it as a tring? Is there a specific class in java the can do it, or should I do it manually

You can use the Formatter class, but even so I think it's simpler if you get that the way you want it then put it in a simple method yoiy can call every time you want to display a money value. (That also puts you in a good place if you want to change currency to (eg) Euros, with . as the thousands delimiter and , for the cents)

Edited 3 Years Ago by JamesCherrill

This question has already been answered. Start a new discussion instead.