Need help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Need help

  1. #1
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108

    Angry Need help

    I am writing a program which simulates boats moving on a lake.
    There is 4 types of boats:

    Small Motor Boat
    Small Sail Boat
    Large Sail Boat
    Large Motor Boat


    #1 Small moto boats stop for all boats
    #2 Small sail boats stop or turn for large large sailboats and large motorboats
    #3 Large sailboats stop or turn for large motorboats
    #4 Large Motorboats do not stop for any boats

    When a boat stops it is supposed to start moving again:

    I wrote a method that enforces theese rules, but it does not work correctly. Can anyone tell me what I am doing wrong?

    Here is my code:

    Code:
    import java.util.Random;
    class boatprogram7 {
    
      public static void main(String[] args) {
    
        Boat boats[] = { new SmallSailBoat(0.6, 0.3, 1.1, 10.4, 35, false, 65),
            new SmallMotorBoat(2.6, 5.8, 2.2, 2.1, 10, 12),
            new LargeMotorBoat(2.1, 5.5, 2.2, 2.1, 2, 11),
            new LargeSailBoat(3.2, 2.7, 7.2, -4.9, 32, true, 122)};
    
        double time = 0;
        double deltatime = 0.5;
    
        while (time <= 10.0) {
          System.out.println();
          System.out.println("Time: " + time);
          System.out.println();
          for (int i = 0; i < boats.length; i++) {
            boats[i].calculateNearest(boats);
            Boat nearest = boats[i].getNearest();
            System.out.println("This is a "+boats[i].getType()+" at " +boats[i].toString());
            System.out.println("Nearest Boat is a " + nearest.getType()+" at " + nearest.toString());
            System.out.println("The distance between is: " + boats[i].distance(nearest));
            boats[i].navigation();
          }
    
          time += deltatime;
    
          for (int i = 0; i < boats.length; i++) {
            boats[i].moveboat(deltatime);
          }
        }
      }
    }
    
    =============================================
    abstract class Boat {
      private double x = 0;
      private double y = 0;
      private double vx = 0;
      private double vy = 0;
      int maxspeed =0;
      boolean moving = true;
      int type;
      int r;
      Boat nearestboat;
      public Boat(double x, double y, double vx, double vy) {
        this.x = x;
        this.y = y;
        this.vx = vx;
        this.vy = vy;
      }
      public double round(double x){
          double z =((double)(Math.round(x*100)))/100;
          return z;
      }
      public void moveboat(double deltatime) {
        x += deltatime * vx;
        y += deltatime * vy;
      }
    
      public void stop() {
        vy = 0;
        vx = 0;
      }
    
    public void navigation(){
          Random generator = new Random();
          if(moving){
              if(distance(nearestboat)<=30){
                  switch (type){
                      case 1:{
                          stop();
                          System.out.println(getType()+" has stopped");
                          System.out.println();
                          moving = false;
                      }
                      case 2:{
                          if(nearestboat.type==3|nearestboat.type==4){   
                          this.r = generator.nextInt(11);
                          if(r<=5){
                              stop();
                              System.out.println(getType()+" has stopped");
                              System.out.println();
                              moving = false;
                          }
                          else if(r>=6){
                              vx = generator.nextInt(this.maxspeed);
                              vy = generator.nextInt(this.maxspeed);
                              System.out.println(getType()+ "changed direction");
                              System.out.println();
                          }
                            }
                          else if(nearestboat.type==2){
                              System.out.println();
                          }
                          }
                      case 3:{
                          if(nearestboat.type==4){
                              this.r = generator.nextInt(11);
                          if(r<=5){
                              stop();
                              System.out.println(getType()+" has stopped");
                              System.out.println();
                              moving = false;
                          }
                          else if(r>=6){
                              vx = generator.nextInt(this.maxspeed);
                              vy = generator.nextInt(this.maxspeed);
                              System.out.println(getType()+ "changed direction");
                              System.out.println();
                          }
                              
                          }
                      }
                      }
                  }
              else{
                  System.out.println();
              }
              }
          else{
              vx = generator.nextInt(this.maxspeed);
              vy = generator.nextInt(this.maxspeed);
              moving = true;
              System.out.println(getType()+" started moving");
              System.out.println();
          }
    }
    
    
      public double distance(Boat otherboat) {
        double z = Math.sqrt(Math.pow((x - otherboat.x), 2)
            + Math.pow((y - otherboat.y), 2));
        return round(z);
      }
    
      public String toString() {                                        //Return boat location
        return ("x = " + round(x) + " , y = " + round(y));
      }
    
      public abstract String getType();                                 
      
      public void calculateNearest(Boat boats[]) {                      //Process array of boats to
        double nearestdistance = Double.MAX_VALUE;                      //calculate the nearest boat
    
        for (int j = 0; j < boats.length; j++) {
          if (boats[j] != this) {
            double d = distance(boats[j]);
            if (d < nearestdistance) {
              nearestdistance = d;
              nearestboat = boats[j];
            }
          }
        }
      }
      
      public Boat getNearest() {                                         //Return the nearest boat
        return nearestboat;                                              //requires calculateNearest to
      }                                                                  //be called before
    }
    
    ==========================================
    class SmallSailBoat extends Boat {
    int mastheight = 0;
    boolean sailsfurled = false;
    double saildirection = 0;
      
    public String getType() {
        this.type = 2;
        return "Small Sail Boat";
      }
    
      public SmallSailBoat(double x, double y, double vx, double vy, int mastheight,
          boolean sailsfurled, double saildirection) {
        super(x, y, vx, vy);
        this.mastheight = mastheight;
        this.sailsfurled = sailsfurled;
        this.saildirection = saildirection;
        this.maxspeed = 12;
      }
    }
    
    ==========================================
    class LargeSailBoat extends SmallSailBoat {
    
        public String getType() {
        this.type = 3;
        return "Large Sail Boat";
      }
    
      public LargeSailBoat(double x, double y, double vx, double vy, int mastheight,
          boolean sailsfurled, double saildirection) {
        super(x, y, vx, vy,mastheight, sailsfurled, saildirection);
        this.maxspeed = 12;
      }
    }
    
    ==========================================
    class SmallMotorBoat extends Boat {
      private double fuellevel = 0;
      private double fueltanksize = 0;
     
      public String getType() {
        this.type = 1;
        return "Small Motor Boat";
      }
    
      public SmallMotorBoat(double x, double y, double vx, double vy, double fuellevel, double fueltanksize) {
        super(x, y, vx, vy);
        this.fuellevel = fuellevel;
        this.fueltanksize = fueltanksize;
        this.maxspeed = 20;
      }
    }
    
    ==========================================
    class LargeMotorBoat extends SmallMotorBoat {
      
        public String getType() {
        this.type = 4;
        return "Large Motor Boat";
      }
    
      public LargeMotorBoat(double x, double y, double vx, double vy, double fuellevel, double fueltanksize) {
        super(x, y, vx, vy,fuellevel,fueltanksize);
        this.maxspeed = 10;
      }
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    You have this nice hierarchy...why not do something object-oriented? This is java!

    you could define an abstract method on Boat, perhaps something like:

    Code:
    public abstract void reactTo(LargeMotorBoat other);
    
    public abstract void reactTo(SmallMotorBoat other);
    
    public abstract void reactTo(LargeSailBoat other);
    
    public abstract void reactTo(SmallSailBoat other);
    Then, each of the classes just implement these methods (which will be 1-liners), to do what is supposed to be done when "this" has a near-collision with other. When you are in reactTo(LargeMotorBoat), for example, and "this" is a SmallSailBoat, then you do what is appropriate for that instance. Then in navigate(), instead of this huge switch statement, you just say:

    Code:
    if(distance(nearestBoat) <=30) {
      this.reactTo(nearestBoat);
    }
    Yeah!!! Now that's some pretty OO!

  3. #3
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108
    So in each class I should overload the method for reacting to other boats?

  4. #4
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    yep.

  5. #5
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108

    Talking

    THANKS ALOT )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center