Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

[CSE 007] Lab 3: if/Selection Statements


Date Assigned: 10 Feb 2023 Date Due: 16 Feb 2023 @11:59pm

Submission: Lab3.pdf, Card.java

Part 1: Code Analysis and Exam Prep


To Do: The following code compiles correctly. What will be displayed if the initial value of c is assigned (in separate runs of the program) according to the a, b, c, d and e in the box below? Be sure to explain why your answer makes sense (how/why the output is    what it is) for full credit. Save your work in a file named Lab3.pdf.

Part 2: Playing Cards using Selection Statements

Objective: The purpose of this exercise is to give you practice using if/selection statements and the Math class.

Scenario: You are a magician and you are practicing your card tricks. Since you don’t     want to reveal your secrets, you write a program that will pick a random card from the   deck so you can practice your tricks alone. Use a random number generator to select a    number from 0 to 51 (inclusive).  Each number represents one card, and the suits are      grouped: Cards 0-12 represent the diamonds, 13-25 represent the clubs, then hearts, then spades. In all suits, card identities ascend in step with the card number: 13 is the ace of   clubs, 14 is the 2 of clubs, and 25 is the king of clubs.

Follow the steps below to write your java program.

1.   Write your java code in a program called Card.java

2.   Generate a random number between 0 and 51 and assign it to cardNumber

1.  Use  Math.random () to generate an integer corresponding to a card in a deck (52 total)

2.   See relevant notes below for more information about using this function

3.   Declare two String variables: a String corresponding to the name of the suit and a String corresponding to the identity of the card (String suitName, cardIdentity;).

4. Use if statements to assign the suit name. Determine the suit name by using integer divide. (cardNumber / 13)

1.   Cards 0-12 represent the diamond

2.   13-25 represent clubs,

3.   26-38 represents hearts

4.   39-51 represents spades

5. Use a switch statement to assign the card identity. Use the modulus operation to determine the card identity as follows: (cardNumber % 13)

1.   0 = Ace

2.   1 - 9 = correspond to numbered cards 2-10

3.   10 = Jack

4.   11 = Queen

5.   12 = King

6.   Print out the name of the randomly selected card.

7.   Make sure your code is properly commented and you use good style (camelCase, proper indentation, etc).

Examples ofRandom Numbers:

Random Number = 51 (CardNumber)

51 / 13 = 3  Suit is Spade (SuitName)

51 % 13 = 12  Identity is King (Card Identity)

King of Spades

Random number = 24 (CardNumber)

24 / 13 = 1 Suit is Clubs (SuitName)

24 % 13 = 11 Identity is Queen (Card Identity)

Queen of Clubs

Here are four sample runs of the program: (One card/number generated per run)

Random Number: 18; You picked the 6 of Clubs

Random Number: 36; You picked the Jack of Hearts

Random Number: 39; You picked the Ace of Spades

Random Number:  3; You picked the 4 of Diamonds

Important note: this lab can be completed multiple ways: using lots of if statements,      using multiple switch statements, using the modulus operator, etc.  It is crucial that you understand all the different ways the lab can be completed, because they are all related to the new material we have just learned in the class. So, to prepare for the exam, do the lab a few different ways. However, you need only submit ONE program for this lab.

Recall:

1.   Remember to include a header and comments in your code.

2.   Read through your compiler errors before asking for help.

3.   SAVE AND COMPILE AS YOU GO!

4.  When using a switch statement, all statements under a matching condition will run unless there is the keyword break.

5.   There is no import needed to use the Math class

6.   Class Math has a method random () that returns a random double in the range [0,1). Typically, we want a random number among some range of integers (or       whole numbers). For example, we want to pick a random number from 2 to 13     (including 13), so we can generate a random number from 0  to 11 and then add 2 as shown below.

int number = (int) (Math.random() * 12) + 2;

In general, use the following to generate an int from baseNum to upperBound, inclusive.

int number = (int) (Math.random()* (upperBound-baseNum+1))+baseNum;

Since random() returns a double, we use (int) to cast the double to an integer   (truncating any decimals). We do this because we will use these integers for our switch statements. You will need to consider the range of numbers you want to randomly select from, given the problem statement above.

0: Ace | Diamonds

1: 2 |Diamonds

2: 3 |Diamonds

3: 4 |Diamonds

4: 5 |Diamonds

5: 6 |Diamonds

6: 7 |Diamonds

7: 8 |Diamonds

8: 9 |Diamonds

9: 10 |Diamonds

10:Jack |Diamonds

11:Queen |Diamonds

12:King |Diamonds

13:Ace |Clubs

14:2 |Clubs

15:3 |Clubs

16:4 |Clubs

17:5 |Clubs

18:6 |Clubs

19:7 |Clubs

20:8 |Clubs

21:9 |Clubs

22:10 |Clubs

23:Jack |Clubs

24:Queen |Clubs

25:King |Clubs

26:Ace |Hearts

27:2 |Hearts

28:3 |Hearts

29:4 |Hearts

30:5 |Hearts

31:6 |Hearts

32:7 |Hearts

33:8 |Hearts

34:9 |Hearts

35:10 |Hearts

36:Jack |Hearts

37:Queen |Hearts

38:King |Hearts

39:Ace |Spades

40:2 |Spades

41:3 |Spades

42:4 |Spades

43:5 |Spades

44:6 |Spades

45:7 |Spades

46:8 |Spades

47:9 |Spades

48:10 |Spades

49:Jack |Spades

50:Queen |Spades

51:King |Spades

Grading Rubric

Part 1: 20 points (+4 pts each test: +2 pts for correct ans and +2 pts for explanation) Part 2: 80 points

x/10 points - good programming practices are implemented

x/30 points - reasonable code submitted

x/10 points - code compiles

x/5 points - use an if/else for suit

x/5 points - use switch for rank

x/5 points - generates a random number in the correct range

●   x/15 points - Tested 5 times and each print correct rank/suit (check by printing random number)