[SOLVED] CSE110 -Assignment #4

30.00 $

Category:

Description

5/5 - (1 vote)

Topics

  • Object Oriented Programming (Chapter 8) o Encapsulation o Implementing classes o Implementing methods o Object construction o Constructors

Use the following Guidelines:

  • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc).
  • Keep identifiers to a reasonably short length.
  • User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects).
  • Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.
  • Use white space to make your program more read

Part 1: Test Program Development (5 pts)

Step 1 (2 Pts): Write a Java program called Assignment4.java, which reads the user input command and display a comment corresponding to the command.

  • Command A: Display ” *** Make A new FiveCards *** “
  • Command B: Display ” *** Change One Card ***”
  • Command C: Display ” *** Display Data ***”
  • Command Q: Display ” *** End of Program *** “, and quit the program
  • Others : Display ” *** Invalid command. Try Again *** “

The following is the CommandTemplate.java used in the previous project. Start from modifying this code if you have no idea what to do.

 

 

Step 2 (3 Pts): Go to the Part2 and develop FiveCards.java in the same directory of Assignment4.java. Once the Part2 is finished, modify each command block in Assignment4.java as:

  • The top level: Delete the num0 and num1 if you have them, and add the statement at the line after Scanner initialization.

FiveCards myCards = new FiveCards();

  • Command A: After displaying the message, add the statements:

System.out.println(“Type five letters without space”); String str = in.nextLine(); myCards.setCard(str); myCards.calculateScore();

System.out.println(myCards.displayData());

 

  • Command B: After displaying the message, add the statements:

System.out.println(“Type one position to change”); int pos = in.nextInt();

String lineBreak = in.nextLine();  //to skip the line break myCards.changeOne(pos); myCards.calculateScore();

System.out.println(myCards.displayData());

 

  • Command C: After displaying the message, add the statement:

 

System.out.println(myCards.displayData());

Part 2: Programming (15 pts) 

This assignment is to write a class definition (not a program, no main method) named

FiveCards (saved in a file FiveCards.java). The class has the following instance variables:

 

private  String     cards      private   int     score

private   int  changes private   String    history

 

  • The cards is a string data of five characters among “1234567890JQK”. No space between the five letters.
  • The score is 0 for no pair, 1 for one pair, 2 for two pairs, 3 for three cards, and 4 for full-house (one pair and three cards).
  • The changes is a counter to keep the number of calling change functions.
  • The history is a string data to keep all history of five cards such as following lines.

 

cards score 0 \n
cards  … score 1 \n

 

The FiveCards class must include the following constructor and methods: (If your class does not contain any of the following methods, points will be deducted).

Method Description of the Method
public  FiveCards()

 

Make a new FiveCards object and initialize the instance variables cards, score, changes,

and history as “”, 0, 0, and “”  (2 Pts).

public void setCards(String str) Set the cards with the input string data.  (2 Pts).
public void  changeOne (int pos) Replace one character of the given position [0,4] with a random letter from “1234567890JQK”. Increase the value of changes by one. (3 Pts)
public void calculateScore() Calculate the pairs and update the score as:

 

0: No pair among 5 letters in the cards.

1: One pair in the cards

2: Two pairs in the cards

3: Three same letters in the cards

4: Full House (one pair and three cards)   

 

You may use the calculatePair (char) 13 times for each letter, which is explained below.

 (5 Pt)

private  int

(char c)

Return how many times the input character is used in the cards. For example, when the cards is “01KQK”, the calculatePair(‘K’) returns 2.
calculatePair
*) This may be NOT necessary, if you do in the
different way.
public String displayData() Call the calculateScore() in the method to

update the score, and return a string data of history for each change. Don’t use System.out method in the method. Look at the format in Sample Output (3 Pts)

 

Sample Score and Change

 

[Cards][Score][Changes]

12345    0    1

22345    1    2

22335    2    3

22235    3    4

22255    4    5

72255    2    6

72259    1    7

7K259    0    8

 

void XXX(int x) {      }

     String YYY(int x, int y){return “test”;}

 

Note: 

  • Make only one in. If your program does not run correctly because of this issue, you lose the points (-3Pts).
  • Use only the Java statements that have been covered in class to date. DO NOT use any other items out of the Chapter 1- 5, and 8 (Array, Array List, exit() etc are not allowed to use). If in doubt, ask the instructor or TA. If you use them, then you lose the points of task. (-3Pts).