[Solved] Computer Science 268: Assignment 2

$25

File Name: Computer_Science_268:_Assignment_2.zip
File Size: 320.28 KB

SKU: [Solved] Computer Science 268: Assignment 2 Category: Tag:
5/5 - (1 vote)
  1. Read three sentences from the console application. Each sentence should not exceed 80characters. 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 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 of words 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.
  2. Write a program that plays the Rock-Paper-Scissors-Lizard-Spock game. Refer tohttp://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 eitherof 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 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 scissors
  3. 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 Luhns 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 Luhnsalgorithm to test its validity, and if it is valid, print the name of the company that offers that creditcard number.
  4. 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 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 win You rolled 1 + 2 = 3; you lose You rolled 2 + 2 = 4; you establish the value point 4; Roll again 2 + 3 = 5; roll Roll again 2 + 1 = 3; roll Roll again 2 + 2 = 4; you win You rolled 2 + 6 = 8; you establish the value point 8; Roll again 4 + 4 = 8; you win You rolled 3 + 2 = 5; you establish the value point 5; Roll again 1 + 1 = 2; roll Roll again 2 + 2 = 4; rollComputer Science 268: Introduction to Programming in Java Page 5 of 12 Roll again 1 + 1 = 2; roll Roll again 3 + 4 = 7; you loseDevelop a program that plays craps with a player three times. At the end, the program prints thenumber of times the player won and the number of times the player lost.
  5. Create three classes: Village, Citizen, and ComputeIntellect.The Village class has an instance variable called numberOfCitizens and an array that holds amaximum of 100 Citizen objects.The Citizen class has citizenId and educationalQualification as instance variables.The ComputeIntellect class has a distributionOfQualification() method.Create 100 Citizen objects using citizenId for the range [1 to 100]. Randomly generate theeducational qualification in the range [1 to 4], where 1 = high school, 2 = undergraduate, 3 =postgraduate, and 4 = doctorate.Store these 100 objects in a Village object using an array any array of your choice.The distributionOfQualification() method loops through the 100 objects and counts thenumber of citizens corresponding to each of the four educational qualifications.
  6. Implement a Java method that prints out the day of the week for a given day (1. . . 31),month (1 . . . 12) and year in the range of March 1900 to February 2100.Calculate the day of the week for the dates between March 1900 and February 2100 as follows:First, you have to calculate the total number of days from 1900/1/1 to the given date (see below fordetails).Secondly, you divide this number by 7 with an integer remainder: This now is the day of the week,with 0 as Sunday, 1 as Monday, and so on.To calculate the total number of days, you have to implement the following steps: Subtract 1900 from the given year, and multiply the result by 365 Add the missing leap years by adding (year 1900) / 4.Computer Science 268: Introduction to Programming in Java Page 7 of 12 If the year itself is a leap year and the month is January or February, you have to subtract 1 fromthe previous result. Now add all the days of the months of the given year to the result (in the case of February, it isalways 28 because the additional day for a leap year has already been added in the calculation).
  7. Create a Person class that includes the name of the person, the weight of the person (inpounds), and the height of the person (in inches). For the data listed in the table below, create fourPerson objects. Compute their individual body mass index (BMI) and store it as part of theseobjects. Further, determine their weight category (see below) and add that information as part ofthe object as well. Store each of these four Person objects, their corresponding BMI, and weightcategory in a different ArrayList and develop get and set methods to access elements in thatArrayList.Name Weight (pounds) Height (inches)Andrew 125.5 55.1Boyd 150.0 67Cathy 135 72.3
  8. The following is a score of a single game in badminton. The top row is the score forPlayer 1. The second row is the score for Player 2. Represent this data in an ArrayList in a classcalled BadmintonScoring.0 1 2 3 4 5I. Compute the maximum points scored by Player 1 and Player 2.II. Compute the maximum number of points scored in a continuous sequence by Player 1 andPlayer 2. Hint: Player 1 scored the sequence 0-1-2, which implies s/he scored 2 points in acontinuous sequence. Similarly, for Player 2, 16-17-18-19-20-21 implies that s/he scored 5points in a continuous sequence.III. Extend BadmintonScoring to associate each point scored by a player with a particular strokethat earned that point, using the notion of association list. You can represent each point as anobject and store the score of a player in an association list (refer to Chapter 7, section 7.4.2 fordetails). For example, when Player 1 scored his/her first point, instead of just 1, it could havebeen {1, slice}. Thus, each point is augmented with the type of stroke from the following list:a. sliceb. drivec. smashd. drope. net-shotIV. Store the following score of a single game using the modified BadmintonScoring class.V. Identify the type of stroke that earned most points for each player.
  9. Assume that a robot is placed in position [0, 0]. Now randomly generate a move. The movecould take the robot to one of the eight possible adjacent slots {up, down, left, right, left-upcorner, left-down-corner, right-up-corner, and right-down-corner} these slots are representedby {1, 2, 3, 4, 5, 6, 7, 8}. However, at [0, 0], the robot only has three possible slots to move to right, down, right-down-corner.Create another robot called R2 and place it on [9, 9].Now randomly generate an integer in the range of [1 to 8]. This first random integer correspondsto a possible move for Robot R1. If the move is valid, then move R1 to its new slot. A move isinvalid if it takes the robot out of bounds of the [1010] matrix. If the move is invalid, then keepgenerating random integers until a valid move is found.Repeat this procedure for the second Robot R2.If both R1 and R2 are in the same slot, then stop, print the final slot, print the sequence ofrandom numbers that led R1 to this slot, and the print the sequence of random numbers thatled R2 to the same slot.Implement this program with a Robot class and a MovingRobot subclass.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] Computer Science 268: Assignment 2
$25