aspose file tools*
The moose likes Beginning Java and the fly likes using classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using classes" Watch "using classes" New topic
Author

using classes

vladimir mujakovic
Greenhorn

Joined: Feb 12, 2013
Posts: 18
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

Joined: Nov 09, 2012
Posts: 76

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.


OCAJP 7
vladimir mujakovic
Greenhorn

Joined: Feb 12, 2013
Posts: 18
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

Joined: Aug 05, 2005
Posts: 3739
    
  16
Your PairofDice.roll method should just call the roll method on your two Die instances
Your getSum method should return d1.getValue() + d2.getValue()


Joanne
vladimir mujakovic
Greenhorn

Joined: Feb 12, 2013
Posts: 18
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

Joined: Feb 12, 2013
Posts: 18
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

Joined: Aug 05, 2005
Posts: 3739
    
  16
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

Joined: Feb 12, 2013
Posts: 18
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

Joined: Aug 05, 2005
Posts: 3739
    
  16
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

Joined: Mar 17, 2011
Posts: 8244
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
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

Joined: Feb 26, 2001
Posts: 5018
    
    8

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
vladimir mujakovic
Greenhorn

Joined: Feb 12, 2013
Posts: 18
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

Joined: Feb 12, 2013
Posts: 18
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

Joined: Mar 17, 2011
Posts: 8244
    
  23

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

Joined: Feb 26, 2001
Posts: 5018
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using classes