I m getting an error at "w.calculateSalary(sal) in Driver class" in the following code in java? how should i print the "sal" val | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I m getting an error at "w.calculateSalary(sal) in Driver class" in the following code in java? how should i print the "sal" val

ENTITY CLASS: public class Worker { private int hw; private double payRate; public void setData(int hours,double pay) { hw=hours; payRate=pay; } public double calculateSalary(double sal) { if(hw>40) { sal=hw*payRate; } else { payRate*=2; } return sal; } } DRIVER CLASS: public class UseWorker { public static void main(String[] args) { Worker w=new Worker(); Scanner sc=new Scanner(System.in); System.out.println("Enter Hours Worked by User"); int hr=sc.nextInt(); System.out.println("Enter PAYRATE of User"); double pr=sc.nextDouble(); w.setData(hr, pr); w.calculateSalary(sal); } }

12th Oct 2020, 12:32 PM
Harsh Vyas
Harsh Vyas - avatar
15 Answers
+ 1
Harsh Vyas need just 1 more change.. Look at this method, correct way.. It works as requirement I think.. public double calculateSalary() { double sal; if(hw<=40) { sal=hw*payRate; } else { sal = hw*(payRate*2); //for power 2 write like this //sal = hw*(Math.pow(payRate,2)); } return sal; } }
12th Oct 2020, 1:13 PM
Jayakrishna 🇮🇳
+ 1
System.out.println( w.calculateSalary()) ; //just put like this, said it above... You're welcome..
12th Oct 2020, 1:33 PM
Jayakrishna 🇮🇳
0
What are trying with this code..? Sum of calculations are not using you anywhere... And not printing or using returned value.. In that method calling sal is not defined you.. Harsh Vyas You are not imported Scanner class..
12th Oct 2020, 12:40 PM
Jayakrishna 🇮🇳
0
w.calculateSalary(sal); sal variable is not defined anywhere in the code.
12th Oct 2020, 12:44 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
jaya i have imported the scanner class just tell me how do i access the sal from entity class?
12th Oct 2020, 12:49 PM
Harsh Vyas
Harsh Vyas - avatar
0
@Bahha how should i access from the entity class
12th Oct 2020, 12:50 PM
Harsh Vyas
Harsh Vyas - avatar
0
System.out.println( w.calculateSalary()) ; //just put like this.. And in entity class public double calculateSalary() { .. } Saying this to work your program only.. Because you are not added description of problem.. If it is not what you looking, just add description.. Harsh Vyas Edit: Add printing... Sop(w.calculateSalary()) ;
12th Oct 2020, 12:53 PM
Jayakrishna 🇮🇳
0
or you can just define a salary and pass it to the method. below, double pr =..... add double sal = yourvalue ; System.out.println(w.calculateSalary(sal) ) ;
12th Oct 2020, 12:57 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
Harsh Vyas wheer you posted..? Iam not find any? You can add directly here or tell where it is? Save code in playground and paste link here if you not getting required output after making needed changes which said here above..
12th Oct 2020, 1:01 PM
Jayakrishna 🇮🇳
0
Q). WRITE AN OOPS PROGRAM TO CREATE AN ENTITY CLASS CALLED "WORKER" WITH 2 DATA MEMBERS CALLED "hw" and "payRate" of type "int" and "double" respectively.provide the following method in your class: 1.setData()--Which excepts 2 args and initialize "hw" and "payRate" with them 2.calculateSalary()---which should calculate and return salary of the worker. rule for calculating salary is: 1.upto 40 hrs---sal=hw*payRate 2.above 40 hrs---sal=(payRate)^*2; Design a main class "UseWorker" which should accept "hours worked" and "payRate" from user, create and initialize Worker object and should Display the salary of the worker.
12th Oct 2020, 1:03 PM
Harsh Vyas
Harsh Vyas - avatar
0
Jayakrishna🇮🇳 here's that problem!
12th Oct 2020, 1:04 PM
Harsh Vyas
Harsh Vyas - avatar
0
Jayakrishna🇮🇳 how to print sal from main
12th Oct 2020, 1:25 PM
Harsh Vyas
Harsh Vyas - avatar
0
Jayakrishna🇮🇳 ohk I got it
12th Oct 2020, 1:30 PM
Harsh Vyas
Harsh Vyas - avatar
0
Jayakrishna🇮🇳 thank you for assisting me
12th Oct 2020, 1:30 PM
Harsh Vyas
Harsh Vyas - avatar
0
Yes I have done this
12th Oct 2020, 1:54 PM
Harsh Vyas
Harsh Vyas - avatar