Think about object...

Hi...

Firstly...i thought that java is a straight forward programming language, like C++. But now, i realize, there are lot of thing to understand...especially creating an object.

So...here, i really need your (everybody who knows) to help me to describe about creating object. Below is my exercise DiceDemo.java (almost two weeks i tried to create Dice object, but nothing happen):

class DiceDemo {

public static void main(string[ ] args) {

Dice d1, d2;

d1 = new Dice( );

d1.setDice(6,3);

d2 = new Dice( );

d2.setDice(5,2);

System.out.print("d1: ");

d1.setThrow( );

System.out.println( );

System.out.print("d2: ");

d2.setThrow( );

System.out.println( );

}

}

Thanks.

[790 byte] By [Ayonga] at [2007-11-26 18:36:36]
# 1
What's wrong with it?
zadoka at 2007-7-9 6:10:37 > top of Java-index,Java Essentials,New To Java...
# 2

When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

What exact problem are you having?

Also, Java is simpler than C++. It has simpler rules. Some things might take more steps to accomplish than in C++, but that's because Java's steps are simpler.

jverda at 2007-7-9 6:10:37 > top of Java-index,Java Essentials,New To Java...
# 3
Since C++ is such a "straight forward programming language", why not code your assignment in C++, then post it here and we can give you tips about translating it into Java. The translation should be easy.
DrLaszloJamfa at 2007-7-9 6:10:38 > top of Java-index,Java Essentials,New To Java...
# 4
Post the code for Dice Class and use code tages ........
qUesT_foR_knOwLeDgea at 2007-7-9 6:10:38 > top of Java-index,Java Essentials,New To Java...
# 5

Sorry...i am not good in english.

My problem is:

--> how to create an object for DiceDemo.java below:

class DiceDemo {

public static void main(string[ ] args) {

Dice d1, d2;

d1 = new Dice();

d1.setDice(6,3);

d2 = new Dice();

d2.setDice(5,2);

System.out.print("d1: ");

d1.setThrow();

System.out.println();

System.out.print("d2: ");

d2.setThrow();

System.out.println();

}

}

Ayonga at 2007-7-9 6:10:38 > top of Java-index,Java Essentials,New To Java...
# 6
> Sorry...i am not good in english.> > My problem is:> --> how to create an object for DiceDemo.java below:> new DiceDemo();
zadoka at 2007-7-9 6:10:38 > top of Java-index,Java Essentials,New To Java...
# 7

> Sorry...i am not good in english.

>

> My problem is:

> --> how to create an object for DiceDemo.java below:

You're already creating object with your new statements.

The problem isn't your English, it's your general communication. You need to provide details about what problems you're having. Your code shows that you're aware of using new to create objects, so what specific difficulty are you having?

jverda at 2007-7-9 6:10:38 > top of Java-index,Java Essentials,New To Java...
# 8

Here i put an example of object for dice class:

public class Dice {

public int throw;

public int face;

public Dice (int t, int f) {

throw=t;

face=f;

}

public void setThrow (int t, int f) {

.....

}

public void setDice () {

.....

}

}

here, i don't know how to set their methods , parameters or constructors to point DiceDemo class.

Ayonga at 2007-7-9 6:10:38 > top of Java-index,Java Essentials,New To Java...
# 9

> here, i don't know how to set their methods ,

> parameters or constructors to point DiceDemo class.

I don't know what you mean by this, but if Dice is your "real" class and DiceDemo is just a test harness, then Dice should not know about DiceDemo.

Your DiceDemo class is already using the Dice class. That should be good enough.

What's going wrong? Are you getting compiler errors? Exceptions when you run it? Unexpected behavior?

Please provide details.

jverda at 2007-7-9 6:10:38 > top of Java-index,Java Essentials,New To Java...
# 10

> Here i put an example of object for dice class:

>

> public class Dice {

>public int throw;

> public int face;

>

> lic Dice (int t, int f) {

>throw=t;

> face=f;

>}

> public void setThrow (int t, int f) {

>.....

> }

>

> public void setDice () {

>.....

> }

> }

>

> here, i don't know how to set their methods ,

> parameters or constructors to point DiceDemo class.

You don't have to point to anything(if thats what i have deciphered from what you have given). I still haven't got it perfectly what you are trying to say.

qUesT_foR_knOwLeDgea at 2007-7-9 6:10:38 > top of Java-index,Java Essentials,New To Java...
# 11

Ah, I see. You need someone to just do your homework for you. You have just been given the blueprint (method names, API) of the Dice class and want someone to write the implementation of it for you.

No, I'll pass thank you. I'd rather you figure it out on your own like you're supposed to do and that the course is meant for, or have you flunk out early so you can figure out what you should study instead.

warnerjaa at 2007-7-9 6:10:38 > top of Java-index,Java Essentials,New To Java...
# 12
>public int throw;throw is a keyword in Java (Isn't it a keyword in C++, also?)
DrLaszloJamfa at 2007-7-9 6:10:38 > top of Java-index,Java Essentials,New To Java...
# 13

you can not call a variable "throw" its not legal.

i don't full understand your question but I think your DiceDemo should contain references to Dice and not the other way round. Looking at your first code you already know how to call methods from other classes. Can you be more specific?

kikemellya at 2007-7-9 6:10:38 > top of Java-index,Java Essentials,New To Java...
# 14
I must type slow.
kikemellya at 2007-7-9 6:10:38 > top of Java-index,Java Essentials,New To Java...
# 15
Yes. It is my exercise...but no one tell me to do. i just want to understand about java / object oriented. i was looking around, including my module...but, still confusing.Otherwise, thanks.
Ayonga at 2007-7-21 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 16

> Yes. It is my exercise...but no one tell me to do. i

> just want to understand about java / object oriented.

> i was looking around, including my module...but,

> still confusing.

>

> Otherwise, thanks.

But what is confusing? You can explain your problem and the forum members would always help.

qUesT_foR_knOwLeDgea at 2007-7-21 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 17
Yes. It is my exercise...but no one tell me to do. i just want to understand about java / object oriented. i was looking around, including my modules...but it still confusing me. i really need help...thanks.
Ayonga at 2007-7-21 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 18
Yes. It is my exercise...but no one tell me to do. i just want to understand about java / object oriented. i was looking around, including my modules...but it still confusing me. i really need help...thanks.
Ayonga at 2007-7-21 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 19
Somebody needs to hit the record player. It keeps repeating the same track!
warnerjaa at 2007-7-21 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 20
Stop posting the same thing again and again and be specific.
qUesT_foR_knOwLeDgea at 2007-7-21 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 21
For starters, I recommend telling us the requirements of Dice (why is throw a field, for example) then posting your Dice class with specific questions about it. So far, you have been vague and incoherent.
DrLaszloJamfa at 2007-7-21 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 22

@OP: the Java language is a little language and all objects are

"dynamic" (in C++ speak), i.e. you always have to explicitly "new" your

objects from the heap one way or another.

There's no trickery dickery behind your back concerning copy ctors nor

default ctors, i.e. you always have to do it yourself. If you need a new Dice

object, you create it yourself:Dice d= new Dice(); // default ctor; or:

Dice d= new Dice(3, 6); // your ctor; but not:

Dice d; // no object here.

kind regards,

Jos

JosAHa at 2007-7-21 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 23

> But what is confusing? You can explain your problem

> and the forum members would always help.

Actually...a lot of thing about java. i can't explain more in English. but one thing at this time - i want to sleep first. maybe, tomorrow. Now, my time is 4.30 AM....

Thanks.

Ayonga at 2007-7-21 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 24
Sorry for repeating posting...i hit many times because my browser temporarily not responding.regards.
Ayonga at 2007-7-21 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 25

The thing that make me not understand now is how to create a source file for the blueprint as below:

class DiceDemo {

public static void main(string[ ] args) {

Dice d1, d2;

d1 = new Dice();

d1.setDice(6,3);

d2 = new Dice();

d2.setDice(5,2);

System.out.print("d1: ");

d1.setThrow();

System.out.println();

System.out.print("d2: ");

d2.setThrow();

System.out.println();

}

}

So, output for this DiceDemo.java is:

6:3

5:2

Here is my explanation about dice class:

- this program called DiceDemo.java create one object: Dice.

- So, Dice object need a source file to compile this program...let say Dice.java.

- Now, i need to create that source file with:

Method

setDice(player1,player2)

=> player throw their dice and get the value.

=> at the first, value both of players = 0.

setThrow( )

=> no parameter are given.

- That only i can explain....i can't describe more specific, because i am new with java.

Thanks in advance...

For now, i was got a good example at:

http://java.sun.com/docs/books/tutorial/java/concepts/class.html

also

http://java.sun.com/docs/books/tutorial/java/javaOO/objects.html

Message was edited by:

Ayong

Ayonga at 2007-7-21 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 26

class Dice

{

private var1,var2;

public void setDice(int var1,int var2)

{

this.var1=var1;

this.var2=var2;

}

public void setThrow()

{

System.out.println(var1" : "var2);

}

}

qUesT_foR_knOwLeDgea at 2007-7-21 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 27
Thats the code for Dice. What happened to the indentation. Won't the forum do it for us?
qUesT_foR_knOwLeDgea at 2007-7-21 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 28

Big thanks qUesT_foR_knOwLeDge...the Dice class is very pretty!

i know that it is a simple code of java. actually, i did not know how to describe the dice object, their event or setting their parameter to point to the DiceDemo.java.

so...until now i wonder, why this object declare outside from DiceDemo declaration?

before, i am using C++ for flash and web designing...not for professional programming. So, even i know about C++...it just a simple and limited code.

Ayonga at 2007-7-21 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 29

> Big thanks qUesT_foR_knOwLeDge...the Dice class is

> very pretty!

>

> i know that it is a simple code of java. actually, i

> did not know how to describe the dice object, their

> event or setting their parameter to point to the

> DiceDemo.java.

>

> so...until now i wonder, why this object declare

> outside from DiceDemo declaration?

>

> before, i am using C++ for flash and web

> designing...not for professional programming. So,

> even i know about C++...it just a simple and limited

> code.

ROFL!!!!!!!!!!!

Edit: "Go to ludicrous speed!!!!!"

masijade.a at 2007-7-21 17:25:07 > top of Java-index,Java Essentials,New To Java...
# 30
> Thats the code for Dice. > > What happened to the indentation. Won't the forum do> it for us?No, it has never been like that.
kajbja at 2007-7-21 17:25:12 > top of Java-index,Java Essentials,New To Java...
# 31

> > class Dice

> {

> private var1,var2;

> public void setDice(int var1,int var2)

> {

> this.var1=var1;

> this.var2=var2;

> }

>

> public void setThrow()

> {

> System.out.println(var1" : "var2);

> }

> }

>

Beautiful....

(shakes head, walks away muttering)

UN-believeable.

%

duffymoa at 2007-7-21 17:25:12 > top of Java-index,Java Essentials,New To Java...
# 32

you better say thank you!! :-D

class DiceDemo {

public static void main(String[] args)

{

Dice d1 = new Dice(6,3);

Dice d2 = new Dice(5,2);

System.out.print("d1: " + d1.getNumOne() +", " + d1.getNumTwo());

System.out.print("\nd2: " + d2.getNumOne() +", " + d2.getNumTwo());

}

}

class Dice

{

private int num1, num2;

Dice(int number1, int number2)

{

num1 = number1;

num2 = number2;

}

public int getNumOne()

{

return num1;

}

public int getNumTwo()

{

return num2;

}

}

ronana at 2007-7-21 17:25:12 > top of Java-index,Java Essentials,New To Java...
# 33
are you guys kidding me? thank you for what?%
duffymoa at 2007-7-21 17:25:12 > top of Java-index,Java Essentials,New To Java...
# 34
for answering his question!
ronana at 2007-7-21 17:25:12 > top of Java-index,Java Essentials,New To Java...
# 35
> for answering his question!But the answer isn't correct. Where's your setThrow method?
kajbja at 2007-7-21 17:25:12 > top of Java-index,Java Essentials,New To Java...
# 36
i dont need to use a setThrow method, i created two objects.
ronana at 2007-7-21 17:25:12 > top of Java-index,Java Essentials,New To Java...
# 37

> > for answering his question!

>

> But the answer isn't correct. Where's your setThrow

> method?

kajbj right....! ;-)

DiceDemo class are given as a default blueprint....then, i want to create a new class for dice that was declared as an object in DiceDemo.java....

Thanks a lot...

Ayonga at 2007-7-21 17:25:12 > top of Java-index,Java Essentials,New To Java...
# 38

class DiceDemo {

public static void main(String[] args)

{

Dice dice = new Dice(6,3);

System.out.print("\nUsing Constrcutor, Dice is : " + dice.getNumOne() +", " + dice.getNumTwo());

dice.setThrow(1,2);

System.out.print("\nUsing Set Throw, Dice is : " + dice.getNumOne() +", " + dice.getNumTwo());

}

}

class Dice

{

private int num1, num2;

Dice(int number1, int number2)

{

num1 = number1;

num2 = number2;

}

public void setThrow(int number1, int number2)

{

num1 = number1;

num2 = number2;

}

public int getNumOne()

{

return num1;

}

public int getNumTwo()

{

return num2;

}

}

ronana at 2007-7-21 17:25:12 > top of Java-index,Java Essentials,New To Java...
# 39

one without constructors

class DiceDemo {

public static void main(String[] args)

{

Dice dice = new Dice();

dice.setThrow(1,2);

System.out.print("\nUsing Set Throw, Dice is : " + dice.getNumOne() +", " + dice.getNumTwo());

}

}

class Dice

{

private int num1, num2;

public void setThrow(int number1, int number2)

{

num1 = number1;

num2 = number2;

}

public int getNumOne()

{

return num1;

}

public int getNumTwo()

{

return num2;

}

}

ronana at 2007-7-21 17:25:12 > top of Java-index,Java Essentials,New To Java...
# 40

> The thing that make me not understand now is how to

> create a source file for the blueprint as below:

>

> class DiceDemo {

> public static void main(string[ ] args) {

> Dice d1, d2;

> d1 = new Dice();

> d1.setDice(6,3);

> d2 = new Dice();

> d2.setDice(5,2);

>

> ystem.out.print("d1: ");

> d1.setThrow();

> System.out.println();

> System.out.print("d2: ");

> d2.setThrow();

> System.out.println();

> }

> }

>

> So, output for this DiceDemo.java is:

>

> 6:3

> 5:2

>

> Here is my explanation about dice class:

> - this program called DiceDemo.java create one

> object: Dice.

> - So, Dice object need a source file to compile this

> program...let say Dice.java.

> - Now, i need to create that source file with:

>Method

> setDice(player1,player2)

>=> player throw their dice and get the value.

> => at the first, value both of players = 0.

>

>setThrow( )

>=> no parameter are given.

> - That only i can explain....i can't describe more

> specific, because i am new with java.

Ronan...here i put my last post about my problem. Hopefully, it help you to help me...

Regards.

Ayonga at 2007-7-21 17:25:12 > top of Java-index,Java Essentials,New To Java...
# 41
i'm not sure what you mean? can you type out the entire specification from your assignment please.
ronana at 2007-7-21 17:25:12 > top of Java-index,Java Essentials,New To Java...
# 42

> for answering his question!

I'm thinking that an abstraction of Dice that doesn't include Random is pretty useless.

I'd like to write a Craps simulation with your Dice class. I'd beat the house every time by setting the values to what would win for me. I'd take you for every cent you have.

%

duffymoa at 2007-7-21 17:25:12 > top of Java-index,Java Essentials,New To Java...
# 43
It's like playing with loaded Dice.%
duffymoa at 2007-7-21 17:25:12 > top of Java-index,Java Essentials,New To Java...
# 44

> i'm not sure what you mean?

>

> can you type out the entire specification from your

> assignment please.

OK...first, i am sorry if you can't understand me...i'm not good in english.

Here, i repost the program called DiceDemo.java. It creates two object of Dice. d1 refer to first Dice, and d2 refer to second Dice...

class DiceDemo {

public static void main(string[ ] args) {

Dice d1, d2;

d1 = new Dice();

d1.setDice(6,3);

d2 = new Dice();

d2.setDice(5,2);

System.out.print("d1: ");

d1.setThrow();

System.out.println();

System.out.print("d2: ");

d2.setThrow();

System.out.println();

}

}

So, output for this DiceDemo.java is:

6:3

5:2

To compile this program...i need a source file for Dice such as Dice.java. So, my problem is, i don't know how to create that file (Dice.java).

a bit specification about Dice.java:

=> setDice(player1, player2)

both player throw their dice and get their value;

at first, the value both of them = 0;

=> setThrow( )

no parameter are given;

Thanks again....

Message was edited by:

Ayong

Ayonga at 2007-7-21 17:25:12 > top of Java-index,Java Essentials,New To Java...
# 45
I am unable to understand what you want exactly. The code that i gave works,won't it?
qUesT_foR_knOwLeDgea at 2007-7-21 17:25:17 > top of Java-index,Java Essentials,New To Java...
# 46
> I am unable to understand what you want exactly. The> code that i gave works,won't it?Does anyone else think Ayong is a troll? His posts have the specific gravity of lead.
DrLaszloJamfa at 2007-7-21 17:25:17 > top of Java-index,Java Essentials,New To Java...
# 47
> Does anyone else think Ayong is a troll? His posts> have the specific gravity of lead.His posting history doesn't indicate anything, but he is asking the same thing again and again inspite of getting the answer.
qUesT_foR_knOwLeDgea at 2007-7-21 17:25:17 > top of Java-index,Java Essentials,New To Java...
# 48
> I am unable to understand what you want exactly. The> code that i gave works,won't it?Yes, it works... thanks a lot qUesT!i just reply what Ronan want to know........Sorry Doc..:-(
Ayonga at 2007-7-21 17:25:17 > top of Java-index,Java Essentials,New To Java...
# 49

any closer to what you want?

public static void main(String[] args)

{

Dice dice = new Dice();

dice.setThrow(1,2); //we dont use this is this example

System.out.print("\Dice is : " + dice.getNum1() +", " + dice.getNum2());

}

}

class Dice

{

private int num1, num2;

public void setThrow(int number1, int number2)

{

num1 = number1;

num2 = number2;

}

public double getNum1()

{

return Math.floor(Math.random() *6) + 1;

}

public double getNum2()

{

return Math.floor(Math.random() * 6) + 1;

}

}

ronana at 2007-7-21 17:25:17 > top of Java-index,Java Essentials,New To Java...
# 50

> I'd like to write a Craps simulation with your Dice

> class. I'd beat the house every time by setting the

> values to what would win for me. I'd take you for

> every cent you have.

>

> %

Is he making a Craps simulation?

I assume a beginner would be demonstrating knowledge of get and set methods........ dont you?

ronana at 2007-7-21 17:25:17 > top of Java-index,Java Essentials,New To Java...
# 51
> any closer to what you want?Not...absolutely not! For beginning, i don't want to go so far...i like to use a simple code. Anyway, many thanks to you...Regard.
Ayonga at 2007-7-21 17:25:17 > top of Java-index,Java Essentials,New To Java...
# 52
@Ronan. Did you even read reply 48 what the op said?
qUesT_foR_knOwLeDgea at 2007-7-21 17:25:17 > top of Java-index,Java Essentials,New To Java...
# 53
> @Ronan. Did you even read reply 48 what the op said?Yes i did quest.But It's quite possible that the random way of throwing a dice could appeal to him after seeing it working.........
ronana at 2007-7-21 17:25:17 > top of Java-index,Java Essentials,New To Java...
# 54

> Is he making a Craps simulation?

I have no idea.

> I assume a beginner would be demonstrating knowledge

> of get and set methods........ dont you?

I assume nothing.

'm saying that a class like the Dice without any significant behavior, when it's easy to see how it could be made more significant, is worth pointing out.

%

duffymoa at 2007-7-21 17:25:17 > top of Java-index,Java Essentials,New To Java...
# 55
Thanks we would not have realised that only for your input.... The concept of Die sure is a difficult one....
ronana at 2007-7-21 17:25:17 > top of Java-index,Java Essentials,New To Java...