In: Computer Science
Write any java programming to meet the following requirements.
Your project must meet the following requirements:
1. Specify input specification
Design input
2. Specify Output Specification
Design Output
3. The class must include set methods and get methods ( with or without parameters both)
4. Must include Array structure
5. Must include Input or Output Files or both
6. Demonstrate your knowledge of an Applet
SOURCE CODE: *Please follow the comments to better understand the code. **Please look at the Screenshot below and use this code to copy-paste. ***The code in the below screenshot is neatly indented for better understanding. This is a very big question. I have solved all of them. Please give me an upvote dear, Much Appreciated.!!
TwoDice.java
import java.util.Random; public class TwoDice { private int dLeft; private int dRight; Random random=new Random(); // default constructor which sets dLeft and dRight to random "real" dice value. public TwoDice() { dLeft=1+random.nextInt(6); dRight = 1 + random.nextInt(6); } // constructor with parameters which sets the dice to specific values. public TwoDice(int dLeft, int dRight) { // If the parameters are not between 1-6, force it to be 1. if(dLeft>=1 && dLeft<=6) this.dLeft = dLeft; else this.dLeft = 1; // If the parameters are not between 1-6, force it to be 1. if(dRight>=1 && dRight<=6) this.dRight = dRight; else this.dRight = 1; } // accessors getDLeft(), getDRight(), and getTotal( ) public int getDLeft() { return dLeft; } public int getDRight() { return dRight; } public int getTotal() { return dLeft+dRight; } // mutator setDLeft( ), setDRight( ) public void setDLeft(int dLeft) { this.dLeft = dLeft; } public void setDRight(int dRight) { this.dRight = dRight; } // and roll( ) which rolls them to a value in the parameter. public void roll() { dLeft=1+random.nextInt(6); dRight = 1 + random.nextInt(6); } // toString( ) e.g. returns "4, 2" which are the values of the two dice public String toString() { return dLeft+", "+dRight; } // printMe( ) for debugging. It calls on toString( ). public void printMe() { System.out.println(this.toString()); } // equals( ) to compare one pair of dice with another pair of dice. // Have it return true if one pair of dice has the same values as the other pair, // regardless of which is the left and which is the right dice. public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof TwoDice)) return false; TwoDice twoDice = (TwoDice) o; return (dLeft == twoDice.dLeft && dRight == twoDice.dRight) || (dLeft == twoDice.dRight && dRight == twoDice.dLeft); } }
======
TwoDiceClient.java
import java.util.Scanner; public class TwoDiceClient { public static void main(String[] args) { System.out.println("Welcome To Yourname Dice Game...!!"); Scanner scanner=new Scanner(System.in); System.out.print("What is your name? "); String name=scanner.nextLine(); System.out.print("How many rounds to play? "); int rounds=scanner.nextInt(); TwoDice twoDice=new TwoDice(); int computerWins=0, userWins=0, tied=0; // start here // repeat for rounds times for(int i=1;i<=rounds;i++) { // print the output System.out.print("Play "+i+": "+name+" "+twoDice.getDLeft()+", Computer "+twoDice.getDRight()+"."); // check who won if(twoDice.getDLeft() > twoDice.getDRight()) { System.out.println(" " + name + " wins."); // increment count userWins++; } else if(twoDice.getDLeft() < twoDice.getDRight()) { System.out.println(" Computer wins."); // increment count computerWins++; } else { System.out.println(" Tied."); // increment count tied++; } // ask to press enter System.out.println("Please enter to play another round."); scanner.nextLine(); // Roll the dice twoDice.roll(); } System.out.println("Score: "+name+" won "+userWins+" times. Computer won "+computerWins+" times. Tied "+tied+" times."); } }
OUTPUTS:
CODE SCREENSHOTS: