Results 1 to 4 of 4
Thread: Need help finding error in code
- 05-02-2010, 09:17 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
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); } } }
- 05-02-2010, 10:40 PM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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 10:43 PM.
- 05-04-2010, 04:37 AM #3
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 05-04-2010, 04:51 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
Help Finding Compiler error solution please
By jamesr2b in forum New To JavaReplies: 5Last Post: 04-30-2009, 06:07 AM -
help me in finding the entry points in the source code of java.....
By aks.nitw in forum Advanced JavaReplies: 0Last Post: 11-25-2008, 09:24 AM -
Error Code???
By andmartha in forum New To JavaReplies: 11Last Post: 10-04-2008, 02:16 AM -
Pls help with a code error.
By saytri in forum New To JavaReplies: 8Last Post: 12-24-2007, 08:10 PM -
error in code
By dirtycash in forum New To JavaReplies: 2Last Post: 12-06-2007, 11:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks