Member Avatar for JamieVern

Hello, All. I'm a beginner with Java writing and would greatly appreciate a hand with the problem below:

A student has decided to fly his remote controlled airplane. The airplane has about 12 minutes of flight without any extra weight. For every 1/2 of a pound added, the lifetime of the airplane is reduced by one minute. Additionally, the following moves drain the battery life accordingly:

Maneuver Battery Life Reduction (minutes)

Take-off 0.2
Loop 0.3
Land 0.2
Fly 100 feet 0.3

If the battery life reaches zero (or less than zero), the means the plane will crash. This means the student must land the airplane to recharge. Upon recharging, the battery is reset to the maximum lifetime (minus the weight).

You are to write two classes. The first class, is called RCPLANE. In this class you must have the following:

  1. A constructor which requires the user to input the weight of the load used. If the weight of the load is greater than 2 pounds, the plane will not take off. In this case, a message should indicate the weight is too heavy, and the program exits.
  2. A method called TakeOff, which “instructs” the plane to take off. If the plane is already in the air, then a message should indicate the plane is already in the air, and no changes to battery life is needed.
  3. A method called Land, which “instructs” the plane to land. If the plane is already on the ground, then a message should indicate the plane is already on the ground, and no changes to battery life is needed.
  4. A method called Loop, which “instructs” the plane to loop in the air. The plane can only do this if it is on the ground. In this case, a message should indicate the plane is on the ground, and no changes to battery life is needed.
  5. A method called Fly, which “instructs” the plane to fly. The user must input the distance to fly. The plane can only do this if it is in the air. If the plane is already in the air, then a message should indicate the plane is already in the air, and no changes to battery life is needed.
  6. A method called GetDistance, which returns the total distance travelled
  7. A method called GetBatteryLife, which returns the total battery life
  8. A method called Recharge, which recharges the battery and resets the distance travelled to zero

The second class, which is a main class, is called RCPLANETESTER. In this class, you must construct the airplane, and give it the following sequential directions:

  1. Take off
  2. Fly 200 feet
  3. Loop
  4. Take off
  5. Fly 300 feet
  6. Loop
  7. (Display Battery Life)
  8. Fly 300 feet
  9. Loop
  10. Fly 250 Feet
  11. Loop
  12. (Display Battery Life)
  13. (Display Total Distance)
  14. Recharge
  15. Land
  16. Fly 200 feet
  17. Loop
  18. Recharge
  19. Takeoff
  20. Fly 1000 feet
  21. (Display Battery Life)
  22. (Display Total Distance)
  23. Fly 10000 feet

(Note that you may decide to write the methods in any order as you can handle them in the questions, but the orders suggested above are the best if you decide to follow those. Note however that all methods must be written in order to get the full points. Recall that partial credits are also obtainable for your best efforts)

Recommended Answers

All 10 Replies

OK, the description is a bit intimidating, but it's also very detailed. You just need to follow the instructions and it should be fine. Given that, can you tell us what you are having trouble with? It would also help if you could post your existing code, so we can see how far along you have gotten.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

Member Avatar for JamieVern

Sorry, guys. This is my first time posting.

Here is RCPLANE:

package rcplane;
import java.util.Scanner;

public class RCPLANE 
{
    double batterylife;
    int weight;
    int distance;
    Scanner input = new Scanner(System.in);

    public void RCPLANE1(double initialbatterylife)
    {
        System.out.print("Please enter the weight of the plane.  The weight must be less than two pounds.");
        weight = input.nextInt();
        batterylife = 12;

        if (weight > 2)
        {
            System.out.print("The weight entered is greater than two pounds");
            System.exit(0);
        }
        else
        {
            batterylife = batterylife - ((weight - 1) * .5);
        }    
    }

     public double TakeOff()
     {
         batterylife = batterylife - 0.2;
         if (batterylife <= 0)
        {
            System.out.println("The plane has crashed due to low battery life");
        }
         return batterylife;

     }        

     public double Land()
     {
         batterylife = batterylife - 0.2;
         if (batterylife <= 0)
        {
            System.out.println("The plane has crashed due to low battery life");
        }
         return batterylife;
     }

     public double Loop()
     {
         batterylife = batterylife - 0.3;
         if (batterylife <= 0)
         {
            System.out.println("The plane has crashed due to low battery life");
         }
         return batterylife;
     }

     public double Fly(int distance)
     {
         int flydistance;
         flydistance = input.nextInt();
         batterylife = batterylife - (.003 * flydistance);
         if (batterylife <= 0)
         {
            System.out.println("The plane has crashed due to low battery life");
         }
         return batterylife;
     }

     public void GetDistance()
     {
         distance = 0;
         distance = distance + flydistance;
         System.out.print("Total Distance:  " + distance);
     }

     public void GetBatteryLife()
     {
         System.out.print("Batterylife:  " + batterylife);
     }

     public void Recharge()
     {
         batterylife = 12;
         distance = 0;
     }
}
Member Avatar for JamieVern

Here is RCPLANETESTER:

public class RCPLANETESTER 
{

    public static void main(String[] args) 
    {
           RCPLANE rcplane = new RCPLANE();
           rcplane.TakeOff();
           rcplane.Fly(200);
           rcplane.Loop();

           rcplane.TakeOff();
           rcplane.Fly(300);
           rcplane.Loop();
           rcplane.GetBatteryLife();
           rcplane.Fly(300);
           rcplane.Loop();
           rcplane.Fly(250);
           rcplane.Loop();
           rcplane.GetBatteryLife();
           rcplane.GetDistance();
           rcplane.Recharge();
           rcplane.Land();
           rcplane.Fly(200);
           rcplane.Loop();
           rcplane.Recharge();
           rcplane.TakeOff();
           rcplane.Fly(1000);
           rcplane.GetBatteryLife();
           rcplane.GetDistance();
           rcplane.Fly(10000);

}
}    
Member Avatar for JamieVern

The issues I'm having are just getting it running. I think I did something wrong with making RCPLANETESTER the main class. It can't reference RCPLANE (line 6). I'm not sure if all the methods are set up and referenced correctly.

What is happening is that you aren't defining a valid constructor; you have a method static void RCPLANE1(double initialbatterylife), but this only looks like a c'tor. First off, the name is misspelled, with a '1' after the class name. Second, c'tors do not have a return type, not even void.

Member Avatar for JamieVern

I've corrected that, but still get an error at Line 74 (RCPLANE) and Line 6 (RCPLANETESTER)

One class is in the package rcplane, the other is in no package. Either put both classes in the same package, or import rcplane.RCPLANE in your tester program.

OK, the error you are getting is that flydistance is not declared in the method GetDistance(), correct? And if you look, you'll that this is indeed the case. However, what I suspect really has happened is that part of the code for Fly() was transposed to GetDistance() by mistake, possibly while editing the code. I think what you intended was more along these lines:

     public double Fly(int distance)
     {
         int flydistance;
         flydistance = input.nextInt();
         distance += flydistance;
         batterylife -= (.003 * flydistance);
         // note the use of the 'add-accumulate' and 'subtract-accumulate'
         // operators, they can prove quite useful.

         if (batterylife <= 0)
         {
            System.out.println("The plane has crashed due to low battery life");
         }
         return batterylife;
     }

     public void GetDistance()
     {
         System.out.print("Total Distance:  " + distance);
     }

Note that I also removed the line

         distance = 0;

from GetDistance(); you will want to move it to the beginning of the constructor. As it is now, not only are you resetting distance each time you poll it, before you have displayed it, you never initialize it in the first place, so the values between when you call the c'tor and when you call GetDistance() will be invalid.

I'd like to make a few suggestions, as well. First, you might find it helpful to put the amounts of charge used by the given maneuvers into named constants; this should make it more readable and easier to change later if need be, and is a good habit to get into. You could even collect them together into an enumeration class, to make their purpose clearer:

enum Maneuver 
{
    TAKEOFF(-0.2), 
    FLY (-0.03), 
    LOOP(-0.3), 
    LAND(-0.2), 
    RECHARGE(12.0);  

    private final double deltaCharge;

    Maneuver(double dc)
    {
        this.deltaCharge = dc;
    }

    double charge()
    {
        return this.deltaCharge;
    }
}

While using these would be a bit verbose, it would make explicit a lot of things that aren't necessarily obvious.

You might also want to write a utility method to wrap up all the cases where you are resetting and testing the battery life. This would reduce the repetition, and make it eaiser both to read and to debug.

    private double SetCharge(Maneuver action, double repetitions)
    {
        batterylife += action.charge() * repetitions;

        if (batterylife <= 0)
        {
            System.out.println("The plane has crashed due to low battery life");
        }
         return batterylife;
    }

With this, the Fly() method becomes:

     public double Fly(int distance)
     {
         int flydistance;
         flydistance = input.nextInt();
         distance += flydistance;
         return SetCharge(Maneuver.FLY, flydistance);
     }

Oops, in that last part, repetitions should be an int, not a double.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.