Java questions for parking lots. You can check them out when you are bored.

Source: Internet
Author: User
Question 3:
Simulate the activity of a car in a parking lot.
Assume there is a 5x5 parking lot. the horizontal axis is-2 to + 2, and the vertical axis is-2 + 2. A car stops at the center (0, 0) and faces the top (called the North ).

There are three types of user input: Forward (F), turn left at 90 degrees (l) , Turn right to 90 degrees (R ). Simulate vehicle activities based on the user input sequence. When the program ends, the condition is: 1) when the car exceeds the dot matrix range, it is deemed that the car falls into the trench (Fall ) But cannot be moved, the program no longer processes the input, and ends; 2) the input (s) ends. The position and direction of the final vehicle are given after each step (using N, S, E , W indicates north, south, east, and west.) programming requirements: Describe the above process using Java code in an object-oriented method that you think is appropriate. And accept the input to get the result. Input can be carried out in any way you think is convenient, such as stdin, to read files Or write it in a startup class in a certain way. You can also modify it before each operation. . You do not have to check the validity of the input. Each step is output in stdout. Format: input content, result X coordinate , Ordinate, direction Example 1: This is a forward step, turn twice, and then forward to the first example, input: fllffffffffstart, NF, NL, wl, 0, 1, SF, 0, SF, 0,-1, SF, 0,-2, SF, 0,-3, sfall Example 2: input: frfrfrsstart, 0, 0, NF,, NR, EF, wR, SF, Sr, WF, wR, nstop 1_package cxg; public class car {private int stationx = 0;
Private int stationy = 0;
Private int direction = 1;
 
Public int getdirection (){
Return direction;
}
Public void setdirection (INT direction ){
This. Direction = direction;
}
Public int getstationx (){
Return stationx;
}
Public void setstationx (INT stationx ){
This. stationx = stationx;
}
Public int getstationy (){
Return stationy;
}
Public void setstationy (INT stationy ){
This. stationy = stationy;
}
 
} -------------------------------------------------------------------------------- Package cxg; import java. Io. bufferedreader;
Import java. Io. ioexception;
Import java. Io. inputstreamreader;
Import java. util. arraylist;
Import java. util. iterator;
Import java. util. List; public class coreprocess {
 
Private Final int Maxx = 2;
Private Final int Minx =-2;
Private Final int Maxy = 2;
Private Final int miny =-2;
 
Private int nowx = 0;
Private int Nowy = 0;
Private int directionnum;
 
 
// N: 1 E: 2 S: 3 W: 4
Private Final int [] dirnum = {1, 2, 3, 4 };
Private Final char [] direction = {'n', 'E', 's', 'w '};
Private Final char [] control = {'F', 'l', 'R '};
Private Final char exit ='s ';
// The current VO. Save the status of the previous step.
Private Car tempvo = new car ();
 
/*
* Stackmap {infovo}
*/
Private list statelist = new arraylist ();
 
Public static void main (string [] ARGs ){
Coreprocess instance = new coreprocess ();
Instance. getinput ();
Instance. viewprint ();
}
 
Private void getinput (){
Try {
Bufferedreader stdin = new bufferedreader (
New inputstreamreader (system. In ));
String userinput;
Userinput = stdin. Readline ();
// While (userinput = stdin. Readline ())! = NULL ){
System. Out. println ("Custom input:" + userinput );
System. Out. println ("START, 0, 0, N ");
Processinput (userinput );
//}
Stdin. Close ();

} Catch (ioexception e ){
System. Err. println ("couldn't get I/O .");
System. Exit (1 );
}
}
 
// Process console display
Private void viewprint (){
// It is better if jdk1.5autoboxing is used
Iterator ite = statelist. iterator ();
While (ITE. hasnext ()){
Car Vo = (CAR) ite. Next ();
// It is more convenient to use jdk1.5printf () for processing.
System. Out. println (VO. getdirection () + "," + VO. getstationx () + "," + VO. getstationy ());
}
}
 
Private Boolean checkisfall (int x, int y ){
Boolean flag = true;
If (x> Maxx | x <Minx ){
Flag = false;
}
If (Y> Maxy | Y <miny ){
Flag = false;
}
// Add {1, 1} {1,-1}
If (x = 1 & Y = 1) | (x = 1 & Y =-1 )){
Flag = false;
System. Out. println ("this car has turn over at {x =" + x + ", y =" + Y + "}");
}
Return flag;
}
 
Private void processinput (string input ){
// Case insensitive
Char inchar;
Int Len = input. Length ();
For (INT I = 0; I <Len; I ++ ){
Inchar = input. charat (I );
If (! Operation (inchar )){
Break;
}
}
}
 
Private Boolean operation (char inchar ){
Boolean flag = true;
Car thisvo = new car ();

// Process exit
If (inchar = exit ){
System. Exit (0 );
}

// Processing direction
Else if (inchar = control [1] | inchar = control [2]) {
Directionnum = tempvo. getdirection ();
If (inchar = control [2]) {
Direnum num = (direnum num + 1) % 4;
Dosetdirection (direnum num, thisvo );
} Else {
If (direnum num> 1 ){
Direnum num = (direnum num-1) % 4;
Dosetdirection (direnum num, thisvo );
} Else if (directionnum = 1 ){
Direnum num = 4;
Dosetdirection (direnum num, thisvo );
}
}

// Process forward
} Else if (inchar = control [0]) {// F
Nowx = tempvo. getstationx ();
Nowy = tempvo. getstationy ();
Int fangxiang = tempvo. getdirection ();

If (fangxiang = dirnum [0]) {// n: 1
Nowy ++;
Tempvo. setstationy (Nowy );
Flag = dosetallproperties (nowx, Nowy, tempvo. getdirection (), thisvo );
}
Else if (fangxiang = dirnum [1]) {// E: 2
Nowx ++;
Tempvo. setstationx (nowx );
Flag = dosetallproperties (nowx, Nowy, tempvo. getdirection (), thisvo );
}
Else if (fangxiang = dirnum [2]) {// S: 3
Nowy --;
Tempvo. setstationy (Nowy );
Flag = dosetallproperties (nowx, Nowy, tempvo. getdirection (), thisvo );
}
Else if (fangxiang = dirnum [3]) {// W: 3
Nowx --;
Tempvo. setstationx (nowx );
Flag = dosetallproperties (nowx, Nowy, tempvo. getdirection (), thisvo );
}

} // Process the forward end

Return true;
}
 
 
Private void dosetdirection (INT direction, car VO ){
Tempvo. setdirection (Direction );
VO. setdirection (Direction );
VO. setstationx (tempvo. getstationx ());
VO. setstationy (tempvo. getstationy ());
Statelist. Add (VO );
}
 
Private Boolean dosetallproperties (int x, int y, int direction, car VO ){
If (! Checkisfall (nowx, Nowy )){
Return false;
}
VO. setdirection (Direction );
VO. setstationx (X );
VO. setstationy (y );
Statelist. Add (VO );

Return true;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.