How to make code more efficient?
Hey guys, I created a program for my class called CountDiceRollsArray thats purpose is to roll a single die the specified number of times by the user and then display a table that shows the six face values of the die and how many times that face value was rolled within the specified number of times. I wrote the program and it works well. I was just wondering if someone out there that has experience with programming can tell me how I can write the code better or if any one of my variables can be made into an array or used more efficiently. I was wondering if it is "proper" java etiquette to call methods form other methods, like I did with my display() method or if thats frowned upon in the java community and I should try to call everything from the main. Here is my code. Thanks!
Code Java:
import java.util.Scanner;
import java.util.Random;
public class CountDiceRolls
{
public static void main(String[] args)
{
int numberOfDieRolls = rollADie();
title();
faceValue(numberOfDieRolls);
}
public static int rollADie()
{
Scanner input = new Scanner(System.in);
System.out.print("How Many Times Would You Like To Roll A Die: ");
int num = input.nextInt();
return num;
}
public static void title()
{
System.out.print("Face\tTimes\n");
}
public static void faceValue(int numberOfDieRolls)
{
Random random = new Random();
int [] faceVal = new int [6];
for(int count = 0; count < numberOfDieRolls; count++)
{
int face = 1 + random.nextInt(6);
if(face == 1)
{
faceVal[0]++;
}
else if(face == 2)
{
faceVal[1]++;
}
else if(face == 3)
{
faceVal[2]++;
}
else if(face == 4)
{
faceVal[3]++;
}
else if(face == 5)
{
faceVal[4]++;
}
else if(face == 6)
{
faceVal[5]++;
}
}
display(faceVal);
}
public static void display(int [] faceVal)
{
System.out.print(" 1\t" + faceVal[0] + "\n 2\t" + faceVal[1] + "\n 3\t" + faceVal[2] + "\n 4\t" + faceVal[3] + "\n 5\t" + faceVal[4] + "\n 6\t" + faceVal[5]);
}
}
Re: How to make code more efficient?
My rule: if it ain't broke don't fix it. That being said...one of the powers of java is that it is object oriented, which your program does not take advantage of.
Re: How to make code more efficient?
Best way to optimize code as well as reduce amount of code you have to write: use math.
ex.:
Quote:
Code Java:
if(face == 1)
{
faceVal[0]++;
}
else if(face == 2)
{
faceVal[1]++;
}
else if(face == 3)
{
faceVal[2]++;
}
else if(face == 4)
{
faceVal[3]++;
}
else if(face == 5)
{
faceVal[4]++;
}
else if(face == 6)
{
faceVal[5]++;
}
These if/else statements are completely unnecessary, and quite frankly make your code difficult to read and maintain. Find the relationship between the value of face vs. the index in faceVal that needs to be incremented.
then change it to a single statement:
Code Java:
faceVal[some_index]++; // what should some_index equal? hint: it involves the value of face
As far as displaying output in other methods, it's something I tend to avoid. The purpose of helper methods is to allow your code to compute something that can be useful later. That's not to say that you can't have methods which produce side effects being called from somewhere else other than the main method (it's very common in GUI programming), but you should try to separate methods which generate side-effects from methods which perform computation (aka. represent some model).
So in your code I would have faceValue return the array faceVal, then call display(faceValue()) in your main method. This design aspect is more generally know as the "Model-View-Controller" or MVC design method, where the model represents the underlying data structure and computation, the view is the interface (gui, console, etc.), and the controller links the two and passes communications between the two. The model should have no knowledge of the view or controller and the view should have no knowledge of the model or controller. Only the controller needs to have knowledge of both in order to perform its job.
fyi: this last bit isn't just java etiquette, it's a general programming design philosophy.