Two Roll Dicer Java object-oriented way










-2















Hi I am new to Java OOP and I have some problems in running the program



The problem is there is no output and the loop never stops.



This is my Main.java



public class Main 

public static void main(String args)

Dice firstDie = new Dice();
Dice secondDie = new Dice();
do
count++;
if(firstDie==secondDie )
same=true;
System.out.println("It took "+count+ " times " + firstDie.getValue() + " and " + secondDie.getValue());


while(!same);




and this is my Dice.java



public class Dice 
private int value;
public Dice()
value = (int)(Math.random()*6)+1;
public int getValue() return value;




in my Main.java class when I write int count = 0; and boolean same = false; the loop never ends.










share|improve this question

















  • 2





    Welcome to Stack Overflow and the wonderfully frustrating world of computer programming. One of the skills you need to learn along the way is debugging your code when it doesn't work quite the way you expect or want. For some tips on doing this, read this great article. After you do so, come back with more questions including what you learned while debugging.

    – Code-Apprentice
    Nov 14 '18 at 18:33















-2















Hi I am new to Java OOP and I have some problems in running the program



The problem is there is no output and the loop never stops.



This is my Main.java



public class Main 

public static void main(String args)

Dice firstDie = new Dice();
Dice secondDie = new Dice();
do
count++;
if(firstDie==secondDie )
same=true;
System.out.println("It took "+count+ " times " + firstDie.getValue() + " and " + secondDie.getValue());


while(!same);




and this is my Dice.java



public class Dice 
private int value;
public Dice()
value = (int)(Math.random()*6)+1;
public int getValue() return value;




in my Main.java class when I write int count = 0; and boolean same = false; the loop never ends.










share|improve this question

















  • 2





    Welcome to Stack Overflow and the wonderfully frustrating world of computer programming. One of the skills you need to learn along the way is debugging your code when it doesn't work quite the way you expect or want. For some tips on doing this, read this great article. After you do so, come back with more questions including what you learned while debugging.

    – Code-Apprentice
    Nov 14 '18 at 18:33













-2












-2








-2








Hi I am new to Java OOP and I have some problems in running the program



The problem is there is no output and the loop never stops.



This is my Main.java



public class Main 

public static void main(String args)

Dice firstDie = new Dice();
Dice secondDie = new Dice();
do
count++;
if(firstDie==secondDie )
same=true;
System.out.println("It took "+count+ " times " + firstDie.getValue() + " and " + secondDie.getValue());


while(!same);




and this is my Dice.java



public class Dice 
private int value;
public Dice()
value = (int)(Math.random()*6)+1;
public int getValue() return value;




in my Main.java class when I write int count = 0; and boolean same = false; the loop never ends.










share|improve this question














Hi I am new to Java OOP and I have some problems in running the program



The problem is there is no output and the loop never stops.



This is my Main.java



public class Main 

public static void main(String args)

Dice firstDie = new Dice();
Dice secondDie = new Dice();
do
count++;
if(firstDie==secondDie )
same=true;
System.out.println("It took "+count+ " times " + firstDie.getValue() + " and " + secondDie.getValue());


while(!same);




and this is my Dice.java



public class Dice 
private int value;
public Dice()
value = (int)(Math.random()*6)+1;
public int getValue() return value;




in my Main.java class when I write int count = 0; and boolean same = false; the loop never ends.







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 18:29









AlexisAlexis

213




213







  • 2





    Welcome to Stack Overflow and the wonderfully frustrating world of computer programming. One of the skills you need to learn along the way is debugging your code when it doesn't work quite the way you expect or want. For some tips on doing this, read this great article. After you do so, come back with more questions including what you learned while debugging.

    – Code-Apprentice
    Nov 14 '18 at 18:33












  • 2





    Welcome to Stack Overflow and the wonderfully frustrating world of computer programming. One of the skills you need to learn along the way is debugging your code when it doesn't work quite the way you expect or want. For some tips on doing this, read this great article. After you do so, come back with more questions including what you learned while debugging.

    – Code-Apprentice
    Nov 14 '18 at 18:33







2




2





Welcome to Stack Overflow and the wonderfully frustrating world of computer programming. One of the skills you need to learn along the way is debugging your code when it doesn't work quite the way you expect or want. For some tips on doing this, read this great article. After you do so, come back with more questions including what you learned while debugging.

– Code-Apprentice
Nov 14 '18 at 18:33





Welcome to Stack Overflow and the wonderfully frustrating world of computer programming. One of the skills you need to learn along the way is debugging your code when it doesn't work quite the way you expect or want. For some tips on doing this, read this great article. After you do so, come back with more questions including what you learned while debugging.

– Code-Apprentice
Nov 14 '18 at 18:33












3 Answers
3






active

oldest

votes


















0














Probably you have to compare the value firstDie.getValue() and secondDie.getValue().



What you are trying to do is compare the 2 instances of class Dice . When you compare those, unless they are the same object, it cannot be same.
Hence, same is not made true at all and this goes to a loop.



For more info, please refer, equals() method in java because that is what is called when two instances/objects are compared.



Edit:
Even if you try below code snippet of do-while (ie. compare the value firstDie.getValue() and secondDie.getValue(),



do 
count++;
int first = firstDie.getValue();
int sec = secondDie.getValue();
System.out.println(first+ " "+sec);
if(first==sec )
same=true;
System.out.println("It took "+count+ " times " + first + " and " + sec);


while(!same);


It still goes to loop because of the fact that the value of Dice class is set only once i.e. in constructor.
So the ideal way is to write,



public Dice() 
value = 0;
public int getValue() return (int)(Math.random()*6)+1; }





share|improve this answer

























  • @Alexis, please check now.

    – CS_noob
    Nov 14 '18 at 19:11











  • even this work : /

    – Alexis
    Nov 14 '18 at 19:13











  • @Alexis, is this still going to loop?

    – CS_noob
    Nov 14 '18 at 19:16


















0














You are comparing the classe's instances and not the values stored within them.
Notice that on your println you are referring to each of the dices values (which is what you want to compare and have match), but on your comparison you are checking the objects.



Since they are 2 different instances they will never match, and therefore there will be no output and the loop never stops.






share|improve this answer






























    0














    try firstDie.getValue() == secondDie.getValue() instead of firstDie== secondDie



    You are checking for the two instances to be equal if you say firstDie== secondDie. These two are never equal.






    share|improve this answer

























    • How can we use firstDie.value if value of Dice class is private?

      – CS_noob
      Nov 14 '18 at 18:53











    • so in my Dice.java class I have to set private int value to public

      – Alexis
      Nov 14 '18 at 18:54






    • 1





      @Alexis, that is not an elegant solution as it defeats the purpose of OOP concept called as Encapsulation.

      – CS_noob
      Nov 14 '18 at 18:56











    • i didnot see it is private...made changes

      – Jayanth
      Nov 15 '18 at 4:42










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306642%2ftwo-roll-dicer-java-object-oriented-way%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Probably you have to compare the value firstDie.getValue() and secondDie.getValue().



    What you are trying to do is compare the 2 instances of class Dice . When you compare those, unless they are the same object, it cannot be same.
    Hence, same is not made true at all and this goes to a loop.



    For more info, please refer, equals() method in java because that is what is called when two instances/objects are compared.



    Edit:
    Even if you try below code snippet of do-while (ie. compare the value firstDie.getValue() and secondDie.getValue(),



    do 
    count++;
    int first = firstDie.getValue();
    int sec = secondDie.getValue();
    System.out.println(first+ " "+sec);
    if(first==sec )
    same=true;
    System.out.println("It took "+count+ " times " + first + " and " + sec);


    while(!same);


    It still goes to loop because of the fact that the value of Dice class is set only once i.e. in constructor.
    So the ideal way is to write,



    public Dice() 
    value = 0;
    public int getValue() return (int)(Math.random()*6)+1; }





    share|improve this answer

























    • @Alexis, please check now.

      – CS_noob
      Nov 14 '18 at 19:11











    • even this work : /

      – Alexis
      Nov 14 '18 at 19:13











    • @Alexis, is this still going to loop?

      – CS_noob
      Nov 14 '18 at 19:16















    0














    Probably you have to compare the value firstDie.getValue() and secondDie.getValue().



    What you are trying to do is compare the 2 instances of class Dice . When you compare those, unless they are the same object, it cannot be same.
    Hence, same is not made true at all and this goes to a loop.



    For more info, please refer, equals() method in java because that is what is called when two instances/objects are compared.



    Edit:
    Even if you try below code snippet of do-while (ie. compare the value firstDie.getValue() and secondDie.getValue(),



    do 
    count++;
    int first = firstDie.getValue();
    int sec = secondDie.getValue();
    System.out.println(first+ " "+sec);
    if(first==sec )
    same=true;
    System.out.println("It took "+count+ " times " + first + " and " + sec);


    while(!same);


    It still goes to loop because of the fact that the value of Dice class is set only once i.e. in constructor.
    So the ideal way is to write,



    public Dice() 
    value = 0;
    public int getValue() return (int)(Math.random()*6)+1; }





    share|improve this answer

























    • @Alexis, please check now.

      – CS_noob
      Nov 14 '18 at 19:11











    • even this work : /

      – Alexis
      Nov 14 '18 at 19:13











    • @Alexis, is this still going to loop?

      – CS_noob
      Nov 14 '18 at 19:16













    0












    0








    0







    Probably you have to compare the value firstDie.getValue() and secondDie.getValue().



    What you are trying to do is compare the 2 instances of class Dice . When you compare those, unless they are the same object, it cannot be same.
    Hence, same is not made true at all and this goes to a loop.



    For more info, please refer, equals() method in java because that is what is called when two instances/objects are compared.



    Edit:
    Even if you try below code snippet of do-while (ie. compare the value firstDie.getValue() and secondDie.getValue(),



    do 
    count++;
    int first = firstDie.getValue();
    int sec = secondDie.getValue();
    System.out.println(first+ " "+sec);
    if(first==sec )
    same=true;
    System.out.println("It took "+count+ " times " + first + " and " + sec);


    while(!same);


    It still goes to loop because of the fact that the value of Dice class is set only once i.e. in constructor.
    So the ideal way is to write,



    public Dice() 
    value = 0;
    public int getValue() return (int)(Math.random()*6)+1; }





    share|improve this answer















    Probably you have to compare the value firstDie.getValue() and secondDie.getValue().



    What you are trying to do is compare the 2 instances of class Dice . When you compare those, unless they are the same object, it cannot be same.
    Hence, same is not made true at all and this goes to a loop.



    For more info, please refer, equals() method in java because that is what is called when two instances/objects are compared.



    Edit:
    Even if you try below code snippet of do-while (ie. compare the value firstDie.getValue() and secondDie.getValue(),



    do 
    count++;
    int first = firstDie.getValue();
    int sec = secondDie.getValue();
    System.out.println(first+ " "+sec);
    if(first==sec )
    same=true;
    System.out.println("It took "+count+ " times " + first + " and " + sec);


    while(!same);


    It still goes to loop because of the fact that the value of Dice class is set only once i.e. in constructor.
    So the ideal way is to write,



    public Dice() 
    value = 0;
    public int getValue() return (int)(Math.random()*6)+1; }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 19:09

























    answered Nov 14 '18 at 18:35









    CS_noobCS_noob

    4571312




    4571312












    • @Alexis, please check now.

      – CS_noob
      Nov 14 '18 at 19:11











    • even this work : /

      – Alexis
      Nov 14 '18 at 19:13











    • @Alexis, is this still going to loop?

      – CS_noob
      Nov 14 '18 at 19:16

















    • @Alexis, please check now.

      – CS_noob
      Nov 14 '18 at 19:11











    • even this work : /

      – Alexis
      Nov 14 '18 at 19:13











    • @Alexis, is this still going to loop?

      – CS_noob
      Nov 14 '18 at 19:16
















    @Alexis, please check now.

    – CS_noob
    Nov 14 '18 at 19:11





    @Alexis, please check now.

    – CS_noob
    Nov 14 '18 at 19:11













    even this work : /

    – Alexis
    Nov 14 '18 at 19:13





    even this work : /

    – Alexis
    Nov 14 '18 at 19:13













    @Alexis, is this still going to loop?

    – CS_noob
    Nov 14 '18 at 19:16





    @Alexis, is this still going to loop?

    – CS_noob
    Nov 14 '18 at 19:16













    0














    You are comparing the classe's instances and not the values stored within them.
    Notice that on your println you are referring to each of the dices values (which is what you want to compare and have match), but on your comparison you are checking the objects.



    Since they are 2 different instances they will never match, and therefore there will be no output and the loop never stops.






    share|improve this answer



























      0














      You are comparing the classe's instances and not the values stored within them.
      Notice that on your println you are referring to each of the dices values (which is what you want to compare and have match), but on your comparison you are checking the objects.



      Since they are 2 different instances they will never match, and therefore there will be no output and the loop never stops.






      share|improve this answer

























        0












        0








        0







        You are comparing the classe's instances and not the values stored within them.
        Notice that on your println you are referring to each of the dices values (which is what you want to compare and have match), but on your comparison you are checking the objects.



        Since they are 2 different instances they will never match, and therefore there will be no output and the loop never stops.






        share|improve this answer













        You are comparing the classe's instances and not the values stored within them.
        Notice that on your println you are referring to each of the dices values (which is what you want to compare and have match), but on your comparison you are checking the objects.



        Since they are 2 different instances they will never match, and therefore there will be no output and the loop never stops.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 18:39









        ricol070ricol070

        445211




        445211





















            0














            try firstDie.getValue() == secondDie.getValue() instead of firstDie== secondDie



            You are checking for the two instances to be equal if you say firstDie== secondDie. These two are never equal.






            share|improve this answer

























            • How can we use firstDie.value if value of Dice class is private?

              – CS_noob
              Nov 14 '18 at 18:53











            • so in my Dice.java class I have to set private int value to public

              – Alexis
              Nov 14 '18 at 18:54






            • 1





              @Alexis, that is not an elegant solution as it defeats the purpose of OOP concept called as Encapsulation.

              – CS_noob
              Nov 14 '18 at 18:56











            • i didnot see it is private...made changes

              – Jayanth
              Nov 15 '18 at 4:42















            0














            try firstDie.getValue() == secondDie.getValue() instead of firstDie== secondDie



            You are checking for the two instances to be equal if you say firstDie== secondDie. These two are never equal.






            share|improve this answer

























            • How can we use firstDie.value if value of Dice class is private?

              – CS_noob
              Nov 14 '18 at 18:53











            • so in my Dice.java class I have to set private int value to public

              – Alexis
              Nov 14 '18 at 18:54






            • 1





              @Alexis, that is not an elegant solution as it defeats the purpose of OOP concept called as Encapsulation.

              – CS_noob
              Nov 14 '18 at 18:56











            • i didnot see it is private...made changes

              – Jayanth
              Nov 15 '18 at 4:42













            0












            0








            0







            try firstDie.getValue() == secondDie.getValue() instead of firstDie== secondDie



            You are checking for the two instances to be equal if you say firstDie== secondDie. These two are never equal.






            share|improve this answer















            try firstDie.getValue() == secondDie.getValue() instead of firstDie== secondDie



            You are checking for the two instances to be equal if you say firstDie== secondDie. These two are never equal.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 4:41

























            answered Nov 14 '18 at 18:44









            JayanthJayanth

            404314




            404314












            • How can we use firstDie.value if value of Dice class is private?

              – CS_noob
              Nov 14 '18 at 18:53











            • so in my Dice.java class I have to set private int value to public

              – Alexis
              Nov 14 '18 at 18:54






            • 1





              @Alexis, that is not an elegant solution as it defeats the purpose of OOP concept called as Encapsulation.

              – CS_noob
              Nov 14 '18 at 18:56











            • i didnot see it is private...made changes

              – Jayanth
              Nov 15 '18 at 4:42

















            • How can we use firstDie.value if value of Dice class is private?

              – CS_noob
              Nov 14 '18 at 18:53











            • so in my Dice.java class I have to set private int value to public

              – Alexis
              Nov 14 '18 at 18:54






            • 1





              @Alexis, that is not an elegant solution as it defeats the purpose of OOP concept called as Encapsulation.

              – CS_noob
              Nov 14 '18 at 18:56











            • i didnot see it is private...made changes

              – Jayanth
              Nov 15 '18 at 4:42
















            How can we use firstDie.value if value of Dice class is private?

            – CS_noob
            Nov 14 '18 at 18:53





            How can we use firstDie.value if value of Dice class is private?

            – CS_noob
            Nov 14 '18 at 18:53













            so in my Dice.java class I have to set private int value to public

            – Alexis
            Nov 14 '18 at 18:54





            so in my Dice.java class I have to set private int value to public

            – Alexis
            Nov 14 '18 at 18:54




            1




            1





            @Alexis, that is not an elegant solution as it defeats the purpose of OOP concept called as Encapsulation.

            – CS_noob
            Nov 14 '18 at 18:56





            @Alexis, that is not an elegant solution as it defeats the purpose of OOP concept called as Encapsulation.

            – CS_noob
            Nov 14 '18 at 18:56













            i didnot see it is private...made changes

            – Jayanth
            Nov 15 '18 at 4:42





            i didnot see it is private...made changes

            – Jayanth
            Nov 15 '18 at 4:42

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306642%2ftwo-roll-dicer-java-object-oriented-way%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20