Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7641305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:53:19+00:00 2026-05-31T08:53:19+00:00

I come from a procedural programming background, and need some help grasping how methods

  • 0

I come from a procedural programming background, and need some help grasping how methods pass variables back and forth. The methods work with explicitly declared values, but when I try to pass values from one method to another, I get all kinds of “error: cannot find symbol” messages.

I suspect that I’m either a.) declaring the variables in the wrong place and they clear out at the end of the method or b.) how I’m coding the send/return of variables is wrong, or c.) both. More importantly, while I’ve read my textbook and a number of online tutorial resources, I’m still having trouble grasping how the syntax is supposed to work. Can someone clue me in?

The program: In the code provided, I’m trying to roll five six-sided dice. One method rolls a single die, the other calls that method multuiple times and writes the values to the array…I think.

Thanks in advance,
d

public class FiveDice
{
// MAIN METHOD
   public static void main(String[] args)
   {
// SET VARIABLES FOR DIE HIGH AND LOW VALUES, NUMBER OF DICE TO ROLL
      final int LOWEST_DIE_VALUE = 1;
      final int HIGHEST_DIE_VALUE = 6;
      final int DICE_TO_ROLL = 5;
// ROLL A SINGLE DIE VIA METHOD rollADie()
          int roll = rollADie(HIGHEST_DIE_VALUE,LOWEST_DIE_VALUE);
      System.out.println("roll  " + roll);
    }
//
// RETURNS THE RESULT OF A SINGLE DIE ROLL
    public static int rollADie(int HIGHEST_DIE_VALUE,int LOWEST_DIE_VALUE)
    {
        int roll;
        roll = ((int)(Math.random()*100)%HIGHEST_DIE_VALUE+LOWEST_DIE_VALUE);
        return roll;
    }
//
// CALL rollADie TO ROLL DICE_TO_ROLL (above) DICE; RETURN ARRAY OF ROLLED DICE
    public static int[] rollTheDice(int DICE_TO_ROLL, int HIGHEST_DIE_VALUE,int LOWEST_DIE_VALUE)
    {
            int rollNum;
        int rolledDie;
                for(rollNum=1;rollNum<=DICE_TO_ROLL;rollNum++)
                {
        int[] rolledDie = new rollADie(HIGHEST_DIE_VALUE,LOWEST_DIE_VALUE)
        {
           rolledDie
        };
        return rolledDie[];
            }
      }

   } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:53:21+00:00Added an answer on May 31, 2026 at 8:53 am

    I’ll be honest, this is a total mess (but we were all new at one time, right)? What you really need to do is just go through the tutorials on writing Java apps, and especially object-oriented concepts, and learn as you go. The fundamental object-oriented concept, that methods represent actions/abilities of an object they’re attached to, as opposed to being a list of mostly standalone functions/subroutines, will really help you see what’s different about OO-programming vs. functional programming.

    I’ll go section-by-section and try to help you out here.

    Section 1:

    // MAIN METHOD
    public static void main(String[] args)
    {
      // SET VARIABLES FOR DIE HIGH AND LOW VALUES, NUMBER OF DICE TO ROLL
      final int LOWEST_DIE_VALUE = 1;
      final int HIGHEST_DIE_VALUE = 6;
      final int DICE_TO_ROLL = 5;
    
      // ROLL A SINGLE DIE VIA METHOD rollADie()
      int roll = rollADie(HIGHEST_DIE_VALUE,LOWEST_DIE_VALUE);
      System.out.println("roll  " + roll);
    }
    

    Ok, so you’ve got a main method. Good start. All Java apps need this. However:

    • You’re declaring final variables in the main method, which isn’t really necessary. Remove the final key words here, re-capitalize the variable names to the camelCase convention, and just pass them in as normal variables.
    • Also, and this is just a style point, you’re declaring lowestDieValue followed by highestDieValue, which makes total sense, but then you’re passing them in highest first, then lowest. This feels a little weird and inconsistent, so I’ve flipped them around.
    • Finally, there’s no reason to do your comments in ALL CAPS either, so as another style point, I would just use normal case. In your code, however, I think your variable and method names are descriptive enough; you really don’t need the comments at all, so I’ve taken them out.

    So section 1, after these changes, should look something like:

    public static void main(String[] args) {
      int lowestDieValue = 1;
      int highestDieValue = 6;
      int diceToRoll = 5;
    
      int roll = rollADie(lowestDieValue, highestDieValue);
      System.out.println("roll  " + roll);
    }
    

    Section 2:

    • Re-order the variable names as noted above, and don’t use ALL CAPS – by convention ALL CAPS are reserved for constants (i.e. final variables). When you pass it into a method, they’re not finals/constants.
    • This method is overly verbose. It does exactly one useful thing, so it can be written in just a single line.

    When you make these changes, section 2 should look like:

    public static int rollADie(int lowestDieValue, int highestDieValue) {
      return ((int)(Math.random()*100)%highestDieValue+lowestDieValue);
    }
    

    Section 3:

    This method can be greatly simplified. We’re basically trying to accomplish three things:

    1. Create an array in which to store the rolled die values
    2. Loop through rolling dice to get the values, storing each created value in the array
    3. Return the resulting int[] (integer array)

    You’ve got several problems in this method, including calling the return statement in the middle of a for loop (which is compilable, but is technically incorrect in your case as it results in your method returning early with only one value in the array).

    I’m just going to completely re-write this method, including renaming/re-capitalizing your variable names according to the aforementioned conventions. The rewrite cleanly accomplishes the 3 things this method needs to do (as specified above):

    public static int[] rollTheDice(int numDiceToRoll, int lowestDieValue, int highestDieValue) {
      int[] allDiceRolls = new int[numDiceToRoll];
    
      for(int i = 0; i < allDiceRolls.length; i++) {
        allDiceRolls[i] = rollADie(lowestDieValue, highestDieValue);
      }
    
      return allDiceRolls;
    }
    

    Finally:

    • Style: it’s always been my habit to put opening { at the end of a line, as opposed to on their own line, but that’s just a style thing. Either style is valid and means the same thing when compiled.
    • Code: Your class is called “FiveDice” but you don’t actually roll 5 dice in your main method, nor do you ever call the rollTheDice method.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I come from a Java background, where packages are used, not namespaces. I'm used
I come from a mainly PHP background and make good use of the Apache
I come from a CVS background. I'm currently investigating using SVN for a project.
I come from a background of MoM. I think I understand ESB conceptually. However,
I come from the Java world, where you can hide variables and functions and
I come from more of a Java background. In the last year or two,
I come from a Java background and with any servlets-based technology, it's trivial to
Please Help me.I am trying to over come this paoblem from last 2 days
I come from a fairly strong OO background, the benefits of OOD & OOP
When I was learning Java coming from a background of some 20 years of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.