Hi all! I'm new to Daniweb and to programming. I'm learning Java with the book Java how to program 7th edition. I'm stuck at an exercise where I'm supposed to create three objects that each inputs an hourly rate, number of hours, and a name for the employee given by a user. If the number of hours exceeds 40 it's a 50% increase in the hourly rate. Then output it all.
The code runs fine until line 25 in "GrossPayTest". Error I get:
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '2' +
at GrossPayTest.main(GrossPayTest.java:25)
I have looked at line 25 (GrossPayTest) for some time now but don't get it. Please help.
import java.util.Scanner;
public class GrossPay {
private double rate;
private double hours;
private double salary;
private String name;
public GrossPay(double hour, double pay, String Name){
hours = hour;
rate = pay;
name = Name;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public double getHours()
{
return hours;
}
public double getRate(){
return rate;
}
public void setNrHour()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number of hours worked: ");
hours = in.nextDouble();
}
public void setRate()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter hourly rate: ");
rate = in.nextDouble();
}
public void setName()
{
Scanner in = new Scanner(System.in);
System.out.println("Set name: ");
name = in.nextLine();
}
//If above 40 hours, each hour + 50%.
public double generateIncome(){
double h;
double increaseAmount;
if (hours > 40){
salary = 40 * rate;
h = hours - 40;
increaseAmount = h * rate * 1.5;
salary += increaseAmount;
}
else{
salary = hours * rate;
}
return salary;
}
}
public class GrossPayTest {
public static void main (String args []){
GrossPay Employee1 = new GrossPay (0,0,"Nilsen");
GrossPay Employee2 = new GrossPay (0,0,"Jonsen");
GrossPay Employee3 = new GrossPay (0,0,"Evensen");
Employee1.setName();
Employee1.setRate();
Employee1.setNrHour();
Employee1.generateIncome();
Employee2.setName();
Employee2.setRate();
Employee2.setNrHour();
Employee2.generateIncome();
Employee3.setName();
Employee3.setRate();
Employee3.setNrHour();
Employee3.generateIncome();
System.out.printf ("Name of employee: %s has worked %.2f hours" +
" and earned %2.f this week", Employee1.getName(), Employee1.getRate(), Employee1.getSalary());
}
}