Question

The class Engineer Test below is used to test two other class named Project & Engineer. A project has three attributes: projN
0 0
Add a comment Improve this question Transcribed image text
Answer #1

EngineerTest.class

public class EngineerTest {
    public static void main(String[] args) {

        Engineer fatima = new Engineer(1, "Fatima", 0.3);
        Engineer bayan = new Engineer(2, "Bayan", 0.2);

        Project mall = new Project(101, "Doha Mall", 2000000);
        Project cinema = new Project(202, "Khor Cinema", 500000);
        Project theater = new Project(303, "Wakra Theater", 300000);
        Project bowling = new Project(404, "Bowling Center", 500000);
        Project skating = new Project(505, "Skating Arena", 400000);

        fatima.addProject(mall);
        fatima.addProject(cinema);
        fatima.addProject(theater);
        System.out.printf("Fatima's Total Profit = %.2f QR\n", fatima.getProfit());

        System.out.println("-----------------------------------------");

        bayan.addProject(bowling);
        bayan.addProject(skating);
        bayan.addProject(mall);
        System.out.printf("Bayan's Total Profit = %.2f QR\n", bayan.getProfit());

        System.out.println("-----------------------------------------");

    }
}

Project.class

public class Project {
    private  int projNo;
    private String projname;
    private double revenue;

    //constructor to initialize variables using setters()
    public Project(int projNo, String projname, double revenue) {
        setProjNo(projNo);
        setProjname(projname);
        setRevenue(revenue);
    }

    public int getProjNo() {
        return projNo;
    }

    public void setProjNo(int projNo) {
        this.projNo = projNo;
    }

    public String getProjname() {
        return projname;
    }

    public void setProjname(String projname) {
        this.projname = projname;
    }

    public double getRevenue() {
        return revenue;
    }

    public void setRevenue(double revenue) {
        this.revenue = revenue;
    }
}

Engineer.class

import java.util.ArrayList;
import java.util.List;

public class Engineer {
    private int jobId;
    private String name;
    private double rate;
    private List<Project> projList= new ArrayList<>();

    //constructor to initialize the variables
    public Engineer(int jobId, String name, double rate) {
        this.jobId = jobId;
        this.name = name;
        this.rate = rate;
    }

    //method to add projects to the array list projList  of type Project
    void addProject(Project p){
        projList.add(p);
    }

    //float return type used since %.2f is used in main() to format the number to 2 decimal places
    float getProfit(){
        float profit=0.0f;
        /**
         * Forloop that takes each project from project list as object and stores in variable p
         * getter method of Project class getRevenue() called to get revenue of respective projects
         * Add all the project revenue with the rate of engineer
         * Sum of the results stored in variable profit on each iteration
         */
        for(Project p : projList){
            profit+=p.getRevenue()*rate;
        }
        return profit;
    }
}

Output

Fatimas Total Profit 840000.00 QR Bayans Total Profit = 580000.00 QR

Explanation

The EngineerTest class creates two engineer objects that initializes the constructor in the Engineer class. Whenever the engineer class is called with the object name. the values that was initialized with that object can be retrieved.

In the next step many project objects are created in Engineer class and values are initialized using the constructor defined. Each engineer object is used to add a project by passing the project object to the addProject() which creates a list of projects under that engineer object. And finally for each Engineer object, the getprofit() is called. In that function, the revenue for the projects assigned to an engineer is fetched from the project class using the getter method getRevenue() and is added with the rate of the respective engineer. The result is then returned back and printed.

Working

When the getProfit() method called for fatima object which has the following projects added,

Project Revenue
mall 2000000
cinema 500000
theater 300000

Rate of engineer fatima per project = 0.3

So the result will be calculated as 200000*0.3 + 500000*0.3 + 300000*0.3 = 840000.00

The same will be applied in case of Bayan.

Hope this answer helped. A like would be appreciated. Thank You :)

Add a comment
Know the answer?
Add Answer to:
The class Engineer Test below is used to test two other class named Project & Engineer....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • Write a Java class named Employee to meet the requirements described in the UML Class Diagram...

    Write a Java class named Employee to meet the requirements described in the UML Class Diagram below: Note: Code added in the toString cell are not usually included in a regular UML class diagram. This was added so you can understand what I expect from you. If your code compiles cleanly, is defined correctly and functions exactly as required, the expected output of your program will solve the following problem: “An IT company with four departments and a staff strength...

  • Open BlueJ and create a new project (Project->New Project...). Create a new class named ListsDemo. Open the source code and delete all the boilerplate code. Type in the code to type (code to type...

    Open BlueJ and create a new project (Project->New Project...). Create a new class named ListsDemo. Open the source code and delete all the boilerplate code. Type in the code to type (code to type with bigger font) exactly as shown, filling in your name and the date in the places indicated. The code is provided as an image because you should type it in rather than copy-paste. If you need the code as text for accessibility, such as using a...

  • // Client application class and service class Create a NetBeans project named StudentClient following the instructions...

    // Client application class and service class Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. Add a class named Student to the StudentClient project following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. After you have created your NetBeans Project your application class StudentC lient should contain the following executable code: package studentclient; public class...

  • Complete the CashRegister class by implementing the methods and adding the correct attributes (characteristics) as discussed...

    Complete the CashRegister class by implementing the methods and adding the correct attributes (characteristics) as discussed in class. Once complete, test the class using the CashRegisterTester class. Make sure you have 3 total items in the cash register. I have two classes, the first is CashRegisterTester below: public class CashRegisterTester { public static void main(String[] args) { //Initialize all variables //Construct a CashRegister object CashRegister register1 = new CashRegister(); //Invole a non-static method of the object //since it is non-static,...

  • By editing the code below to include composition, enums, toString; must do the following: Prompt the...

    By editing the code below to include composition, enums, toString; must do the following: Prompt the user to enter their birth date and hire date (see Fig. 8.7, 8.8 and 8.9 examples) in addition to the previous user input Create a new class that validates the dates that are input (can copy date class from the book) Incorporate composition into your class with these dates Use enums to identify the employee status as fulltime (40 or more hours worked for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT