Problem with identifying the right Access Modifier | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

Problem with identifying the right Access Modifier

Hello everyone, I've struggled with identifying which Access Modifier I should use in this code (https://code.sololearn.com/cA12a84A19A1/#java). This is the description of the test: You are a manager at company that have two employees, James and Tom. You wrote a program to calculate total employee salary: it creates two objects for workers and sets the corresponding parameters. But something is wrong. The program doesn't provide access to attributes. Fix it. Also complete the method to calculate and return the total salary. I've tried several things, but I can't get it right... Ops: I know that there's a method for "set/get", but I'm not quite there yet. Hope you guys can help me! Thanks!

22nd Jan 2021, 11:34 AM
Binh Tan Nguyen
Binh Tan Nguyen - avatar
6 Respuestas
0
//complete the function to calculate the total salary public static int CalculateTotalSalary(int s1, int s2) { return s1+s2; }
22nd Jan 2021, 11:57 AM
Rohit
0
RKK Would you please explain why, int s1, int s2 and not int worker1.salary, and int worker2.salary?
22nd Jan 2021, 12:00 PM
Binh Tan Nguyen
Binh Tan Nguyen - avatar
0
Binh Tan Nguyen That's because function CalculateTotalSalary does not have any idea about the objects worker1 and worker2, so it's intuitive. Besides this the function CalculateTotalSalary don't care about anything except calculating the total salary or in more simple words it's only work is to get two value from the parameter and simply return the addition value of two salaries. public static int CalculateTotalSalary(int s1, int s2) { return s1+s2; } CalculateTotalSalary(worker1.salary, worker2.salary); The above code snippets assign the value of worker1.salary to s1 and similarly the value of worker2.salary to s2 respectively. We call this pass by value.
22nd Jan 2021, 1:55 PM
Rohit
0
RKK, Again, thank you so much! Makes perfect sense!
22nd Jan 2021, 2:26 PM
Binh Tan Nguyen
Binh Tan Nguyen - avatar
0
you're most welcome 😊
22nd Jan 2021, 2:33 PM
Rohit
0
public class Program { public static void main(String[] args) { //James Worker worker1 = new Worker(); worker1.name = "James"; worker1.salary = 200000; //Tom Worker worker2 = new Worker(); worker2.name = "Tom"; worker2.salary = 150000; System.out.println(CalculateTotalSalary(worker1.salary,worker2.salary)); } //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U public static int CalculateTotalSalary(int s1, int s2){ return s1+s2; } } class Worker{ public String name; public int salary; }
4th Sep 2021, 6:18 AM
Fazal Haroon
Fazal Haroon - avatar