Solved

Java errors - some random thing

Posted on 2006-11-20
14
199 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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi there,

to run, try using:

java -classpath .:$CLASSPATH Yum

See if that works...

Thanks,
Nish
0
 

Author Comment

by:junaid_raja30
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

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

Expert Comment

by:ADSLMark
Comment Utility
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
Comment Utility
How can you print blank lines in Java ? Thats all.... thank you sooo much Mark
0
 
LVL 10

Expert Comment

by:ADSLMark
Comment Utility
System.out.println();
0
 

Author Comment

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

Expert Comment

by:ADSLMark
Comment Utility
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
Comment Utility
Great coool.... Thanx bruv ... really learnt alot here today ... here you go i am sending you the points.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now