Results 1 to 14 of 14
  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Smile New to Programming!!HW Help!

    HEEEY Guy . Im having trouble exactly getting my code to do what the problem says here it is..[
    Loops. Using the Die class defined in Chapter 4, design and implement a driver class called CountFaces whose main method rolls a die 100 times and counts how many times each face comes up. Using (a) while() loop, (b) do .. while() and (c) for loop, print the results. Use switch, not if statement.


    public class Crapz {
    public static void main(String[] args) {

    die die1 = new die();
    int x;
    x = 0;
    die1.getFaceValue();
    die1.roll();

    while (x <=100) {
    System.out.println(die1.roll());
    x ++;
    }
    do {
    System.out.println(die1.roll());
    x++;
    } while(x <= 100);




    for ( x=1 ;x<=100;x++) {
    System.out.println(die1.roll());
    x ++;
    }
    }
    }


    ps i have no clue how to actually collect the numbers to store them...

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Quote Originally Posted by Bgreen7887 View Post
    ps i have no clue how to actually collect the numbers to store them...
    -->
    Quote Originally Posted by Bgreen7887 View Post
    Use switch, not if statement.
    -->
    Read: The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your do/while loop won't work unless you reset x to 0. One way to store the results is to create an array of 7 ints before a loop, (I'd just ignore the 0th item in the array), get the int returned from the roll and increase the number in the array corresponding to that roll by one. For instance, if roll returns 4, then increase the 4th item in the array by

    myArray[4]++;

    or

    myArray[4] = myArray[4] + 1;

    Only you won't hard-code this as "4" but instead will use an int variable to hold the result of the roll and use that number as your array index. So in pseudocode:

    Java Code:
    create an array of 7 ints called myArray
    for x goes from 0 to less than 100
       get roll and store in rollResult variable.
       Increase the value held by myArray[rollResult] by one
    end for loop

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Quote Originally Posted by Fubarable View Post
    Your do/while loop won't work unless you reset x to 0. One way to store the results is to create an array of 7 ints before a loop, (I'd just ignore the 0th item in the array), get the int returned from the roll and increase the number in the array corresponding to that roll by one. For instance, if roll returns 4, then increase the 4th item in the array by

    myArray[4]++;

    or

    myArray[4] = myArray[4] + 1;

    Only you won't hard-code this as "4" but instead will use an int variable to hold the result of the roll and use that number as your array index. So in pseudocode:

    Java Code:
    create an array of 7 ints called myArray
    for x goes from 0 to less than 100
       get roll and store in rollResult variable.
       Increase the value held by myArray[rollResult] by one
    end for loop
    Mhm, i think that would be a good solution, but that is not what the assignment is asking for (i think the goal is, to bring the switch statement closer to him)

    my pseudocode:
    Java Code:
    create six int variables
    for x goes from 0 to less than 100
       get roll and store in rollResult variable.
       //maybe in a method:
       switch with rollResult - increase the the right variable in the right case
    end for loop

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by eRaaaa View Post
    Mhm, i think that would be a good solution, but that is not what the assignment is asking for (i think the goal is, to bring the switch statement closer to him)

    my pseudocode:
    Java Code:
    .....
    You could very well be right. Let's see if the OP replies.

  6. #6
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Hi guys thanks for the advices .. I AM STILL STRUGGLING TO COMPILE THIS.I CAN GET IT TO ROLL THE DICE 100TIMES AND PRINT.. CANT THINK OF WHAT TO DO. I DONT WANT ANSWERS JUST GUIDANCE!!!:( WILL NOT QUIT) !!!!!!!!

  7. #7
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    In replies to you Fubarable
    is this right so far about rolling to die and storing the result in a variable?

    public class ugh {
    public static void main(String[]args) {
    int a,s,d,f,g,h,FaceValue;
    a = 0;
    s=0;
    die die1 = new die();

    while (a<=5) {
    die1.getFaceValue();
    FaceValue = s;
    }




    }
    }

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    HEEEY Guy . Im having trouble exactly getting my code to do what the problem says here it is..[
    Loops. Using the Die class defined in Chapter 4, design and implement a driver class called CountFaces whose main method rolls a die 100 times and counts how many times each face comes up. Using (a) while() loop, (b) do .. while() and (c) for loop, print the results. Use switch, not if statement.

    Java Code:
    public class CountFaces {
    public static void main(String[] args) {
    
    die die1 = new die();
    // die has six faces i.e 1,2,3,4,5,6
    int face1=0; // occurence of 1 rolled
    int face2=0; // occurence of 2 rolled
    int face3=0; // occurence of 3 rolled
    int face4=0; // occurence of 4 rolled
    int face5=0; // occurence of 5 rolled
    int face6=0; // occurence of 6 rolled
    int x=0;
    while (x <=100) {
    // assuming die1.roll() returns an int value of 1,2,3,4,5 or 6
    int rolledNumber=die1.roll();
    switch(rolledNumber){
    
    case 1:
        face1++;
        break;
    case 2:
        face2++;
        break;
    case 3:
        face3++;
         break;
    case 4:
        face4++;
        break;
    case 5:
        face5++;
        break;
    case 6:
        face6++;
         break;
    default:
         break;
    } // end of switch
    
    x++;
    
    
    
    }
    }
    // not sure why you would need a for loop
    System.out.println("1 occurred "+face1+" times");
    System.out.println("2 occurred "+face2+" times");
    System.out.println("3 occurred "+face3+" times");
    System.out.println("4 occurred "+face4+" times");
    System.out.println("5 occurred "+face5+" times");
    System.out.println("6 occurred "+face6+" times"); 
    }
    }
    not sure about my switch statement as I never use them. you could do this as do while but I don't see the point if x is set to 0 to begin with anyway.
    i.e replace while loop with

    Java Code:
    int x=0;
    do{
    
    // sumplace inside the do while 
    x++;
    }while(x<=100);

  9. #9
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    wow thats an astronomical chance that your in Ireland and have the same exact problem as me. First off you code is way more draw out than mines. The bad thing is that im in the same spot i was 5 hours ago. I've just started programming and trying to improve but class is getting quite intense...

  10. #10
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Erm I am in Ireland (not with your problem) but trying to help you solve it :D .. probably did ;)

  11. #11
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Wowwww thankss you definitely did!i need advice as to get better fast i have the time..yourr good!:):):):):):)

  12. #12
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Hmm advice to get better? code every oppurtunity, read good books and apply the knowledge, LEARN HOW TO DEBUG, be a tad bit obsessive about getting code to work, have fun.

    I would start by understanding the logic of this program's solution.

  13. #13
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Thanks again!:)

  14. #14
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

Similar Threads

  1. new to programming need help with something
    By surg3y3 in forum New To Java
    Replies: 4
    Last Post: 01-26-2010, 04:13 AM
  2. programming website?
    By ragarwal in forum Forum Lobby
    Replies: 11
    Last Post: 12-21-2009, 01:37 PM
  3. GUI Programming Help
    By sirwiggles in forum New To Java
    Replies: 4
    Last Post: 04-28-2009, 05:53 AM
  4. New to Programming . . .Need Help
    By DSutta22 in forum New To Java
    Replies: 2
    Last Post: 09-10-2008, 06:19 AM
  5. programming
    By abcdefg in forum New To Java
    Replies: 9
    Last Post: 03-10-2008, 11:34 AM

Posting Permissions

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