Reputation: 15
I was assigned a project to create a class, and use methods without editing the "EmployeeTester" class. How can i get the name from the object harry instead of setting name ="harry"? And also can I simplify my code more?
public class EmployeeTester
{
/**
* main() method
*/
public static void main(String[] args)
{
Employee harry = new Employee("Hacker, Harry", 50000.0);**\\
harry.raiseSalary(25.0); // Harry gets a 25 percent raise!
System.out.println(harry.getName() + " now makes " +
harry.getSalary());
}
}
public class Employee
{
// instance variables
private double salary;
private String name;
/**
* Constructor for objects of class Employee
*/
public Employee(String employeeName, double currentSalary)
{
//Initializes instance variables
salary = currentSalary;
name = employeeName;
}
public String getName()
{
//Sets name to harry
name = "Harry";
return name;
}
public void raiseSalary(double byPercent)
{
//multiplies by 1.25 percent to get the 25% raise
salary = salary *(1 + byPercent/100);
return;
}
public double getSalary()
{
//returns the salary
return salary;
}
}
Upvotes: 0
Views: 111
Reputation: 26
It can be like this
class Employee
{
private double salary;
private String name;
public Employee(String titleName, double salary) {
this.name = titleName.split(",")[1];
this.salary =salary;
}
public void raiseSalary(double byPercent) {
salary = salary *(1 + byPercent/100);
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
}
Upvotes: 1