CS Car Program

Labels:
The java program Sir Paolo typed up during our CS period today. I was trying so hard not to use "kanina".

I still don't understand why he also sent Chester a copy. :-j


import java.util.*;

public class Car2 {

int position_x;
int position_y;
String direction = "up";

public void moveForward(int amount) {
if(direction == "up"){
position_y = position_y + amount;
} else if(direction == "right") {
position_x = position_x + amount;
} else if(direction == "down") {
position_y = position_y - amount;
} else if(direction == "left") {
position_x = position_x - amount;
}
}

public void moveBackward(int amount) {
if(direction == "up"){
position_y = position_y - amount;
} else if(direction == "right") {
position_x = position_x - amount;
} else if(direction == "down") {
position_y = position_y + amount;
} else if(direction == "left") {
position_x = position_x + amount;
}
}

public void turnRight(){
if(direction == "up"){
direction = "right";
} else if(direction == "right") {
direction = "down";
} else if(direction == "down") {
direction = "left";
} else if(direction == "left") {
direction = "up";
}
}

public void turnLeft(){
if(direction == "up"){
direction = "left";
} else if(direction == "right") {
direction = "up";
} else if(direction == "down") {
direction = "right";
} else if(direction == "left") {
direction = "down";
}
}

public double getDisplacement(){
double displacement = (position_x * position_x) + (position_y * position_y);
return Math.sqrt(displacement);
}
}
0 comments:

Post a Comment

Hi. :-h


Chem2

Recent Entries

Recent Comments