1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Having trouble with last part of JAVA programming assignment.

  1. Feb 9, 2014 #1
    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 (Text):


    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 (Text):

    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 (Text):

    /**
     * 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();
        }
    }
     
  2. jcsd
  3. Feb 9, 2014 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  4. Feb 9, 2014 #3
    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 (Text):
    public void deploy()
        {
         fuelOnboard/2;  
        }
    It says that's it is not a statement.
     
  5. Feb 9, 2014 #4

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    Code (Text):

    fuelOnboard/2;  
     
    is an expression, not a statement. It calculates something, but you didn't say what to do with the answer.
    you don't have to make it equal "some other" variable. You have probably already seen statements like
    Code (Text):

    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.
     
  6. Feb 10, 2014 #5
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Having trouble with last part of JAVA programming assignment.
  1. Java programming (Replies: 4)

  2. Java program (Replies: 3)

Loading...