Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using classes

 
vladimir mujakovic
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi my question is about working with multiple classes. in my latest assignment for school I have to create a class that is supposed to feed information to another class. in this case its a class that is supposed to hold information about 1 six-sides die. this class is named Die, the other class is supposed to know about rolling 2 Dice, this class is called PairOfDice. I have already written the program and it works but, no thanks to the Die Class I dont even think the program uses it. Because the pairOfDice class does all the work.

My Question is how can use the PairOfDice class to use the die class to accomplish the goal? (the project is a program that rolls 2 dice)

this is my code:


 
Stuie Clarky
Ranch Hand
Posts: 98
Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your pairOfDice class currently performs all the same functionality pretty much as the Die class. What I think your pair class should do is create two new Die objects instead, and use the methods of the Die class to do what is necessary. The good news is that your code is very nearly there, just a few small changes and it should work as required.
 
vladimir mujakovic
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have thought about doing that, part of the issue is im supposed to have both values combined into a sumOfDice but the problem is when get to the part public int getSum() , i cant add the objects together because it requires integer values. this is where i am

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your PairofDice.roll method should just call the roll method on your two Die instances
Your getSum method should return d1.getValue() + d2.getValue()
 
vladimir mujakovic
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
figured out how to add them, now im wondering what i need the public PairOfDice() for even if i disable those two statements inthere, the application works fine. Why? the assignment states that methos sets both dice to default 6 sided die.


 
vladimir mujakovic
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Your PairofDice.roll method should just call the roll method on your two Die instances
Your getSum method should return d1.getValue() + d2.getValue()


thank you heh, i posted at the same time as you. What do you think is the purpose of the PairOfDice() method? the program seems to run fine without any code inthere.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vladimir mujakovic wrote:What do you think is the purpose of the PairOfDice() method? the program seems to run fine without any code inthere.

We'd have to see the program to answer that
 
vladimir mujakovic
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the whole program, the main method should describe whats going on. you roll, select to do it again if you wish etc. the validator class is there to avoid exceptions.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vladimir mujakovic wrote:What do you think is the purpose of the PairOfDice() method? the program seems to run fine without any code inthere.

What do you mean by this ? If there was no code in the PairOfDice class (not method) then your program wouldn't work.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vladimir mujakovic wrote:this is the whole program...

A couple of little pointers for you:
1. Have a look at the java.util.Random class. It's a lot better than Math.random(), and has methods for returning integers.
2. Rather than a PairOfDice class, which seems to have limited scope, what about a Dice class that can be initialized with any number of them? Then a pair of dice just becomes new Dice(2). Indeed, if you want to get really fancy, you could base it on a Collection, and have add(Die) and remove() methods - but maybe that's overkill.

Winston
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:2. Rather than a PairOfDice class, which seems to have limited scope, what about a Dice class that can be initialized with any number of them?

Of course, if your assignment says to use a PairOfDice class, then that is what you should do. You can then try implementing Winston's suggestion as a learning exercise on your own afterwards.
 
Junilu Lacar
Bartender
Pie
Posts: 7774
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vladimir mujakovic wrote:figured out how to add them, now im wondering what i need the public PairOfDice() for even if i disable those two statements inthere, the application works fine. Why? the assignment states that methos sets both dice to default 6 sided die.


Understand what these lines of code do and you'll understand why there are two assignments that are redundant pointless.
 
vladimir mujakovic
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
vladimir mujakovic wrote:figured out how to add them, now im wondering what i need the public PairOfDice() for even if i disable those two statements inthere, the application works fine. Why? the assignment states that methos sets both dice to default 6 sided die.


Understand what these lines of code do and you'll understand why there are two assignments that are redundant pointless.


yeah i realized i declare and initialize both times, which is incidentally what was at the root of my question. thank you
 
vladimir mujakovic
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
vladimir mujakovic wrote:this is the whole program...

A couple of little pointers for you:
1. Have a look at the java.util.Random class. It's a lot better than Math.random(), and has methods for returning integers.
2. Rather than a PairOfDice class, which seems to have limited scope, what about a Dice class that can be initialized with any number of them? Then a pair of dice just becomes new Dice(2). Indeed, if you want to get really fancy, you could base it on a Collection, and have add(Die) and remove() methods - but maybe that's overkill.

Winston


yeah I've used it before, but this assignment calls for this method spcifically. I prefer java.util.Random myself.

in fact one of the questions i posed to my professor is why there is so many redundancies in the structure (the class structure was provided for the assignment), when I wrote it the first time I didnt even need a Die Class but the point of the assignment is apparently to learn how to work with multiple classes.

thanks
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vladimir mujakovic wrote:in fact one of the questions i posed to my professor is why there is so many redundancies in the structure (the class structure was provided for the assignment), when I wrote it the first time I didnt even need a Die Class but the point of the assignment is apparently to learn how to work with multiple classes.

Actually, a Die class is not a bad idea at all. In fact whenever you're creating classes that emulate things, they should usually be singular. You can then use (or wrap) any number of them you want - and that's where collections come in...

Winston
 
Junilu Lacar
Bartender
Pie
Posts: 7774
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vladimir mujakovic wrote:I didnt even need a Die Class but the point of the assignment is apparently to learn how to work with multiple classes.


And the point of using multiple classes is to assign different responsibilities and break down the problem into smaller, more manageable parts. Sure, you can write the program as a big ball of mud in the main method. Or you could break the problem up into smaller logical units called objects and let each object be responsible for solving one small part of the overall problem. The art and skill of designing comes in the way you break things up and how you assign responsibilities.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic