Click here to Skip to main content
15,909,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, guys
I hope you can help me with my program. i'm still learning java, and it feels like a foreign language to me. so i hope you will be kind enough to help me.

my question is:
Create a class that represents a die. The class should have a single attribute: the number of sides. You should include a
default constructor that sets the sides to 6. You should also include a parameterized constructor that takes the number
of sides as a parameter. Include a public roll method that returns a random number between 1 and the number of sides
that the die has. A 6 sided die can have values between 1 and 6. A 20 sided die can have values between 1 and 20.
Include a test main that demonstrates that your class works.

So far I am trying to create a default constructor but failed miserably :(

package die;
Java
import java.util.Scanner;
import java.util.Random;


public class Die {
    private int NumberOfSides;
    public Die(){
        NumberOfSides=6;
    }
public int getNumberOfSides(){
    return NumberOfSides;
}
}
class dieRoll{
    public static void main(String[] args) {
        Die dieObject=new Die();
        System.out.println(dieObject.getNumberOfSides());

    }
Posted
Updated 18-Feb-15 16:26pm
v2
Comments
Mohibur Rashid 18-Feb-15 22:30pm    
You have constructed the default construct.
You haven't add any function that will generate random number.
You haven't add any function to set the number of sides.
The better class name would be Dice

1 solution

have you tried running the above program, i don't think the program will run, below is the executable code:

C#
import java.util.Scanner;
import java.util.Random;


class Die {
    private int NumberOfSides;
    public Die(){
        NumberOfSides=6;
    }
public int getNumberOfSides(){
    return NumberOfSides;
}
}
 public class dieRoll{
    public static void main(String[] args) {
        Die dieObject=new Die();
        System.out.println(dieObject.getNumberOfSides());

    }
}


Save the file with dieRoll name, compile it and run it.

But , i cannot see the roll method in it, you should probably right a roll method and and dieObject.rollmehtod which will return the random number between 1 to 6.

Regards,
panduranga.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900