+ 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); } }
15 ответов
+ 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;
}
}
+ 1
System.out.println( w.calculateSalary()) ; //just put like this, said it above...
You're welcome..
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..
0
w.calculateSalary(sal);
sal variable is not defined anywhere in the code.
0
jaya i have imported the scanner class just tell me how do i access the sal from entity class?
0
@Bahha how should i access from the entity class
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()) ;
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) ) ;
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..
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.
0
Jayakrishna🇮🇳 here's that problem!
0
Jayakrishna🇮🇳 how to print sal from main
0
Jayakrishna🇮🇳 ohk I got it
0
Jayakrishna🇮🇳 thank you for assisting me
0
Yes I have done this