A
amir
Guest
DieTest.java would be the main, what recommendations would you have to make this code more professional? Are there certain design patterns, OO, ... suggestions that you would have?
Thank you in advance.
//********************************************************************
// Die.java Author: bob
//
// Represents a die with six sides that can be rolled.
//********************************************************************
public class Die{
private int face;
public Die (){
roll();
}
public void roll (){
face = 1 + (int)(Math.random() * 6 );
}
public int getFace (){
return face;
}
public String toString(){
String faceName;
faceName="The value " + face + " is showing";
return faceName;
}
}
//********************************************************************
// DieTest.java Author: bob
//
// Exercises Die with frequency
//********************************************************************
import java.util.*;
import java.text.*;
public class DieTest{
public static void main (String args[]){
String output = "\tValues\n";
String dummy;
Scanner scanner;
int randomFace;
Die d = new Die();
int[] theArray = new int [100];
int[] theFaces = new int [7];
for (int rollNo=0; rollNo < theArray.length; rollNo++)
{
d.roll();
randomFace = d.getFace();
theArray[rollNo] = randomFace;
if ((rollNo % 10)== 0) {
output += "\n" + randomFace;}
else{
output += " " + randomFace;}
++theFaces[randomFace];
}
output += "\n\n\t Frequency table";
for (int counter = 1; counter < theFaces.length; counter++)
{
output += "\n Face: " + counter + "\t Frequency: " + theFaces[counter];
}
System.out.print(output);
System.out.print("\n quit: ");
scanner = new Scanner(System.in);
dummy = scanner.next();
dummy = dummy.toUpperCase().trim();
if (! dummy.equals("Q")){
System.out.println("Use q or Q to quit: ");
dummy = scanner.next();
}
else{
System.exit(0);
}
}
}
Thank you in advance.
//********************************************************************
// Die.java Author: bob
//
// Represents a die with six sides that can be rolled.
//********************************************************************
public class Die{
private int face;
public Die (){
roll();
}
public void roll (){
face = 1 + (int)(Math.random() * 6 );
}
public int getFace (){
return face;
}
public String toString(){
String faceName;
faceName="The value " + face + " is showing";
return faceName;
}
}
//********************************************************************
// DieTest.java Author: bob
//
// Exercises Die with frequency
//********************************************************************
import java.util.*;
import java.text.*;
public class DieTest{
public static void main (String args[]){
String output = "\tValues\n";
String dummy;
Scanner scanner;
int randomFace;
Die d = new Die();
int[] theArray = new int [100];
int[] theFaces = new int [7];
for (int rollNo=0; rollNo < theArray.length; rollNo++)
{
d.roll();
randomFace = d.getFace();
theArray[rollNo] = randomFace;
if ((rollNo % 10)== 0) {
output += "\n" + randomFace;}
else{
output += " " + randomFace;}
++theFaces[randomFace];
}
output += "\n\n\t Frequency table";
for (int counter = 1; counter < theFaces.length; counter++)
{
output += "\n Face: " + counter + "\t Frequency: " + theFaces[counter];
}
System.out.print(output);
System.out.print("\n quit: ");
scanner = new Scanner(System.in);
dummy = scanner.next();
dummy = dummy.toUpperCase().trim();
if (! dummy.equals("Q")){
System.out.println("Use q or Q to quit: ");
dummy = scanner.next();
}
else{
System.exit(0);
}
}
}