Having trouble with last part of JAVA programming assignment.

Click For Summary

Discussion Overview

The discussion revolves around a JAVA programming assignment focused on creating a system to track fuel consumption for a fleet of ships. Participants are addressing issues related to the implementation of classes and methods, specifically the Ship and Fleet classes, and how to accurately calculate and display fuel consumption.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes the requirements for the Ship and Fleet classes, including methods for deploying and refueling ships, and printing a summary of fuel consumption.
  • Another participant points out that the current implementation does not track fuel consumption accurately, suggesting that additional data is needed to calculate total fuel used during deployments.
  • A participant expresses confusion about modifying the deploy method to divide fuelOnboard by two without assigning it to a variable, indicating a misunderstanding of how expressions and statements work in JAVA.
  • Another participant clarifies that the division operation is an expression and must be assigned to a variable to affect the state of fuelOnboard, highlighting a similar issue in another method.
  • One participant makes a side comment about the real-world fuel consumption of US Navy Carriers, noting that they are nuclear-powered and do not consume fuel in the manner described in the assignment.

Areas of Agreement / Disagreement

Participants generally agree on the need for corrections in the code, but there is no consensus on the best approach to implement the required functionality, particularly regarding how to track fuel consumption accurately.

Contextual Notes

Participants have identified limitations in the current implementation, such as the lack of a mechanism to track fuel consumption over multiple deployments and potential misunderstandings regarding variable assignments in JAVA.

vysero
Messages
134
Reaction score
0
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: "Carrier"), 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).

Create a BlueJ project called Outlab2, add a class called Driver and paste this code into it. Driver should not be modified.
Carefully review the Driver as it contains clues about the structure of the rest of the program.
Your Fleet class need 4 methods:
A constructor that takes 4 Ships as parameters.
A method called deploy that will deploy each ship in the fleet.
A method called refuel that will refuel each ship in the fleet.
A method called printSummary that will print, for each ship, the ship's name and the number of fuel units that ship has consumed.
From reviewing the Driver, you can see that you will need a Ship class as well. The constructor of this class will take the ship's name and fuel capacity as parameters.
Infer from the Problem Statement what instance variable and methods you need in the Ship class.


Here is what I have got done so far. The Ship method seems to be working and atm my printSummary method is only printing the 4 ship names and their current fuel. I realized that I need to get it to print the fuel they have used but I have no idea how:

Ship Class:
Code:
public class Ship
{ 
    private String name;
    private int fuelCapacity;
    private int fuelOnboard;

    /**
     * Constructor for objects of class ship
     */
    public Ship(String inName, int inFuelCapacity)
    {
        name = inName;
        fuelCapacity = inFuelCapacity;
        fuelOnboard = fuelCapacity;
    }
    
    public void refuled()
    {
     fuelCapacity = fuelOnboard;
    }
    
    public void deploy()
    {
     fuelCapacity = fuelOnboard/2;
    }
   
    public String getName()
    {
      return name;
    }
    
    public int getFuelBurned()
    {
     return fuelOnboard;
    }

Fleet class:
Code:
public class Fleet
{
   private Ship ship1;
   private Ship ship2;
   private Ship ship3;
   private Ship ship4;

    /**
     * Constructor for objects of class Fleet
     */
    public Fleet(Ship inShip1, Ship inShip2, Ship inShip3, Ship inShip4)
    {
        ship1 = inShip1;
        ship2 = inShip2;
        ship3 = inShip3;
        ship4 = inShip4;
    }

    public void deploy ()
    {
     ship1.deploy();
     ship2.deploy();
     ship3.deploy();
     ship4.deploy();
    }
    
    public void reFuel()
    {
     ship1.refuled();
     ship2.refuled();
     ship3.refuled();
     ship4.refuled();
    }
    
    public void printSummary()
    {
     System.out.println(ship1.getName()+ship1.getFuelBurned()+ship2.getName()+ship2.getFuelBurned()+ship3.getName()+ship3.getFuelBurned()+ship4.getName()+ship4.getFuelBurned());
    }

}

and here is the Driver class which was given to us:
Code:
/**
 * Driver for Outlab2.
 * 
 * @author yaw
 * @version 22 Jan 2014
 */
public class Driver
{
    public static void main(String[] args)
    {
        //Creating 4 instances of Ship
        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);
        
        //Creating instance of Fleet
        Fleet fleet1 = new Fleet(ship1, ship2, ship3, ship4);
        
        //Deploying the fleet twice
        fleet1.deploy();
        fleet1.deploy();
        
        //Refuel the fleet once
        fleet1.reFuel();
        
        //Print summary
        fleet1.printSummary();
    }
}
 
Physics news on Phys.org
Most of your solution looks OK, but in the Ship class, think again about what "Fuel capacity" and "Fuel on board" mean. Then, fix the bugs in your code!

You can't calculate the total fuel used just from the data that is stored in your program so far. You need to keep track of how much fuel is used each time the fleet is deployed.
 
Okay see I thought my Ship class was wrong. I just noticed that the deploy method only works once the way I have it now. Is there some way for me to in my deploy method simply say fuelOnboard/2; without making it equal some other variable? Like all I want it to do is divide it by two twice right? Well when I simply say:

Code:
public void deploy()
    {
     fuelOnboard/2;   
    }

It says that's it is not a statement.
 
Code:
fuelOnboard/2;
is an expression, not a statement. It calculates something, but you didn't say what to do with the answer.
... without making it equal some other variable?
you don't have to make it equal "some other" variable. You have probably already seen statements like
Code:
x = x + 2;
That means "work out the value of the right hand side" (i.e., the value of x+2) "and then assign it to the variable on the left hand side." In other words, increase the value of x by 2.

You found the bug in the deploy() method, but there is a similar bug in another method in the Ship class.
 
This has nothing to do with the problem, but I just have to say: None of the currently deployed US Navy Carriers consume fuel that way - because they are nuclear.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K