Making it more OOP Help Java Program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Making it more OOP Help Java Program

  1. #1
    Join Date
    Jul 2009
    Location
    USA
    Posts
    49

    Exclamation Making it more OOP Help Java Program

    Hi,

    So I have to modify code so that it is OOP rather than all algorithmic. See more details after the code below.

    See my code so far:

    import java.util.Scanner;
    import java.util.Random;


    class Die {
    private static final int MAX_NUMBER = 6;
    private static final int MIN_NUMBER = 1;
    private static final int NO_NUMBER = 0;

    public static int number;

    //constructor
    public Die() {
    number = NO_NUMBER;
    }

    //Rolls of Die
    public void roll() {
    number = (int) (Math.floor(Math.random() *
    (MAX_NUMBER - MIN_NUMBER + 1)) + MIN_NUMBER);
    }


    //Returns the number on this die
    public int getNumber() {
    return number;
    }
    }
    public class RollDice extends Die {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    //Uses the scanner to ask for # of rolls using the three dice.

    RollDice r = new RollDice();
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the number of rolls: ");
    int x, sum1,sum2,sum3,sum4,sum5,sum6;
    x = input.nextInt();
    Die one, two, three;
    int oneCntr = 0;
    int twoCntr = 0;
    int threeCntr = 0;
    int fourCntr = 0;
    int fiveCntr = 0;
    int sixCntr = 0;


    one = new Die();
    two = new Die();
    three = new Die();

    one.roll();
    two.roll();
    three.roll();
    //Old code
    for(int i = 1; i <= x; i++){
    int num = number;
    if(number==1){
    sum1= oneCntr++;
    }
    if(number==2){
    sum2= twoCntr++;
    }
    if(number==3){
    sum3= threeCntr++;
    }
    if(number==4){
    sum4= fourCntr++;
    }
    if(number==5){
    sum5= fiveCntr++;
    }
    if(number==6){
    sum6= sixCntr++;
    }
    }


    System.out.println("Results are " + one.getNumber() + " " +
    two.getNumber() + " " +
    three.getNumber() );

    System.out.println("The face value 1 occured:" +oneCntr);
    System.out.println("In percent face value 1 occurred:" +oneCntr*100/x);
    System.out.println("The face value 2 occured:" +twoCntr);
    System.out.println("In percent face value 2 occurred:"+twoCntr*100/x);
    System.out.println("The face value 3 occurred:" +threeCntr);
    System.out.println("In percent face value 3 occurred:" +threeCntr*100/x);
    System.out.println("The face value 4 occured:" +fourCntr);
    System.out.println("In percent face value 4 occurred:" +fourCntr*100/x);
    System.out.println("The face value 5 occured:" +fiveCntr);
    System.out.println("In percent face value 5 occurred:" +fiveCntr*100/x);
    System.out.println("The face value 6 occured:" +sixCntr);
    System.out.println("In percent face value 6 occurred:" +sixCntr*100/x);



    }//end of main

    }//end of RollDice.java


    Objective:
    To ask the user to enter the number of rolls he or she wants (this part I got done with)

    To output the the results( I got it but it makes only one face value to have all the results)

    To output the results by percentage of rolls from the die.
    (I got this too but it makes only one face value count, and its always coming out 100 percent)

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Making it more OOP Help Java Program

    Please post your code inside &#91;CODE]...&#91;/CODE] tags so it stays formatted.

    Your percentage calculation isn't correct because you're using integer arithmetic when you need floating-point arithmetic. Try making one element in the expression a float or a double.

    Other than that, I don't really see what your question is... did you have a question?

    Judge a man by his questions, rather than his answers...
    Voltaire
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Jul 2009
    Location
    USA
    Posts
    49

    Exclamation Re: Making it more OOP Help Java Program

    Read my objectives I had earlier. But to repeat again, the task for this program is suppose to ask the user how many rolls he or she wants, then it outputs the outcomes, and outputs the percentage of the face values of the dices rolled.


    Code:
     import java.util.Scanner;
    import java.util.Random;
    
    
    class Die {
    private static final int MAX_NUMBER = 6;
    private static final int MIN_NUMBER = 1;
    private static final int NO_NUMBER = 0;
    
    public static int number;
    
    //constructor
    public Die() {
    number = NO_NUMBER;
    }
    
    //Rolls of Die
    public void roll() {
    number = (int) (Math.floor(Math.random() *
    (MAX_NUMBER - MIN_NUMBER + 1)) + MIN_NUMBER);
    }
    
    
    //Returns the number on this die
    public int getNumber() {
    return number;
    }
    }
    public class RollDice extends Die {
    
    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    
    //Uses the scanner to ask for # of rolls using the three dice.
    
    RollDice r = new RollDice();
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the number of rolls: ");
    int x, sum1,sum2,sum3,sum4,sum5,sum6;
    x = input.nextInt();
    Die one, two, three;
    int oneCntr = 0;
    int twoCntr = 0;
    int threeCntr = 0;
    int fourCntr = 0;
    int fiveCntr = 0;
    int sixCntr = 0;
    
    
    one = new Die();
    two = new Die();
    three = new Die();
    
    one.roll();
    two.roll();
    three.roll();
    //Old code
    for(int i = 1; i <= x; i++){
    int num = number;
    if(number==1){
    sum1= oneCntr++;
    }
    if(number==2){
    sum2= twoCntr++;
    }
    if(number==3){
    sum3= threeCntr++;
    }
    if(number==4){
    sum4= fourCntr++;
    }
    if(number==5){
    sum5= fiveCntr++;
    }
    if(number==6){
    sum6= sixCntr++;
    }
    }
    
    
    System.out.println("Results are " + one.getNumber() + " " +
    two.getNumber() + " " +
    three.getNumber() );
    
    System.out.println("The face value 1 occured:" +oneCntr);
    System.out.println("In percent face value 1 occurred:" +oneCntr*100/x);
    System.out.println("The face value 2 occured:" +twoCntr);
    System.out.println("In percent face value 2 occurred:"+twoCntr*100/x);
    System.out.println("The face value 3 occurred:" +threeCntr);
    System.out.println("In percent face value 3 occurred:" +threeCntr*100/x);
    System.out.println("The face value 4 occured:" +fourCntr);
    System.out.println("In percent face value 4 occurred:" +fourCntr*100/x);
    System.out.println("The face value 5 occured:" +fiveCntr);
    System.out.println("In percent face value 5 occurred:" +fiveCntr*100/x);
    System.out.println("The face value 6 occured:" +sixCntr);
    System.out.println("In percent face value 6 occurred:" +sixCntr*100/x);
    
    
    
    }//end of main
    
    }//end of RollDice.java

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Making it more OOP Help Java Program

    The point of the CODE tags is to keep the formatting of the code. If the code isn't formatted, there's little point using them, and few people will read it.

    I read your objectives, and I understand what you want the program to do, but I don't see what the problem is - what exactly do you need help doing? is there something stopping your progress? Is there something you don't understand? Is something not working the way you want? If so, please explain.

    Perhaps this will help you frame it more usefully.

    A prudent question is one-half of wisdom...
    F. Bacon
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    May 2009
    Location
    Surrey, UK
    Posts
    285

    Re: Making it more OOP Help Java Program

    There are a few things wrong in your code: you have three dice and then you roll them only once each, you put the result of the roll in the static (that is a class rather than instance) variable number and then in the loop you refer to this variable by a direct reference, not through the Die objects you created, finally you copy this variable to another one, num, that is never used. Also, what are the sumx variables for? You are using them to assign the value of the counters before incrementing and then you don't use them again.

    I think the loop should be something like this:
    Code:
    for (int i = 0, Die die; i < x; i++) {
    	die.roll();
    	switch (die.getNumber()) {
    		case 1: oneCntr++; break;
    		case 2: twoCntr++; break;
    		case 3: threeCntr++; break;
    		case 4: fourCntr++; break;
    		case 5: fiveCntr++; break;
    		case 6: sixCntr++; break;
    		default: break;
    	}
    }
    This will not fix everything but at least will do the calculations as you intend them.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center