Having trouble with last part of JAVA programming assignment.

In summary: The others are also not consuming fuel. They are military ships, not cruise ships. And they are not all deployed at the same time. And they are not all refueled at the same time. But that's probably all outside the scope of your assignment.
  • #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:
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
  • #2
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.
 
  • #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:
public void deploy()
    {
     fuelOnboard/2;   
    }

It says that's it is not a statement.
 
  • #4
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.
 
  • #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.
 

Related to Having trouble with last part of JAVA programming assignment.

1. How do I troubleshoot errors in my JAVA code?

To troubleshoot errors in your JAVA code, you can start by carefully reviewing the error messages and checking for any syntax errors. You can also use a debugger tool to track the flow of your code and identify any logical errors. Additionally, seeking help from online resources or consulting with a more experienced programmer can also be helpful in resolving issues.

2. What are some common mistakes that may cause errors in JAVA programming?

Some common mistakes that may cause errors in JAVA programming include missing semicolons, typos in variable names, using incorrect data types, and forgetting to close brackets or parentheses. It is important to carefully check for these types of mistakes when troubleshooting errors in your code.

3. How can I improve my problem-solving skills in JAVA programming?

To improve your problem-solving skills in JAVA programming, it is important to practice regularly and familiarize yourself with the language's syntax and rules. You can also challenge yourself with more complex projects and try to solve them on your own before seeking help. Additionally, learning from others and seeking feedback on your code can also aid in improving your skills.

4. What resources are available for troubleshooting JAVA programming issues?

There are many resources available for troubleshooting JAVA programming issues, including online forums, tutorial websites, and official documentation from Oracle. You can also seek help from online communities or reach out to your peers or instructors for assistance.

5. How can I prevent future errors in my JAVA code?

To prevent future errors in your JAVA code, it is important to follow good coding practices, such as commenting your code, using descriptive variable names, and organizing your code into smaller, manageable sections. It is also helpful to test your code as you write it and regularly review and revise your code for any potential errors or bugs.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
Back
Top