• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

object-oriented programming

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My professor wants me to force the value of d1 (dice 1) to the value of d2 (dice 2) and the only way I can think to do it is skip all the dice ling and just println it. I guess I don't understand how to compare the 2 items and make FORCE them to equal each other. He also says, "Remember cascading (you can code a call anywhere the returned data type makes sense)" If I can only fix this by using one line of code how can I cascade? Please help.

public class Lab17A {
public static void main(String[] args) {
Dice d1 = new Dice();
Dice d2 = new Dice();
System.out.println("Start: " + d1.getValue() + ", " + d2.getValue());
do {
d1.roll();
d2.roll();

// YOUR STATEMENT GOES HERE

System.out.println("Rolled: " + d1.getValue() + ", " + d2.getValue());
} while(d1.getValue() != d2.getValue());
System.out.println("Game over");
}
}
class Dice {
private int sides;
private int value;
public Dice() {
setSides(6);
roll();
}
public void setSides(int n) {
if (n >= 2)
sides = n;
else
sides = 6;
}
public void setValue(int n) {
if (n > 0 && n <= sides)
value = n;
}
public int getSides() {
return sides;
}
public int getValue() {
return value;
}
public void roll() {
value = (((int)(Math.random() * 1000)) % sides) + 1;
}
}
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change your terminology from "force" to "set" and the solution may be clearer.

You have two Dice objects: d1 and d2.

The Dice objects have a Value stored inside of them.

Each of these Dice objects has a method to "getValue" and "setValue".

In order to make d1's value equal to d2's value, you need to "get" the Value from d2 and "set" it in d1.

By properly combining the method calls you should be able to do this in one line of code. (I assume that what the professor means by cascading.)
[ October 21, 2005: Message edited by: Scott Johnson ]
 
Ryan Newcombe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help Scott. I try to make them equal....It doesn't work. Am I heading in the right direction?


public class Lab17A {
public static void main(String[] args) {
Dice d1 = new Dice();
Dice d2 = new Dice();
System.out.println("Start: " + d1.getValue() + ", " + d2.getValue());
do {
d1.roll();
d2.roll();

d2.getValue() = d1.setValue();

System.out.println("Rolled: " + d1.getValue() + ", " + d2.getValue());
} while(d1.getValue() != d2.getValue());
System.out.println("Game over");
}
}
class Dice {
private int sides;
private int value;
public Dice() {
setSides(6);
roll();
}
public void setSides(int n) {
if (n >= 2)
sides = n;
else
sides = 6;
}
public void setValue(int n) {
if (n > 0 && n <= sides)
value = n;
}
public int getSides() {
return sides;
}
public int getValue() {
return value;
}
public void roll() {
value = (((int)(Math.random() * 1000)) % sides) + 1;
}
}
 
Ryan Newcombe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it!!!
Thanks for the rationalization.
-Ryan
 
reply
    Bookmark Topic Watch Topic
  • New Topic