-
Need a hint.
I am curently working on a program what simulates boats moving on a lake, there are rules that apply to different boats. I need to get the distance from one boat to another, check if the closest boat is a certain kind of boat and follow the rules given for that boat. My problem is that I am not sure where I should put the method that would calculate the closest boat and its type (Sail\Motor), by where I mean Either main method of the program , or the superclass from which the different boatclasses are being extended.
Here is what I got so far:
Code:
class boatprogram5{
static int numboats=4;
static boatclass boat[]=new boatclass[numboats+1];
public static void main(String[]args){
boatclass boat0=new sailboatclass(1.6,0.3,1.1,10.4,35,false,65);
boat[1]=boat0;
boat0=new motorboatclass(30.5,10.8,-5.2,-1.4,18,25);
boat[2]=boat0;
boat0=new sailboatclass(3.8,2.4,7.2,-4.9,30,true,120);
boat[3]=boat0;
boat0=new motorboatclass(7.4,14.8,0.2,8.6,12,16);
boat[4]=boat0;
double time=0;
double deltatime=2.0;
while(time<=10.0){
System.out.println("Time: "+cpj.nf(time,7,2));
for(int i=1;i<=numboats;i++){
double nearestdistance=Double.MAX_VALUE;
for(int j=1;j<=numboats;j++){
if(i!=j){
if(boat[i].distance(boat[j])<nearestdistance){
nearestdistance=boat[i].distance(boat[j]);
boat[i].nearestboat=boat[j];
}
}
}
System.out.println("Boat "+i+" is at "+boat[i].positionstring());
System.out.println(" nearest boat is at "+boat[i].nearestboat.positionstring());
System.out.print(" distance: "+cpj.nf(boat[i].nearestdistance(),7,2));
String nearestboattype="";
if(boat[i].nearestboat instanceof sailboatclass)
{nearestboattype=" Sailboat";}
if(boat[i].nearestboat instanceof motorboatclass)
{nearestboattype=" Motorboat";}
System.out.println(" type="+nearestboattype);
}
time+=deltatime;
for(int i=1;i<=numboats;i++){
boat[i].moveboat(deltatime);
}
}
}
}
abstract class boatclass{
double x=0;
double y=0;
double vx=0;
double vy=0;
boatclass nearestboat;
public boatclass(double x,double y,double vx,double vy){
this.x=x; this.y=y;
this.vx=vx; this.vy=vy;
}
void moveboat(double deltatime){
x+=deltatime*vx;
y+=deltatime*vy;
}
void stop(){
vy=0;
vx=0;
}
double distance(boatclass otherboat){
double z=Math.sqrt(Math.pow((x-otherboat.x),2)+Math.pow((y-otherboat.y),2));
return z;
}
double nearestdistance() {
if(distance(nearestboat)<=3){
stop();
System.out.println(" Boat has stopped stopped");
}
return distance(nearestboat);
}
String positionstring(){
return("x ="+cpj.nf(x,7,2)+" y ="+cpj.nf(y,7,2));
}
}
class sailboatclass extends boatclass{
private int mastheight=0;
boolean sailsfurled=false;
double saildirection=0;
public sailboatclass(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;
}
}
class motorboatclass extends boatclass{
double fuellevel=0;
private double fueltanksize=0;
public motorboatclass(double x,double y,double vx,double vy,double fuellevel,double fueltanksize){
super(x,y,vx,vy);
this.fuellevel=fuellevel;
this.fueltanksize=fueltanksize;
}
}
Thanks in advance.
-
Personally, instead of using instanceof here, I would put a getType() on the superclass (see below).
Code:
if(boat[i].nearestboat instanceof sailboatclass)
{nearestboattype=" Sailboat";}
if(boat[i].nearestboat instanceof motorboatclass)
{nearestboattype=" Motorboat";}
I am not sure where I should put the method that would calculate the closest boat and its type (Sail\Motor)
I would put these on the superclass. Making it abstract allows you hide the implementation details in the super, and forces children to implement it instead.
Calculating closest boat would fit well in either the superclass or external from the heirarchy. since you are calculating the closest boat from _a_ boat, though, one could argue putting that on the superclass makes sense.
I would pass an array of boatclass to the method, and just check where boat[i] != this
Code:
abstract class boatclass{
//children must override to return their string type
public abstract String getType();
public void calculateNearest(boatclass boats[]) {
double nearestdistance = Double.MAX_VALUE;
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];
}
}
}
}
//must call calculateNearest first
boatclass getNearest() {
return nearestboat;
}
}
-
I don't understand what you mean by
I would pass an array of boatclass to the method, and just check where boat[i] != this
-
See the method i posted 
This is mostly just a move of what you already had, moved out of main() into the superclass:
Code:
public void calculateNearest(boatclass boats[]) {
double nearestdistance = Double.MAX_VALUE;
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];
}
}
}
}
-
oh, just noticed that you are using 1-based arrays. I compiled and ran your program, and got arrayoutofbounds exceptions with the code snippet i posted.
Here is a fixed up version (You'll have to insert your cpj.nf(...,7,2) statements, because I removed them so it would compile...
cheers.
Code:
class boatprogram5 {
public static void main(String[] args) {
Boat boats[] = { new SailBoat(1.6, 0.3, 1.1, 10.4, 35, false, 65),
new MotorBoat(30.5, 10.8, -5.2, -1.4, 18, 25),
new SailBoat(3.8, 2.4, 7.2, -4.9, 30, true, 120),
new MotorBoat(7.4, 14.8, 0.2, 8.6, 12, 16), };
double time = 0;
double deltatime = 2.0;
while (time <= 10.0) {
System.out.println("Time: " + time);
for (int i = 0; i < boats.length; i++) {
boats[i].calculateNearest(boats);
Boat nearest = boats[i].getNearest();
System.out.println("Boat " + i + " is at " + boats[i].toString());
System.out.println(" nearest boat is at " + nearest.toString());
System.out.print(" distance: " + boats[i].distance(nearest));
System.out.println(" type= " + nearest.getType());
}
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;
private 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 void moveboat(double deltatime) {
x += deltatime * vx;
y += deltatime * vy;
}
public void stop() {
vy = 0;
vx = 0;
}
public double distance(Boat otherboat) {
double z = Math.sqrt(Math.pow((x - otherboat.x), 2)
+ Math.pow((y - otherboat.y), 2));
return z;
}
public String toString() {
return ("x =" + x + " y =" + y);
}
public abstract String getType();
public void calculateNearest(Boat boats[]) {
double nearestdistance = Double.MAX_VALUE;
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];
}
}
}
if (distance(nearestboat) <= 3) {
stop();
System.out.println(" Boat has stopped");
}
}
//must call calculateNearest first
public Boat getNearest() {
return nearestboat;
}
}
class SailBoat extends Boat {
public String getType() {
return "SailBoat";
}
private int mastheight = 0;
private boolean sailsfurled = false;
private double saildirection = 0;
public SailBoat(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;
}
}
class MotorBoat extends Boat {
private double fuellevel = 0;
private double fueltanksize = 0;
public String getType() {
return "MotorBoat";
}
public MotorBoat(double x, double y, double vx, double vy, double fuellevel,
double fueltanksize) {
super(x, y, vx, vy);
this.fuellevel = fuellevel;
this.fueltanksize = fueltanksize;
}
}
-
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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|