Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Having trouble with communicating between methods, in need of assistance.

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Having trouble with communicating between methods, in need of assistance.

    Okay so here's the assignment:

    Problem Statement
    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).

    Assignment
    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.

    Alright so what I have done so far:
    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;
        }
     
        public int refuled()
        {
         fuelOnboard = fuelCapacity;
        }
     
        public int deploy()
        {
         fuelOnboard = fuelOnboard/2;
        }
     
        public String getName()
        {
          return name;
        }
     
        public int getFuelCapacity()
        {
         return fuelCapacity;
        }
     
    }

    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 ()
        {
     
     
        }
     
        public void reFuel()
        {
     
        }
     
        public void printSummary()
        {
     
        }
     
    }

    and the Driver (which was provided):

    /**
     * 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();
        }
    }



    As you can see I am having trouble with communicating between my Fleet class and my Ship class. Even if I am able to use my deploy method in my Ship class I would still be wondering how to use it on all the ships at once. Also, as you can see I made ship1, ship2... so forth of type Ship and I wasn't sure if this was correct or not.

    Not sure if my Ship class is correct either. The reason I say that is when I look at the driver. The driver makes me think I should only have two return statements in my Ship class while the deploy method from Ship seems like it should be in Fleet. But the problem statement clearly states in my opinion that Ship should have a deploy and refuel method along with Fleet.

    I just need some of your guy's advice on how to interpret the problem statement correctly. Is my ship class correct? If so, then what am I missing about calling the Ship method from the Fleet class?

    --- Update ---

    Will simply calling all four of them work like:

    ship1.deploy();
    ship2.deploy();
    ship3.deploy();
    ship4.deploy();


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Having trouble with communicating between methods, in need of assistance.

    As you can see I am having trouble with communicating between my Fleet class and my Ship class.
    How can we see that?

    These lines from the assignment description tell you what methods are required and what they do:
    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
    Your Fleet class need 4 methods:
    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.
    Your update is on the right track, as long as those four lines are in the Fleet.deploy() method. I think you're getting it. Keep going. (I don't think you have a way to get the amount of fuel consumed by each ship yet.)

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with communicating between methods, in need of assistance.

    Right so I think you're referring to the printSummary method. Here's what I have so far:

     public static void printSummary()
        {
         System.out.println(Ship.getName()+Ship.getFuelCapacity());
        }

    Unfortunately, this is only printing ship 4 the destroyer and its fuel capacity. Also, its not reporting that the fuel capacity was lowered any.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Having trouble with communicating between methods, in need of assistance.

    Yes, that's what I was referring to, but specifically that the program doesn't report "the number of fuel units that ship has consumed" as required by the assignment. The current code only prints out one ship because you don't call it on all 4 as you should:

    System.out.println(ship1.getName()+ship1.getFuelCa pacity());
    System.out.println(ship2.getName()+ship2.getFuelCa pacity());
    // etc. . .

    That code is still not correct in that it doesn't report what the assignment asks for, but it'll get you showing more than one ship. Remember, Ship is the class, and ship1, ship2, etc. are Ship objects or instances of the class Ship.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with communicating between methods, in need of assistance.

    Hmm still kind of stuck. Not sure what you're getting at exactly, do I need to create a method in class Ship that tracks the fuel capacity and name of the ship and then call that from the fleet class? Do I even need a return statement for my fuel capacity and name in the ship class at all?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Having trouble with communicating between methods, in need of assistance.

    Essentially, you got it. Each Ship object reports its fuel state or fuel used through a method called on that object. The statement to get the units of fuel used by ship1 could be something like:

    ship1.getFuelUnitsUsed().

    I'll leave the accounting up to you.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with communicating between methods, in need of assistance.

    I get what you're saying, I think. I need a method that will give me the amount of units of fuel each ship has used and its name right? So not just the onBoard amount. For instance, if a ship has 100 units of fuel and its called twice then refilled it will have used 75 units. However, I feel a bit clueless as to how to handle the "accounting" part...

    --- Update ---

    Do I need to make a method that mimics the calculations happening in the deploy method? Or am I suppose to actually track what happens to the fuel as the ship1.deploy is called from the Fleet method? Like I have no idea how I would do that... clueless.

    --- Update ---

    Sorry for posting again but maybe this is what you were saying. Like if I have a method that mimics the calculations:

    public int getFuelBurned()
    {
    x = fuelCapacity/2;
    y = x/2;
    return x+y;
    }

    Is that right? Or is there like a track.deploy call im unaware of?

  8. #8
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with communicating between methods, in need of assistance.

    Turns out my deploy method in my ship class is wrong as well. I thought I had it working but when i double checked I noticed I was wrong. So there has to be a way to make the deploy method do nothing but divide the current fuelOnboard by 2 whenever its called. However I cant just say:

    public void deploy()
        {
         fuelOnboard/2;
        }

    because this is not a statement... im so lost now...

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Having trouble with communicating between methods, in need of assistance.

    You need to reread the assignment and write the methods to do what the assignmment specifies. For example, the assignment specifies that a carrier's fuel capacity is 125 units. You have the fuel capacities of the other ships listed later in your code. Then, each time the Fleet deploys, the ship consumes half its fuel onboard IN UNITS. The Fleet deploy() method will call each of the ship object's deploy() method, and the ship object's deploy method will cause the ship to "consume half of the fuel it has onboard." So the Ship's deploy method looks something like:
    public void deploy()
    {
        fuel = fuel / 2;
    }
    And then to keep track of the fueld consumed, there should be a variable, unitsOfFuelConsumed (or similar) that is modified each time the ship deploys, so add it to the deploy() method:
    public void deploy()
    {
        unitsOfFuelConsumed += fuel / 2;
        fuel = fuel / 2;
    }

Similar Threads

  1. Having trouble understanding recursive methods??
    By orbin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 17th, 2012, 01:08 AM
  2. Having trouble writing the methods for this program
    By michael305rodri in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 1st, 2012, 11:31 PM
  3. Communicating with a Telnet server
    By EoD in forum Java Networking
    Replies: 5
    Last Post: October 11th, 2012, 06:13 PM
  4. Replies: 17
    Last Post: July 27th, 2012, 12:52 AM
  5. Basic OOP having trouble determining how to handle methods
    By Nikkon2131 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 21st, 2011, 06:15 PM