Welcome Guest [Log In] [Register]



Add Reply
Objects and Classes - Part 3 (Java)
Topic Started: Jul 13 2007, 11:08 AM (800 Views)
rig
1a5a6a4
Administrators
Note: Some of this has been partially covered but its good to know stuff again and possibly different uses and more.

Firstly some terms to know

Void - Returns nothing
Random gen = new Random(); //Creates a new random number generator
gen.nextInt(SomeNumber); //nextInt() is a method for the generator object
//Which actually generates the random number, from zero to SomeNumber
Constructor - What sets the object to be different from others
Instanziation - Where we create the object, using our decided constructor
Overloading - Rather than dice(), its be dice(x), or dice(x,y) -- Another
constructer except this one is when there is "too much for the other to
handle" i.e. the name overloading.

You may be wondering... classes? "I think I already know how to make those",
or "Classes? What are you talking about?"

Quote:
 

public class dice
{


Does that explain classes good enough? I hope, but actually classes are
things that do things... simplify things, so you dont have to do them over
and over again.
"Instances" of classes are called objects (Seems easy enough, especially if
you know GML). Instead of ball.x, think dice_a.roll, although there'll be
much much more.

Quote:
 

import java.util.Random;

public class dice
{
//instead of what we usually do we are going to declare the

variables here

int sides;
int roll;
Random gen;


How do we determine how many sides the die has... here is how

Quote:
 

//Going to make a regular six sided die
public dice()
{
gen = new Random();
sides = 6;
}
//What if we want to also be able to set dice? Overloading.
public dice(int x)
{
gen = new Random();
//We use dice(SomeNumber) and it takes an integer somenumber and
//sides = SomeNumber;

sides = x;
}


Great... so now in our main program we can do this:

Quote:
 

public class roller
{
public static void main()
{
dice first = new dice();
//We just created a dice object, which has 6 sides
dice second = new dice(8);
//An eight sided die will also work, called second



Depending on your compiler you may need to show some inheiritance but that
depends on your compiler so I cant help on that, it might say in error
messages when you attempt to compile.
Now how do we get a random number from this die? Add this to your dice

class.

Quote:
 

//This wont return anything (because its void)
public void roll()
{
roll=gen.nextInt(sides)+1;
//We generate a number with a max of our sides (Random Numbers start from zero)
}

//This will return an integer (because its int) This is how we get the roll
public int getroll()
{
return roll;
}


Once you understand this, you can also combine the two or make other
variations too.
...Now lastly lets go back to our main program

Quote:
 

first.roll();
System.out.println("First Die(6): "+first.getroll());
second.roll();
System.out.println("First Die(8): "+second.getroll());


Any questions, comments, things that didnt quite make sense, I worded
badly, or didnt quite finish an idea, please tell me so I can further
improve the quality of my tutorials!


->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->

Here is the final dice class:

Code:
 


import java.util.Random;

public class dice
{
   //instead of what we usually do we are going to declare the variables here
   int sides;
   int roll;
   Random gen;
   //Going to make a regular six sided die
   public dice()
       {
           gen = new Random();
           sides = 6;
       }
       //What if we want to also be able to set dice? Overloading.
       public dice(int x)
       {
           gen = new Random();
           //We use dice(SomeNumber) and it takes an integer somenumber and
           //sides = SomeNumber;
           sides = x;
       }
       //This wont return anything (because its void)
       public void roll()
       {
           roll=gen.nextInt(sides)+1;
           //We generate a number with a max of our sides (Random Numbers start from zero)
       }
       //This will return an integer (because its int) This is how we get the roll
       public int getroll()
       {
           return roll;
       }
}


Final Program Code:

Code:
 

public class roller
{
   public static void main()
   {
       dice first = new dice();
       //We just created a dice object, which has 6 sides
       dice second = new dice(8);
       //An eight sided die will also work, called second
       first.roll();
       System.out.println("First Die(6): "+first.getroll());
       second.roll();
       System.out.println("First Die(8): "+second.getroll());
   }
}
Posted Image
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Tutorials · Next Topic »
Add Reply


Theme designed by Sith of Outline