Solved

Java errors - some random thing

Posted on 2006-11-20
14
206 Views
Last Modified: 2008-02-01
Hi I am trying to make a game in java its a very simple one, called YUM using DICES.

I have made two files NDice.java and Yum.java

NDice.java - where u can set the dices and other properties for the game.

Yum.java - contains the main application.

I firstly want to the get the simple version working, I already have done the NDice.java file, and its working except for one function but i have commented that out at the moment. Below are the two files.

//file: NDice.java

public class NDice
{
    private int number;
    private int[] dice;
    int total;

    public NDice(int n)
    {
      int i;
      number = n;
      dice = new int[number];
      for (i=0; i<number; i++)
          {
            dice[i] = (int)(Math.random() *6) + 1;
          }
    }

    public NDice()
    {
      int i;
      number = 4;
      dice = new int[number];

      for (i=0; i<number; i++)
          {
            dice[i] = (int)(Math.random()*6) + 1;
          }
    }

    public void print()
    {
      int i;

      for(i=0; i<number; i++)
          {
            System.out.print(dice[i] + " ");
          }
      System.out.println();
    }

    public int getDice(int select)
    {
      return dice[select - 1];
    }

    public void setDice(int select, int newval)
    {
      dice[select - 1] = newval;
    }

    public void roll()
    {
      int i;
      for(i=0; i<number; i++)
          {
            dice[i] = (int)(Math.random() * 6) + 1;
          }
    }


    //public void getNumberOfOccurrences(int value)
    //{
    //int value = 4;
    //int i;
    //for(i=0; i<number; i++)
    //    {
    //      if (dice[i] == value)
    //          {
                  //return number of specified dices
    //          }
    //    }
    //}


    public int sum(int number)
    {
      int score = 0;
      int i;
      for (i = 0; i<number; i++)
          {
            score += dice[i];
          }
      return score;
    }


    public int getHighest()
    {
      int i;
      int score = 0;
      for(i=0; i<number; i++)
          {
            if(score<=dice[i])
                {
                  score=dice[i];
                }
            
          }
      return score;
    }

    public boolean areAllvaluesIdentical()
    {
      for(int i=0; i<number - 1; i++)
          {
            if(dice[i] != dice[i+1])
            {
                return false;
            }
          }
      return true;
    }

}

THE MAIN FILE:

//file: Yum.java


class Yum
{
    public static void main( String args[] )
    {
      
      System.out.println("Player 1");
      
      System.out.println("      Roll 1  ");
      
      NDice roll1 = new NDice(5);
      int high1 = roll1.getHighest();
      System.out.println(roll1 + "          Score = " + high1 );

      System.out.println("      Roll 2  ");

      NDice roll2 = new NDice(4);
      int high2 = roll2.getHighest();
      System.out.println(roll2 + "          Score = " + high2 );

      System.out.println("      Roll 3  ");

      NDice roll3 = new NDice(3);
      int high3 = roll3.getHighest();
      System.out.println(roll3 + "          Score = " + high3);


      System.out.println("      Roll 4  ");

      NDice roll4 = new NDice(2);
      int high4 = roll4.getHighest();
      System.out.println(roll4 + "          Score = " + high4);

      System.out.println("      Roll 5  ");

      NDice roll5 = new NDice(1);
      int high5 = roll5.getHighest();
      System.out.println(roll5 + "          Score = " + high5);
   

      int player1total = high1 + high2 + high3 + high4 + high5;
      System.out.println("Player 1 Total Score: " + player1total);
      //int val1 = t.getDice(4);
      //System.out.print(val1)




      System.out.println("Player 2");

      System.out.println("      Roll 1  ");
 
      NDice roll6 = new NDice(5);
      int high6 = roll6.getHighest();
      System.out.println(roll1 + "          Score = " + high1 );

      System.out.println("      Roll 2  ");

      NDice roll7 = new NDice(4);
      int high7 = roll7.getHighest();
      System.out.println(roll1 + "          Score = " + high1 );

      System.out.println("      Roll 3  ");

      NDice roll8 = new NDice(3);
      int high8 = roll8.getHighest();
      System.out.println(roll1 + "          Score = " + high1 );

      System.out.println("      Roll 4  ");

      NDice roll9 = new NDice(2);
      int high9 = roll9.getHighest();
      System.out.println(roll1 + "          Score = " + high1 );

      
      System.out.println("      Roll 5  ");

      NDice roll10 = new NDice(1);
      int high10 = roll10.getHighest();
      System.out.println(roll1 + "          Score = " + high1 );


      int player2total = high6 + high7 + high8 + high9 + high10;
      System.out.println("Player 2 Total Score: " + player2total);

          }
}



ERRORS RECIEVED:

Exception in thread "main" java.lang.NoClassDefFoundError: NDice.java
   at 0x40268e17: java.lang.Throwable.Throwable(java.lang.String) (/usr/lib/./libgcj.so.3)
   at 0x4025bc8e: java.lang.Error.Error(java.lang.String) (/usr/lib/./libgcj.so.3)
   at 0x4025d6b6: java.lang.LinkageError.LinkageError(java.lang.String) (/usr/lib/./libgcj.so.3)
   at 0x4025eb36: java.lang.NoClassDefFoundError.NoClassDefFoundError(java.lang.String) (/usr/lib/./libgcj.so.3)
   at 0x402ad075: gnu.gcj.runtime.FirstThread.run() (/usr/lib/./libgcj.so.3)
   at 0x4024fc4c: _Jv_ThreadRun(java.lang.Thread) (/usr/lib/./libgcj.so.3)
   at 0x4021c8ac: _Jv_RunMain(java.lang.Class, byte const, int, byte const, boolean) (/usr/lib/./libgcj.so.3)
   at 0x08048910: ?? (??:0)
   at 0x42015574: __libc_start_main (/lib/tls/libc.so.6)
   at 0x080486c1: ?? (??:0)


They both compile well using: javac NDice.java Yum.java
But when running the file, there are random errors. Please if someone can help it will be great. Any suggestions are welcome.

Thanx



0
Comment
Question by:junaid_raja30
  • 7
  • 6
14 Comments
 
LVL 10

Expert Comment

by:ADSLMark
ID: 17983003
Works fine for me:

javac NDice.java Yum.java
java Yum

and i get as output:
Player 1
      Roll 1
NDice@82ba41          Score = 6
      Roll 2
NDice@923e30          Score = 6
      Roll 3
NDice@130c19b          Score = 5
      Roll 4
NDice@1f6a7b9          Score = 6
      Roll 5
NDice@7d772e          Score = 1
Player 1 Total Score: 24
Player 2
      Roll 1
NDice@82ba41          Score = 6
      Roll 2
NDice@82ba41          Score = 6
      Roll 3
NDice@82ba41          Score = 6
      Roll 4
NDice@82ba41          Score = 6
      Roll 5
NDice@82ba41          Score = 6
Player 2 Total Score: 24

You should start Yum and not NDice and always remove the .java (that's just for filenaming), what is important is the class name.

Mark
0
 

Author Comment

by:junaid_raja30
ID: 17983423
Ok, i will try that out, but i need some help on the getNumberOfOccurrences function - the one that has been commented out, what i cannot understand is how to perform that function.

I know i need a counter but how can i do that.
0
 
LVL 3

Expert Comment

by:nishayr
ID: 17983940
Hi there,

to run, try using:

java -classpath .:$CLASSPATH Yum

See if that works...

Thanks,
Nish
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:junaid_raja30
ID: 17984158
i got it to work .... thnx Mark, but the only thing is that instead of  NDice@82ba41 there should be the numbers that are being rolled?? Dunno why is that.... is there anyone to help on that

and also i mentioned about the  

getNumberOfOccurrences function - the one that has been commented out, what i cannot understand is how to perform that function.

I know i need a counter but how can i do that.
0
 
LVL 10

Accepted Solution

by:
ADSLMark earned 175 total points
ID: 17985222
Add the method to NDice.java, I think you want sth like this:

public String toString()
{
  String res = "";
  for(int i=0;i<number;i++)
    res += dice[i]+" ";
  return res;
}

For the getNumberOfOccurrences method, i think you want the number of times that a dice with the value 'value' has been rolled, in that case:

public int getNumberOfOccurrences(int value)
{
  int ret = 0;
  for(int i=0; i<number; i++)
  {
     if (dice[i] == value)
     {
        ret++;
     }
  }
  return ret;
}

I hope it helps.
Mark
0
 

Author Comment

by:junaid_raja30
ID: 17986446
thanks a ton mark i got that working,

the only question left was if i was going to enter, say 10 dices to roll from command line, i need to create a program to actually output the same thing but with different number of dices, so say if i was to play a game with 10 dices it should do 10 rolls..etc...

do you have any explanation here?

Thanx
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 17986742
I see, well you have to parse the args in the main method. I have cleaned your program a bit to help you. Take a look:

--Yum.java
class Yum
{
    //Roll a number of dices
    public static int rollDices(int num)
    {
        int sum = 0;

        for(int i=0;i<num;i++)
        {
            NDice roll = new NDice(num-i);
            int highest = roll.getHighest();

            System.out.println("Roll "+(i+1)+" => "+roll.toString()+" (highest="+highest+")");

            sum += highest;
        }

        return sum;
    }

    //Print usage
    public static void printUsage()
    {
        System.out.println("Usage: java Yum <number>");
        System.out.println("where <number> is a positive natural number, bigger than 0.");
    }

    //Program entry
    public static void main(String[] args)
    {
        if(args.length==0)
        {
            printUsage();
            return;
        }

        try
        {
            int numRolls = Integer.parseInt(args[0]);
            if(numRolls<=0)
            {
                printUsage();
                return;
            }

            System.out.println("Player 1");
            int player1_total = rollDices(numRolls);
            System.out.println("Player 1 Total Score: "+player1_total);
            System.out.println();

            System.out.println("Player 2");
            int player2_total = rollDices(numRolls);
            System.out.println("Player 2 Total Score: "+player2_total);

            if(player1_total>player2_total)
            {
                System.out.println("Player 1 wins!");
            }
            else if(player1_total<player2_total)
            {
                System.out.println("Player 2 wins!");
            }
            else
            {
                System.out.println("Draw!");
            }
        }
        catch(NumberFormatException nfe)
        {
            printUsage();
            return;
        }
    }
}

--NDice.java
public class NDice
{
    private int number;
    private int[] dice;
    int total; //@not used

    //Default constructor
    //@not used
    public NDice()
    {
        this(4);
    }

    //Constructor
    public NDice(int n)
    {
        number = n;
        dice = new int[n];

        roll();
    }

    //Get dice
    //@not used
    public int getDice(int select)
    {
        return dice[select - 1];
    }

    //Set dice
    //@not used
    public void setDice(int select, int newval)
    {
        dice[select - 1] = newval;
    }

    //Roll dices
    public void roll()
    {
        for(int i=0;i<number;i++)
            dice[i] = (int) Math.floor((Math.random() * 6) + 1);
    }

    //Get number of occurrences of value in list
    //@not used
    public int getNumberOfOccurrences(int value)
    {
        int ret = 0;

        for(int i=0;i<number;i++)
        {
            if(dice[i] == value)
            {
                ret++;
            }
        }

        return ret;
    }

    //Get sum of the dices
    //@not used
    public int sum(int number)
    {
        int sum = 0;

        for(int i=0;i<number;i++)
        {
            sum += dice[i];
        }

        return sum;
    }

    //Get highest dice
    public int getHighest()
    {
        int max = 0;

        for(int i=0;i<number;i++)
        {
            if(dice[i] > max)
            {
                max = dice[i];
            }
        }

        return max;
    }

    //Check if all values are the same
    //@not used
    public boolean areAllvaluesIdentical()
    {
        for(int i=0;i<number-1;i++)
        {
            if(dice[i] != dice[i+1])
            {
                return false;
            }
        }

        return true;
    }

    //String representation of NDice
    public String toString()
    {
        String res = "";

        for(int i=0;i<number;i++)
        {
            res += dice[i]+" ";
        }

        return res;
    }
}
0
 

Author Comment

by:junaid_raja30
ID: 17988486
So the final thing is to create a main from these two files?
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 17988512
Just put the code in two files (as named before the code with the -- in front) and then just compile those two, and run with java Yum <number>

Mark
0
 

Author Comment

by:junaid_raja30
ID: 17996413
How can you print blank lines in Java ? Thats all.... thank you sooo much Mark
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 17996582
System.out.println();
0
 

Author Comment

by:junaid_raja30
ID: 17997514
Just wanted to ask what was the catch(NumberFormatException nfe) in the Yum.java file for?
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 17997740
If you enter a non-number, then an exception is thrown (a NumberFormatException), which is catched by the

try{}
catch(){}

construction. So for example if you try to run with:

java Yum abbc

it will throw a NumberFormatException exception.

Mark
0
 

Author Comment

by:junaid_raja30
ID: 17997860
Great coool.... Thanx bruv ... really learnt alot here today ... here you go i am sending you the points.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question