Java object-oriented solution to practical problem instances (i)

Source: Internet
Author: User
Tags gety

in a campaign to facilitate the exchange of information, our reconnaissance department set the key heights of the campaign to (X=0,y=0) and stipulates that each eastward increase -meters,xPlus1, each northward increased -meters,yPlus1. At the same time, the military Intelligence department also cracked the enemy's command signal sent to the tank, of which there are three kinds of signals (l,r,m) to control the movement of the tank,Land theRmake the tank turn left and right, respectively,Mmeans the tank is in line. -Meter, other signals such asTfor time synchronization,Pused for a more accurate position.

one day, our scouts found a tank for the enemy, and the scout immediately put the tank's coordinates ( P, Q) and tank forward direction (W: West,E: East,N: North,S : South) is sent to the command, while the signal receiver is activated, the signal received by the tank is sent to the command in real time, and the command is able to control the tank's position in real time using the howitzer, and the tank is destroyed precisely by the mortar.

Suppose that the information the Scout sends to the command is as follows:

Tank Coordinates: (one, one,one)

tank Operation Direction: W

the signal received by the tank is: Mtmprpmtmlmrprmtplmmtlmrrmp

Please calculate the location of the tank by programming (Not limited in programming language )

Java implementation code is as follows:

1  PackageBST;2 3  Public classBst {4     //Tank Initial coordinates5ZB ZB =NewZB (11,39);6     //Tank initial operation direction7String Direction = "W";8     9     //calculates the final coordinates of the tank according to the received signal programming.Ten      PublicZB Getzb (String News) { One          Astring[] str = news.split (""); -          -          for(inti = 0;i<str.length;i++){ the             if("L". Equals (Str[i])) { -                 Switch(direction) { -                  Case"W":d irection= "S"; Break; -                  Case"E":d irection= "N"; Break; +                  Case"N":d irection= "W"; Break; -                  Case"S":d irection= "E"; Break; +                 default: Break; A                 } at}Else if("R". Equals (Str[i])) { -                 Switch(direction) { -                  Case"W":d irection= "N"; Break; -                  Case"E":d irection= "S"; Break; -                  Case"N":d irection= "E"; Break; -                  Case"S":d irection= "W"; Break; in                 default: Break; -                 } to}Else if("M". Equals (Str[i])) { +                 Switch(direction) { -                  Case"W": Zb.setx (Zb.getx ()-1); Break; the                  Case"E": Zb.setx (Zb.getx () +1); Break; *                  Case"N": Zb.sety (Zb.gety () +1); Break; $                  Case"S": Zb.sety (Zb.gety ()-1); Break;Panax Notoginseng                 default: Break; -                 } the             } +         } A          the         returnZB; +          -     } $      $     //print out the final coordinates of the tank -      Public Static voidMain (string[] args) { -BST BST =NewBst (); theZB ZB = Bst.getzb ("MTMPRPMTMLMRPRMTPLMMTLMRRMP"); -SYSTEM.OUT.PRINTLN ("The tank is located at:" +zb.tostring ());Wuyi     } the  - } Wu  - //Tank Coordinate class About classzb{ $     intx; -     inty; -      -      PublicZB (intXinty) { A         Super(); +          This. x =x; the          This. y =y; -     } $  the      Public intGetX () { the         returnx; the     } the      -      Public voidSetX (intx) { in          This. x =x; the     } the      About      Public intGetY () { the         returny; the     } the      +      Public voidSety (inty) { -          This. y =y; the     }Bayi      the     //overriding the ToString method the      PublicString toString () { -         return"(" +x+ "," +y+ ")"; -     } the}

Java object-oriented solution to practical problem instances (i)

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.