5 Replies - 633 Views - Last Post: 24 September 2008 - 05:33 PM Rate Topic: -----

#1 bwat47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-September 08

trouble getting custom class to work

Post icon  Posted 24 September 2008 - 04:42 PM

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:

/* 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:

/* 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:

You rolled Die@61de33


Is This A Good Question/Topic? 0
  • +

Replies To: trouble getting custom class to work

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: trouble getting custom class to work

Posted 24 September 2008 - 05:13 PM

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 September 2008 - 05:14 PM

Was This Post Helpful? 0
  • +
  • -

#3 bwat47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-September 08

Re: trouble getting custom class to work

Posted 24 September 2008 - 05:24 PM

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:

// 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:

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


Was This Post Helpful? 0
  • +
  • -

#4 Mach1Guy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: trouble getting custom class to work

Posted 24 September 2008 - 05:26 PM

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

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


	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

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



so that you actually call the rollDie method on the result class :)
Was This Post Helpful? 0
  • +
  • -

#5 bwat47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-September 08

Re: trouble getting custom class to work

Posted 24 September 2008 - 05:31 PM

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!
Was This Post Helpful? 0
  • +
  • -

#6 Mach1Guy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: trouble getting custom class to work

Posted 24 September 2008 - 05:33 PM

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 :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1