Enhance the Die class with a toString() method that returns a graphical represen
ID: 3544556 • Letter: E
Question
Enhance the Die class with a toString() method that returns a graphical representation of the Die value. A couple of simple examples:
A simple way to design text shapes (also known as ASCII shapes) is to embed line feeds in addition to using hard line feeds and string concatenation to ensure everything lines up:
Now that you have fancy ASCII output from your Die class it is time to enhance the PairOfDice class as well to show both die graphically. Add a toString() method to the PairOfDice class that prints out both Die in a graphical format. You shouldn't re-implement the graphics from the Die class but rather use the string returned from toString() of the Die class to generate the PairOfDice graphical representation.
If you get done with the first two parts and you want to try something harder try to print out both die graphics side by side by interleaving the lines of both Die string representations.
One way to get the individual lines of a String with embedded line feeds is to use a Scanner class on a string:
Explanation / Answer
FIrst One:
Main Class:
import java.util.*;
public class DiceGame
{
public static void main(String args[])
{
// To get a random number
Random r1 = new Random();
int rand1 = r1.nextInt(6);// + 1;
int tot=rand1+1;
System.out.println("die = " + tot+" ");
PairOfDice obj = new PairOfDice();
obj.toString(tot);
}
}
sideClass:
public class PairOfDice {
public String toString(int tot)
{
//To make all the dice 1-6
String roll1 = " |-------| | | | * | | | |-------|";
String roll2 = " |-------| | * | | | | * | |-------|";
String roll3 = " |-------| | * | | * | | * | |-------|";
String roll4 = " |-------| | * * | | | | * * | |-------|";
String roll5 = " |-------| | * * | | * | | * * | |-------|";
String roll6 = " |-------| | * * | | * * | | * * | |-------|";
//Make dice appear
{
if(tot == 1)
System.out.println(roll1);
else if (tot == 2)
System.out.println(roll2);
else if (tot == 3)
System.out.println(roll3);
else if (tot == 4)
System.out.println(roll4);
else if (tot == 5)
System.out.println(roll5);
else if (tot == 6)
System.out.println(roll6);
}
return roll1;
}
}
Second One:
import java.util.*;
public class DiceGame
{
public static void main(String args[])
{
// To get a random number
Random r1 = new Random();
int rand1 = r1.nextInt(6);// + 1;
int tot=rand1+1;
int rand2 = r1.nextInt(6);// + 1;
int tot1=rand2+1;
System.out.println("Two dice = " + tot+" "+tot1+" ");
PairOfDice obj = new PairOfDice();
obj.toString(tot,tot1);
}
}
Sideclass:
public String toString(int tot,int tot1)
{
//To make all the dice 1-6
String roll1 = " |-------| | | | * | | | |-------|";
String roll2 = " |-------| | * | | | | * | |-------|";
String roll3 = " |-------| | * | | * | | * | |-------|";
String roll4 = " |-------| | * * | | | | * * | |-------|";
String roll5 = " |-------| | * * | | * | | * * | |-------|";
String roll6 = " |-------| | * * | | * * | | * * | |-------|";
//Make dice appear
{
if(tot == 1)
System.out.println(roll1);
else if (tot == 2)
System.out.println(roll2);
else if (tot == 3)
System.out.println(roll3);
else if (tot == 4)
System.out.println(roll4);
else if (tot == 5)
System.out.println(roll5);
else if (tot == 6)
System.out.println(roll6);
}
{
if(tot1 == 1)
System.out.println(roll1);
else if (tot1 == 2)
System.out.println(roll2);
else if (tot1 == 3)
System.out.println(roll3);
else if (tot1 == 4)
System.out.println(roll4);
else if (tot1 == 5)
System.out.println(roll5);
else if (tot1 == 6)
System.out.println(roll6);
}
return roll1;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.