logo

Craps and Village

   

Added on  2019-09-26

13 Pages2521 Words105 Views
 | 
 | 
 | 
Assignment 2Weight: 30% of your final gradeDue: after Unit 7This assignment comprises 10 questions. Questions 1−7 are worth 20 marks each, and questions 8−10 are worth 40 marks each. Answer any three questions from questions 1−7 and only one question from questions 8−10. You are expected to complete this assignment before you start Unit 8. Upload your completed assignment to the Assignment 2 link on the course home page for marking and tutor feedback.Be sure to complete the final step—click on the Send for Marking button to notify your tutor.Prerequisite: Read what an API is in the textbook Introduction to Programming Using Java by David J. Eck on pages 142−143.When solving the problems in this assignment, you must follow the application programming interface (API) expected in each problem. You should implement all the attributes and operations mentioned in the API. Notice that there is no main method in the APIs. That is, you should not perform any data processing within the main method. You should rather use the main method to test other methods, prompt the user for some inputs, and display the results returned by your methods.1.[20 marks] Read three sentences from the console application. Each sentence should not exceed 80 characters. Then, copy each character in each input sentence in a [3 x 80] character array. The first sentence should be loaded into the first row in the reverse order of characters – for example, “mary had a little lamb” should be loaded into the array as “bmal elttil a dah yram”. The second sentence should be loaded into the second row in the reverse order of words – for example, “mary had a little lamb” should be loaded into the array as “lamb little a had mary”. The third sentence should be loaded into the third row where if the index of the array is divisible by 5, then the corresponding character is replaced by the letter ‘z’ – for example, “mary had a little lamb” should be loaded into the array as “mary zad azlittze lazb” – that is, characters in index positions 5, 10, 15, and 20 were replaced by ‘z’.Note that an empty space is also a character, and that the index starts from position 0. Now print the contents of the character array on the console.Computer Science 268: Introduction to Programming in JavaPage 1 of 13
Craps and Village_1

ReversedSentenceAttributesOperations+ public static String change5thPosition(String s)+ public static String printChar2DArray(char[][] arr)+ public static String reverseByCharacter(String s)+ public static String reverseByWord(String s)+ public static String truncateSentence(String s)2.[20 marks] Write a program that plays the Rock-Paper-Scissors-Lizard-Spock game. Refer to http://en.wikipedia.org/wiki/Rock-paper-scissors-lizard-Spock for more information. Normally, one player is a human and the other is the computer program. However, in this exercise, the program will generate two players who play against each other. The play continues until either of the computer-generated players wins four consecutive times. In this game, two random integers are generated in the range of [1 to 5], one per player. 1 refers to Rock, 2 refers to Paper, 3 refers to Scissors, 4 refers to Lizard, and 5 refers to Spock. For example, if the computer randomly generates integers 2 and 5 in the first iteration, 2 is for the first player and 5 is for the second player. Based on Rule 8 in the following 10 rules, Paper (2) disproves Spock (5), so Player 1 wins. Repeat it to generate one more pair and determine who wins that iteration. Continue the iterations until one player wins four consecutive times.Rule 1: Scissors cut paperRule 2: Paper covers rockRule 3: Rock crushes lizardRule 4: Lizard poisons SpockRule 5: Spock smashes (or melts) scissorsRule 6: Scissors decapitate lizardRule 7: Lizard eats paperRule 8: Paper disproves SpockRule 9: Spock vaporizes rockRule 10: Rock breaks scissorsComputer Science 268: Introduction to Programming in JavaPage 2 of 13
Craps and Village_2

RockPaperScissorsLizardSpockAttributes+ private int consecutiveWins+ private int lastWinner+ public static final int LIZARD+ public static final int PAPER+ public static final int PLAYER1+ public static final int PLAYER2+ public static final int ROCK+ public static final int SCISSORS+ public static final int SPOCKOperations+ public int getConsecutiveWins()+ public int getLastWinner()+ public int random()+ public static String convert(int i)+ public void play(int player1, int player2)3.[20 marks] Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. It must start with 4 for Visa cards, 5 for Master cards, 37 for American Express cards, and 6 for Discover cards. In 1954, Hans Luhn of IBM proposed the following algorithm for validating credit card numbers:a.Double every second digit from right to left (e.g., if number is 3 => 3 * 2 => 6) and add them together. b.If this doubling results in a two-digit number, then add the two digits to get a single-digit number (e.g., if number is 5 => 5 * 2 => 10 => 1+0 => 1). So, for the credit card number 4388576018402626, doubling all second digits from the right results in (2 * 2 = 4) + (2 * 2 = 4) + (4 * 2 = 8) + (1 * 2 = 2) + (6 * 2 = 12 = 1 + 2 = 3) + (5 * 2 = 10 = 1 + 0 = 1) + (8 * 2 = 16 = 1 + 6 = 7) + (4 * 2 = 8). This totals to 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37. Add all digits in the odd places from right to left. The leftmost digit of the credit card number is at index 0; 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38.Add results from steps (a) and (b) and see if divisible by 10. If it is, then the card number is valid; otherwise invalid. 37 + 38 = 75 is not divisible by 10, so it is an invalid credit card number.Implement Luhn’s algorithm in a program to determine whether a given credit card number is valid or not. You must test if the number of digits in the input is in the valid range (13 to 16), run Luhn’s algorithm to test its validity, and if it is valid, print the name of the company that offers that credit card number.Computer Science 268: Introduction to Programming in JavaPage 3 of 13
Craps and Village_3

4.[20 marks] Craps is a dice game where two dice are rolled. Each die has six faces representing values1, 2, 3, 4, 5, or 6.I.If the sum is 2, 3, or 12 (called craps), you lose; II.If the sum is 7 or 11 (called natural), you win;III.If the sum is any other value (4, 5, 6, 8, 9, or 10), a value point is established, and you continue to roll until you either roll a sum of the value point or a 7. If the sum of the new roll is equal to the value point, then you win; if the sum of the new roll is equal to 7, then you lose.Remember, in option (III), you continue to roll until you get a 7 or the value point.Sample runs:You rolled 5 + 6 = 11; you winYou rolled 1 + 2 = 3; you loseYou rolled 2 + 2 = 4; you establish the value point 4;Roll again 2 + 3 = 5; roll Roll again 2 + 1 = 3; rollRoll again 2 + 2 = 4; you winYou rolled 2 + 6 = 8; you establish the value point 8;Roll again 4 + 4 = 8; you winYou rolled 3 + 2 = 5; you establish the value point 5;Roll again 1 + 1 = 2; rollComputer Science 268: Introduction to Programming in JavaPage 4 of 13
Craps and Village_4

End of preview

Want to access all the pages? Upload your documents or become a member.