In: Computer Science
String result = "Name: " + name + "\nJob: " + job + "\nSalary: " + salary;
return result;
( System.out.println(helen); )
Add System.out.println(); right after it to add a blank line.
String result = "Name: " + name + "\nJob: " + job + "\nSalary: " + String.format("$%,.2f",salary);
Your output should be:
Helen Lee
45000.0
Brandon Charles
Name: Helen Lee
Job: Accountant
Salary: $51,750.00
Name: Brandon Charles
Job: Analyst
Salary: $52,800.00
Program
//class Employee
class Employee
{
//instance variables
private String name;
private String job;
private double salary;
//parameterized constructor
public Employee(String name, String job, double
salary)
{
this.name = name;
this.job = job;
this.salary = salary;
}
//method to return the name of Employee
public String getName()
{
return name;
}
//method to return the salary of Employee
public double getSalary()
{
return salary;
}
//method to raise the salary of Employee
public void giveRaise(double raisePercent)
{
salary = salary *(1 +
raisePercent);
}
//method return String contains Employee
information
public String toString()
{
String result = "Name: " + name +
"\nJob: " + job + "\nSalary: " +
String.format("$%,.2f",salary);
return result;
}
}
//Lab3A class
class Lab3A
{
//main method
public static void main (String[] args) {
//Declare and instantiate an
Employee object named helen
Employee helen = new
Employee("Helen Lee", "Accountant", 45000.00);
//Declare and instantiate an
Employee object named brandon
Employee brandon = new
Employee("Brandon Charles", "Analyst", 48000.00);
//Call getName for the helen object
and print the returned value
System.out.println(helen.getName());
//Call getSalary for the helen
object and print the returned value.
System.out.println(helen.getSalary());
//Call getName for the brandon
object and print the returned value.
System.out.println(brandon.getName());
//Give helen a 15% raise by calling
the giveRaise method for helen
helen.giveRaise(0.15);
//Give brandon a 10% raise by
calling the giveRaise method for brandon
brandon.giveRaise(0.10);
//print all of helen’s
information
System.out.println(helen);
//print all of brandon's
information
System.out.println(brandon);
}
}
Output:
Helen Lee
45000.0
Brandon Charles
Name: Helen Lee
Job: Accountant
Salary: $51,750.00
Name: Brandon Charles
Job: Analyst
Salary: $52,800.00
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you. However if you are
satisfied with the answer please don't forget to give your
feedback. Your feedback is very precious to us, so don't give
negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid
plagiarism.
Thank you.