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