/**
* Author: Kevin
* Date: 2011-07-17
* Function: the problem of Mars car.
* China firmly opposes Obama-Dalai MEETING
* The war is actually begun! Now you are the commander of the fleet.
* You can input command to control the Mars car to go straight,
* Turn left, turn right, and the car maybe your loyal soldier.
* The command input like this string: ll1lrr89l2,
* And at last report the location of the car to commander.
* L means turn left, R means turn right, number0-9 means how far the car can go
* Request: 1). extremeprogramming 2). Pair Programming 3). Test driven development
*/
Import java. Io. bufferedreader;
Import java. Io. inputstreamreader;
Import java. util. collections;
Public class testmarscar {
Public static void main (string [] ARGs) throws exception {
Bufferedreader BF = new bufferedreader (New inputstreamreader (system. In ));
System. Out
. Println ("now, the Mars car is loarding on the Mars, input the loaction ");
System. Out. println ("input x :( number )");
String x = BF. Readline ();
System. Out. println ("input y :( number )");
String y = BF. Readline ();
System. Out. println ("input direction :( N, W, S, E )");
String F = BF. Readline ();
System. Out. println ("the car is loaded in (" + x + "," + Y + "," + F
+ ")");
// Create marscar object
Marscar car = new marscar (X, Y, F );
While (true ){
System. Out. println ("\ n ***********************"
+ "\ Npress 1or 2 to select *" + "\ N1: new command *"
+ "\ N2: Exit *"
+ "\ N *********************** \ ninput number :");
Repeated S1 = new partition (system. In );
Int ints1 = integer. parseint (s1.next ());
If (ints1 = 1 ){
System. Out. println ("input new command like (L, R, 0-9 ):");
S = new partition (system. In );
String commands = S. Next ();
For (INT I = 0; I <commands. Length (); I ++ ){
String command = string. valueof (commands. charat (I ));
If (command. Equals ("L ")){
Car. turnleft (car. getf ());
} Else if (command. Equals ("R ")){
Car. goright (car. getf ());
} Else {
Int n = integer. parseint (command );
Car. goahead (N );
}
// Show the location of Mars car.
System. Out. println ("the new location is (" + car. getx ()
+ "," + Car. Gety () + "," + car. getf () + ")");
}
} Else if (ints1 = 2 ){
System. Exit (0 );
} Else {
System. Out. println ("You command is wrong! ");
}
}
}
}
//-----------------------------------------
// Class marscar
Class marscar {
Private string X; // X-Coordinate
Private string y; // y-Coordinate
Private string F; // direction
Public String getx (){
Return X;
}
Public void setx (string X ){
This. x = X;
}
Public String Gety (){
Return y;
}
Public void sety (string y ){
This. Y = y;
}
Public String getf (){
Return F;
}
Public void SETF (string f ){
This. f = F;
}
// Constructor
Public marscar (string X, string y, string f ){
This. x = X;
This. Y = y;
This. f = F;
}
Public void turnleft (string f ){
System. Out. println ("Go left ");
If (F. Equals ("N ")){
SETF ("W ");
} Else if (F. Equals ("e ")){
SETF ("N ");
} Else if (F. Equals ("S ")){
SETF ("e ");
} Else if (F. Equals ("W ")){
SETF ("S ");
}
}
Public void goright (string f ){
System. Out. println ("Go right ");
If (F. Equals ("N ")){
SETF ("e ");
} Else if (F. Equals ("e ")){
SETF ("S ");
} Else if (F. Equals ("S ")){
SETF ("W ");
} Else if (F. Equals ("W ")){
SETF ("N ");
}
}
// Input number to control how far the car go ahead
Public void goahead (int n ){
Int intx = integer. parseint (X );
Int inty = integer. parseint (y );
If (this. getf (). Equals ("N ")){
Intx + = N;
} Else if (this. getf (). Equals ("e ")){
Inty + = N;
} Else if (this. getf (). Equals ("S ")){
Intx-= N;
} Else if (this. getf (). Equals ("W ")){
Inty-= N;
}
This. x = string. valueof (intx );
This. Y = string. valueof (inty );
}
}