9 Replies - 415 Views - Last Post: 13 October 2011 - 07:51 AM Rate Topic: -----

#1 remo123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-October 11

how to use equals method in the Die class

Posted 13 October 2011 - 06:52 AM

how to use equals method in Die class
Is This A Good Question/Topic? 0
  • +

Replies To: how to use equals method in the Die class

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: how to use equals method in the Die class

Posted 13 October 2011 - 07:03 AM

If you could please :code:

Then list any errors that you are getting or what it is doing that you don't want it to do.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,686
  • Joined: 27-December 08

Re: how to use equals method in the Die class

Posted 13 October 2011 - 07:04 AM

This is how you invoke the equals() method.
if(obj.equals(otherObj)){
   //code
}



You can also override or overload it when you create a class.
class Foo{

  //overload
   public boolean equals(Foo other){/*code*/}

  //override
   public boolean equals(Object other){/*code*/}
   
  
}


Was This Post Helpful? 0
  • +
  • -

#4 remo123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-October 11

Re: how to use equals method in the Die class

Posted 13 October 2011 - 07:20 AM

i need to use equals method in dice game i.e in Die class ,,pls suggest me some methods
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,686
  • Joined: 27-December 08

Re: how to use equals method in the Die class

Posted 13 October 2011 - 07:21 AM

Your question is ambiguous. I demonstrated usage of the equals() method. What purpose are you trying to achieve? What have you tried? We don't have enough information to help you.
Was This Post Helpful? 0
  • +
  • -

#6 remo123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-October 11

Re: how to use equals method in the Die class

Posted 13 October 2011 - 07:23 AM

public class Die
{
private int facevalue;
private final int Max=6;
private Random dieGen; // generator

public Die()
{
facevalue = 1;
dieGen = new Random();
}

//Math.random creates random integers from 1 - 6
public void roll()
{
facevalue = dieGen.nextInt(Max) + 1;
}


public void setFaceValue(int value)
{
facevalue=value;
}
public void getFaceValue()
{
return facevalue;
}
public String toString()
{
String result="you rolled " +facevalue;
return result;
}
}
Was This Post Helpful? 0
  • +
  • -

#7 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: how to use equals method in the Die class

Posted 13 October 2011 - 07:25 AM

As mac said we do not have enough information.

I am assuming Die is a single in a pair of dice and you are not talking about Motherboard components. Which means you should probably compare the values that the Die show.

if(die1.getValue() > die2.getValue())
     System.out.println("Die 1 has the greater value, thus winning this round!");
else System.out.println("Died 2 has the greater value, thus winning this round!");



Edit - As I said, except change the getValue() method to your getFaceValue();

This post has been edited by Fuzzyness: 13 October 2011 - 07:26 AM

Was This Post Helpful? 0
  • +
  • -

#8 remo123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-October 11

Re: how to use equals method in the Die class

Posted 13 October 2011 - 07:25 AM

how can i insert equal method in my code
Was This Post Helpful? 0
  • +
  • -

#9 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: how to use equals method in the Die class

Posted 13 October 2011 - 07:33 AM

Put the equals method in your Die class, have it take an object, cast the object as a Die, then call the getFaceValue() and compare them how need be.
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: how to use equals method in the Die class

Posted 13 October 2011 - 07:51 AM

You know what remo123? You aren't working on this at all. I have the very distinct feeling that we are literally DOING your homework for you. You simply want a solution and move on without learning. There is enough information in this post for you to be able to figure out what you need. Please do not be a begger.

Topic Closed.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1