java - get "getDistance" from Location Class and use it in Target Class using an if condition -


//the location class in seperate file

package tld; import java.util.*; public class location { private double x; private double y; scanner scan= new scanner(system.in);  public location(double x, double y){ x= scan.nextdouble(); y= scan.nextdouble();}  public double getx(){ return x;}  public double gety(){ return y;}  public void setx(double x){ this.x=x;}  public void sety(double y){ this.y=y;}  public string tostring(){ return "location: x= "+getx()+" ; y= "+gety();}  public double getdistance(location l){   double x1=scan.nextdouble();  double y1=scan.nextdouble();  l=new location(x1,y1); return (math.sqrt((math.pow((x-x1),2))+(math.pow((y-y1), 2))));} } 

.

//the target class in seperate file

package tld; import java.util.*; public class target{ scanner scan = new scanner(system.in); private location loc; private double speed; private string type; private boolean threat; private int count = 0;  public target(double x, double y, double speed){     x=scan.nextdouble();     y=scan.nextdouble();     speed=scan.nextdouble();     if(speed<500)          type="helicopter";     else type="jet";      threat = false;     count++;}  public string gettype(){     return type;}  public double getspeed() {     return speed;}  public void setspeed(double speed) {     this.speed = speed;}  public location getloc() {     return loc;}  public void setloc(location loc) {     this.loc = loc;}  public boolean isthreat(location l){      if(location.getdistance()<100){          threat=true;}      else threat=false;      return threat;}  public string tostring(){      return "target " + gettype() + ", speed= " + getspeed() + "mph, [location: x=" + location.x1+ "; y=" + loc.x + "]"; }} 

my error in class.. in "boolean isthreat method"

.

//main driver in seperate file , still not finished

package tld; import java.util.*; public class tld { public static void main(string[] args) {     location l = new location(100,200); system.out.println("please enter 4 targets"); scanner scan = new scanner(system.in); string [] target=new string[4]; for(int i=0; i<target.length; i++) target[i]=scan.next();   }  } 

note i'm not allowed use advanced commands or classes

since getdistance not static, need have instance of location before can call it. luckily, do have instance. see loc variable declared @ top of class? that's need!

so change line

if(location.getdistance()<100){ 

to this:

if(loc.getdistance(l)<100){ 

"why have put l there?" might ask. because getting distance between 2 locations! first location loc what's second location? method signature, think want l, parameter, second location.

also, there lots more places in code makes little sense. guess of want do:

public class location {     private double x;     private double y;      public location(double x, double y){         this.x = x;         this.y = y;     }      public double getx(){         return x;     }      public double gety(){         return y;     }      public void setx(double x){         this.x=x;     }      public void sety(double y){         this.y=y;     }      public string tostring(){         return "location: x= "+getx()+" ; y= "+gety();     }      public double getdistance(location l){         double x1=l.getx();         double y1=l.gety();         return (math.sqrt((math.pow((x-x1),2))+(math.pow((y-y1), 2))));     } }  public class target{     private location loc;     private double speed;     private string type;     private boolean threat;     private static int count = 0;      public target(double x, double y, double speed){         loc = new location(x, y);         this.speed = speed;         if(speed<500)             type="helicopter";         else type="jet";         threat = false;         count++;     }      public string gettype(){         return type;     }      public double getspeed() {         return speed;     }      public void setspeed(double speed) {         this.speed = speed;     }      public location getloc() {         return loc;     }      public void setloc(location loc) {         this.loc = loc;     }      public boolean isthreat(location l){         if(loc.getdistance(l)<100){             threat=true;}         else threat=false;         return threat;     }      public string tostring(){         return "target " + gettype() + ", speed= " + getspeed() + "mph, [location: x=" + loc.getx()+ "; y=" + loc.gety() + "]";     } } 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -