Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to make code more efficient?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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!

    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]);
        }
     
     
    }
    Last edited by helloworld922; October 21st, 2011 at 01:53 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.:
              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:

    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.
    Last edited by helloworld922; October 21st, 2011 at 09:15 AM.

Similar Threads

  1. How to make a simple and efficient web control panel.
    By kelsmith11 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 18th, 2011, 02:28 PM
  2. need help to make my code flexible.
    By sagar474 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 29th, 2011, 12:11 PM
  3. How to make HyperLink in java code?
    By abhiM in forum Java Theory & Questions
    Replies: 1
    Last Post: August 24th, 2011, 07:16 AM
  4. Make my code follow MVC
    By alpvii in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 29th, 2010, 07:48 AM
  5. Replies: 4
    Last Post: September 5th, 2010, 10:29 AM