Question

In: Computer Science

I am writing a jave program. I have seen the topic before but I want to...

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;
}
  
}

Solutions

Expert Solution

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:


Related Solutions

I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
I have to create an essay on this topic. I have a few sources but am...
I have to create an essay on this topic. I have a few sources but am looking for someone else's logical explanation as well to help me understand. Please don't just copy paste a textbook page here. I have a textbook. Thanks! Q: Current Monetary policy Impact on Money Market Instruments
I am writing a program that will work with two other files to add or subtract...
I am writing a program that will work with two other files to add or subtract fractions for as many fractions that user inputs. I need to overload the + and - and << and >> opperators for the assignment. The two files posted cannot be modified. Can someone correct the Fraction.ccp and Frction.h file that I am working on? I'm really close. // // useFraction.cpp // // DO NOT MODIFY THIS FILE // #include "Fraction.h" #include<iostream> using namespace std;...
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need...
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need to make sure I did it correctly. I'm not sure if my formula is correct. I appreciate any help, thank you! TASK: Write a program that will calculate the final balance in an investment account. The user will supply the initial deposit, the annual interest rate, and the number of years invested. Solution Description: Write a program that calculates the final balance in an...
I am writing a matlab program where I created a figure that has a few different...
I am writing a matlab program where I created a figure that has a few different elements to it and now I need to move that figure into a specific excel file into a specific set of boxes in the excel file. With numbers and text I have always used the xlswrite function for this in order to put data into specific boxes. How do I do the same with this figure? The figure I have is called like this:...
What am i doing wrong. I want for the program to run through then when it...
What am i doing wrong. I want for the program to run through then when it gets to "do you want to play again?enter yes or no" if they choose yes I want it to run again if they choose no then end. def play(): print("Welcome to the Game of Life!") print("A. Banker") print("B. Carpenter") print("C. Farmer") user_input = input("What is your occupation? ").upper() if user_input == "A": money = 100 elif user_input == "B": money = 70 elif user_input...
Program this scenario in C. Scenario: I am in highschool and i want to go see...
Program this scenario in C. Scenario: I am in highschool and i want to go see a movie. I have a certain amount of money to spend. Matinees are $8.50 and Evening Showings are $11.75. There is a G, PG, PG-13, and R movie at the theater. Ask the highschooler their age and how much money they have. With this info determine what showtime they can go see and what movies they are not allowed to see. Make it fun...
I am Writing a C-Program to read and write files. but none of my code is...
I am Writing a C-Program to read and write files. but none of my code is working like it should be. Please fix all code and supply output response. Please try to use existing code and code in comments. But if needed change any code that needs to be changed. Thank you in advance //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void rfile(void); void wfile(void); struct personnel {...
in Java, I am writing a program where you need to pick a color. The color...
in Java, I am writing a program where you need to pick a color. The color must be red, blue, or green and is a user input. If they put in a different value I need the program to stop running. So I essentially need to write code that says, if variableColor does not equal "red", "blue", or "green" then I need to print the statement "invalid response" and exit the program. How can I write this?
I am writing a research proposal on the below topic. Research question: How do Muslim women's...
I am writing a research proposal on the below topic. Research question: How do Muslim women's internalized views of religion, science, modesty, and reproductive healthcare inform the access of their American born female children to mammograms for breast cancer? Hypothesis: Muslim born women who moved to the United states strongly hold on to their values of their religion, their views on science and modesty, and their apprehension to reproductive healthcare which impacts their female American born children's access to quality...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT