- #1
vysero
- 134
- 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:
Fleet class:
and here is the Driver class which was given to us:
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();
}
}