Home Ask Login Register

Developers Planet

Your answer is one click away!

Liz Rehbein February 2016

Missing Return Statement Error Basic Program

Im writing a small program for a class at school and have run into the "missing return statement error." Ive tried googling the error to see if I can find a generic answer that might help me but nothing Ive done to my code has been able to get rid of this error so that my program can compile. For reference, here is the assignment for the program I am trying to write:

You have been commissioned by the US Navy to develop a system for tracking the amount of fuel consumed by fleets of ships. Each ship has a name (ex: "USS Montana"), fuel capacity (the maximum amount of fuel the ship can carry), and amount of fuel currently onboard. In this problem, fuel is measured in "units" and the capacity of each ship is an integer number (ex: The carrier's capacity is 125 fuel units). Each fleet has exactly four ships in it. When a fleet is deployed, each ship in the fleet is deployed. When a ship is deployed, it consumes half of the fuel it has onboard. When a fleet is refueled, each ship in the fleet is refueled. When a ship is refueled, it is totally filled up (its onboard amount equals its capacity)

And here is my code:

/**
 * Navy Ship Descriptions
 * 
 * @author Elizabeth Rehbein
 * @version 01/28/16
 */
public class Ship
{

    // instance variables 
    private String name;        //ship name
    private double fuelCapacity;       //ship fuel capacity
    private double fuelCurrent;  //the ship's current fuel on ship after deployments
    private double fuelConsumed;   //the amount of fuel a ship has consumed after deployments


    //constructor
    public Ship(String startName, double startFuelCapacity, double startFuelCurrent, double startFuelConsumed)
    {
        // initialise instance variables
        name=startName;
        fuelCapacity=startFuelCapacity;
        fuelCurrent=startFuelCurrent;
        fuelConsumed=startFuelConsumed;
    }

   //get methods
    public String getName()
    {
        return name;    //returns the name        

Answers


YoungHobbit February 2016

return statement missing here:

public double reFuel()
//refueling each ship returns it to its full fuel capacity
{
    fuelCurrent = fuelCapacity;
    // add return fuelCurrent here;
}

Missing return statement here as well : by looking at the content, this does not require any return statement. make it void.

public double printSummary()
//prints out fuelConsumed for each ship
{
    System.out.println(ship1.getName() +" 's total fuel consumption is " + ship1.getFuelConsumed());
    System.out.println(ship2.getName() +" 's total fuel consumption is " + ship2.getFuelConsumed());
    System.out.println(ship3.getName() +" 's total fuel consumption is " + ship3.getFuelConsumed());
    System.out.println(ship3.getName() +" 's total fuel consumption is " + ship4.getFuelConsumed());
}

Also this definition for constructor is having four parameters but you are not passing all the argument in the object constructor in the main() method.

public Ship(String startName, double startFuelCapacity, double startFuelCurrent, double startFuelConsumed)
{
    // initialise instance variables
    name=startName;
    fuelCapacity=startFuelCapacity;
    fuelCurrent=startFuelCurrent;
    fuelConsumed=startFuelConsumed;
}

    // more errors here: pass all the four arguments.
    Ship ship1 = new Ship("Carrier", 150);
    Ship ship2 = new Ship("Anti-Submarine", 35);
    Ship ship3 = new Ship("Patrol", 22);
    Ship ship4 = new Ship("Destroyer", 83);


cuoka February 2016

This method should return a double. If you dont want to return anything, replace double to void.

public double reFuel()
//refueling each ship returns it to its full fuel capacity
{
    fuelCurrent = fuelCapacity;
}   


seahawk February 2016

The culprit is

public double printSummary()
//prints out fuelConsumed for each ship
{
System.out.println(ship1.getName() +" 's total fuel consumption is " + ship1.getFuelConsumed());
System.out.println(ship2.getName() +" 's total fuel consumption is " + ship2.getFuelConsumed());
System.out.println(ship3.getName() +" 's total fuel consumption is " + ship3.getFuelConsumed());
System.out.println(ship3.getName() +" 's total fuel consumption is " + ship4.getFuelConsumed()); }

 public double reFuel()
//refueling each ship returns it to its full fuel capacity
{
    fuelCurrent = fuelCapacity;
}   

Return statement in this function is missing.

Either change the return value to void or return a double.


Mohammad Eghlima February 2016

here:

public double reFuel()
    //refueling each ship returns it to its full fuel capacity
    {
        fuelCurrent = fuelCapacity;
    }

and here:

//prints out the fuel consumed for each ship in the fleet
    public double printSummary()
    //prints out fuelConsumed for each ship
    {
    System.out.println(ship1.getName() +" 's total fuel consumption is " + ship1.getFuelConsumed());
    System.out.println(ship2.getName() +" 's total fuel consumption is " + ship2.getFuelConsumed());
    System.out.println(ship3.getName() +" 's total fuel consumption is " + ship3.getFuelConsumed());
    System.out.println(ship3.getName() +" 's total fuel consumption is " + ship4.getFuelConsumed());
   }

You have two methods, you have said that they will return double but in their body there is no return A_DOUBE. fix them with adding return statement or just make them void depends on your problem.

Post Status

Asked in February 2016
Viewed 2,006 times
Voted 14
Answered 4 times

Search




Leave an answer


Quote of the day: live life