Write a java program to simulate a die
Course:- JAVA Programming
Reference No.:- EM13926503

Assignment Help
Expertsmind Rated 4.9 / 5 based on 47215 reviews.
Review Site
Assignment Help >> JAVA Programming

1. Write a Java program to simulate a die. A die has values of either 1, 2, 3, 4, 5 or 6 on the face. You should use the Math.Random() or the java.util.Random() class to generate the values on the die. The program should prompt the user to enter how many times the die should be rolled. The output should show the results of each roll along with the total number of times 1, 2, 3, 4, 5 or 6 occurred for all rolls and the total sum comprised of the sum of all rolls. Here is a sample run:

How many times do you want to roll the die? 10

Thank you! The die roll results for 10 rolls were: 2 3 4 5 3 2 6 2 1 6 The number of occurrences of each face were as follows:

1: 1 2: 3 3: 2 4: 1 5: 1 6: 2

The sum of die rolls was: 34

2. Design a Java class named Polygon that contains:

A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.
A private double data field named sideLength that defines the length of each side. The default value should be 10.0.
A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.
A private double data field named yCoord that defines the y-coordinate of the center of the polygon. The default value should be 0.0.
A no argument constructor that creates a Polygon using the default number of sides, default side length and default x- and y-coordinates.
A constructor that creates a Polygon using a specified number of sides, side length and x- and y-coordinates.
Getter and setter methods for all data fields

A getPerimeter() method that returns a double value representing the sum of all sides.

A toString() method that displays the number of sides, side length, x-coordinate and y-coordinate values in String format.

Be sure your code compiles. (In the next question, you will create a TestPolygon class and test each of the methods)

Write a Java test program, named TestPolygon, to create five different polygons. When creating the five polygons, create one using the no argument constructor. For the remaining four, feel free to use any number of sides, side length and x-, and y-coordinates that are not equal to the default values and not equal to each other. For each of the five polygons, call all of the methods and display the results. For example for a Polygon with 3 sides, side length of 2.0 and x-coordinate and y-coordinates of 1.0, the following output may be appropriate.

toString() results: (3, 2.0, 1.0,1.0) getNumSides() results: 3 getSideLength() results: 2.0 getXCoord() results: 0.0 getYCoord() results: 0.0 getPerimeter() results: 6.0 setNumSides(4) results: 4 setSideLength(3) results: 3.0 setXCoord(2.0) results: 2.0 setYCoord(2.0) results: 2.0

Keep in mind, for five Polygons, you will have five different output results. Also, note there is no requirement to actually draw a Polygon.

Put your comment

Ask Question & Get Answers from Experts
Browse some more (JAVA Programming) Materials
design an adt for a two color double stack adt that consists of two stacks one red and one blue and has its operations color coded versions of the regular stack adt operations
Write a 4-5-page research-oriented paper in APA format that focuses on the "Popularity of Java". The paper must cite at least 5 references, not including the textbook or the
An interface - DangerousMonster which inherits the Monster interface and has a method called destroy() with void return type.(Note: Just like a class can inherit from another
Write a program that will calculate the percentage wins of a team. The program will input the number of wins and losses that a team acquired during a season. The wins should
Balancing Binary Search Trees,  Consider the file BST.java (a link to this file is provided below for downloading purposes) which defines a generic Binary Search Tree class.
Declare and initialize an integer variable named currentYear. Initialize this variable with the value of the currentYear. Use four digits for year. Use four digits for t
The first player claims a Poker hand (see "Hand Ranks" below for legal calls). When making a claim, a player says what all five dice are, not just the name of the hand. For
Modify the Java application using NetBeans IDE to meet these additional and changed business requirements:  The application will now compare the total annual compensation of a