javamethods

Compiler error found: void required: double on simple method java assignment


We just learned new methods and my professor wants us to call said method 3 times to calculate different tax amounts for a jewelry store. Here is a snippet of the instructions:

a. Use prompts and the Scanner to read in values for diamondCost, settingCost and numOrdered b. calculate the base cost by adding the values in diamondCost and settingCost c. calculate luxuryTax by calling the calcExtraCost method passing in the baseCost and luxuryRate as parameters. d. calculate stateTax by calling the calcExtraCost method passing in the baseCost and stateRate as parameters. e. calculate laborCost by calling the calcExtraCost method passing in the baseCost and laborRate as parameters.

I keep getting the compiler error stated above but for the life of me can't figure out why. As far as I know i've declared everything as a double. Here is my code so far:

import javax.swing.JOptionPane;
import java.util.Scanner;
import java.util.Locale;
import java.text.NumberFormat;

public class Project6
{

public static final double LUXURY_RATE = 0.2;
public static final double STATE_RATE = 0.10;
public static final double LABOR_RATE = 0.05;

   //This is the main method, taking user input and displaying values from calcExtraCost method
   public static void main(String[] args)
   {

   Scanner keyboard = new Scanner(System.in);
   NumberFormat dollar = NumberFormat.getCurrencyInstance(Locale.US);

   int numOrdered = 0;

   double diamondCost = 0.0;
   double settingCost = 0.0;
   double baseCost = 0.0;
   double totalCost = 0.0;
   double laborCost = 0.0;
   double stateTax = 0.0;
   double luxuryTax = 0.0;
   double finalAmountDue = 0.0;

   System.out.println("What is the cost of the diamond?");
   diamondCost = keyboard.nextDouble();

   System.out.println("What is the setting cost of the diamond?");
   settingCost = keyboard.nextDouble();

   System.out.println("How many would you like to order?");
   numOrdered = keyboard.nextInt();

   baseCost = (diamondCost + settingCost);

   luxuryTax = calcExtraCost(baseCost, LUXURY_RATE);


   }

   public static void calcExtraCost(double bseCost, double rate)
   {

   double total = (bseCost * rate);

   }



}   

I need to call this method to calculate the variables mentioned above, but keep getting said compiler error. I've checked all over and asked everyone I know but can't seem to find an answer as to what mistake i'm making. I'm brand new to java so I was hoping someone could help me understand what i'm doing incorrectly. So far i've only tried to calculate luxuryTax using the method calcExtraCost but I can't proceed because of the compiler error. As far as I can tell everything has been declared a double, so I don't know why it's returning as a void.


Solution

  • Your calcExtraCost should return a type of double. Since you are assigning it here: luxuryTax = calcExtraCost(baseCost, LUXURY_RATE);, void methods cannot be assigned to any other variable. Change this method:

    public static void calcExtraCost(double bseCost, double rate)
    {
    
      double total = (bseCost * rate);
    
    }
    

    To:

    public static double calcExtraCost(double bseCost, double rate)
    {
    
      double total = (bseCost * rate);
      return total;
    }