Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help writing a Template Class

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help writing a Template Class

    ***if i get useful help with this assignment i will consider hiring you for help with future assignments****


    I have been trying to figure this out for hours... My book and the lectures do not provide any clear examples of how to do this. It's so frustrating because I have other school courses to worry about as well but this is just taking FOREVER


    Write a template class to represent a dice. The dice has one attribute which is number of sides. The class should have all the usual class methods and an additional method that will “roll” the dice. This method uses the Random number generator to “roll” a number between one and the number of sides of the dice; the method returns the number rolled.


    This is what I have so far:
    import java.util.random;
     
    public class Dice {
     
    	{
    		int sideOne;
    		int sideTwo;
    		int sideThree;
    		int sideFour;
    		int sideFive;
    		int sideSix;
     
    		public Dice() {
    			sideOne = 1;
    			sideTwo = 2;
    			sideThree = 3;	
    			sideFour = 4;
    			sideFive = 5;
    			sideSix = 6;
     
    		}
     
    		//use the random class in the methods
     
     
    	}
     
    }




    After I'm done with this class I need to write an application class that rolls two dice but I'll try to figure that out on my own once i get this template class written
    Last edited by copeg; October 5th, 2010 at 09:12 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help writing a Template Class

    For future reference, please surround your code with the code tags. Much easier to read.

    Have a look at the API for Random (Java Platform SE 6). It provides functions to retrieve random values. If you wish to retrieve a random number between an interval, use any of functions to retrieve a floating point number and mulitple this times the width of the interval plus the start of the interval. For more information, search the forums for 'dice', you'll pull up many posts similar to yours with useful information

  3. #3
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help writing a Template Class

    If I'm reading this correctly, then this is all you need to do.
    import java.util.Random;
     
    public class Dice {
    	int numOfSides;
        Random rand;
     
        public Dice(int numOfSides) {
        	this.numOfSides = numOfSides;
        	rand = new Random();
        }
     
        public int rollDie(){
        	return rand.nextInt(numOfSides);
        }    
    }

    If you need a main method then it would look like this:

    import java.util.Random;
     
    public class Dice {
    	int numOfSides;
        Random rand;
     
        public Dice(int numOfSides) {
        	this.numOfSides = numOfSides;
        	rand = new Random();
        }
     
        public static int rollDie(){
        	return rand.nextInt(numOfSides);
        }
     
        public static void main(String[] args){
        	Dice dice = new Dice(6);
        	System.out.println(dice.rollDie());
        } 
    }
    Last edited by Brt93yoda; October 5th, 2010 at 09:40 PM.

Similar Threads

  1. writing to JEditorPane
    By nasi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 8th, 2010, 09:23 PM
  2. [SOLVED] Writing " to a File
    By Sai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2010, 05:21 AM
  3. [SOLVED] Writing integers to a file
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 06:18 PM
  4. Help writing some Scanner code
    By bChEos in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 3rd, 2010, 04:27 AM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM