In: Computer Science
I am writing a jave program. I have seen the topic before but I
want to do it this way. And I got an error says:
BonusAndDayOffRew.java:14: error: variable monthlySales might not
have been initialized
getSales(monthlySales);
^
Here is the description:A retail company assigns a $5000 store
bonus if monthly sales are $100,000 or more. Additionally, if their
sales exceed 125% or more of their monthly goal of $90,000, then
all employees will receive a message stating that they will get a
day off.
Here is my Java code:
import java.util.Scanner;
public class BonusAndDayOffRew
{
public static void Main(String[] args)
{
//Declare important variables
double monthlySales;
getSales(monthlySales);
bonusAward(monthlySales);
dayAward(monthlySales);
}
//Module 1: Ask user to input monthlysales
public static double getSales(double monthlySales)
{
Scanner keyboard= new Scanner(System.in);
System.out.println("Enter your monthly sales: ");
monthlySales= keyboard.nextDouble();
System.out.println("The monthly sales is :"+ monthlySales);
return monthlySales;
}
//Module 2: this module will determine if a bonus is awarded
public static double bonusAward(double monthlySales)
{
if (monthlySales>= 100000)
System.out.println("Congratulation, You get a bonus of
$5,000!!!");
return monthlySales;
}
//Module 3: determine if employees get a day off
public static double dayAward(double monthlySales)
{
if (monthlySales>=112500)
System.out.println("Congratulation, You get a day off!!!");
return monthlySales;
}
}
In java the variables should be initialized before using them in
any statement.
The error clearly indicates that monthlySales is not
initialized
initialize it with some default value such as 0.0
By making this change, the error will go away.
There is another problem in the code. The value inputted in getSales() is not updated in the main method. As method is returning the inputted value, it can be received in the monthlySales variable to get reflected in main method.
CODE:
import java.util.Scanner;
public class BonusAndDayOffRew
{
public static void main (String[]args) {
// Declare important variables
// In java the variables should be initialized before using them in
any statement.
// The error clearly indicates that monthlySales is not
initialized
// initialize it with some default value such as 0.0
double monthlySales = 0.0;
// double is not an class, so monthlySales is not an object.
// in java, objects are passed as references means update made in
them gets reflected in actual variable.
// but when classes are not used they are treated as values, means
modification not updated in the actual variable.
// as functions are returning the value, it should be received in
some variable.
monthlySales = getSales (monthlySales);
monthlySales = bonusAward (monthlySales);
monthlySales = dayAward (monthlySales);
}
//Module 1: Ask user to input monthlysales
public static double getSales (double monthlySales) {
Scanner keyboard = new Scanner (System.in);
System.out.println ("Enter your monthly sales: ");
monthlySales = keyboard.nextDouble ();
System.out.println ("The monthly sales is :" + monthlySales);
return monthlySales;
}
//Module 2: this module will determine if a bonus is awarded
public static double bonusAward (double monthlySales) {
if (monthlySales >= 100000)
System.out.println ("Congratulation, You get a bonus of
$5,000!!!");
return monthlySales;
}
//Module 3: determine if employees get a day off
public static double dayAward (double monthlySales) {
if (monthlySales >= 112500)
System.out.println ("Congratulation, You get a day off!!!");
return monthlySales;
}
}
OUTPUT: