Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.71 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3. // Main method contains code for questions 6, 8, 12 and 13
  4.  
  5. class Main {
  6.  
  7. //Question 1 add-function
  8. public static int add(int a, int b) {
  9. return a + b;
  10. }
  11.  
  12.  
  13. // Question 2 age-calculator
  14. public static String CalculateAge(int a, int b){
  15. int firstAge = b - a - 1;
  16. int secondAge = b - a;
  17. String Answer = "You are either " + firstAge + " or " + secondAge;
  18. return Answer;
  19. }
  20.  
  21.  
  22. // Question 3 exes-and-ohs
  23. public static boolean sameXAndO(String XO){
  24. int numOfX = 0;
  25. int numOfO = 0;
  26. boolean sameNumOfXAndO;
  27. for (int i = 0; i < XO.length(); i++){
  28. switch(Character.toLowerCase(XO.charAt(i))){
  29. case 'x':
  30. ++numOfX;
  31. break;
  32. case 'o':
  33. ++numOfO;
  34. break;
  35.  
  36. default:
  37. }
  38. }
  39.  
  40. if (numOfX == numOfO) {
  41. sameNumOfXAndO = true;
  42. } else {
  43. sameNumOfXAndO = false;
  44. }
  45. return sameNumOfXAndO;
  46. }
  47.  
  48. // Question 4 endsly
  49. public static boolean endsly(String a){
  50. boolean endsInLy;
  51.  
  52. if(a.charAt(a.length() - 2) == 'l' && a.charAt(a.length() - 1) == 'y'){
  53. endsInLy = true;
  54. } else {
  55. endsInLy = false;
  56. }
  57. return endsInLy;
  58. }
  59. // Question 5 chessboard
  60. public static void chessboard(int size){
  61.  
  62. StringBuilder startsWithSpace = new StringBuilder();
  63. StringBuilder startsWithPound = new StringBuilder();
  64. String chessBoard = null;
  65. for(int i = 1; i<=(size/2); i++){
  66. startsWithPound.append("# ");
  67. }
  68.  
  69. for (int i = 1; i<=(size/2); i++){
  70. startsWithSpace.append(" #");
  71. }
  72.  
  73. if (size%2 == 1){
  74. startsWithPound = startsWithPound.append("#");
  75. startsWithSpace = startsWithSpace.append(" ");
  76. }
  77.  
  78. for (int i = 1; i<=(size); i++){
  79. if (i==1){
  80. chessBoard = startsWithSpace.toString();
  81. } else if (i%2 ==0){
  82. chessBoard = chessBoard + "\n" + startsWithPound.toString();
  83. } else if(i%2==1){
  84. chessBoard = chessBoard + "\n" +startsWithSpace.toString();
  85. }
  86. }
  87. System.out.println(chessBoard);
  88. }
  89.  
  90. // Question 7 string-elide
  91. public static void elide(String aString){
  92. String newString = null;
  93.  
  94. if (aString.length() <= 6){
  95. System.out.println(aString);
  96. } else if(aString.length() > 6){
  97. System.out.println(aString.substring(0, 3) + "...");
  98. }
  99. }
  100.  
  101. // Question 9 Count Code
  102. public static void countCode(String text){
  103. int numberOfWordCode = 0;
  104.  
  105. for (int i = 0; i < text.length(); i++){
  106. if (text.charAt(i) != 'c'){
  107. continue;
  108. } else if (text.charAt(i) == 'c'){
  109. if(text.charAt(i+1) == 'o'){
  110. if(text.charAt(i+3) == 'e'){
  111. ++numberOfWordCode;
  112. }
  113. }
  114. }
  115. }
  116. System.out.println(numberOfWordCode);
  117. }
  118.  
  119.  
  120. // Question 10
  121. public static int countVowels(String text){
  122. int numOfVowels = 0;
  123. for (int i = 0; i < text.length(); i++){
  124. switch(text.charAt(i)){
  125. case 'a':
  126. case 'e':
  127. case 'i':
  128. case 'o':
  129. case 'u':
  130. ++numOfVowels;
  131. break;
  132. default:
  133. }
  134. }
  135. return numOfVowels;
  136. }
  137.  
  138. // Question 11 cut-a-string-at-character
  139. public static String cutStringAtCharacter(String someText, char aChar){
  140. String leftoverString = null;
  141.  
  142. for (int i = 0; i < someText.length(); i++){
  143. if (someText.charAt(i) == aChar){
  144. leftoverString = someText.substring((i+1), (someText.length()-1));
  145. break;
  146. }
  147. }
  148. return leftoverString;
  149. }
  150.  
  151.  
  152.  
  153. // ↓ Main method contains code for questions 6, 8, 12 and 13
  154.  
  155. public static void main(String[] args) {
  156. String elideString = "Thanks for checking my homework!";
  157. int chessBoardDimensions = 7;
  158.  
  159. /* I left some answers calling the function through a system.out.println, and others just by calling the function itself because I wasn't sure which method is preferred and/or more robust, so it's somewhat arbitrary */
  160.  
  161. /* Question 1 */ System.out.println(add(4,5));
  162.  
  163. /* Question 2 */ System.out.println(CalculateAge(1990, 2016));
  164.  
  165. /* Question 3*/ System.out.println(sameXAndO("XOOX"));
  166.  
  167. /* Question 4*/ System.out.println(endsly("endlessly"));
  168.  
  169. /* Question 5*/ chessboard(chessBoardDimensions);
  170.  
  171. /* Question 7*/ elide(elideString);
  172.  
  173. /* Question 9*/ countCode("kskscocededdecozenfgfgcode");
  174.  
  175. /* Question 10*/ System.out.println(countVowels("there are 11 vowels in this sentence"));
  176.  
  177. /* Question 11*/ System.out.println(cutStringAtCharacter("I wonder what will print?", 'a'));
  178.  
  179. // Question 6 Scanner-hungry-hippos
  180. System.out.println("Please enter a food:");
  181. String UserInput = readInput();
  182. String Answer = null;
  183. char firstLetter = Character.toLowerCase(UserInput.charAt(0));
  184. if (firstLetter == 'h'){
  185. Answer = "Yum!";
  186. } else {
  187. Answer = "Yuck!";
  188. }
  189. System.out.println(Answer);
  190.  
  191. // Question 8 triangle
  192. StringBuilder poundTriangle = new StringBuilder();
  193. for (int i = 1; i <=7; i++){
  194. poundTriangle.append("#");
  195. System.out.println(poundTriangle);
  196. }
  197.  
  198. // Question 12 twelve-days
  199. for (int i = 12; i > 0; i--){
  200. switch(i){
  201. case 12:
  202. System.out.println("12 lords a leaping");
  203. case 11:
  204. System.out.println("11 ladies dancing");
  205. case 10:
  206. System.out.println("10 pipers pipping");
  207. }
  208. }
  209.  
  210. for (int i = 1; i <= 12; i++){
  211. String dayNumber;
  212. if (i == 1) {
  213. dayNumber = Integer.toString(i) + "st";
  214. } else if (i == 2) {
  215. dayNumber = Integer.toString(i) + "nd";
  216. } else if (i == 3) {
  217. dayNumber = Integer.toString(i) + "rd";
  218. } else {
  219. dayNumber = Integer.toString(i) + "th";
  220. }
  221.  
  222. String repeatingLyric = "On the " + dayNumber + " day of christmas my true love gave to me";
  223. System.out.println(repeatingLyric);
  224.  
  225.  
  226. switch (i){
  227. case 1:
  228. System.out.println("a Partridge in a pear tree");
  229. continue;
  230. }
  231.  
  232. switch (i){
  233. case 12:
  234. System.out.println("12 Bells ringing");
  235. case 11:
  236. System.out.println("11 ladies dancing");
  237. case 10:
  238. System.out.println("10 Pipers piping");
  239. case 9:
  240. System.out.println("9 Drummers drumming");
  241. case 8:
  242. System.out.println("8 Maids a milking");
  243. case 7:
  244. System.out.println("7 Swans a swimming");
  245. case 6:
  246. System.out.println("6 Geese a laying");
  247. case 5:
  248. System.out.println("5 Gold rings");
  249. case 4:
  250. System.out.println("4 Colly birds");
  251. case 3:
  252. System.out.println("3 French hens");
  253. case 2:
  254. System.out.println("2 Turtle doves");
  255. case 1:
  256. System.out.println("and a Partridge in a pear tree");
  257. }
  258. }
  259.  
  260. // Question 13 scanner-ice-cream-start-up, continued on line 307
  261. int numOfToppings = 0;
  262. double iceCreamCost = 2.33;
  263.  
  264. Random random = new Random();
  265. String deliveryTime = Integer.toString(random.nextInt(50));
  266.  
  267. // I found the above code on stack overflow for finding a random number. I understand how it works, as it is creating a new variable in the Random class and assigns it a new class constructor. Then I created a string variable that takes the value of a randomly generated integer found by using the nextInt() method on the random variable created in the line before it, and turns that value into a string. Hope this makes sense, still trying to learn the java lingo.
  268.  
  269. Scanner scanner = new Scanner(System.in);
  270. System.out.println("Hello! Welcome to Kwik Kustards, the Uber for Ice Cream! Before we start, may I please know your name?");
  271. String userName = scanner.nextLine();
  272. System.out.println("Thank you " + userName + ", let's get started. Which flavor of ice cream would you like? We literally have everything so don't worry, we probably have what you're looking for :)");
  273. String iceCreamFlavor = scanner.nextLine();
  274. System.out.println("Great! You chose " + iceCreamFlavor + ". Now, for the toppings. Please respond with yes or no. Would you like rainbow sprinkles?");
  275. String topping1 = scanner.nextLine();
  276. System.out.println("Fantastic. would you like chocolate chips?");
  277. String topping2 = scanner.nextLine();
  278. System.out.println("Alright, finally, would you like cookie dough?");
  279. String topping3 = scanner.nextLine();
  280.  
  281. if (topping1.equalsIgnoreCase("yes")){
  282. ++numOfToppings;
  283. }
  284.  
  285. if (topping2.equalsIgnoreCase("yes")){
  286. ++numOfToppings;
  287. }
  288.  
  289. if (topping3.equalsIgnoreCase("yes")){
  290. ++numOfToppings;
  291. }
  292.  
  293.  
  294. String finalCost = Double.toString(totalCostofIceCream(numOfToppings));
  295. System.out.println("Great, you have ordered " + iceCreamFlavor + " ice cream with " + Integer.toString(numOfToppings) + " toppings. Your total is $" + finalCost + ", and your ice cream will arrive in about " + deliveryTime + " minutes");
  296.  
  297. }
  298.  
  299.  
  300. public static String readInput(){
  301. Scanner scanner = new Scanner(System.in);
  302. return scanner.next();
  303. }
  304.  
  305. // Question 13 Continued
  306. public static double totalCostofIceCream(int toppings){
  307. double base = 2.33;
  308. double costOfToppings = toppings * 0.33;
  309. double totalCost = base + costOfToppings;
  310. return totalCost;
  311. }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement