2

I am trying to practice just writing a simple application with basic java concepts to reinforce these in my mind and I am getting an error that I can't seem to figure out. I'm sure this is some stupid oversight on my part, or perhaps something I don't know yet as I am learning. Any help at all would be greatly appreciated!

This is the message I get when trying to compile this code below:

error: constructor Dice in class Dice cannot be applied to given types;
        Dice di = new Dice();
                  ^
  required: int,int
  found: no arguments
  reason: actual and formal argument lists differ in length
3 errors



//import statements
import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;



class Dice 
{//Begin Dice Class

    //initiate constructor for "Random" class
    Random r = new Random();

    //declare Dice class variables
    int p1d6 = r.nextInt(6-0) + 1;
    int p2d6 = r.nextInt(6-0) + 1;


    //initiate class constructors
    public Dice (int p1d6, int p2d6)
    {
        this.p1d6 = p1d6;
        this.p2d6 = p2d6;
    }


    //create set and get methods for class variables
    public void setP1d6(int p1d6)
    {
        this.p1d6 = p1d6;
    }
    public int getP1d6()
    {
        return p1d6;
    }


    public void setP2d6(int p2d6)
    {
        this.p2d6 = p2d6;
    }
    public int getP2d6()
    {
        return p2d6;
    }


    //toString method that outputs the class variables
    //public String toString ()
    //{
    //  return "P1D6: " + p1d6 + "\n" + "P2D6: " + p2d6 + "\n";
    //}

}// End Dice Class

public class DiceGame2
{
    public static void main( String ars[] )
    {

        //initiate constructor for Dice class
        Dice di = new Dice();

        System.out.println( "--- Welcome to the Dice Game v2! ---" ); // welcomes player
        System.out.println("P1D6: " + di.getP1d6() + "\n");
        System.out.println("P2D6: " + di.getP2d6() + "\n");
        System.out.println("P1D6: " + di.getP1d6() + "\n");
        System.out.println("P2D6: " + di.getP2d6() + "\n");
        System.out.println("P1D6: " + di.getP1d6() + "\n");
        System.out.println("P2D6: " + di.getP2d6() + "\n");
    }
}

3 Answers 3

6

Your Dice constructor takes 2 ints as arguments. You're trying to call the constructor without supplying those arguments. Either supply those arguments, or create a constructor that doesn't take arguments.

Sign up to request clarification or add additional context in comments.

Comments

1

The only constructor you created takes 2 arguments

public Dice (int p1d6, int p2d6)
{
    this.p1d6 = p1d6;
    this.p2d6 = p2d6;
}

You are trying to create a Dice object with 0 arguments

Dice di = new Dice();

There are 2 was solutions, first, you could change you the way you are calling the constructor.

Dice di = new Dice(1, 2);

Second, you could create a Constuctor in your Dice class that takes 0 arguments.

public Dice()
{
}

Comments

0

Calling a no-args constructor

    //initiate constructor for Dice class
    Dice di = new Dice();

is problem when you declared that the only constructor required arguments:

    public Dice (int p1d6, int p2d6)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.