Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2007, 03:18 AM
Member
 
Join Date: Jul 2007
Posts: 40
toby is on a distinguished road
Help with my assignment java
I have an assignment:
Design and implement a class called PairOfDice, composed of tow die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that return the current sum of the die.
Create a driver class called RollingDice2 to instantiate and use a PairofDice object.

This is what I have so far I just wanted to see if I'm at least on the right track for this program.

Code:
public class PairofDie { private final int MAX = 6; private int faceValue; public Die1() { faceValue = 1; } public int roll() { faceValue = (int)(Math.random() * Max) + 1; return faceValue; } public void setFaceValue (int value) { faceValue = value; } Public int getFaceValue() { return faceValue; } public String toString() { string result = Integer.toString (faceValue); return result; } } public Die2() { faceValue = 1; } public int roll() { faceValue = (int)(Math.random() * Max) + 1; return faceValue; } public void setFaceValue (int value) { faceValue = value; } Public int getFaceValue() { return faceValue; } public String toString() { string result = Integer.toString (faceValue); return result; } }
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 07:59 AM
Member
 
Join Date: Jul 2007
Posts: 35
carl is on a distinguished road
Ok, to start, you'll need these variables for your PairofDie class:
Code:
private Die die1; private Die die2;
Now, for die1 and die2, you'll notice they are declared as Die objects. This means that you'll have to create a Die class that will create a Die object, like so:
Code:
class Die { private final int MAX = 6; private int facevalue; public Die() { /* Constructor */ } public void roll() { facevalue = (int)(Math.random() * MAX) + 1; } }
In the above, you'll include your methods for getting and setting the values of the Die facevalue.

Then in your your PairofDie constructor, you'd create your two dice:
Code:
public PairofDie() { die1 = new Die(); die2 = new Die(); }
Rolling your dice would be a matter of:
Code:
public void rolldice() { die1.roll(); die2.roll(); }
Then you'd include a method to return the sum of die1 and die2 in your PairofDie class.

After all that, you'll create that RollingDice class that'll have the main method which will instantiate and utilize the PairofDie class.

3 files (and classes) in all: Die.java, PairofDie.java, and RollingDice.java.
Greetings.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java assignment - couple methods don't know how to figure out Snowboardmylife New To Java 1 04-16-2008 12:52 PM
Java assignment xtianah77 New To Java 1 02-18-2008 01:54 AM
Please help... assignment for school confused2000 New To Java 3 11-12-2007 10:12 AM
for Assignment plz help assamhammad New To Java 1 11-06-2007 10:35 PM
java assignment, need help bad. carlos123 New To Java 1 11-06-2007 06:53 PM


All times are GMT +3. The time now is 10:38 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org