Results 1 to 4 of 4
  1. #1
    BobTedmen is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default Need help finding error in code

    i have this project where two trains are heading the same direction i need to tell when and where the trains will collide there are two towns 100 miles apart based of the trains last reported position and speed (assume constant speed) if the computed position is east or west of the two towns no collision is reported and collisions can only occur at times greater or equal to the time at which the trains last reported.
    I have most of this project done however it doesn't report properly and i am not sure if my equations are correct you could help me correct my error(s) i would be great-full.

    this was made using Net Beans 6.8
    ( i know i have horrible spelling)

    Java Code:
    /*
     * Main
     */
    
    package collisionmonitor;
    
    public class Main {
    
        private static final String IF_COLLIDE=" Will Collide ";
        private static final String IF_NOT_COLLIDE=" Will not Collide";
    
        public static void main(String[] args) {
            
            Train train1= new Train("East Bound",4.0,0.0,40.0);
            Train train2=new Train("West Bound",4.5,50.0,-40.0);
            CollisionMonitor.collisionDector(train1, train2);
            CollisionMonitor areff= new CollisionMonitor();
            print(areff);
            
            
            System.out.println(" Test time "+ CollisionMonitor.t);
            System.out.println(" Test position "+CollisionMonitor.d);
    
            
    
    
        }
    
        private static void print(CollisionMonitor areff){
    
          
    
            if(areff.isIfCollisionTrue()){
    
                System.out.println
                        ("Trains Named "
                        +areff.getTrain1()
                        +" and "
                        +areff.getTrain2()+IF_COLLIDE+" at time "
                        +areff.getCollisionTime()
                        +" and position "+areff.getCollisionPosition());
    
            }else{
    
                System.out.println
                        ("Trains Named "
                        +areff.getTrain1()
                        +" and "
                        +areff.getTrain2()+IF_NOT_COLLIDE);
    
            }
    
    
        }
    
    }

    Java Code:
     
    /*
     * Train class
     */
    
    package collisionmonitor;
    
    
    public class Train {
    
      
         //constants
        private static final String DEFAULT_NAME="Unknown Train";
        private static final double DEFAULT_TIME=1.0;
        private static final double DEFAULT_POSITION=1.0;
        private static final double DEFAULT_SPEED=1.0;
        //instance fileds
        private String nameOfTrain;
        private double timeOfReport;
        private double position;
        private double speed;
    
    
        public Train(){
    
            this(DEFAULT_NAME,DEFAULT_TIME,DEFAULT_POSITION,DEFAULT_SPEED);
        }
    
        public Train(String myNameOfTrain, double myTimeOfReport, double myPosition, double mySpeed){
            setNameOfTrain(myNameOfTrain);
            setTimeOfReport(myTimeOfReport);
            setPosition(myPosition);
            setSpeed(mySpeed);
    
        }//end full prametor constructor
    
    
        
    
        public String getNameOfTrain() {
            return nameOfTrain;
        }
    
        public void setNameOfTrain(String nameOfTrain) {
            this.nameOfTrain = nameOfTrain;
        }
    
        public double getTimeOfReport() {
            return timeOfReport;
        }
    
        public void setTimeOfReport(double timeOfReport) {
            this.timeOfReport = timeOfReport;
        }
    
        public double getPosition() {
            return position;
        }
    
        public void setPosition(double position) {
            this.position = position;
        }
    
        public double getSpeed() {
            return speed;
        }
    
        public void setSpeed(double speed) {
            this.speed = speed;
        }
    
    
    }
    Java Code:
     
    
    /*
     * CollisionMonitor 
     */
    
    package collisionmonitor;
    
    
    public class CollisionMonitor {
    
       private static final String DEFAULT_TRAIN1="Unkown Train1";
       private static final String DEFAULT_TRAIN2="Unkown Train2";
       private static final double DEFAULT_COLLISION_TIME=0.0;
       private static final double DEFAULLT_POSITION=0.0;
       private static final boolean DEFAULT_IF_COLLISION=false;
    
       public static double t;
       public static double d;
       private static String nameOne;
       private static String nameTwo;
       private static double t1;
    
       private  String train1;
       private  String train2;
       private  double collisionTime;
       private  double collisionPosition;
       private  boolean ifCollisionTrue;
    
       CollisionMonitor(String myTrain1, String myTrain2,
               double myCollisionTime,double myCollisionPosition, boolean ifCollisionTrue){
    
           setTrain1(myTrain1);
           setTrain2(myTrain2);
           setCollisionTime( myCollisionTime);
           setCollisionPosition(myCollisionPosition);
           setIfCollisionTrue(ifCollisionTrue);
    
    
       }//end full pramator constructor
    
       CollisionMonitor(){
           this(DEFAULT_TRAIN1,DEFAULT_TRAIN2,
                DEFAULT_COLLISION_TIME,DEFAULLT_POSITION,DEFAULT_IF_COLLISION);
       }
    
        public  String getTrain1() {
            return train1;
        }
    
    
        public  void setTrain1(String train1) {
            this.train1 = train1;
        }
    
        public  String getTrain2() {
            return train2;
        }
    
        public  void setTrain2(String train2) {
            this.train2 = train2;
        }
    
        public  double getCollisionTime() {
            return collisionTime;
        }
    
        public  void setCollisionTime(double collisionTime) {
            this.collisionTime = collisionTime;
        }
    
        public  double getCollisionPosition() {
            return collisionPosition;
        }
    
        public  void setCollisionPosition(double collisionPosition) {
            this.collisionPosition = collisionPosition;
        }
         public boolean isIfCollisionTrue() {
            return ifCollisionTrue;
        }
    
        public  void setIfCollisionTrue(boolean ifCollisionTrue) {
            this.ifCollisionTrue = ifCollisionTrue;
        }
    
      
    
    
        public static void collisionDector(Train testTrain, Train testTrain2){
    
    
            nameOne=testTrain.getNameOfTrain();
            t1=testTrain.getTimeOfReport();
                //timeOne
            double p1=testTrain.getPosition();
                //positionOne
            double v1=testTrain.getSpeed();
                //speedOne
    
            nameTwo=testTrain2.getNameOfTrain();
                 // name of train
            double t2=testTrain2.getTimeOfReport();
                //timeTwo
            double p2=testTrain2.getPosition();
                //positionTwo
            double v2=testTrain2.getSpeed();
                //speedTwo
    
            
    
            //t=(p2*-p1*-v2t2*+v1t1*)/(v1-v2)
    
    
                //time of collision
            t=(p2-p1-v2*t2+v1*t1)/(v1-v2);
    
            d=((v1+v1)/2)*(t-t1);
    
    
    
    
        }
    
        public void collisionReporter(){
    
            if(d>100&&t<=t1){
               
            setCollisionTime(t);
            setCollisionPosition(d);
            setTrain1(nameOne);
            setTrain2(nameTwo);
            setIfCollisionTrue(true);
    
            }else{
            setTrain1(nameOne);
            setTrain2(nameTwo);
            setIfCollisionTrue(false);
            }
    
            }
    
    
    
       
    
    }

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by BobTedmen View Post
    ...you could help me correct my error(s) i would be great-full.
    Perhaps it would be wise to report precisely what your errors are and which lines cause them.

    I also note that you got decent help in your previous question but never thanked nor even acknowledged that help. Doing so may go a long way towards motivating others to help you again. Just a thought.
    Last edited by curmudgeon; 05-02-2010 at 11:43 PM.

  3. #3
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by curmudgeon View Post
    Perhaps it would be wise to report precisely what your errors are and which lines cause them.

    I also note that you got decent help in your previous question but never thanked nor even acknowledged that help. Doing so may go a long way towards motivating others to help you again. Just a thought.
    Hm, your lack of response suggests that this question just isn't that important to you then, and so it's not that important to me. buena suerte, amigo.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    If you comes with an error, please post it here. Just looking at the code it's really difficult to comment. And also I don't think no one could check your code and fix the errors. Specifically ask your question, I'll help you to solve the problem.

Similar Threads

  1. Help Finding Compiler error solution please
    By jamesr2b in forum New To Java
    Replies: 5
    Last Post: 04-30-2009, 07:07 AM
  2. Replies: 0
    Last Post: 11-25-2008, 10:24 AM
  3. Error Code???
    By andmartha in forum New To Java
    Replies: 11
    Last Post: 10-04-2008, 03:16 AM
  4. Pls help with a code error.
    By saytri in forum New To Java
    Replies: 8
    Last Post: 12-24-2007, 09:10 PM
  5. error in code
    By dirtycash in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 12:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •