In this task you must write a class with which we can roll five dice and then check if we have got Yatzy (all five dice show the same value). The purpose is for you to learn how to handle an array and to sort the elements in an array. Task You have probably come into contact with the dice game Yatzy before. In that game, it is a matter of collecting points by throwing five dice and putting together different combinations, such as full house (two of a number and three of another), two pairs or yatzy. Getting yatzy means that all dice show the same number. Your task is to write a class called Yatzy with which you should be able to roll the five dice and then check if it became yatzy or not. The idea is that this class should include a method for each type of combination that is possible to get points for, but that you should only implement the method that controls yatzy. The class must contain an instance variable named dices. This is an array with room for five integers. It is in this array that the values of the dice are saved after a roll. The value of dice 1 is stored in element 1 (index 0), the value of dice 2 is stored in element 2 (index 1), and so on. In the class, there should be a method called rollDisces that does not take any parameters. When this method is called, new values for all five dice must be randomized and stored in the array dices. There should be another method called rollDices (that is, a superimposed method), but that takes five parameters. All parameters are of the boolean type and indicate whether a certain dice should be rolled or not. true indicates that the dice should be rolled while false indicates that the dice should not be rolled. Ex. If the first parameter is true, dice 1 should be rolled (given a new random value), if the second parameter is false, dice 2 should not be rolled, and so on. When a new object of the class is created, all dice must be automatically rolled once. The method that controls whether the values of the dice are a yatzy or not should be called isYatzy and should not take any parameters. This method should return a boolean whose value is true if it is enyatzy and false if it is not a yatzy. When you check all scoring combinations in this game, it can be easier if the dice roll is sorted in descending order (dice with the highest value first). To check if we have a pair (at least two dice equal) we can then e.g. sort the dice and compare if the first dice has the same value as the second dice. If this is the case, we have a pair (which is also the highest possible pair because the dice are sorted in descending order). If these two dice are not equal, we compare dice 2 and dice 3, then dice 3 and dice 4 etc. You should write a method called variety that sorts the dice in descending order. The method should not take any parameters nor return anything. Finally, you should overshadow the toString method so that it returns a string with all dice values. Examples of strings that can be returned: "4 5 1 1 3", "1 2 3 4 6", "3 3 3 3 3". Write a test class where you create an object of the Yatzy class and then demonstrate all the methods in the class in an appropriate way.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

write a Java code! 

Overview
In this task you must write a class with which we can roll five dice and then check if we have got Yatzy (all five dice show the same value). The purpose is for you to learn how to handle an array and to sort the elements in an array.

Task
You have probably come into contact with the dice game Yatzy before. In that game, it is a matter of collecting points by throwing five dice and putting together different combinations, such as full house (two of a number and three of another), two pairs or yatzy. Getting yatzy means that all dice show the same number.
Your task is to write a class called Yatzy with which you should be able to roll the five dice and then check if it became yatzy or not. The idea is that this class should include a method for each type of combination that is possible to get points for, but that you should only implement the method that controls yatzy.

The class must contain an instance variable named dices. This is an array with room for five integers. It is in this array that the values of the dice are saved after a roll. The value of dice 1 is stored in element 1 (index 0), the value of dice 2 is stored in element 2 (index 1), and so on.

In the class, there should be a method called rollDisces that does not take any parameters. When this method is called, new values for all five dice must be randomized and stored in the array dices.

There should be another method called rollDices (that is, a superimposed method), but that takes five parameters. All parameters are of the boolean type and indicate whether a certain dice should be rolled or not. true indicates that the dice should be rolled while false indicates that the dice should not be rolled. Ex. If the first parameter is true, dice 1 should be rolled (given a new random value), if the second parameter is false, dice 2 should not be rolled, and so on.

When a new object of the class is created, all dice must be automatically rolled once.

The method that controls whether the values of the dice are a yatzy or not should be called isYatzy and should not take any parameters. This method should return a boolean whose value is true if it is enyatzy and false if it is not a yatzy.

When you check all scoring combinations in this game, it can be easier if the dice roll is sorted in descending order (dice with the highest value first). To check if we have a pair (at least two dice equal) we can then e.g. sort the dice and compare if the first dice has the same value as the second dice.

If this is the case, we have a pair (which is also the highest possible pair because the dice are sorted in descending order). If these two dice are not equal, we compare dice 2 and dice 3, then dice 3 and dice 4 etc.

You should write a method called variety that sorts the dice in descending order. The method should not take any parameters nor return anything.

Finally, you should overshadow the toString method so that it returns a string with all dice values. Examples of strings that can be returned:
"4 5 1 1 3", "1 2 3 4 6", "3 3 3 3 3".
Write a test class where you create an object of the Yatzy class and then demonstrate all the methods in the class in an appropriate way. Examples of driving: Attached as picture.

Rolling all the dices...
3 4 6 4 2
Rolling dices 4 and 5...
3 4 6 4 1
Sorting the dices...
6 4 4 3 1
Checking for yatzy...
Sorry, 6 4 4 3 1 in not yatzy!
Transcribed Image Text:Rolling all the dices... 3 4 6 4 2 Rolling dices 4 and 5... 3 4 6 4 1 Sorting the dices... 6 4 4 3 1 Checking for yatzy... Sorry, 6 4 4 3 1 in not yatzy!
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY