Question

In: Computer Science

Create a Java project called Lab3A and a class named Lab3A. Create a second new class...

  1. Create a Java project called Lab3A and a class named Lab3A.
  2. Create a second new class named Employee.
  3. In the Employee class:
    1. Add the following private instance variables:
      1. name (String)
      2. job (String)
      3. salary (double)
    2. Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.)
    3. Add a public String method named getName (no parameter) that returns name.
    4. Add a public double method named getSalary (no parameter) that returns salary.
    5. Add a public void method named giveRaise with a double parameter named raisePercent. It should give the employee a raise by changing the salary to be equal to salary * (1 + raisePercent).
    6. Add a public String method toString (no parameters). It should create a String variable with the employee’s instance data. (I will give you the code here, but add this to your notes, because you will do this on future assignments.)

String result = "Name: " + name + "\nJob: " + job + "\nSalary: " + salary;

return result;

  1. Back in the main Lab3A class.
    1. Declare and instantiate an Employee object named helen sending the following parameters to the constructor ("Helen Lee", "Accountant", 45000.00)
    2. Declare and instantiate an Employee object named brandon sending the following parameters to the constructor ("Brandon Charles", "Analyst", 48000.00)
    3. Call getName for the helen object and print the returned value.
      ( System.out.println(helen.getName());   )
    4. Call getSalary for the helen object and print the returned value.
    5. Call getName for the brandon object and print the returned value.
    6. Give helen a 15% raise by calling the giveRaise method for helen and sending 0.10 as the parameter. (You don’t print anything here.)
    7. Give brandon a 10% raise.
    8. Use our super-secret toString shortcut to print all of helen’s information.

( System.out.println(helen);    )

Add System.out.println(); right after it to add a blank line.

  1. Use the same coding to print all of brandon’s info.
    1. If you run this, you’ll see that the salaries print a lot of decimal places after the raises. Let’s go back to the toString method and fix this. (Add this to your notes for future use.)

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

Solutions

Expert Solution

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.


Related Solutions

Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
In java: -Create a class named Animal
In java: -Create a class named Animal
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include the following three static methods described below. However, don't implement them right away. Instead, start by returning the default value followed by a // TODO comment. public static int sumOfDigits(int n) This method returns the sum of all the digits. sumOfDigits(-34) -> 7 sumOfDigits(1038) -> 12 public static int countSmiles(char[] letters, int index) This method counts the number of colons followed by a closing...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT