Welcome to Dream.In.Code
Become a Java Expert!

Join 149,947 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,366 people online right now. Registration is fast and FREE... Join Now!




trouble getting custom class to work

 
Reply to this topicStart new topic

trouble getting custom class to work

bwat47
24 Sep, 2008 - 03:42 PM
Post #1

New D.I.C Head
*

Joined: 23 Sep, 2008
Posts: 24


My Contributions
This is the first time we are making our own classes and I am kind of lost. I have read the chapter like 10 times but the book is too vague.

Basically I have to create a class called Die to represent a Die. It has to have a method called rollDie that generates a number between 1 and 6. I have to have a constructor that initializes the die to 1. I ahve to make a program to test it so I am trying to make a program that calls the class and generates a random number when run ann outputs it. bit it outputs a bunch of random numbers and letters.

here is the class I made:

CODE

/* Problem6
* Brandon Watkins
* 9/23/08
* Die Class
*
* generates random number between 1 and 6 when die is rolled
*/

import java.util.*;
import java.lang.*;

public class Die {
    
    public int Die;
    public Die() {
        
        Die = 1; //Constructor initializes die as 1
    }
    
    public int rollDie (int rollDie)
    {
        rollDie = (int) (Math.random() * 6) + 1;
        return Die; // generates random number between 1 and 6
    
    }    
        
}


here is the program I have so far:

CODE
/* Problem6
* Brandon Watkins
* 9/23/08
*
*
* generates random number between 1 and 6 when die is rolled
*/


// Import Classes Uncomment as needed
import java.util.*;
import java.lang.*;

//replace ,program name> below with your program's name without the < and >

public class Problem6
{
  static Scanner console = new Scanner (System.in);
  
  public static void main(String[] args)
  {
    
    //variable declaration
    
    Die result = new Die();
    
    
  
    
    //executable statements
    System.out.println("You rolled " + result);
    
    
  }
  
}


here is that I output, I want it to say You rolled ( a number between 1 and 6 is printed)

here is what i get:

CODE
You rolled Die@61de33

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Trouble Getting Custom Class To Work
24 Sep, 2008 - 04:13 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
public int Die; variables shouldn't start with capitals, only classes should be this way.

Your class isn't working because you want to output a number and you are outputing the object itself. With your current code you would want to put: System.out.println("You rolled " + result.Die);

it outputs the @##### because this is the memory address that your object is stored at, adding the .Die will access the variable within the object.

This post has been edited by William_Wilson: 24 Sep, 2008 - 04:14 PM
User is offlineProfile CardPM
+Quote Post

bwat47
RE: Trouble Getting Custom Class To Work
24 Sep, 2008 - 04:24 PM
Post #3

New D.I.C Head
*

Joined: 23 Sep, 2008
Posts: 24


My Contributions
alright, that helps a bit, now everytime I run it it says I roll 1 every single time instead of a random number.

here's the current code:

CODE
// Import Classes Uncomment as needed
import java.util.*;
import java.lang.*;

//replace ,program name> below with your program's name without the < and >

public class Problem6
{
  static Scanner console = new Scanner (System.in);
  
  public static void main(String[] args)
  {
    
    //variable declaration
    
    Die result = new Die();
    
    
  
    
    //executable statements
    System.out.println("You rolled " + result.die);
    
    
  }
  
}


here's the class:

CODE
import java.util.*;
import java.lang.*;

public class Die {
    
    public int die;
    public Die() {
        
        die = 1; //Constructor initializes die as 1
    }
    
    public int rollDie (int rollDie)
    {
        rollDie = (int) (Math.random() * 6) + 1;
        return die; // generates random number between 1 and 6
    
    }    
        
}

User is offlineProfile CardPM
+Quote Post

Mach1Guy
RE: Trouble Getting Custom Class To Work
24 Sep, 2008 - 04:26 PM
Post #4

D.I.C Head
Group Icon

Joined: 4 Dec, 2006
Posts: 79



Thanked: 4 times
Dream Kudos: 25
My Contributions
ok you got a few problems here

in your Die class I'm not exactly sure why you are passing an int into the rollDie method, this is uneccesary. it just needs to return a value. plus another mistake in this method is that you are returning the value die, instead of the random integer rollDie

CODE

    public int rollDie (int rollDie)
    {
        rollDie = (int) (Math.random() * 6) + 1;
        return Die; // generates random number between 1 and 6
    
    }    

CODE

    public int rollDie ()
    {
        int rollDie = (int) (Math.random() * 6) + 1;
        return rollDie; // generates random number between 1 and 6
    
    }





and one other thing, when you output the values in the Problem6 class, you need something like this

CODE

System.out.println("You rolled " + result.rollDie());


so that you actually call the rollDie method on the result class smile.gif
User is offlineProfile CardPM
+Quote Post

bwat47
RE: Trouble Getting Custom Class To Work
24 Sep, 2008 - 04:31 PM
Post #5

New D.I.C Head
*

Joined: 23 Sep, 2008
Posts: 24


My Contributions
ah thanks man, I did the changes you posted and changed
System.out.println("You rolled " + result.die);

to

System.out.println("You rolled " + result.rollDie());

and now it works great!
User is offlineProfile CardPM
+Quote Post

Mach1Guy
RE: Trouble Getting Custom Class To Work
24 Sep, 2008 - 04:33 PM
Post #6

D.I.C Head
Group Icon

Joined: 4 Dec, 2006
Posts: 79



Thanked: 4 times
Dream Kudos: 25
My Contributions
your welcome, but I really hope your understanding the concepts here. I just realized I probably shouldn't post the code like that, but post a detailed explanation and then let you attempt it again. good luck smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 05:22PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month