?
Solved

Java errors - some random thing

Posted on 2006-11-20
14
Medium Priority
?
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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 700 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

777 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