What is wrong with the three lines of code? this is java on BlueJ Constructor fo
ID: 3902285 • Letter: W
Question
What is wrong with the three lines of code? this is java on BlueJ
Constructor for objects of class 03A3 public Q3A3 (int nl, int n2, int n3) //declaring variables int weekday-n1;//number of hours an employee has worked during the weekdays int overtime-n2;//the number of hours the employee has worked overtime during the weekdays int weekend-n3;// number of hours the employee has worked during the weekends //Calculating weekly salary int salary (weekday*18)+(overtime 15)+(weekend 20); / /Output information System.out.println("-- System.out.println(tNumber of regular hours worked on weekdays:ItitIt" +weekday ); System.out.print1n("( Number of overtime hours worked on weekdays:ttt. + overtime +·t).)i System.out.printin(tNumber of hours worked on weekends Ititltit" weekend"t") System.out.println("(tTotal Two Weeks Salary is:Itltitit" +salary t") System.out.println("(Explanation / Answer
Variables are generally declared outside the constructors so that all constructors(if there are more than 1), as well as the member functions, can access. In this given scenario, you have declared variables inside the constructor, which means they are already defined and then they are declared again in the constructor which might give an error.
The correct way for this is:
int weekday;
int overtime;
int weekend;
public Q3A3(int n1, int n2, int n3)
{
weekday = n1;
overtime = n2;
weekend = n3;
}
All the three data members(variables) are declared in the class definition and they are assigned values in the constructor.
Everything else looks fine in your code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.