0

Access Modifiers Total Salaries

Could some one help me please? I am supposed to fix the code to correctly modify the access to the attributes and complete the method to calculate and return the sum of the total salaries. I keep getting illegal start of expression. This is what I got so far: https://code.sololearn.com/c1ZqS2QujBaG/?ref=app

2nd Feb 2021, 9:36 PM
Guy Robbins
4 Answers
+ 6
1. Do not use access modifier inside a method 2. Name and salary variables are marked as private 3. In the CalculateTotalSalary method, you doesn't add any parameters solution: 1. remove all private keyword inside main 2. change name and salary access-modifier to public (replace "private name" and "private salary" to "public name" and "public salary") 3. add two parameters in CalculateTotalSalary method, for example. "CalculateTotalSalary(int a, int b) { return a+b; }"
2nd Feb 2021, 10:01 PM
Yousef Ady
Yousef Ady - avatar
+ 1
Thank you so much Yousef Ady. Thanks to you I see the simple errors I easily overlooked. I am new to coding so I appreciate your informative feedback
2nd Feb 2021, 10:06 PM
Guy Robbins
+ 1
If it is necessary to keep attributes private you can use a private constructor and public method to set name and salary with new object returned for that class and later access values using get method. https://code.sololearn.com/c7V0x2Om4r0x/?ref=app
2nd Feb 2021, 10:09 PM
Abhay
Abhay - avatar
0
// Created by Guy Robbins public class Program { public static void main(String[] args) { // James Worker worker1 = new Worker(); worker1.setName("James"); System.out.println("name: " + worker1.getName()); worker1.setSalary(200000); System.out.println("salary: " + worker1.getSalary()); // Tom Worker worker2 = new Worker(); worker2.setName("Tom"); System.out.println("name: " + worker2.getName()); worker2.setSalary(150000); System.out.println("salary: " + worker2.getSalary()); int sum = worker1.getSalary() + worker2.getSalary(); System.out.println("sum salary: " + sum); } } public class Worker { private String name; public String getName() { return name; } public void setName(String a) { this.name = a; } private int salary; public int getSalary() { return salary; } public void setSalary(int b) { this.salary = b; } }
3rd Apr 2021, 6:38 AM
Dicky Galuh Kurniawan
Dicky Galuh Kurniawan - avatar