Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  
  5. * This class translates messages from aliens on planets
  6.  
  7. * Beaumonde and Regina into Earth English.
  8.  
  9. * @author Kristy Boyer
  10.  
  11. *
  12.  
  13. */
  14.  
  15. public class AlienTranslator {
  16.  
  17. /**
  18.  
  19. * This main method creates an AlienTranslator and then calls the
  20.  
  21. * translateInput method. Students should not modify this method.
  22.  
  23. * @param args
  24.  
  25. */
  26.  
  27. public static void main(String[] args) {
  28.  
  29. AlienTranslator a = new AlienTranslator();
  30.  
  31. Scanner s = new Scanner(System.in);
  32.  
  33.  
  34.  
  35. a.translateInput(s);
  36.  
  37. }
  38.  
  39. /**
  40.  
  41. * This method gets the necessary input from the user regarding planet
  42.  
  43. * and the message to be translated.
  44.  
  45. * Students should fill in the missing code in this method.
  46.  
  47. * @param in A Scanner created on the appropriate stream of input.
  48.  
  49. */
  50.  
  51. public String translateInput(Scanner in) {
  52.  
  53. //TODO: Get from user which planet the inter-galactic message is from.
  54.  
  55. //1=Beaumonde and 2=Regina
  56.  
  57. //Store value in an int variable named planet. If it is not valid, print
  58.  
  59. //an error to the user and exit the program. Otherwise (a valid planet)
  60.  
  61. //Get the inter-galactic message that needs to be translated.
  62.  
  63. //Store it in a String variable named message
  64.  
  65. //---- > Fill in Student Code Here < ------
  66.  
  67. System.out.println("Welcome! Please state what language the message is in? 1 for Beaumondian, 2 for Reginian.");
  68. int Planet = in.nextInt();
  69.  
  70. if (Planet == 1){
  71. String x = "Beaumonde";
  72. }
  73. else if (Planet == 2){
  74. String x = "Regina";
  75. }
  76. else{
  77. System.out.println("Not a valid planet. Please try again later.");
  78. }
  79. System.out.println("Please type in the message.");
  80. in.nextLine();
  81. String message = in.nextLine();
  82. //---- > End Student Code < -----
  83.  
  84. //Teaching staff code - STUDENTS DO NOT MODIFY - extracts characters
  85.  
  86. //one at a time from the String message and stores each character in
  87.  
  88. //the char variable named charToTranslate
  89.  
  90. //The result variable is used to build and store the translated message
  91.  
  92. // which will be sent back when this method is finished.
  93.  
  94. String result = "";
  95.  
  96. for (int i=0; i < message.length(); i++) {
  97.  
  98. char charToTranslate = message.charAt(i);
  99.  
  100. //End teaching staff code
  101.  
  102. //TODO: translate the character stored in variable charToTranslate
  103.  
  104. //and append the translated character to the result variable
  105.  
  106. //----- > Fill in Student Code Here < -----
  107.  
  108.  
  109.  
  110. if (charToTranslate <= 'a' && charToTranslate >= 'y'){
  111. result += (char)(message.charAt(i) + 2);
  112.  
  113. }
  114.  
  115. if (charToTranslate > 'z' || charToTranslate > 'Z'){
  116. result += (char)(message.charAt(i) - (26 - i));
  117.  
  118. }
  119.  
  120. System.out.println(message);
  121.  
  122. //----- > End Student Code < -----
  123.  
  124. } //ends the for loop
  125.  
  126. //TODO: perform the necessary output as specified in the requirements
  127.  
  128. // **** This TODO was added to this starter code on Mon 1/25 2:55pm ****
  129.  
  130. //------> Fill in Student Code < -------
  131.  
  132. //------> End Student Code < -------
  133.  
  134. //Sends back the value of the result variable
  135.  
  136. return result;
  137.  
  138. } //ends the translateInput method
  139.  
  140. } //ends the AlienTranslator class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement