Assignment 2. Weight:. 30% of your final grade. Due: af

Added on - 25 Sep 2019

  • 13

    pages

  • 2521

    words

  • 105

    views

  • 0

    downloads

Showing pages 1 to 4 of 13 pages
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−10are worth 40 marks each. Answer anythreequestions from questions 1−7 and onlyonequestion fromquestions 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 andtutor feedback.Be sure to complete the final step—click on theSend for Markingbutton to notify your tutor.Prerequisite:Read what an API is in the textbookIntroduction to Programming Using Javaby 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 theattributesandoperationsmentioned inthe API.Notice that there is nomainmethod in the APIs. That is, you should not perform any data processingwithin themainmethod. You should rather use themainmethod to test other methods, prompt theuser 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 80characters. Then, copy each character in each input sentence in a [3x80] character array.The first sentence should be loaded into the first row in the reverse order ofcharacters– forexample, “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 ofwords– forexample, “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 by5, then the corresponding character is replaced by the letter ‘z’ – for example, “mary had a littlelamb” should be loaded into the array as “mary zad azlittze lazb” – that is, characters in indexpositions 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 printthe contents of the character array on the console.Computer Science 268: Introduction to Programming in JavaPage1of13
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 tohttp://en.wikipedia.org/wiki/Rock-paper-scissors-lizard-Spockfor 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 eitherof the computer-generated players winsfourconsecutive times.In this game, two random integers are generated in the range of [1 to 5], one per player. 1 refers toRock, 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 thefirst 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 winsthat 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 JavaPage2of13
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 between13 and 16 digits. It must start with 4 for Visa cards, 5 for Master cards, 37 for American Expresscards, and 6 for Discover cards. In 1954, Hans Luhn of IBM proposed the following algorithm forvalidating credit card numbers:a.Double every second digit from right to left (e.g., if number is 3 => 3 * 2 => 6) and add themtogether.b.If this doubling results in a two-digit number, then add the two digits to get a single-digitnumber (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 rightresults 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 validor not. You must test if the number of digits in the input is in the valid range (13 to 16), run Luhn’salgorithm to test its validity, and if it is valid, print the name of the company that offers that creditcard number.Computer Science 268: Introduction to Programming in JavaPage3of13
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 (calledcraps), you lose;II.If the sum is 7 or 11 (callednatural), you win;III.If the sum is any other value (4, 5, 6, 8, 9, or 10), a value point is established, and you continueto roll until you either roll a sum of the value point or a 7. If the sum of the new roll is equal tothe 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; rollRoll 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 JavaPage4of13
desklib-logo
You’re reading a preview
card-image

To View Complete Document

Become a Desklib Library Member.
Subscribe to our plans

Unlock This Document